- Home /
Unity 3d Sprite Shader (How do I limit Max Brightness to 1 with Multiple Lights Hitting)
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);
}
Have you ever found the solution for this? I'm stuck... Whenever I use the same shader as you I have the same problem of lights overlapping and the sprite getting white. I found that if you use BlendOp $$anonymous$$ax it uses only the strongest light. The problem of BlendOp $$anonymous$$ax is that it makes the opacity of the object relative on how much light it is receiving as a result it makes the sprites behind to blend colors.
Your answer
Follow this Question
Related Questions
Header for material properties in inspector? 2 Answers
Fog not working in my Shader~ 0 Answers
Addition to Standard Shader stopping it from batching? 0 Answers
"Stencil Operation" property setup? 1 Answer
A Cg shader semantic problem 2 Answers