SHARE
TWEET

HLSL Pixelshader Texture Blending

a guest Jun 5th, 2014 204 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //input.Int is the interpolated position in object space, as opposed to the clip space in SV_Position
  2. float3 pos = input.Int.xyz;
  3.  
  4. //load normal map and calculate missing component
  5. float3 n;
  6. n.xz = (g_Normal.Sample(samAnisotropic, input.Tex).xy * 2 - 1);
  7. n.y = sqrt(1 - min(1.0f, n.x * n.x + n.z * n.z));
  8. n = normalize(n);
  9.  
  10. //calculate slope and height for texture blending
  11. float slope = 1 - n.y;
  12. float height = (sqrt(max(0, pos.x * pos.x + pos.y * pos.y + pos.z * pos.z)) - g_TerrainScaling.x)/(g_TerrainScaling.y);
  13.  
  14. //alphas for texture blending
  15. float a1 = smoothstep(smoothstep(smoothstep(smoothstep(smoothstep(slope + 0.15f)))));                           //dirt
  16. float a2 = smoothstep(smoothstep(smoothstep(smoothstep(height + 0.1f))));                                       //rock
  17. float a3 = smoothstep(smoothstep(smoothstep(smoothstep(smoothstep(smoothstep(height - 0.1f))))));               //snow
  18. float a4 = smoothstep(smoothstep(abs(pos.y/g_TerrainScaling.x) + 0.45f));                                       //desert
  19.  
  20.  
  21.                
  22. //load different textures
  23. float3 grass = t_Grass.Sample(samAnisotropic, tex * 50).xyz;
  24. float3 dirt = t_Dirt.Sample(samAnisotropic, tex * 50).xyz;
  25. float3 rock = t_Rock.Sample(samAnisotropic, tex * 50).xyz;
  26. float3 sand = t_Sand.Sample(samAnisotropic, tex * 50).xyz;
  27. float3 snow = t_Snow.Sample(samAnisotropic, tex * 50).xyz;
  28. float3 water = t_Water.Sample(samAnisotropic, tex * 25).xyz;
  29.  
  30.        
  31.  
  32. d = (((grass * (1 - a1) + a1 * dirt) * (a4) + (1 - a4) * sand) * (1 - a2) + a2 * rock) * (1 - a3) + a3 * snow;   //(1 - a3) * ((1 - a2) * ((1 - a1) * grass + dirt * a1) + snow * a2) + rock * a3;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top