2
\$\begingroup\$

I have a scene with a point light and alpha textured (RGBA) triangle strips. I want to apply the point light color with a shader.

Shader "Custom/Test02" {
    Properties {
        _MainTex("Texture (RGBA)", 2D) = "gray" {}
    }
    SubShader {
        Tags { "Queue"="Transparent" "RenderType"="Transparent"  }

        Blend SrcAlpha OneMinusSrcAlpha

        Pass {
            Tags { "LightMode" = "ForwardBase" }
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            uniform sampler2D   _MainTex;
            uniform float4      _LightColor0;

           struct vertexInput {
                float4 vertex   : POSITION;
                float4 texcoord : TEXCOORD0;
             };

            struct vertexOutput {
                float4 position : SV_POSITION;
                float4 color    : COLOR;
                float2 uv       : TEXCOORD0;
            };

            vertexOutput vert(vertexInput input) {
                vertexOutput result;

                result.position = mul(UNITY_MATRIX_MVP, input.vertex);
                result.uv       = input.texcoord;
                result.color    = float4(_LightColor0.rgb, 1.0f);
                // result.color    = float4(1.0, 1.0, 1.0, 1.0);
                return result;
            }

            float4 frag (vertexOutput input) : COLOR {
                float4 color = tex2D(_MainTex, input.uv);
                return color * input.color;
            }
            ENDCG
        }
    }
}

The color in _LightColor0 is always black. How do I get the proper point light color? If the color is set to float4(1.0, 1.0, 1.0, 1.0); everything appears but without color from the point lights.

\$\endgroup\$
1
\$\begingroup\$

I was thinking wrong. I do not more passes to receive every light in the scene. I took a shortcut and used a surface shader in the end. I post it here for reference

Shader "Custom/Test01" {
    Properties {
        _MainTex ("Texture (RGBA)", 2D) = "white" {}
    }

    SubShader {
        Tags {
                "Queue" = "Transparent"
                "RenderType" = "Transparent"
        }

        Cull Back
        Offset 0, -1

        CGPROGRAM
        #pragma surface surf BlinnPhong alpha noforwardadd
        #include "UnityCG.cginc"

        struct Input {
                float4 color : COLOR;
                float2 uv_MainTex;
        };

        uniform sampler2D _MainTex;
        void surf(Input IN, inout SurfaceOutput o) {
                float4 texel = tex2D(_MainTex, IN.uv_MainTex);
                o.Albedo = texel.rgb * IN.color.rgb;
                o.Alpha = texel.a;
        }
        ENDCG
    }
}
\$\endgroup\$
0
\$\begingroup\$

_LightColor0 is part of Lighting.cginc. It's an automatic include for surface shaders. In order to add it to your frag shader you need to put #include "Lighting.cginc"in your Pass

From the docs:

Lighting.cginc - standard surface shader lighting models; automatically included when you’re writing surface shaders.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.