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;
- float3 worldPos;
- };
- void surf (Input IN, inout SurfaceOutput o)
- {
- // Calculate the tiling factor based on the model scale
- float3 modelScale = float3(length(UNITY_MATRIX_MTX0[0]), length(UNITY_MATRIX_MTX0[1]), length(UNITY_MATRIX_MTX0[2]));
- float tilingFactor = _TileFactor / max(modelScale.x, max(modelScale.y, modelScale.z));
- // Calculate the UV coordinates for each axis using the world position
- float2 uvX = IN.worldPos.yz * tilingFactor;
- float2 uvY = IN.worldPos.xz * tilingFactor;
- float2 uvZ = IN.worldPos.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