Search code examples
unity-game-engineshaderhlslrenderercg

Why is my shader working in the Scene View but not in the Game View (Unity) (Built-in Render Pipeline)?


So, i made a little unlit shader in Unity. I want the shader to display a circle at the center of the mesh. Here is the code :

Shader "Unlit/UnlitBasicShader"
{
    Properties
    {

        _Color("Tint", Color) = (1.0, 1.0, 1.0, 1.0)
        _ColorCenterOfMass("Color of the center of mass", Color) = (1.0, 1.0, 1.0, 1.0)
        _SizeCenterOfMass("Size of the center of mass", Float) = 1
        
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                
                
                float4 worldPos : TEXCOORD1;
                float4 vertex : SV_POSITION;
            };



            float4 _Color;
            float4 _ColorCenterOfMass;
            float _SizeCenterOfMass;

            v2f vert (appdata v)
            {
                v2f o;
                
                o.worldPos = mul(unity_ObjectToWorld, v.vertex);
                o.vertex = UnityObjectToClipPos(v.vertex);
                
              
                
                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                float3 localSpace = i.worldPos - mul(unity_ObjectToWorld,float4(0,0,0,1)).xyz;
                float distance = sqrt(localSpace.x * localSpace.x + localSpace.y * localSpace.y);
                float circleCenterOfMass = step(distance,_SizeCenterOfMass);
                
                
                fixed4 col = lerp(_Color, _ColorCenterOfMass, circleCenterOfMass);

                
                 
                return col;
            }
            ENDCG
        }
    }
}

The shader is working just fine in the Scene View : Shader in the Scene View

But the circle is weirdly anchored at the center of World Space in the Game View:

Shader in the Game View with a little circle

Shader in the Game View with a bigger circle

I have no clue of what is causing this difference


Solution

  • The problem is rather complicated and requires way more information, that is not specified in the question(i.e inspector window, material with the shader, camera inspector window, etc.) The common issues could be:

    1. Enable post processing on the camera
    2. Check your pipeline settings
    3. Look at your material, there may be some settings unselected(wrong pipeline used, etc.)

    Here is a reference: https://answers.unity.com/questions/1404471/billboard-shader-works-in-scene-view-but-not-game.html

    Hope this helps!