Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Shader "Custom/AutoTileTriplanarShader"
- {
- Properties
- {
- _MainTexX ("Texture X", 2D) = "white" {}
- _MainTexY ("Texture Y", 2D) = "white" {}
- _MainTexZ ("Texture Z", 2D) = "white" {}
- _TileFactor ("Tile Factor", Range(1, 10)) = 1.0
- }
- SubShader
- {
- Tags { "RenderType"="Opaque" }
- LOD 200
- CGPROGRAM
- #pragma surface surf Lambert
- sampler2D _MainTexX;
- sampler2D _MainTexY;
- sampler2D _MainTexZ;
- float _TileFactor;
- struct Input
- {
- float2 uv_MainTex;
- float3 worldNormal;
- };
- void surf (Input IN, inout SurfaceOutput o)
- {
- // Calculate the tiling factor based on the model scale
- float modelScale = max(max(UNITY_MATRIX_M[0].x, UNITY_MATRIX_M[1].y), UNITY_MATRIX_M[2].z);
- float tilingFactor = _TileFactor / modelScale;
- // Calculate the UV coordinates for each axis using the worldNormal
- float2 uvX = IN.worldNormal.yz * tilingFactor;
- float2 uvY = IN.worldNormal.xz * tilingFactor;
- float2 uvZ = IN.worldNormal.xy * tilingFactor;
- // Sample the textures using the calculated UV coordinates
- fixed4 cX = tex2D(_MainTexX, IN.uv_MainTex + uvX);
- fixed4 cY = tex2D(_MainTexY, IN.uv_MainTex + uvY);
- fixed4 cZ = tex2D(_MainTexZ, IN.uv_MainTex + uvZ);
- // Blend the sampled colors based on the absolute value of the worldNormal
- fixed4 finalColor = cX * abs(IN.worldNormal.x) + cY * abs(IN.worldNormal.y) + cZ * abs(IN.worldNormal.z);
- // Output the color
- o.Albedo = finalColor.rgb;
- o.Alpha = finalColor.a;
- }
- ENDCG
- }
- FallBack "Diffuse"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement