Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Custom Shader that works in other projects doesn't "work properly" in new project

Discussion in 'Shaders' started by GixG17, Oct 16, 2020.

  1. GixG17

    GixG17

    Joined:
    Aug 18, 2013
    Posts:
    105
    Years ago, I started a Unity project and, throughout that project's development over the years, I wrote a shader that works pretty well for my needs. Last year, I started a new side project and used that same shader into my new project without any problems.

    Fast forward to today, using Unity 2018.4.25f1, both projects that were created using an older version of Unity work fine but I created yet another new project.

    What I've noticed, using the same version of Unity, is that my shader doesn't "work properly" in this new project. It compiles with no errors and it renders colour (which I can alter via the inspector, like any other shader) but it won't output textures like it does in the other (older) projects.

    Obviously there's something in the old projects that the new one is missing but I can't figure out what it is. I've looked over the internet and it seems to be some settings regarding camera render paths but I've looked into the matter and none of that seems to make a difference.

    Can anyone more experienced with shaders give me an idea of what could be causing the problem? I know it's not much to go by but I don't know what's going on myself.

    Thank you.
     
  2. GixG17

    GixG17

    Joined:
    Aug 18, 2013
    Posts:
    105
    I did more "basic" experimentation and I noticed something:

    If I create a brand new "Standard Surface Shader", and I replace all instanced of _MainTex like this:
    Code (CSharp):
    1. Shader "Custom/test"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Color", Color) = (1,1,1,1)
    6.         _Ground("Ground + Spec(A)", 2D) = "White" {}
    7.         //_MainTex ("Albedo (RGB)", 2D) = "white" {}
    8.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    9.         _Metallic ("Metallic", Range(0,1)) = 0.0
    10.     }
    11.     SubShader
    12.     {
    13.         Tags { "RenderType"="Opaque" }
    14.         LOD 200
    15.  
    16.         CGPROGRAM
    17.         // Physically based Standard lighting model, and enable shadows on all light types
    18.         #pragma surface surf Standard fullforwardshadows
    19.  
    20.         // Use shader model 3.0 target, to get nicer looking lighting
    21.         #pragma target 3.0
    22.  
    23.         sampler2D _Ground;//_MainTex;
    24.  
    25.         struct Input
    26.         {
    27.             float2 uv_MainTex;
    28.         };
    29.  
    30.         half _Glossiness;
    31.         half _Metallic;
    32.         fixed4 _Color;
    33.  
    34.         // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
    35.         // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
    36.         // #pragma instancing_options assumeuniformscaling
    37.         UNITY_INSTANCING_BUFFER_START(Props)
    38.             // put more per-instance properties here
    39.         UNITY_INSTANCING_BUFFER_END(Props)
    40.  
    41.         void surf (Input IN, inout SurfaceOutputStandard o)
    42.         {
    43.             // Albedo comes from a texture tinted by color
    44.             fixed4 c = tex2D (_Ground, IN.uv_MainTex) * _Color;
    45.             o.Albedo = c.rgb;
    46.             // Metallic and smoothness come from slider variables
    47.             o.Metallic = _Metallic;
    48.             o.Smoothness = _Glossiness;
    49.             o.Alpha = c.a;
    50.         }
    51.         ENDCG
    52.     }
    53.     FallBack "Diffuse"
    54. }
    55.  
    It'll compile but it won't display the texture on the surface of the geometry. The object will be tinted by some colour, which I'm assuming it's taking a pixel from the texture file but it all seems very arbitrary (grass texture renders green, but a flat colors like orange, cyan, purple, etc will render in beige).

    Going back to my old shader code, I replaced the first texture sampler (out of 10) and renamed it _MainTex and it fixed it.

    What a strange behavior.