On this page
Object-Scaled UVs
UVs from a painter describe one mesh. The moment you scale that mesh, the texture stretches with it, and a texture that looked perfect on the unit asset looks smeared on the big one and microscopic on the small one. Object scaled UVs break the texture loose from the mesh by generating the UV from position, so the pattern keeps a constant size no matter how the object is scaled.
float2 uv = positionOS * _TexScale; // or positionWS * _TexScale
half4 tex = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv);
The problem, in one number¶
A painted UV says “this corner is at 0.75 of the texture”. Scale the object 2x and that corner is still at 0.75, but the surface is twice as wide, so the texture now covers twice the area with the same number of texels. Detail density has changed, and the artist cannot fix it from the material without breaking the small instances. This is the technique the Unreal Dev Kit course taught a generation of shaders: derive the UV from position times a scale, because position scales with the object while the scale constant does not, and the ratio is stable.
// Double the object, and the pattern doubles its area with it:
// texels per unit of surface stay the same.
float2 uv = IN.positionOS * _TexScale;
Object space or world space: pick the pivot of stability¶
The only decision is which position you feed. Object space scales with the object, which is what you want for a single asset in many sizes: every copy of the crate, small or large, shows the same number of texture repeats, because the pattern is anchored to the asset’s own frame. The trap is that the pattern is also anchored to the object’s origin: two objects of different sizes do not share a continuous pattern, each one tiles from its own zero.
World space is the opposite. The pattern is continuous across the whole level, so neighboring objects share one unbroken field, and scaling an object moves the pattern with the world instead of stretching it. The trap is that moving the object slides the pattern, so a sliding crate does not carry its dirt with it. In practice you use object space for props and characters, and world space for environments, floors, and anything that should read as part of the ground.
The Coordinate Spaces chapter is the reference for where each position comes from, and the tri-planar chapter is what happens when you take this idea seriously enough to sample three of them at once.
What the shader does¶
The shader drives the texture by object space position, so the same asset at three scales shows the same texel density with a different pattern offset. A toggle swaps to world space, where the three become one continuous field.
Illustrative schematic: Same texel density at three scales.
Position times scale, in object space by default, with a toggle to world space so you can feel the difference in anchoring.
Shader "DecompiledArt/Textures/Projections/ObjectScaledUVs/ObjectScaledUVs"
{
Properties
{
_MainTex("Texture (Repeat wrap)", 2D) = "white" {}
[MainColor] _BaseColor("Base Color", Color) = (1, 1, 1, 1)
_TexScale("Texture Scale", Float) = 2
[Enum(ObjectSpace,0,WorldSpace,1)] _Mode("Space", Float) = 0
}
SubShader
{
Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" "Queue" = "Geometry" }
Pass
{
Name "ForwardOnly"
Tags { "LightMode" = "UniversalForward" }
HLSLPROGRAM
#pragma vertex Vert
#pragma fragment Frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
TEXTURE2D(_MainTex); SAMPLER(sampler_MainTex);
struct Attributes
{
float4 positionOS : POSITION;
};
struct Varyings
{
float4 positionCS : SV_POSITION;
float3 posOS : TEXCOORD0;
float3 posWS : TEXCOORD1;
};
CBUFFER_START(UnityPerMaterial)
half4 _BaseColor;
float _TexScale;
half _Mode;
CBUFFER_END
Varyings Vert(Attributes IN)
{
Varyings OUT;
OUT.positionCS = TransformObjectToHClip(IN.positionOS.xyz);
OUT.posOS = IN.positionOS.xyz;
OUT.posWS = TransformObjectToWorld(IN.positionOS.xyz);
return OUT;
}
half4 Frag(Varyings IN) : SV_Target
{
// Take the longest axis as the 2D point so a 3D box maps sanely.
float3 p = (_Mode < 0.5h) ? IN.posOS : IN.posWS;
float2 uv = p.zy * _TexScale; // use the YZ face of the box
half4 tex = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv);
return half4(_BaseColor.rgb * tex.rgb, _BaseColor.a);
}
ENDHLSL
}
}
FallBack Off
}
Scale the object freely and the density holds. Move it, and in object space the pattern stays put while in world space it slides.
🔗 Related Chapters¶
Tri-Planar UVs • Coordinate Spaces • Tiling & Offset
❤️ Support Shader Story¶
If this article helped you, consider supporting the project on Patreon: you’ll get access to the related source files and reference cheat-sheets.
Your support helps keep this library open and free for everyone.