我正在 Unity 中创建一个视频游戏。每个精灵都使用具有 CornucopiaShader.shader 材质的精灵渲染器进行渲染。我遇到的问题是我想将精灵的最大亮度(或颜色)限制为精灵的正常图像,无论有多少点光源照射它的功率,灯光的强度,还有环境光在团结的场景中。当照射精灵的灯光强度低于最大亮度级别时,我希望它像正常点亮的精灵一样运行,如果没有灯光照射到它,则显示为黑色,如果照射到 0.5 的强度,则显示为半亮,等等,以及介于两者之间的一切都像平常一样。问题一:总之,如果三个强度为 5 的光照射到精灵上,我希望精灵看起来只是正常亮度 1,而不是被光冲出白色。
由于玩家可以像纸片马里奥一样旋转并切换侧面,因此当前的着色器代码会以这种方式运行,并且当前从背面照射的光线也应该像当前在着色器中一样照亮两侧。问题2:但我遇到的另一个问题,就像我所包含的四张图像中所看到的那样,当我翻转播放器时,强度会发生变化。
我连续三天试图解决这两个问题,但无法解决。
图片1 https://i.stack.imgur.com/85w2r.png
图2 https://i.stack.imgur.com/koyvc.png
图3 https://i.stack.imgur.com/bN30f.png
图4 https://i.stack.imgur.com/X5f3n.png
Shader "Custom/CornucopiaShader" {
Properties{
_MainCol("Main Tint", Color) = (1,1,1,1)
_MainTex("Main Texture", 2D) = "white" {}
_Cutoff("Alpha cutoff", Range(0,0.5)) = 0.5
}
SubShader{
Tags {"Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" "PreviewType" = "Plane"}
Cull Off
ZWrite Off
LOD 200
ColorMask RGB
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma surface surf SimpleLambert alphatest:_Cutoff addshadow fullforwardshadows alpha:blend
#pragma target 3.0
#include "RetroAA.cginc"
sampler2D _MainTex;
float4 _MainTex_TexelSize;
fixed4 _MainCol;
half4 LightingSimpleLambert(SurfaceOutput s, half3 lightDir, half atten)
{
half4 c;
c.rgb = s.Albedo * _MainCol.rgb * (atten)* _LightColor0.rgb;
c.a = s.Alpha;
return c;
}
struct Input {
float2 uv_MainTex;
};
void surf(Input IN, inout SurfaceOutput o) {
fixed4 c = RetroAA(_MainTex, IN.uv_MainTex, _MainTex_TexelSize);
o.Albedo = lerp(c.rgb, c.rgb, c.a);
o.Alpha = c.a;
}
ENDCG
}
Fallback "Transparent/Cutout/VertexLit"
}
#include "UnityCG.cginc"
#pragma target 3.0
fixed4 RetroAA(sampler2D tex, float2 uv, float4 texelSize){
float2 texelCoord = uv*texelSize.zw;
float2 hfw = 0.5*fwidth(texelCoord);
float2 fl = floor(texelCoord - 0.5) + 0.5;
float2 uvaa = (fl + smoothstep(0.5 - hfw, 0.5 + hfw, texelCoord - fl))*texelSize.xy;
return tex2D(tex, uvaa);
}