Advertisement
moryarti

Untitled

Jun 8th, 2023
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. Shader "Custom/AutoTileTriplanarShader"
  2. {
  3. Properties
  4. {
  5. _MainTexX ("Texture X", 2D) = "white" {}
  6. _MainTexY ("Texture Y", 2D) = "white" {}
  7. _MainTexZ ("Texture Z", 2D) = "white" {}
  8. _NormalMap ("Normal Map", 2D) = "bump" {}
  9. _TileFactor ("Tile Factor", Range(1, 10)) = 1.0
  10. }
  11.  
  12. SubShader
  13. {
  14. Tags { "RenderType"="Opaque" }
  15. LOD 200
  16.  
  17. CGPROGRAM
  18. #pragma surface surf Lambert
  19.  
  20. sampler2D _MainTexX;
  21. sampler2D _MainTexY;
  22. sampler2D _MainTexZ;
  23. sampler2D _NormalMap;
  24. float _TileFactor;
  25.  
  26. struct Input
  27. {
  28. float2 uv_MainTex;
  29. float3 worldNormal;
  30. };
  31.  
  32. void surf (Input IN, inout SurfaceOutput o)
  33. {
  34. // Calculate the model size based on the worldNormal
  35. float modelSize = length(IN.worldNormal);
  36.  
  37. // Calculate the tiling factor based on the model size
  38. float tilingFactor = modelSize * _TileFactor;
  39.  
  40. // Calculate the UV coordinates for each axis using worldNormal
  41. float2 uvX = IN.worldNormal.yz * tilingFactor;
  42. float2 uvY = IN.worldNormal.xz * tilingFactor;
  43. float2 uvZ = IN.worldNormal.xy * tilingFactor;
  44.  
  45. // Sample the textures using the calculated UV coordinates
  46. fixed4 cX = tex2D(_MainTexX, IN.uv_MainTex + uvX);
  47. fixed4 cY = tex2D(_MainTexY, IN.uv_MainTex + uvY);
  48. fixed4 cZ = tex2D(_MainTexZ, IN.uv_MainTex + uvZ);
  49.  
  50. // Sample the normal map using the main texture UV coordinates
  51. fixed3 normal = UnpackNormal(tex2D(_NormalMap, IN.uv_MainTex));
  52.  
  53. // Blend the sampled normals based on the absolute value of the worldNormal
  54. fixed3 finalNormal = cX.a * abs(IN.worldNormal.x) * UnpackNormal(cX) +
  55. cY.a * abs(IN.worldNormal.y) * UnpackNormal(cY) +
  56. cZ.a * abs(IN.worldNormal.z) * UnpackNormal(cZ);
  57.  
  58. // Normalize the final normal
  59. finalNormal = normalize(finalNormal);
  60.  
  61. // Output the normal and color
  62. o.Normal = finalNormal;
  63. o.Albedo = cX.rgb; // Use the color from the X-axis texture
  64. o.Alpha = cX.a;
  65. }
  66. ENDCG
  67. }
  68. FallBack "Diffuse"
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement