1

I am new to shader programming. I was just referring to some blogs to learn more on this area. I came across this article https://halisavakis.com/my-take-on-shaders-grass-shader-part-i/

According to them, they are using a displacement texture to calculate the wind contribution

"We calculate the wind contribution by sampling a displacement texture."

       float4 worldPos = mul(unity_ObjectToWorld, midpoint);

         float2 windTex = tex2Dlod(_WindTexture, float4(worldPos.xz * 
    _WindTexture_ST.xy + _Time.y * _WindSpeed, 0.0, 0.0)).xy;

float2 wind = (windTex * 2.0 - 1.0) * _WindStrength;

The explanation for the code above is

The sampling of the wind texture happens in lines 113-114 and it’s pretty much the same method we’ve seen a bunch of times when it comes to displacement textures: sample the texture, offset the UV’s (here it’s the world position’s x and z components) by time and then map the whole thing from -1 to 1 and multiply it by the strength of the effect.

midpoint here represents midpoint of a triangle. As far as my understanding, in mul(unity_ObjectToWorld, midpoint); we are getting the world position of the midpoint

I dont really understand whats happening in

  float2 windTex = tex2Dlod(_WindTexture, float4(worldPos.xz * 
    _WindTexture_ST.xy + _Time.y * _WindSpeed, 0.0, 0.0)).xy;

and I dont really understand how .xz and .xy works! I searched everywhere and still couldnt get nice grasp over it!

Can someone explain? Thank you!

MrRobot9
  • 2,402
  • 4
  • 31
  • 68
  • 2
    It's called [swizzling](https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-per-component-math). `worldPos.xz * _WindTexture_ST.xy + _Time.y * _WindSpeed` is equivalent to `float2(worldPos.x, worldPos.z) * float2(_WindTexture_ST.x, _WindTexture_ST.y) + float2(_Time.y * _WindSpeed, _Time.y * _WindSpeed)`. – Pluto Jan 29 '20 at 22:48
  • @Pluto Thanks a lot! Do you have any idea on tex2Dlod too? – MrRobot9 Jan 29 '20 at 23:00
  • 2
    You can look at the [docs](https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-tex2dlod). The `tex2Dlod(s, t)` samples `s` at uvs `t.xy` with mipmap LOD `t.w`. In that `float4(worldPos.xz * ...,0 ,0 )` t.w, the last component is 0. – Pluto Jan 29 '20 at 23:11
  • Also tex2Dlod is used here instead of tex2D because it's on the vertex shader side, where no screen-space derivative of the coordinate is available to determine the mipmap level automatically – Brice V. Feb 14 '20 at 11:39

0 Answers0