I am creating a videogame in Unity. Every sprite is rendered with a Sprite Renderer with a Material that has the CornucopiaShader.shader. The problem I have is I want to limit the max brightness (or color) of the sprite to just be a normal image of the sprite regardless of the power of how many point lights are hitting it, the intensity of the lights, and also the ambient light in the unity scene. When the intensity of the lights hitting the sprite is below that max brightness level I want it to act like a normal lit sprite and be black if no lights are hitting it, and be half lit up if an intensity of 0.5 is hitting it etc, and everything in between like normal. Problem 1: In summary if three lights at say 5 intensity hit the sprite, I want the sprite to just look normal brightness of 1 and not flushed out white with light.
Since the player can rotate like paper mario and switch sides the current shader code acts that way, and also currently light that hits from the backface should also light up both sides like it currently does in the shader. Problem 2: But another problem I am having, like is seen in the four images I have included is when I flip the player, the intensity changes.
I have been trying to figure out these two problems for 3 days straight and cannot figure it out.
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);
}
You can't really do this with surface shaders, but you can do it very efficiently with vertex fragment shaders. Unity stores the 4 closest point lights in a set of vectors to be used for per-vertex (non-important) lights. Fortunately, these are also accessible in the fragment shader, so you can use them to shade all 4 lights at once in a single pass! When you have all lights summed together, make sure that their intensity can't go above 1. Here is a quick shader i threw together for you:
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
}
}
}
The "Shade4Lights" function is a modified version of Unity's "Shade4PointLights", with diffuse lambert lighting removed (attenuation only). You'll also have to add your RetroAA function to the texture sampling. Your cutoff value is the "- 0.5" inside the "clip" funciton - you can expose this if you need it. If you need shadow casting for this shader, you can copy/paste the shadow pass from Unity's standard shader (you can download the source code from their page). For shadow receiving, you need to add a few lines to the shader - again check the source code for this.
You can read more about built-in shader variables here:
https://docs.unity3d.com/Manual/SL-UnityShaderVariables.html
来源:https://stackoverflow.com/questions/54994578/unity-3d-sprite-shader-how-do-i-limit-max-brightness-to-1-with-multiple-lights