Skip to content

Commit 359fbae

Browse files
Merge pull request #149 from Unity-Technologies/Add-object-scale-affect-tiling-support
HDRenderPipeline: Add support for object scale affecting tiling for layered material
2 parents bc7aaaa + 5e3ff18 commit 359fbae

4 files changed

Lines changed: 38 additions & 13 deletions

File tree

Assets/ScriptableRenderLoop/HDRenderPipeline/Material/LayeredLit/Editor/LayeredLitUI.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ private class StylesLayer
5151
public readonly GUIContent vertexColorModeText = new GUIContent("Vertex Color Mode", "Mode multiply: vertex color is multiply with the mask. Mode additive: vertex color values are remapped between -1 and 1 and added to the mask (neutral at 0.5 vertex color).");
5252
public readonly GUIContent layerCountText = new GUIContent("Layer Count", "Number of layers.");
5353
public readonly GUIContent layerTilingBlendMaskText = new GUIContent("Tiling", "Tiling for the blend mask.");
54-
54+
public readonly GUIContent objectScaleAffectTileText = new GUIContent("Object Scale affect tiling", "Tiling will be affected by the object scale.");
55+
5556
public readonly GUIContent layerTilingText = new GUIContent("Tiling", "Tiling factor applied to UVSet");
5657
public readonly GUIContent layerTexWorldScaleText = new GUIContent("Tiling", "Tiling factor applied to Planar/Trilinear mapping");
5758
public readonly GUIContent UVBaseText = new GUIContent("Base UV Mapping", "Base UV Mapping mode of the layer.");
@@ -112,6 +113,8 @@ internal struct SerializeableGUIDs
112113
MaterialProperty[] layerUVDetailsMappingMask = new MaterialProperty[kMaxLayerCount];
113114

114115

116+
const string kObjectScaleAffectTile = "_ObjectScaleAffectTile";
117+
MaterialProperty objectScaleAffectTile = null;
115118
const string kUVBlendMask = "_UVBlendMask";
116119
MaterialProperty UVBlendMask = null;
117120
const string kUVMappingPlanarBlendMask = "_UVMappingPlanarBlendMask";
@@ -172,6 +175,7 @@ override protected void FindMaterialProperties(MaterialProperty[] props)
172175
useHeightBasedBlend = FindProperty(kUseHeightBasedBlend, props);
173176
useDensityMode = FindProperty(kUseDensityMode, props);
174177

178+
objectScaleAffectTile = FindProperty(kObjectScaleAffectTile, props);
175179
UVBlendMask = FindProperty(kUVBlendMask, props);
176180
UVMappingPlanarBlendMask = FindProperty(kUVMappingPlanarBlendMask, props);
177181
layerTilingBlendMask = FindProperty(kLayerTilingBlendMask, props);
@@ -677,6 +681,8 @@ bool DoLayersGUI(AssetImporter materialImporter)
677681
useHeightBasedBlend.floatValue = enabled ? 1.0f : 0.0f;
678682
}
679683

684+
m_MaterialEditor.ShaderProperty(objectScaleAffectTile, styles.objectScaleAffectTileText);
685+
680686
EditorGUILayout.Space();
681687

682688
for (int i = 0; i < numLayer; i++)
@@ -802,6 +808,9 @@ protected override void SetupMaterialKeywords(Material material)
802808

803809
void SetupLayersKeywords(Material material)
804810
{
811+
// object scale affect tile
812+
SetKeyword(material, "_LAYER_TILING_UNIFORM_SCALE", material.GetFloat(kObjectScaleAffectTile) > 0.0f);
813+
805814
// Blend mask
806815
LayerUVBaseMapping UVBlendMaskMapping = (LayerUVBaseMapping)material.GetFloat(kUVBlendMask);
807816
UVMappingPlanarBlendMask.floatValue = (UVBlendMaskMapping == LayerUVBaseMapping.Planar) ? 1.0f : 0.0f;

Assets/ScriptableRenderLoop/HDRenderPipeline/Material/LayeredLit/LayeredLit.shader

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,18 +199,21 @@ Shader "HDRenderPipeline/LayeredLit"
199199
[HideInInspector] _LayerCount("_LayerCount", Float) = 2.0
200200

201201
[Enum(None, 0, Multiply, 1, Add, 2)] _VertexColorMode("Vertex color mode", Float) = 0
202+
203+
[ToggleOff] _ObjectScaleAffectTile("_ObjectScaleAffectTile", Float) = 0.0
204+
[Enum(UV0, 0, Planar, 4, Triplanar, 5)] _UVBlendMask("UV Set for blendMask", Float) = 0
205+
[HideInInspector] _UVMappingPlanarBlendMask("_UVMappingPlanarBlendMask", Float) = 0.0
206+
_TexWorldScaleBlendMask("Tiling", Float) = 1.0
202207

203208
// WARNING
204209
// All the following properties that concern the UV mapping are the same as in the Lit shader.
205210
// This means that they will get overridden when synchronizing the various layers.
206211
// To avoid this, make sure that all properties here are in the exclusion list in LayeredLitUI.SynchronizeLayerProperties
207-
_TexWorldScaleBlendMask("Tiling", Float) = 1.0
208212
_TexWorldScale0("Tiling", Float) = 1.0
209213
_TexWorldScale1("Tiling", Float) = 1.0
210214
_TexWorldScale2("Tiling", Float) = 1.0
211215
_TexWorldScale3("Tiling", Float) = 1.0
212-
213-
[Enum(UV0, 0, Planar, 4, Triplanar, 5)] _UVBlendMask("UV Set for blendMask", Float) = 0
216+
214217
[Enum(UV0, 0, Planar, 4, Triplanar, 5)] _UVBase0("UV Set for base0", Float) = 0 // no UV1/2/3 for main layer (matching Lit.shader and for PPDisplacement restriction)
215218
[Enum(UV0, 0, UV1, 1, UV2, 2, UV3, 3, Planar, 4, Triplanar, 5)] _UVBase1("UV Set for base1", Float) = 0
216219
[Enum(UV0, 0, UV1, 1, UV2, 2, UV3, 3, Planar, 4, Triplanar, 5)] _UVBase2("UV Set for base2", Float) = 0
@@ -220,8 +223,7 @@ Shader "HDRenderPipeline/LayeredLit"
220223
[HideInInspector] _UVMappingMask1("_UVMappingMask1", Color) = (1, 0, 0, 0)
221224
[HideInInspector] _UVMappingMask2("_UVMappingMask2", Color) = (1, 0, 0, 0)
222225
[HideInInspector] _UVMappingMask3("_UVMappingMask3", Color) = (1, 0, 0, 0)
223-
224-
[HideInInspector] _UVMappingPlanarBlendMask("_UVMappingPlanarBlendMask", Float) = 0.0
226+
225227
[HideInInspector] _UVMappingPlanar0("_UVMappingPlanar0", Float) = 0.0
226228
[HideInInspector] _UVMappingPlanar1("_UVMappingPlanar1", Float) = 0.0
227229
[HideInInspector] _UVMappingPlanar2("_UVMappingPlanar2", Float) = 0.0
@@ -249,6 +251,7 @@ Shader "HDRenderPipeline/LayeredLit"
249251
#pragma shader_feature _ _DOUBLESIDED _DOUBLESIDED_LIGHTING_FLIP _DOUBLESIDED_LIGHTING_MIRROR
250252

251253
#pragma shader_feature _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
254+
#pragma shader_feature _LAYER_TILING_UNIFORM_SCALE
252255
#pragma shader_feature _LAYER_MAPPING_TRIPLANAR_BLENDMASK
253256
#pragma shader_feature _LAYER_MAPPING_TRIPLANAR_0
254257
#pragma shader_feature _LAYER_MAPPING_TRIPLANAR_1

Assets/ScriptableRenderLoop/HDRenderPipeline/Material/LayeredLit/LayeredLitTessellation.shader

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,17 +200,20 @@ Shader "HDRenderPipeline/LayeredLitTesselation"
200200

201201
[Enum(None, 0, Multiply, 1, Add, 2)] _VertexColorMode("Vertex color mode", Float) = 0
202202

203+
[ToggleOff] _ObjectScaleAffectTile("_ObjectScaleAffectTile", Float) = 0.0
204+
[Enum(UV0, 0, Planar, 4, Triplanar, 5)] _UVBlendMask("UV Set for blendMask", Float) = 0
205+
[HideInInspector] _UVMappingPlanarBlendMask("_UVMappingPlanarBlendMask", Float) = 0.0
206+
_TexWorldScaleBlendMask("Tiling", Float) = 1.0
207+
203208
// WARNING
204209
// All the following properties that concern the UV mapping are the same as in the Lit shader.
205210
// This means that they will get overridden when synchronizing the various layers.
206211
// To avoid this, make sure that all properties here are in the exclusion list in LayeredLitUI.SynchronizeLayerProperties
207-
_TexWorldScaleBlendMask("Tiling", Float) = 1.0
208212
_TexWorldScale0("Tiling", Float) = 1.0
209213
_TexWorldScale1("Tiling", Float) = 1.0
210214
_TexWorldScale2("Tiling", Float) = 1.0
211215
_TexWorldScale3("Tiling", Float) = 1.0
212216

213-
[Enum(UV0, 0, Planar, 4, Triplanar, 5)] _UVBlendMask("UV Set for blendMask", Float) = 0
214217
[Enum(UV0, 0, Planar, 4, Triplanar, 5)] _UVBase0("UV Set for base0", Float) = 0 // no UV1/2/3 for main layer (matching Lit.shader and for PPDisplacement restriction)
215218
[Enum(UV0, 0, UV1, 1, UV2, 2, UV3, 3, Planar, 4, Triplanar, 5)] _UVBase1("UV Set for base1", Float) = 0
216219
[Enum(UV0, 0, UV1, 1, UV2, 2, UV3, 3, Planar, 4, Triplanar, 5)] _UVBase2("UV Set for base2", Float) = 0
@@ -221,7 +224,6 @@ Shader "HDRenderPipeline/LayeredLitTesselation"
221224
[HideInInspector] _UVMappingMask2("_UVMappingMask2", Color) = (1, 0, 0, 0)
222225
[HideInInspector] _UVMappingMask3("_UVMappingMask3", Color) = (1, 0, 0, 0)
223226

224-
[HideInInspector] _UVMappingPlanarBlendMask("_UVMappingPlanarBlendMask", Float) = 0.0
225227
[HideInInspector] _UVMappingPlanar0("_UVMappingPlanar0", Float) = 0.0
226228
[HideInInspector] _UVMappingPlanar1("_UVMappingPlanar1", Float) = 0.0
227229
[HideInInspector] _UVMappingPlanar2("_UVMappingPlanar2", Float) = 0.0
@@ -263,6 +265,7 @@ Shader "HDRenderPipeline/LayeredLitTesselation"
263265
#pragma shader_feature _TESSELLATION_OBJECT_SCALE
264266

265267
#pragma shader_feature _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
268+
#pragma shader_feature _LAYER_TILING_UNIFORM_SCALE
266269
#pragma shader_feature _LAYER_MAPPING_TRIPLANAR_BLENDMASK
267270
#pragma shader_feature _LAYER_MAPPING_TRIPLANAR_0
268271
#pragma shader_feature _LAYER_MAPPING_TRIPLANAR_1

Assets/ScriptableRenderLoop/HDRenderPipeline/Material/Lit/LitData.hlsl

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,34 +314,44 @@ void GetLayerTexCoord(float2 texCoord0, float2 texCoord1, float2 texCoord2, floa
314314

315315
layerTexCoord.blendMask = layerTexCoord.base0;
316316

317+
// On all layers (but not on blend mask) we can scale the tiling with object scale (only uniform supported)
318+
// Note: the object scale doesn't affect planar/triplanar mapping as they already handle the object scale.
319+
float tileObjectScale = 1.0;
320+
#ifdef _LAYER_TILING_UNIFORM_SCALE
321+
// Extract scaling from world transform
322+
float4x4 worldTransform = GetObjectToWorldMatrix();
323+
// assuming uniform scaling, take only the first column
324+
tileObjectScale = length(float3(worldTransform._m00, worldTransform._m01, worldTransform._m02));
325+
#endif
326+
317327
isTriplanar = false;
318328
#ifdef _LAYER_MAPPING_TRIPLANAR_0
319329
isTriplanar = true;
320330
#endif
321331

322332
ComputeLayerTexCoord0( texCoord0, float2(0.0, 0.0), float2(0.0, 0.0), float2(0.0, 0.0),
323-
positionWS, normalWS, _UVMappingPlanar0 > 0.0, isTriplanar, _TexWorldScale0, layerTexCoord, _LayerTiling0);
333+
positionWS, normalWS, _UVMappingPlanar0 > 0.0, isTriplanar, _TexWorldScale0, layerTexCoord, _LayerTiling0 * tileObjectScale);
324334

325335
isTriplanar = false;
326336
#ifdef _LAYER_MAPPING_TRIPLANAR_1
327337
isTriplanar = true;
328338
#endif
329339
ComputeLayerTexCoord1( texCoord0, texCoord1, texCoord2, texCoord3,
330-
positionWS, normalWS, _UVMappingPlanar1 > 0.0, isTriplanar, _TexWorldScale1, layerTexCoord, _LayerTiling1);
340+
positionWS, normalWS, _UVMappingPlanar1 > 0.0, isTriplanar, _TexWorldScale1, layerTexCoord, _LayerTiling1 * tileObjectScale);
331341

332342
isTriplanar = false;
333343
#ifdef _LAYER_MAPPING_TRIPLANAR_2
334344
isTriplanar = true;
335345
#endif
336346
ComputeLayerTexCoord2( texCoord0, texCoord1, texCoord2, texCoord3,
337-
positionWS, normalWS, _UVMappingPlanar2 > 0.0, isTriplanar, _TexWorldScale2, layerTexCoord, _LayerTiling2);
347+
positionWS, normalWS, _UVMappingPlanar2 > 0.0, isTriplanar, _TexWorldScale2, layerTexCoord, _LayerTiling2 * tileObjectScale);
338348

339349
isTriplanar = false;
340350
#ifdef _LAYER_MAPPING_TRIPLANAR_3
341351
isTriplanar = true;
342352
#endif
343353
ComputeLayerTexCoord3( texCoord0, texCoord1, texCoord2, texCoord3,
344-
positionWS, normalWS, _UVMappingPlanar3 > 0.0, isTriplanar, _TexWorldScale3, layerTexCoord, _LayerTiling3);
354+
positionWS, normalWS, _UVMappingPlanar3 > 0.0, isTriplanar, _TexWorldScale3, layerTexCoord, _LayerTiling3 * tileObjectScale);
345355
}
346356

347357
#if defined(_HEIGHTMAP0)

0 commit comments

Comments
 (0)