Skip to content

Commit cbe8949

Browse files
HDRenderPipeline: Add thin material option + Fix issue with transmission and shadow
- Add a thin material option on SSS profile - Shadow was not working anymore with transmission due to merge conflict with shadow branch - Done a pass of format on file in Lit directory - Share constant for number of profile between c#a and hlsl
1 parent a603310 commit cbe8949

8 files changed

Lines changed: 109 additions & 63 deletions

File tree

Assets/ScriptableRenderPipeline/HDRenderPipeline/Editor/HDRenderPipelineInspector.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ private class Styles
3333
public readonly GUIContent shadowsAtlasHeight = new GUIContent("Atlas height");
3434

3535
// Subsurface Scattering Settings
36-
public readonly GUIContent[] sssProfiles = new GUIContent[SubsurfaceScatteringSettings.maxNumProfiles] { new GUIContent("Profile #0"), new GUIContent("Profile #1"), new GUIContent("Profile #2"), new GUIContent("Profile #3"), new GUIContent("Profile #4"), new GUIContent("Profile #5"), new GUIContent("Profile #6"), new GUIContent("Profile #7") };
36+
public readonly GUIContent[] sssProfiles = new GUIContent[SSSConstants.SSS_PROFILES_MAX] { new GUIContent("Profile #0"), new GUIContent("Profile #1"), new GUIContent("Profile #2"), new GUIContent("Profile #3"), new GUIContent("Profile #4"), new GUIContent("Profile #5"), new GUIContent("Profile #6"), new GUIContent("Profile #7") };
3737
public readonly GUIContent sssNumProfiles = new GUIContent("Number of profiles");
3838

3939
// Tile pass Settings
@@ -73,7 +73,7 @@ private class Styles
7373
public readonly GUIContent[] debugViewLightingStrings = { new GUIContent("None"), new GUIContent("Diffuse Lighting"), new GUIContent("Specular Lighting"), new GUIContent("Visualize Cascades") };
7474
public readonly int[] debugViewLightingValues = { (int)DebugLightingMode.None, (int)DebugLightingMode.DiffuseLighting, (int)DebugLightingMode.SpecularLighting, (int)DebugLightingMode.VisualizeCascade };
7575
public readonly GUIContent shadowDebugVisualizationMode = new GUIContent("Shadow Maps Debug Mode");
76-
public readonly GUIContent shadowDebugVisualizeShadowIndex = new GUIContent("Visualize Shadow Index");
76+
public readonly GUIContent shadowDebugVisualizeShadowIndex = new GUIContent("Visualize Shadow Index");
7777
public readonly GUIContent lightingDebugOverrideSmoothness = new GUIContent("Override Smoothness");
7878
public readonly GUIContent lightingDebugOverrideSmoothnessValue = new GUIContent("Smoothness Value");
7979
public readonly GUIContent lightingDebugAlbedo = new GUIContent("Lighting Debug Albedo");
@@ -263,7 +263,7 @@ private void SssSettingsUI(HDRenderPipeline pipe)
263263

264264
EditorGUILayout.PropertyField(m_NumProfiles, styles.sssNumProfiles);
265265

266-
for (int i = 0, n = Math.Min(m_Profiles.arraySize, SubsurfaceScatteringSettings.maxNumProfiles); i < n; i++)
266+
for (int i = 0, n = Math.Min(m_Profiles.arraySize, SSSConstants.SSS_PROFILES_MAX); i < n; i++)
267267
{
268268
SerializedProperty profile = m_Profiles.GetArrayElementAtIndex(i);
269269
EditorGUILayout.PropertyField(profile, styles.sssProfiles[i]);

Assets/ScriptableRenderPipeline/HDRenderPipeline/HDRenderPipeline.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ public void PushGlobalParams(HDCamera hdCamera, ScriptableRenderContext renderCo
473473
Shader.SetGlobalInt("_EnableSSS", debugDisplaySettings.renderingDebugSettings.enableSSS ? 1 : 0);
474474
Shader.SetGlobalInt("_TransmissionFlags", sssParameters.transmissionFlags);
475475
Shader.SetGlobalInt("_TexturingModeFlags", sssParameters.texturingModeFlags);
476+
Shader.SetGlobalInt("_ThinMaterialFlags", sssParameters.thinMaterialFlags);
476477
cmd.SetGlobalFloatArray("_ThicknessRemaps", sssParameters.thicknessRemaps);
477478
cmd.SetGlobalVectorArray("_TintColors", sssParameters.tintColors);
478479
cmd.SetGlobalVectorArray("_HalfRcpVariancesAndLerpWeights", sssParameters.halfRcpVariancesAndLerpWeights);

Assets/ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/Lit.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ public struct BSDFData
109109
public float thickness;
110110
public int subsurfaceProfile;
111111
public bool enableTransmission; // Read from the SSS profile
112-
public Vector3 transmittance;
112+
public bool enableThinMaterial; // Read from the SSS profile
113+
public Vector3 transmittance; // Compute from SSS profile
113114

114115
// SpecColor
115116
// fold into fresnel0

Assets/ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/Lit.cs.hlsl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@
5858
#define DEBUGVIEW_LIT_BSDFDATA_THICKNESS (1043)
5959
#define DEBUGVIEW_LIT_BSDFDATA_SUBSURFACE_PROFILE (1044)
6060
#define DEBUGVIEW_LIT_BSDFDATA_ENABLE_TRANSMISSION (1045)
61-
#define DEBUGVIEW_LIT_BSDFDATA_TRANSMITTANCE (1046)
61+
#define DEBUGVIEW_LIT_BSDFDATA_ENABLE_THIN_MATERIAL (1046)
62+
#define DEBUGVIEW_LIT_BSDFDATA_TRANSMITTANCE (1047)
6263

6364
//
6465
// UnityEngine.Experimental.Rendering.HDPipeline.Lit.GBufferMaterial: static fields
@@ -105,6 +106,7 @@ struct BSDFData
105106
float thickness;
106107
int subsurfaceProfile;
107108
bool enableTransmission;
109+
bool enableThinMaterial;
108110
float3 transmittance;
109111
};
110112

@@ -218,6 +220,9 @@ void GetGeneratedBSDFDataDebug(uint paramId, BSDFData bsdfdata, inout float3 res
218220
case DEBUGVIEW_LIT_BSDFDATA_ENABLE_TRANSMISSION:
219221
result = (bsdfdata.enableTransmission) ? float3(1.0, 1.0, 1.0) : float3(0.0, 0.0, 0.0);
220222
break;
223+
case DEBUGVIEW_LIT_BSDFDATA_ENABLE_THIN_MATERIAL:
224+
result = (bsdfdata.enableThinMaterial) ? float3(1.0, 1.0, 1.0) : float3(0.0, 0.0, 0.0);
225+
break;
221226
case DEBUGVIEW_LIT_BSDFDATA_TRANSMITTANCE:
222227
result = bsdfdata.transmittance;
223228
break;

Assets/ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/Lit.hlsl

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
// SurfaceData is define in Lit.cs which generate Lit.cs.hlsl
66
#include "Lit.cs.hlsl"
7+
#include "SubsurfaceScatteringProfile.cs.hlsl"
78

89
// In case we pack data uint16 buffer we need to change the output render target format to uint16
910
// TODO: Is there a way to automate these output type based on the format declare in lit.cs ?
@@ -52,16 +53,15 @@ TEXTURE2D_ARRAY(_LtcData); // We pack the 3 Ltc data inside a texture array
5253
#define MIN_N_DOT_V 0.0001 // The minimum value of 'NdotV'
5354

5455
// SSS parameters
55-
#define SSS_N_PROFILES 8
56-
#define SSS_LOW_THICKNESS 0.005 // 0.5 cm
5756
#define CENTIMETERS_TO_METERS 0.01
5857

59-
uint _EnableSSS; // Globally toggles subsurface scattering on/off
60-
uint _TransmissionFlags; // 1 bit/profile; 0 = inf. thick, 1 = supports transmission
61-
uint _TexturingModeFlags; // 1 bit/profile; 0 = PreAndPostScatter, 1 = PostScatter
62-
float4 _TintColors[SSS_N_PROFILES]; // For transmission; alpha is unused
63-
float _ThicknessRemaps[SSS_N_PROFILES][2]; // Remap: 0 = start, 1 = end - start
64-
float4 _HalfRcpVariancesAndLerpWeights[SSS_N_PROFILES][2]; // 2x Gaussians per color channel, A is the the associated interpolation weight
58+
uint _EnableSSS; // Globally toggles subsurface scattering on/off
59+
uint _TransmissionFlags; // 1 bit/profile; 0 = inf. thick, 1 = supports transmission
60+
uint _TexturingModeFlags; // 1 bit/profile; 0 = PreAndPostScatter, 1 = PostScatter
61+
uint _ThinMaterialFlags; // 1 bit/profile; 1 = is thin material (allow specific optimization)
62+
float4 _TintColors[SSS_PROFILES_MAX]; // For transmission; alpha is unused
63+
float _ThicknessRemaps[SSS_PROFILES_MAX][2]; // Remap: 0 = start, 1 = end - start
64+
float4 _HalfRcpVariancesAndLerpWeights[SSS_PROFILES_MAX][2]; // 2x Gaussians per color channel, A is the the associated interpolation weight
6565

6666
//-----------------------------------------------------------------------------
6767
// Helper functions/variable specific to this material
@@ -167,6 +167,8 @@ void FillMaterialIdSSSData(float3 baseColor, int subsurfaceProfile, float subsur
167167
_ThicknessRemaps[subsurfaceProfile][1] * thickness);
168168

169169
bsdfData.enableTransmission = IsBitSet(_TransmissionFlags, subsurfaceProfile);
170+
bsdfData.enableThinMaterial = IsBitSet(_ThinMaterialFlags, subsurfaceProfile);
171+
170172
if (bsdfData.enableTransmission)
171173
{
172174
bsdfData.transmittance = ComputeTransmittance( _HalfRcpVariancesAndLerpWeights[subsurfaceProfile][0].xyz,
@@ -277,7 +279,7 @@ void EncodeIntoGBuffer( SurfaceData surfaceData,
277279
}
278280
else if (surfaceData.materialId == MATERIALID_LIT_SSS)
279281
{
280-
outGBuffer2 = float4(surfaceData.subsurfaceRadius, surfaceData.thickness, 0.0, surfaceData.subsurfaceProfile * rcp(SSS_N_PROFILES - 1));
282+
outGBuffer2 = float4(surfaceData.subsurfaceRadius, surfaceData.thickness, 0.0, surfaceData.subsurfaceProfile * rcp(SSS_PROFILES_MAX - 1));
281283
}
282284
else if (surfaceData.materialId == MATERIALID_LIT_SPECULAR)
283285
{
@@ -408,7 +410,7 @@ void DecodeFromGBuffer(
408410
}
409411
else if (supportsSSS && bsdfData.materialId == MATERIALID_LIT_SSS)
410412
{
411-
int subsurfaceProfile = (SSS_N_PROFILES - 0.9) * inGBuffer2.a;
413+
int subsurfaceProfile = (SSS_PROFILES_MAX - 0.9) * inGBuffer2.a;
412414
float subsurfaceRadius = inGBuffer2.r;
413415
float thickness = inGBuffer2.g;
414416
FillMaterialIdSSSData(baseColor, subsurfaceProfile, subsurfaceRadius, thickness, bsdfData);
@@ -721,7 +723,7 @@ void EvaluateBSDF_Directional( LightLoopContext lightLoopContext,
721723

722724
[branch] if (lightData.shadowIndex >= 0)
723725
{
724-
float shadow = GetDirectionalShadowAttenuation(lightLoopContext.shadowContext, positionWS, bsdfData.normalWS, lightData.shadowIndex, L, posInput.unPositionSS);
726+
shadow = GetDirectionalShadowAttenuation(lightLoopContext.shadowContext, positionWS, bsdfData.normalWS, lightData.shadowIndex, L, posInput.unPositionSS);
725727
illuminance *= shadow;
726728
}
727729

@@ -769,8 +771,8 @@ void EvaluateBSDF_Directional( LightLoopContext lightLoopContext,
769771
const float w = 0.15;
770772
float illuminance = saturate((dot(-bsdfData.normalWS, L) + w) / ((1.0 + w) * (1.0 + w)));
771773

772-
// For low thickness, we can reuse the shadowing status for the back of the object.
773-
shadow = (bsdfData.thickness <= SSS_LOW_THICKNESS) ? shadow : 1;
774+
// For thin material we can reuse the shadowing status for the back of the object.
775+
shadow = bsdfData.enableThinMaterial ? shadow : 1;
774776
illuminance *= shadow * cookie.a;
775777

776778
// The difference between the Disney Diffuse and the Lambertian BRDF for transmission is negligible.
@@ -824,7 +826,7 @@ void EvaluateBSDF_Punctual( LightLoopContext lightLoopContext,
824826
[branch] if (lightData.shadowIndex >= 0)
825827
{
826828
float3 offset = float3(0.0, 0.0, 0.0); // GetShadowPosOffset(nDotL, normal);
827-
float shadow = GetPunctualShadowAttenuation(lightLoopContext.shadowContext, positionWS + offset, bsdfData.normalWS, lightData.shadowIndex, L, posInput.unPositionSS);
829+
shadow = GetPunctualShadowAttenuation(lightLoopContext.shadowContext, positionWS + offset, bsdfData.normalWS, lightData.shadowIndex, L, posInput.unPositionSS);
828830
shadow = lerp(1.0, shadow, lightData.shadowDimmer);
829831

830832
illuminance *= shadow;
@@ -876,8 +878,8 @@ void EvaluateBSDF_Punctual( LightLoopContext lightLoopContext,
876878
float illuminance = saturate((dot(-bsdfData.normalWS, L) + w) / ((1.0 + w) * (1.0 + w)));
877879
illuminance *= attenuation;
878880

879-
// For low thickness, we can reuse the shadowing status for the back of the object.
880-
shadow = (bsdfData.thickness <= SSS_LOW_THICKNESS) ? shadow : 1;
881+
// For thin material we can reuse the shadowing status for the back of the object.
882+
shadow = bsdfData.enableThinMaterial ? shadow : 1;
881883
illuminance *= shadow * cookie.a;
882884

883885
// The difference between the Disney Diffuse and the Lambertian BRDF for transmission is negligible.
@@ -932,7 +934,7 @@ void EvaluateBSDF_Projector(LightLoopContext lightLoopContext,
932934

933935
[branch] if (lightData.shadowIndex >= 0)
934936
{
935-
float shadow = GetDirectionalShadowAttenuation(lightLoopContext.shadowContext, positionWS, bsdfData.normalWS, lightData.shadowIndex, L, posInput.unPositionSS);
937+
shadow = GetDirectionalShadowAttenuation(lightLoopContext.shadowContext, positionWS, bsdfData.normalWS, lightData.shadowIndex, L, posInput.unPositionSS);
936938
illuminance *= shadow;
937939
}
938940

@@ -962,8 +964,8 @@ void EvaluateBSDF_Projector(LightLoopContext lightLoopContext,
962964
float illuminance = saturate((dot(-bsdfData.normalWS, L) + w) / ((1.0 + w) * (1.0 + w)));
963965
illuminance *= clipFactor;
964966

965-
// For low thickness, we can reuse the shadowing status for the back of the object.
966-
shadow = (bsdfData.thickness <= SSS_LOW_THICKNESS) ? shadow : 1;
967+
// For thin material we can reuse the shadowing status for the back of the object.
968+
shadow = bsdfData.enableThinMaterial ? shadow : 1;
967969
illuminance *= shadow * cookie.a;
968970

969971
// The difference between the Disney Diffuse and the Lambertian BRDF for transmission is negligible.

0 commit comments

Comments
 (0)