Skip to main content
1 of 4
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

Thanks for your patience. Here's a way to get decent worldspace texturing on flat/hard-edged surfaces for walls & ramps.

Example of the shader tiling the texture evenly

This won't work for purely horizontal surfaces like floors & ceilings (it tries to align the texture with the "up" axis, which is perpendicular in those cases) though we can use alternate methods to handle those spots.

Here since we're in 3D I'm assuming we want to use a lit Surface Shader, so we need to do two things:

  1. Find the #pragma surface line and add "vertex:vert" to the end of it, to say we want to use a custom vertex-modifying function named "vert"

  2. Write a vertex shader called "vert" that takes an inout appdata_full argument and modifies its texture coordinates.

         CGPROGRAM
         // Physically based Standard lighting model, and enable shadows on all light types
         #pragma surface surf Standard fullforwardshadows vertex:vert
    
         void vert(inout appdata_full v) {
    
             // Get the worldspace normal after transformation, and ensure it's unit length.
             float3 n = normalize(mul(unity_ObjectToWorld, v.normal).xyz);
    
             // Get the closest vector in the polygon's plane to world up.
             // We'll use this as the "v" direction of our texture.
             float3 vDirection = normalize(float3(0, 1, 0) - n.y * n);
    
             // Get the perpendicular in-plane vector to use as our "u" direction.
             float3 uDirection = normalize(cross(n, vDirection));
    
             // Get the position of the vertex in worldspace.
             float3 worldSpace = mul(unity_ObjectToWorld, v.vertex).xyz;
    
             // Project the worldspace position of the vertex into our texturing plane,
             // and use this result as the primary texture coordinate.
             v.texcoord.xy = float2(dot(worldSpace, uDirection), dot(worldSpace, vDirection));
         }
    
DMGregory
  • 140.8k
  • 23
  • 257
  • 401