Advertisement
moryarti

Untitled

Jun 8th, 2023
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.         _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 worldNormal;
  28.             float3 worldPos;
  29.         };
  30.        
  31.         void surf (Input IN, inout SurfaceOutput o)
  32.         {
  33.             // Calculate the tiling factor based on the model scale
  34.             float3 modelScale = float3(length(UNITY_MATRIX_MTX0[0]), length(UNITY_MATRIX_MTX0[1]), length(UNITY_MATRIX_MTX0[2]));
  35.             float tilingFactor = _TileFactor / max(modelScale.x, max(modelScale.y, modelScale.z));
  36.            
  37.             // Calculate the UV coordinates for each axis using the world position
  38.             float2 uvX = IN.worldPos.yz * tilingFactor;
  39.             float2 uvY = IN.worldPos.xz * tilingFactor;
  40.             float2 uvZ = IN.worldPos.xy * tilingFactor;
  41.            
  42.             // Sample the textures using the calculated UV coordinates
  43.             fixed4 cX = tex2D(_MainTexX, IN.uv_MainTex + uvX);
  44.             fixed4 cY = tex2D(_MainTexY, IN.uv_MainTex + uvY);
  45.             fixed4 cZ = tex2D(_MainTexZ, IN.uv_MainTex + uvZ);
  46.            
  47.             // Blend the sampled colors based on the absolute value of the worldNormal
  48.             fixed4 finalColor = cX * abs(IN.worldNormal.x) + cY * abs(IN.worldNormal.y) + cZ * abs(IN.worldNormal.z);
  49.            
  50.             // Output the color
  51.             o.Albedo = finalColor.rgb;
  52.             o.Alpha = finalColor.a;
  53.         }
  54.         ENDCG
  55.     }
  56.     FallBack "Diffuse"
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement