Advertisement
moryarti

Untitled

Jun 8th, 2023
369
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.         };
  29.        
  30.         void surf (Input IN, inout SurfaceOutput o)
  31.         {
  32.             // Calculate the tiling factor based on the model scale
  33.             float modelScale = max(max(UNITY_MATRIX_M[0].x, UNITY_MATRIX_M[1].y), UNITY_MATRIX_M[2].z);
  34.             float tilingFactor = _TileFactor / modelScale;
  35.            
  36.             // Calculate the UV coordinates for each axis using the worldNormal
  37.             float2 uvX = IN.worldNormal.yz * tilingFactor;
  38.             float2 uvY = IN.worldNormal.xz * tilingFactor;
  39.             float2 uvZ = IN.worldNormal.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 worldNormal
  47.             fixed4 finalColor = cX * abs(IN.worldNormal.x) + cY * abs(IN.worldNormal.y) + cZ * abs(IN.worldNormal.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