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" {}
- _NormalMap ("Normal Map", 2D) = "bump" {}
- _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;
- sampler2D _NormalMap;
- float _TileFactor;
- struct Input
- {
- float2 uv_MainTex;
- float3 worldNormal;
- };
- void surf (Input IN, inout SurfaceOutput o)
- {
- // Calculate the model size based on the worldNormal
- float modelSize = length(IN.worldNormal);
- // Calculate the tiling factor based on the model size
- float tilingFactor = modelSize * _TileFactor;
- // Calculate the UV coordinates for each axis using 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);
- // Sample the normal map using the main texture UV coordinates
- fixed3 normal = UnpackNormal(tex2D(_NormalMap, IN.uv_MainTex));
- // Blend the sampled normals based on the absolute value of the worldNormal
- fixed3 finalNormal = cX.a * abs(IN.worldNormal.x) * UnpackNormal(cX) +
- cY.a * abs(IN.worldNormal.y) * UnpackNormal(cY) +
- cZ.a * abs(IN.worldNormal.z) * UnpackNormal(cZ);
- // Normalize the final normal
- finalNormal = normalize(finalNormal);
- // Output the normal and color
- o.Normal = finalNormal;
- o.Albedo = cX.rgb; // Use the color from the X-axis texture
- o.Alpha = cX.a;
- }
- ENDCG
- }
- FallBack "Diffuse"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement