4.3 Shader writing, opaque on lighted

Hello, i was building a 2d game in 4.2 and i was using a method to only use color near the player, the rest was BW, the method i was using to do this was a spotlight pointed at the player and then using the shader that a user gave here ( http://forum.unity3d.com/threads/126893-Opaque-on-lighted-parts-shader ) to use the color texture, and then i was putting BW on all the rest.

All cool, but now 4.3 is out and i’m remaking the game to use sprites and sprites manager since its easier to do the animations and such, but i can’t create this effect since that script doesn’t work on sprite renderers (everything goes invisible) and I can’t see how the inbuild Sprite/diffuse shader is built so that i can see what’s wrong or try to adapt the script to make what i want, i can only see the compiled one, and that one is a mess :X

anyone knows what is wrong in that shader as far as sprites are concerned?

the shader is

Shader "Custom/GrayscaleToAlpha" {

    Properties {

        _MainTex ("Texture", 2D) = "white" {}

        _Color ("Main Color", Color) = (1,1,1,1)

    }

    SubShader {

        Tags { "RenderType"="Opaque" }

        LOD 200

        

        CGPROGRAM

        #pragma surface surf LambertMod

 

        half4 LightingLambertMod (SurfaceOutput s, half3 lightDir, half atten) {

            half NdotL = dot (s.Normal, lightDir);

            half4 c;

            clip(_LightColor0.a - 1);

            c.rgb = s.Albedo * _LightColor0.rgb * (NdotL * atten * 2);

            c.a = s.Alpha;

            return c;

        }

 

        struct Input {

            float2 uv_MainTex;

        };

        

        sampler2D _MainTex;

        float4 _Color;

 

        void surf (Input IN, inout SurfaceOutput o) {

            half4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;

            o.Albedo = c.rgb;

            //o.Alpha = c.a;

        }

        ENDCG

    } 

    FallBack "Diffuse"

}

You can download Unity builtin shaders: http://unity3d.com/unity/download/archive
It contains all built in shaders, Sprite-Default and Sprite-Defuse too. You can see how they are built.

It’s a start!!! thanks a lot!!

After studying the shaders for a while i discovered that if i take the Sprites/Diffuse shader and then just add a different version than Lambert with just a clip() on _Lighting.a-1 it all goes weird colors and to the background of everything, anyone knows why?

with

Shader "Sprites/DiffuseTest"
{
	Properties
	{
		[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
		_Color ("Tint", Color) = (1,1,1,1)
		[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
	}

	SubShader
	{
		Tags
		{ 
			"Queue"="Transparent" 
			"IgnoreProjector"="True" 
			"RenderType"="Transparent" 
			"PreviewType"="Plane"
			"CanUseSpriteAtlas"="True"
		}

		Cull Off
		Lighting Off
		ZWrite Off
		Fog { Mode Off }
		Blend SrcAlpha OneMinusSrcAlpha

		CGPROGRAM
		#pragma surface surf LambertMod alpha vertex:vert
		#pragma multi_compile DUMMY PIXELSNAP_ON

		sampler2D _MainTex;
		fixed4 _Color;

		struct Input
		{
			float2 uv_MainTex;
			fixed4 color;
		};
		
		half4 LightingLambertMod (SurfaceOutput s, half3 lightDir, half atten) {

            fixed diff = max (0, dot (s.Normal, lightDir));
			fixed4 c;
			c.rgb = s.Albedo * _LightColor0.rgb * (diff * atten * 2);
			c.a = s.Alpha;
			return c;

        }
		
		void vert (inout appdata_full v, out Input o)
		{
			#if defined(PIXELSNAP_ON)  !defined(SHADER_API_FLASH)
			v.vertex = UnityPixelSnap (v.vertex);
			#endif
			v.normal = float3(0,0,-1);
			
			UNITY_INITIALIZE_OUTPUT(Input, o);
			o.color = _Color;
		}
		

		void surf (Input IN, inout SurfaceOutput o)
		{
			fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * IN.color;
			o.Albedo = c.rgb;
			o.Alpha = c.a;
		}
		ENDCG
	}

Fallback "Sprites/Diffuse"
}

it shows like this
1418468--74503--$shader1.png

just changing the LambertMod to this

half4 LightingLambertMod (SurfaceOutput s, half3 lightDir, half atten) {

            fixed diff = max (0, dot (s.Normal, lightDir));
			fixed4 c;
			clip(_LightColor0.a-1);
			c.rgb = s.Albedo * _LightColor0.rgb * (diff * atten * 2);
			c.a = s.Alpha;
			return c;

        }

and it turns to this
1418468--74504--$shader2.png

Anyone knows why? doesn’t Clip works with Sprites?

It was the background, the color isn’t opaque enough :frowning: