Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Following a tutorial and getting the wrong result... where am I going wrong?

Discussion in 'Shaders' started by Reverend-Speed, Sep 8, 2019.

  1. Reverend-Speed

    Reverend-Speed

    Joined:
    Mar 28, 2011
    Posts:
    180
    Hey folks, shader newbie here, banging my head against a wall here. I have two pieces of code that should be identical but which are producing two different shader results. Would anybody mind looking at the code and trying to spot the difference? (~64 lines of Unity shader code)

    I should be getting the result on the bottom, but instead I'm getting the result on the top in the following image...
    upload_2019-9-8_19-3-10.png

    OFFICIAL TUTORIAL CODE (Working, at the bottom of the attached image):
    Code (CSharp):
    1. Shader "CookbookShaders/Chapter03/ScrollingUVs" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    5.         _ScrollXSpeed ("X Scroll Speed", Range(0,10)) = 2
    6.         _ScrollYSpeed ("Y Scroll Speed", Range(0,10)) = 2
    7.     }
    8.     SubShader {
    9.         Tags { "RenderType"="Opaque" }
    10.         LOD 200
    11.  
    12.         CGPROGRAM
    13.         // Physically based Standard lighting model, and enable shadows on all light types
    14.         #pragma surface surf Standard fullforwardshadows
    15.  
    16.         // Use shader model 3.0 target, to get nicer looking lighting
    17.         #pragma target 3.0
    18.  
    19.         fixed _ScrollXSpeed;
    20.         fixed _ScrollYSpeed;
    21.         sampler2D _MainTex;
    22.  
    23.         struct Input {
    24.             float2 uv_MainTex;
    25.         };
    26.  
    27.         fixed4 _Color;
    28.  
    29.         // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
    30.         // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
    31.         // #pragma instancing_options assumeuniformscaling
    32.         UNITY_INSTANCING_BUFFER_START(Props)
    33.             // put more per-instance properties here
    34.         UNITY_INSTANCING_BUFFER_END(Props)
    35.  
    36.         void surf (Input IN, inout SurfaceOutputStandard o) {
    37.             // Create a separate variable to store our UVs
    38.              // before we pass them to the tex2D() function
    39.              fixed2 scrolledUV = IN.uv_MainTex;
    40.      
    41.              // Create variables that store the individual x and y
    42.              // components for the UV's scaled by time
    43.              fixed xScrollValue = _ScrollXSpeed * _Time;
    44.              fixed yScrollValue = _ScrollYSpeed * _Time;
    45.      
    46.              // Apply the final UV offset
    47.              scrolledUV += fixed2(xScrollValue, yScrollValue);
    48.      
    49.              // Apply textures and tint
    50.              half4 c = tex2D (_MainTex, scrolledUV);
    51.              o.Albedo = c.rgb * _Color;
    52.              o.Alpha = c.a;
    53.         }
    54.         ENDCG
    55.     }
    56.     FallBack "Diffuse"
    57. }
    58.  
    MY CODE (Not working, at the top of the attached image - no visible texture or tint):
    Code (CSharp):
    1. Shader "CookbookShaders/Chapter03/ScrollingUVsRS"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Color", Color) = (1,1,1,1)
    6.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    7.         _ScrollXSpeed ("X Scroll Speed", Range(0,10)) = 2
    8.         _ScrollYSpeed ("Y Scroll Speed", Range(0,10)) = 2
    9.     }
    10.     SubShader
    11.     {
    12.         Tags { "RenderType"="Opaque" }
    13.         LOD 200
    14.  
    15.         CGPROGRAM
    16.         // Physically based Standard lighting model, and enable shadows on all light types
    17.         #pragma surface surf Standard fullforwardshadows
    18.  
    19.         // Use shader model 3.0 target, to get nicer looking lighting
    20.         #pragma target 3.0
    21.  
    22.         fixed _ScrollXSpeed;
    23.         fixed _ScrollYSpeed;
    24.         sampler2D _MainTex;
    25.  
    26.         struct Input
    27.         {
    28.             float2 uv_MainTex;
    29.         };
    30.  
    31.         fixed4 _Color;
    32.  
    33.         // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
    34.         // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
    35.         // #pragma instancing_options assumeuniformscaling
    36.         UNITY_INSTANCING_BUFFER_START(Props)
    37.             // put more per-instance properties here
    38.         UNITY_INSTANCING_BUFFER_END(Props)
    39.  
    40.         void surf (Input IN, inout SurfaceOutputStandard o)
    41.         {
    42.             // Create a separate variable to store our UVs
    43.             // before we pass them to the text2D() function
    44.             fixed2 scrolledUV = IN.uv_MainTex;
    45.  
    46.             // Create variables that store the individual x and y
    47.             // components for the UV's scaled by time
    48.             fixed xScrollValue = _ScrollXSpeed * _Time;
    49.             fixed yScrollValue = _ScrollYSpeed * _Time;
    50.  
    51.             // Apply the final UV offset
    52.             scrolledUV += fixed2(xScrollValue, yScrollValue);
    53.  
    54.             // Apply textures and tint
    55.             half4 c = tex2D (_MainTex, scrolledUV);
    56.             o.Albedo = c.rbg * _Color;
    57.             o.Albedo = c.a;
    58.         }
    59.         ENDCG
    60.        
    61.     }
    62.     FallBack "Diffuse"
    63. }
    64.  
    I've scanned and scanned and rescanned through the code and I just can't see the difference. It's infuriating - can anybody help me here? Much appreciated in advance...!
     
  2. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,322
    Last line sets the albedo to the alpha. Not the output alpha to the alpha
     
    Reverend-Speed likes this.
  3. Reverend-Speed

    Reverend-Speed

    Joined:
    Mar 28, 2011
    Posts:
    180
    THANK YOU, @LennartJohansen . 'Albedo' instead of 'Alpha'. Jesus christ. I hope I get better at spotting that after working with shaders a bit...

    EDIT: Also worth noting, I'd entered the letters rgb in the wrong order on line 56 in my script. Learning! It's happening!
     
    Last edited: Sep 8, 2019