ExampleTriplanarTerrainShader.shader 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. Shader "Custom/TerrainShader" {
  2. // These properties can be modified from the material inspector.
  3. Properties{
  4. _MainTex("Ground Texture", 2D) = "white" {}
  5. _WallTex("Wall Texture", 2D) = "white" {}
  6. _TexScale("Texture Scale", Float) = 1
  7. }
  8. // You can have multiple subshaders with different levels of complexity. Unity will pick the first one
  9. // that works on whatever machine is running the game.
  10. SubShader{
  11. Tags { "RenderType" = "Opaque" } // None of our terrain is going to be transparent so Opaque it is.
  12. LOD 200 // We only need diffuse for now so 200 is fine. (higher includes bumped, specular, etc)
  13. CGPROGRAM
  14. #pragma surface surf Standard fullforwardshadows // Use Unity's standard lighting model
  15. #pragma target 3.0 // Lower target = fewer features but more compatibility.
  16. // Declare our variables (above properties must be declared here)
  17. sampler2D _MainTex;
  18. sampler2D _WallTex;
  19. float _TexScale;
  20. // Say what information we want from our geometry.
  21. struct Input {
  22. float3 worldPos;
  23. float3 worldNormal;
  24. };
  25. // This function is run for every pixel on screen.
  26. void surf(Input IN, inout SurfaceOutputStandard o) {
  27. float3 scaledWorldPos = IN.worldPos / _TexScale; // Get a the world position modified by scale.
  28. float3 pWeight = abs(IN.worldNormal); // Get the current normal, using abs function to ignore negative numbers.
  29. pWeight /= pWeight.x + pWeight.y + pWeight.z; // Ensure pWeight isn't greater than 1.
  30. // Get the texture projection on each axes and "weight" it by multiplying it by the pWeight.
  31. float3 xP = tex2D(_WallTex, scaledWorldPos.yz) * pWeight.x;
  32. float3 yP = tex2D(_MainTex, scaledWorldPos.xz) * pWeight.y;
  33. float3 zP = tex2D(_WallTex, scaledWorldPos.xy) * pWeight.z;
  34. // Return the sum of all of the projections.
  35. o.Albedo = xP + yP + zP;
  36. }
  37. ENDCG
  38. }
  39. FallBack "Diffuse"
  40. }