我在Unity中创建了一个视频游戏。每个精灵都使用Sprite渲染器渲染,材质具有CornucopiaShader.shader。我遇到的问题是我想要将精灵的最大亮度(或颜色)限制为精灵的正常图像,而不管点击它的点数,灯光的强度,以及统一场景中的环境光。当击中精灵的灯光强度低于最大亮度水平时,我希望它像普通的点亮精灵一样,如果没有灯光击中则为黑色,如果强度为0.5则会半亮,等等一切都在正常之间。问题1:总之,如果三个强度为5强度的灯点亮了精灵,我希望精灵只看到1的正常亮度,而不是用光线刷出白色。
由于播放器可以像纸质马里奥一样旋转并切换侧面,因此当前着色器代码以这种方式起作用,并且当前从背面点击的光也应该像在当前在着色器中那样照亮两侧。问题2:但是我遇到的另一个问题,就像在我所包含的四个图像中看到的那样,当我翻转播放器时,强度会发生变化。
我一直试图连续3天弄清楚这两个问题并且无法弄明白。
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);
}
使用表面着色器无法实现此功能,但您可以使用顶点片段着色器非常有效地完成此操作。 Unity将4个最近点的灯存储在一组向量中,用于每顶点(非重要)灯。幸运的是,这些也可以在片段着色器中访问,因此您可以使用它们一次性遮挡所有4个灯光!当你将所有灯光汇总在一起时,请确保它们的强度不能超过1.这是一个快速着色器,我为你拼凑:
Shader "Unlit/ToonTest"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Name "FORWARD"
Tags { "LightMode" = "ForwardBase" "RenderType" = "TransparentCutout" "Queue"="AlphaTest"}
Cull Off
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
#pragma multi_compile_fwdbase
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
half3 normal : NORMAL;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
float3 worldPos : TEXCOORD1;
float3 ambient : TEXCOORD2;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.ambient = ShadeSH9(mul(unity_ObjectToWorld, float4(v.normal, 0.0 ))); // Ambient from spherical harmonics
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
float3 Shade4Lights (
float4 lightPosX, float4 lightPosY, float4 lightPosZ,
float3 lightColor0, float3 lightColor1, float3 lightColor2, float3 lightColor3,
float4 lightAttenSq,
float3 pos)
{
// to light vectors
float4 toLightX = lightPosX - pos.x;
float4 toLightY = lightPosY - pos.y;
float4 toLightZ = lightPosZ - pos.z;
// squared lengths
float4 lengthSq = 0;
lengthSq += toLightX * toLightX;
lengthSq += toLightY * toLightY;
lengthSq += toLightZ * toLightZ;
// don't produce NaNs if some vertex position overlaps with the light
lengthSq = max(lengthSq, 0.000001);
// attenuation
float4 atten = 1.0 / (1.0 + lengthSq * lightAttenSq);
float4 diff = atten; //ndotl * atten;
// final color
float3 col = 0;
col += lightColor0 * diff.x;
col += lightColor1 * diff.y;
col += lightColor2 * diff.z;
col += lightColor3 * diff.w;
return col;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
half3 intensity = Shade4Lights(unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0, unity_LightColor[0], unity_LightColor[1], unity_LightColor[2], unity_LightColor[3], unity_4LightAtten0, i.worldPos);
intensity = min((half3)1, i.ambient + intensity);
col.rgb *= intensity;
clip(col.a - 0.5);
// apply fog
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
}
}
“Shade4Lights”功能是Unity的“Shade4PointLights”的修改版本,删除了漫反射朗伯照明(仅限衰减)。您还必须将RetroAA函数添加到纹理采样中。您的截止值是“剪辑”功能中的“ - 0.5” - 如果需要,您可以公开它。如果您需要为此着色器投影阴影,则可以从Unity的标准着色器复制/粘贴阴影传递(您可以从其页面下载源代码)。对于阴影接收,您需要向着色器添加几行 - 再次检查源代码。
您可以在此处阅读有关内置着色器变量的更多信息:
https://docs.unity3d.com/Manual/SL-UnityShaderVariables.html