0
\$\begingroup\$

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!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.