Thanks for your patience. Here's a way to get decent worldspace texturing on flat/hard-edged surfaces for walls & ramps.
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 starting with a new default Surface Shader we need to do two things:
Find the
#pragma surfaceline and add "vertex:vert" to the end of it, to say we want to use a custom vertex-modifying function named "vert"Write a vertex shader called "vert" that takes an
inout appdata_fullargument 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)); }
