Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Problems with popular grass shader tutorial...!

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

  1. Reverend-Speed

    Reverend-Speed

    Joined:
    Mar 28, 2011
    Posts:
    182
    Hey folks,

    I'm trying to follow this tutorial, but I'm running into problems.. can anybody suggest a solution?

    I'm supposed to be getting a response like this...

    ...but instead, my model is showing up as this:


    I assume this is to do with the relative positions of the origins on the respective models. Presuming this is to do with world origins... but trying to subtract -5 from the normalised values seems to do very little.
    My code is as follows...

    Code (CSharp):
    1. Shader "RS/LRGrass"
    2. {
    3.     Properties
    4.     {
    5.         _Color        ("Color", Color)            = (1,1,1,1)
    6.         _MainTex    ("Albedo (RGB)", 2D)        = "white" {}
    7.         _Glossiness ("Smoothness", Range(0,1))    = 0.5
    8.         _Metallic    ("Metallic", Range(0,1))    = 0.0
    9.  
    10.         _WindTex ("Wind Texture", 2D) = "white" {}
    11.         _WorldSize    ("World Size", vector)        = (1,1,1,1)
    12.         _WindSpeed ("Wind Speed", vector)        = (1,1,1,1)
    13.     }
    14.     SubShader
    15.     {
    16.         Tags { "RenderType"="Opaque" }
    17.         LOD 200
    18.  
    19.         Pass
    20.         {
    21.             CGPROGRAM
    22.  
    23.             #pragma vertex vert
    24.             #pragma fragment frag
    25.            
    26.             struct vertexInput
    27.             {
    28.                 float4 vertex : POSITION;
    29.                 float3 normal : NORMAL;
    30.             };
    31.  
    32.             struct vertexOutput
    33.             {
    34.                 float4 pos : SV_POSITION;
    35.                 float3 normal : NORMAL;
    36.                 float2 sp : TEXCOORD0; //Test sample position
    37.             };
    38.  
    39.             sampler2D    _WindTex;
    40.             vector        _WorldSize;
    41.             vector        _WindSpeed;
    42.  
    43.             vertexOutput vert(vertexInput input)
    44.             {
    45.                 vertexOutput output;
    46.  
    47.                 // convert input to clip and world space
    48.                 output.pos        = UnityObjectToClipPos(input.vertex);
    49.                 float4 normal4    = float4(input.normal, 0.0f);
    50.                 output.normal    = normalize(mul(normal4, unity_WorldToObject).xyz);
    51.  
    52.                 // get vertex world position
    53.                 float4 worldPos = mul(input.vertex, unity_ObjectToWorld);
    54.                 // normalize position based on world Size
    55.                 float2 samplePos = worldPos.xz / _WorldSize.xz;
    56.  
    57.                 // scroll sample position based on time
    58.                 // samplePos += _Time.x * _WindSpeed.xy;
    59.  
    60.                 // Sample wind texture
    61.                 //float windSample = tex2Dlod (_WindTex, float4(samplePos, 0, 0));
    62.  
    63.                 output.sp = samplePos; // Test sample position.
    64.  
    65.                
    66.  
    67.                 return output;
    68.             };
    69.            
    70.             float4 frag (vertexOutput input) : COLOR
    71.             {
    72.                 return float4(input.sp.x, 0, 0, 1); // Temporary measure!
    73.             };
    74.  
    75.             ENDCG
    76.         }
    77.  
    78.         /*
    79.         CGPROGRAM
    80.         // Physically based Standard lighting model, and enable shadows on all light types
    81.         #pragma surface surf Standard fullforwardshadows
    82.        
    83.         // Use shader model 3.0 target, to get nicer looking lighting
    84.         #pragma target 3.0
    85.  
    86.         sampler2D _MainTex;
    87.  
    88.         struct Input
    89.         {
    90.             float2 uv_MainTex;
    91.         };
    92.  
    93.         half _Glossiness;
    94.         half _Metallic;
    95.         fixed4 _Color;
    96.  
    97.         // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
    98.         // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
    99.         // #pragma instancing_options assumeuniformscaling
    100.         UNITY_INSTANCING_BUFFER_START(Props)
    101.             // put more per-instance properties here
    102.         UNITY_INSTANCING_BUFFER_END(Props)
    103.  
    104.         void surf (Input IN, inout SurfaceOutputStandard o)
    105.         {
    106.             // Albedo comes from a texture tinted by color
    107.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    108.             o.Albedo = c.rgb;
    109.             // Metallic and smoothness come from slider variables
    110.             o.Metallic = _Metallic;
    111.             o.Smoothness = _Glossiness;
    112.             o.Alpha = c.a;
    113.         }
    114.         ENDCG
    115.         */
    116.     }
    117.     FallBack "Diffuse"
    118. }
    119.  
    Can anybody help me out with this? Really confused here and I'd like to have it sorted soon...!
     
  2. gamedevbill

    gamedevbill

    Joined:
    May 25, 2018
    Posts:
    17
    From my tests, the matrix and vertex need to switch order in the multiply, to this:

    mul(unity_ObjectToWorld, input.vertex);

    Perhaps Linden's graphics settings are different, or it's a typo.

    Also note that in his image, all his grass starts at a positive coordinate. The scale multiply only adjusts for size of scene, does not fix things being negative.

    Good luck
    gdb
    gamedevbill.com
     
    Last edited: Nov 16, 2019 at 1:11 PM
  3. DrDust265

    DrDust265

    Joined:
    Apr 5, 2013
    Posts:
    3