Advertisement
moryarti

Untitled

Jun 8th, 2023
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. Shader "Custom/AutoTileTriplanarShaderCube"
  2. {
  3. Properties
  4. {
  5. _MainTexX ("Texture X", 2D) = "white" {}
  6. _MainTexY ("Texture Y", 2D) = "white" {}
  7. _MainTexZ ("Texture Z", 2D) = "white" {}
  8. _TileFactor ("Tile Factor", Range(1, 10)) = 1.0
  9. }
  10.  
  11. SubShader
  12. {
  13. Tags { "RenderType"="Opaque" }
  14. LOD 200
  15.  
  16. CGPROGRAM
  17. #pragma surface surf Lambert
  18.  
  19. sampler2D _MainTexX;
  20. sampler2D _MainTexY;
  21. sampler2D _MainTexZ;
  22. float _TileFactor;
  23.  
  24. struct Input
  25. {
  26. float2 uv_MainTex;
  27. float3 localPos;
  28. };
  29.  
  30. void surf (Input IN, inout SurfaceOutput o)
  31. {
  32. // Calculate the tiling factor based on the model scale
  33. float3 modelScale = float3(length(UNITY_MATRIX_MTX0[0]), length(UNITY_MATRIX_MTX0[1]), length(UNITY_MATRIX_MTX0[2]));
  34. float tilingFactor = _TileFactor / max(modelScale.x, max(modelScale.y, modelScale.z));
  35.  
  36. // Calculate the UV coordinates for each axis based on the local position
  37. float2 uvX = IN.localPos.yz * tilingFactor;
  38. float2 uvY = IN.localPos.xz * tilingFactor;
  39. float2 uvZ = IN.localPos.xy * tilingFactor;
  40.  
  41. // Sample the textures using the calculated UV coordinates
  42. fixed4 cX = tex2D(_MainTexX, IN.uv_MainTex + uvX);
  43. fixed4 cY = tex2D(_MainTexY, IN.uv_MainTex + uvY);
  44. fixed4 cZ = tex2D(_MainTexZ, IN.uv_MainTex + uvZ);
  45.  
  46. // Blend the sampled colors based on the absolute value of the localPos
  47. fixed4 finalColor = cX * abs(IN.localPos.x) + cY * abs(IN.localPos.y) + cZ * abs(IN.localPos.z);
  48.  
  49. // Output the color
  50. o.Albedo = finalColor.rgb;
  51. o.Alpha = finalColor.a;
  52. }
  53. ENDCG
  54. }
  55. FallBack "Diffuse"
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement