2
\$\begingroup\$

I wrote the simplest possible surface shader:

Shader "SimpleSurf" {
    Properties {
        _MainTex ("Base (RGB)", 2D) = "white" {}
    }
    SubShader {
        Tags {"RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        #pragma surface surf Lambert

        sampler2D _MainTex;

        struct Input {
            float2 uv_MainTex;
        };

        void surf (Input IN, inout SurfaceOutput o) {
            half4 c = tex2D (_MainTex, IN.uv_MainTex);
            o.Albedo = c.rgb;
            o.Alpha = c.a;
        }
        ENDCG
    } 
    FallBack "Diffuse"
}

When the "Forward" rendering path is enabled the object looks fine! enter image description here

But when the "Vertex lit" rendering path is enabled, it looks became black: enter image description here

Why it happened? How can I use surface shaders (or maybe write a Vertex/Fragment shader which will consider spot lights) which do some effects in VertexLit path?

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$
Shader "SimpleSurf" 
{   
Properties 
{
    _MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader 
{       
    Pass 
    {
        CGPROGRAM
        #pragma vertex vert_img
        #pragma fragment frag

        #include "UnityCG.cginc"

        uniform sampler2D _MainTex;

        float4 frag(v2f_img i) : COLOR 
        {
            return tex2D(_MainTex, i.uv);
        }
        ENDCG
    }
}}

Edit: I misunderstood the question. I've replaced the previous shader with the simplest possible vertex and fragment shader that works in the VertexLit render path. If you want objects to receive per pixel lighting you will need to use Forward or Deferred rendering as per http://docs.unity3d.com/Documentation/Manual/RenderingPaths.html

\$\endgroup\$
1
  • \$\begingroup\$ It works but only due to Fallback operator present. Try to comment Fallback, and you will see that the object has disappeared. \$\endgroup\$
    – Netherwire
    May 17, 2014 at 15:16
-1
\$\begingroup\$

Try setting the fallback to the vertexlit shader. Fallback "VertexLit" I haven't tested it out my self but from my knowledge of the rendering pipeline, I can assume that it will work. Basically, if (and in this situation it will) pixel based diffuse lighting fails, it will look black as there won't be any lighting to compute, it needs something to fallback to, vertex lit.

This edit allows you to preserve the attributes of surface shading without having to stay in deffered or forward rendering paths.

\$\endgroup\$
3
  • \$\begingroup\$ Could you provide more detail? We generally like more in our answers than single sentences. \$\endgroup\$
    – Gnemlock
    Jan 29, 2017 at 22:10
  • \$\begingroup\$ I am overly still unfamiliar with shader language. That said, it is bad form to provide key requested improvement as a comment. Add it to your answer as an edit, not as a comment. Further more, you might want to explain your reasoning. The other answer already gives us a shader; what makes your shader more efficient? Why does setting the fallback to the vertexlit shader fix the problem? \$\endgroup\$
    – Gnemlock
    Mar 11, 2017 at 0:01
  • \$\begingroup\$ Sorry about that, I haven't been on stackexchange for a very long time so so I don't really know the answer requirements. \$\endgroup\$
    – BramFx
    Mar 11, 2017 at 0:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .