Skip to content

Commit cbb5f7d

Browse files
HDRenderPipeline: Add object scale
1 parent dfad3ff commit cbb5f7d

7 files changed

Lines changed: 45 additions & 12 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ Shader "HDRenderPipeline/LayeredLitTesselation"
218218
#pragma shader_feature _ _DOUBLESIDED _DOUBLESIDED_LIGHTING_FLIP _DOUBLESIDED_LIGHTING_MIRROR
219219
// Default is _TESSELLATION_PHONG
220220
#pragma shader_feature _ _TESSELLATION_DISPLACEMENT _TESSELLATION_DISPLACEMENT_PHONG
221+
#pragma shader_feature _TESSELLATION_OBJECT_SCALE
221222

222223
#pragma shader_feature _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
223224
#pragma shader_feature _LAYER_MAPPING_TRIPLANAR_0

Assets/ScriptableRenderLoop/HDRenderPipeline/Material/Lit/Editor/BaseLitUI.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ protected static class Styles
9191
public static GUIContent tessellationFactorTriangleSizeText = new GUIContent("Triangle size", "Desired screen space sized of triangle (in pixel). Smaller value mean smaller triangle.");
9292
public static GUIContent tessellationShapeFactorText = new GUIContent("Shape factor", "Strength of Phong tessellation shape (lerp factor)");
9393
public static GUIContent tessellationBackFaceCullEpsilonText = new GUIContent("Triangle culling Epsilon", "If -1.0 back face culling is enabled for tessellation, higher number mean more aggressive culling and better performance");
94-
//public static GUIContent tessellationObjectScaleText = new GUIContent("Enable object scale", "Scale displacement taking into account the object scale");
94+
public static GUIContent tessellationObjectScaleText = new GUIContent("Enable object scale", "Tesselation displacement will take into account the object scale - Only work with uniform positive scale");
9595
}
9696

9797
public enum SurfaceType
@@ -201,9 +201,8 @@ protected void ShaderOptionsGUI()
201201
if ((DoubleSidedMode)doubleSidedMode.floatValue == DoubleSidedMode.None)
202202
{
203203
m_MaterialEditor.ShaderProperty(tessellationBackFaceCullEpsilon, Styles.tessellationBackFaceCullEpsilonText);
204-
}
205-
// TODO: Implement
206-
// m_MaterialEditor.ShaderProperty(tessellationObjectScale, Styles.tessellationObjectScaleText);
204+
}
205+
m_MaterialEditor.ShaderProperty(tessellationObjectScale, Styles.tessellationObjectScaleText);
207206
EditorGUI.indentLevel--;
208207
}
209208
}
@@ -244,7 +243,7 @@ protected void FindCommonOptionProperties(MaterialProperty[] props)
244243
tessellationFactorTriangleSize = FindProperty(kTessellationFactorTriangleSize, props, false);
245244
tessellationShapeFactor = FindProperty(kTessellationShapeFactor, props, false);
246245
tessellationBackFaceCullEpsilon = FindProperty(kTessellationBackFaceCullEpsilon, props, false);
247-
//tessellationObjectScale = FindProperty(kTessellationObjectScale, props, false);
246+
tessellationObjectScale = FindProperty(kTessellationObjectScale, props, false);
248247
}
249248

250249
protected void SetupCommonOptionsKeywords(Material material)
@@ -403,6 +402,9 @@ protected void SetupCommonOptionsKeywords(Material material)
403402
material.DisableKeyword("_TESSELLATION_DISPLACEMENT");
404403
material.EnableKeyword("_TESSELLATION_DISPLACEMENT_PHONG");
405404
}
405+
406+
bool tessellationObjectScaleEnable = material.GetFloat(kTessellationObjectScale) == 1.0;
407+
SetKeyword(material, "_TESSELLATION_OBJECT_SCALE", tessellationObjectScaleEnable);
406408
}
407409
}
408410

@@ -516,8 +518,8 @@ protected virtual void SetupEmissionGIFlags(Material material)
516518
const string kTessellationShapeFactor = "_TessellationShapeFactor";
517519
MaterialProperty tessellationBackFaceCullEpsilon = null;
518520
const string kTessellationBackFaceCullEpsilon = "_TessellationBackFaceCullEpsilon";
519-
//MaterialProperty tessellationObjectScale = null;
520-
//const string kTessellationObjectScale = "_TessellationObjectScale";
521+
MaterialProperty tessellationObjectScale = null;
522+
const string kTessellationObjectScale = "_TessellationObjectScale";
521523

522524
protected static string[] reservedProperties = new string[] { kSurfaceType, kBlendMode, kAlphaCutoff, kAlphaCutoffEnabled, kDoubleSidedMode };
523525

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,13 @@ float3 GetDisplacement(VaryingsMeshToDS input)
101101
float height = 0.0;
102102
#endif
103103

104+
float3 displ = float3(0.0, 0.0, 0.0);
104105
#ifdef VARYINGS_DS_NEED_NORMAL
105-
return height * input.normalWS;
106-
#else
107-
return float3(0.0, 0.0, 0.0);
106+
displ = height * input.normalWS;
107+
#endif
108+
// Applying scaling of the object if requested
109+
#ifdef _TESSELLATION_OBJECT_SCALE
108110
#endif
111+
112+
return displ;
109113
}

Assets/ScriptableRenderLoop/HDRenderPipeline/Material/Lit/LitTessellation.shader

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ Shader "HDRenderPipeline/LitTessellation"
118118
#pragma shader_feature _ _DOUBLESIDED _DOUBLESIDED_LIGHTING_FLIP _DOUBLESIDED_LIGHTING_MIRROR
119119
// Default is _TESSELLATION_PHONG
120120
#pragma shader_feature _ _TESSELLATION_DISPLACEMENT _TESSELLATION_DISPLACEMENT_PHONG
121+
#pragma shader_feature _TESSELLATION_OBJECT_SCALE
121122

122123
#pragma shader_feature _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
123124
#pragma shader_feature _MAPPING_TRIPLANAR

Assets/ScriptableRenderLoop/HDRenderPipeline/ShaderPass/VaryingMesh.hlsl

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ struct VaryingsMeshToDS
233233
#ifdef VARYINGS_DS_NEED_COLOR
234234
float4 color;
235235
#endif
236+
#ifdef _TESSELLATION_OBJECT_SCALE
237+
float3 objectScale;
238+
#endif
236239
};
237240

238241
struct PackedVaryingsMeshToDS
@@ -262,6 +265,10 @@ struct PackedVaryingsMeshToDS
262265
#ifdef VARYINGS_DS_NEED_COLOR
263266
float4 interpolators5 : TEXCOORD2;
264267
#endif
268+
269+
#ifdef _TESSELLATION_OBJECT_SCALE
270+
float3 interpolators6 : TEXCOORD3;
271+
#endif
265272
};
266273

267274
// Functions to pack data to use as few interpolator as possible, the ShaderGraph should generate these functions
@@ -291,6 +298,9 @@ PackedVaryingsMeshToDS PackVaryingsMeshToDS(VaryingsMeshToDS input)
291298
#ifdef VARYINGS_DS_NEED_COLOR
292299
output.interpolators5 = input.color;
293300
#endif
301+
#ifdef _TESSELLATION_OBJECT_SCALE
302+
output.interpolators6 = input.objectScale;
303+
#endif
294304

295305
return output;
296306
}
@@ -321,7 +331,9 @@ VaryingsMeshToDS UnpackVaryingsMeshToDS(PackedVaryingsMeshToDS input)
321331
#ifdef VARYINGS_DS_NEED_COLOR
322332
output.color = input.interpolators5;
323333
#endif
324-
334+
#ifdef _TESSELLATION_OBJECT_SCALE
335+
output.objectScale = input.interpolators6;
336+
#endif
325337
return output;
326338
}
327339

@@ -353,6 +365,11 @@ VaryingsMeshToDS InterpolateWithBaryCoordsMeshToDS(VaryingsMeshToDS input0, Vary
353365
TESSELLATION_INTERPOLATE_BARY(color, baryCoords);
354366
#endif
355367

368+
#ifdef _TESSELLATION_OBJECT_SCALE
369+
// objectScale doesn't change for the whole object.
370+
ouput.objectScale = input0.objectScale;
371+
#endif
372+
356373
return ouput;
357374
}
358375

Assets/ScriptableRenderLoop/HDRenderPipeline/ShaderPass/VertMesh.hlsl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,15 @@ VaryingsMeshType VertMesh(AttributesMesh input)
9797
{
9898
VaryingsMeshType output;
9999

100-
#if defined(TESSELLATION_ON)
100+
#ifdef TESSELLATION_ON
101101
output.positionWS = TransformObjectToWorld(input.positionOS);
102+
#ifdef _TESSELLATION_OBJECT_SCALE
103+
// Extract scaling from world transform
104+
float4x4 worldTransform = GetObjectToWorldMatrix();
105+
output.objectScale.x = length(float3(worldTransform._m00, worldTransform._m01, worldTransform._m02));
106+
output.objectScale.y = length(float3(worldTransform._m10, worldTransform._m11, worldTransform._m12));
107+
output.objectScale.z = length(float3(worldTransform._m20, worldTransform._m21, worldTransform._m22));
108+
#endif
102109
// TODO deal with camera center rendering and instancing (This is the reason why we always perform tow steps transform to clip space + instancing matrix)
103110
#if defined(VARYINGS_NEED_TANGENT_TO_WORLD) || defined(VARYINGS_DS_NEED_NORMAL)
104111
output.normalWS = TransformObjectToWorldNormal(input.normalOS);

Assets/ScriptableRenderLoop/fptl/Internal-DeferredComputeShading.compute

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ float3 EvalIndirectSpecular(UnityLight light, UnityIndirect ind);
2525
#include "TiledLightingTemplate.hlsl"
2626
#include "TiledReflectionTemplate.hlsl"
2727

28+
UNITY_DECLARE_TEX2D_FLOAT(_CameraDepthTexture);
2829
Texture2D _CameraGBufferTexture0;
2930
Texture2D _CameraGBufferTexture1;
3031
Texture2D _CameraGBufferTexture2;

0 commit comments

Comments
 (0)