Skip to content

Fix Refraction #2220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
4bccdef
Fixed pre-refraction shader with refraction enabled
alelievr Oct 14, 2020
5119f8e
Updated changelog
alelievr Oct 14, 2020
915909b
revert change
alelievr Oct 14, 2020
6c9e11b
Fixed render queue in the SG UI
alelievr Oct 14, 2020
4b4f9e0
Fixed render queue migration issue when moving from 8.x to 10.x
alelievr Oct 13, 2020
18f2d85
Updated changelog
alelievr Oct 13, 2020
b445e69
Fix compilation
alelievr Oct 14, 2020
454f3d8
Fixed render queue migration from 9.0.0 preview 13 and 33
alelievr Oct 15, 2020
f0d45be
Fixed blend mode not updated whern changing refraction model in SG
alelievr Oct 15, 2020
45d54c2
Updated doc
alelievr Oct 15, 2020
7848287
Exposed refraction model in the material (only available when refract…
alelievr Oct 15, 2020
90d8a10
Cleanup
alelievr Oct 15, 2020
fded4dc
Merge branch 'HDRP/staging' into HDRP/fix/pre-refraction-shadergraph
sebastienlagarde Oct 15, 2020
b9aad02
Update LitShaderGraphGUI.cs
sebastienlagarde Oct 15, 2020
33f2540
Added warning in the SG inspector + fixed nullref
alelievr Oct 16, 2020
4ec03aa
Merge branch 'HDRP/fix/pre-refraction-shadergraph' of https://github.…
alelievr Oct 16, 2020
8549991
Added material migration function and bumped last material version in…
alelievr Oct 16, 2020
b676213
Fix refraction model in material upgrade
alelievr Oct 16, 2020
0870bf0
Merge branch 'master' into HDRP/fix/pre-refraction-shadergraph
sebastienlagarde Oct 16, 2020
42210ff
Added a warning when refraction is enabled with pre-refraciton render…
alelievr Oct 20, 2020
d2a9b90
Updated changelog
alelievr Oct 20, 2020
5dc1861
Updated doc
alelievr Oct 20, 2020
32c9b1f
Merge branch 'master' into HDRP/fix/pre-refraction-shadergraph
sebastienlagarde Oct 20, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ MonoBehaviour:
m_WizardActiveTab: 0
m_WizardNeedRestartAfterChangingToDX12: 0
m_WizardNeedToRunFixAllAgainAfterDomainReload: 0
m_LastMaterialVersion: 9
m_LastMaterialVersion: 10
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ MonoBehaviour:
m_WizardActiveTab: 0
m_WizardNeedRestartAfterChangingToDX12: 0
m_WizardNeedToRunFixAllAgainAfterDomainReload: 0
m_LastMaterialVersion: 9
m_LastMaterialVersion: 10
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ MonoBehaviour:
m_WizardActiveTab: 0
m_WizardNeedRestartAfterChangingToDX12: 0
m_WizardNeedToRunFixAllAgainAfterDomainReload: 0
m_LastMaterialVersion: 9
m_LastMaterialVersion: 10
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ MonoBehaviour:
m_WizardActiveTab: 0
m_WizardNeedRestartAfterChangingToDX12: 0
m_WizardNeedToRunFixAllAgainAfterDomainReload: 0
m_LastMaterialVersion: 9
m_LastMaterialVersion: 10
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ MonoBehaviour:
m_WizardActiveTab: 0
m_WizardNeedRestartAfterChangingToDX12: 0
m_WizardNeedToRunFixAllAgainAfterDomainReload: 0
m_LastMaterialVersion: 9
m_LastMaterialVersion: 10
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ MonoBehaviour:
m_ProjectSettingFolderPath: HDRPDefaultResources
m_WizardPopupAtStart: 0
m_WizardActiveTab: 0
m_LastMaterialVersion: 9
m_LastMaterialVersion: 10
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ MonoBehaviour:
m_WizardActiveTab: 0
m_WizardNeedRestartAfterChangingToDX12: 0
m_WizardNeedToRunFixAllAgainAfterDomainReload: 0
m_LastMaterialVersion: 9
m_LastMaterialVersion: 10
4 changes: 3 additions & 1 deletion com.unity.render-pipelines.high-definition/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Fixed an issue where the Exposure Shader Graph node had clipped text. (case 1265057)
- Fixed an issue when rendering into texture where alpha would not default to 1.0 when using 11_11_10 color buffer in non-dev builds.
- Fixed issues with reordering and hiding graphics compositor layers (cases 1283903, 1285282, 1283886).
- Fixed the possibility to have a shader with a pre-refraction render queue and refraction enabled at the same time.
- Fixed a migration issue with the rendering queue in ShaderGraph when upgrading to 10.x;

### Changed

Expand Down Expand Up @@ -50,6 +52,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Added a new ray tracing only function that samples the specular part of the materials.
- Adding missing marker for ray tracing profiling (RaytracingDeferredLighting)
- Added the support of eye shader for ray tracing.
- Exposed Refraction Model to the material UI when using a Lit ShaderGraph.

### Fixed
- Fixed several issues with physically-based DoF (TAA ghosting of the CoC buffer, smooth layer transitions, etc)
Expand Down Expand Up @@ -173,7 +176,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Fixed the denoising and multi-sample not being used for smooth multibounce RTReflections.
- Fixed issue where multiple cameras would cause GC each frame.
- Fixed after post process rendering pass options not showing for unlit ShaderGraphs.
- Fixed a migration issue with the rendering queue in ShaderGraph when upgrading to 10.x;
- Fixed null reference in the Undo callback of the graphics compositor
- Fixed cullmode for SceneSelectionPass.
- Fixed issue that caused non-static object to not render at times in OnEnable reflection probes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ To create a new Lit Material, navigate to your Project's Asset window, right-cli

Unity exposes this section if you select **Transparent** from the **Surface Type** drop-down. For information on the properties in this section, see the [Surface Type documentation](Surface-Type.md#TransparencyInputs).

Be aware that when you enable **Refraction**, make sure to set **Blend Mode** to **Alpha**, otherwise the effect does not work as expected. If you enable **Refraction** and use a **Blend Mode** other than **Alpha**, a warning displays in the material Inspector.

Also, be aware that HDRP does not support **Refraction** in the **Pre-Refraction** render pass. If you enable **Refraction** and use the **Pre-Refraction** render pass, a warning displays in the material and Shader Graph Inspector.

<a name="EmissionInputs"></a>

### Emission inputs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAsse
MigrateDecalRenderQueue,
ExposedDecalInputsFromShaderGraph,
FixIncorrectEmissiveColorSpace,
ExposeRefraction,
};

#region Migrations
Expand Down Expand Up @@ -590,6 +591,23 @@ static void FixIncorrectEmissiveColorSpace(Material material, HDShaderUtils.Shad
}
}

static void ExposeRefraction(Material material, HDShaderUtils.ShaderID id)
{
// Lit SG now have a shader feature for refraction instead of an hardcoded material
if (id == HDShaderUtils.ShaderID.SG_Lit)
{
// Sync the default refraction model from the shader graph to the shader
// We need to do this because the material may already have a refraction model information (from the Lit)
// In order to not break the rendering of the material, we patch the refraction model:
if (material.HasProperty(kRefractionModel))
{
var refractionModel = material.shader.GetPropertyDefaultFloatValue(material.shader.FindPropertyIndex(kRefractionModel));
material.SetFloat(kRefractionModel, refractionModel);
}
HDShaderUtils.ResetMaterialKeywords(material);
}
}

#region Serialization_API
//Methods in this region interact on the serialized material
//without filtering on what used shader knows
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public bool TryUpgradeFromMasterNode(IMasterNode1 masterNode, out Dictionary<Blo
systemData.surfaceType = (SurfaceType)eyeMasterNode.m_SurfaceType;
systemData.blendMode = HDSubShaderUtilities.UpgradeLegacyAlphaModeToBlendMode((int)eyeMasterNode.m_AlphaMode);
// Previous master node wasn't having any renderingPass. Assign it correctly now.
systemData.renderingPass = systemData.surfaceType == SurfaceType.Opaque ? HDRenderQueue.RenderQueueType.Opaque : HDRenderQueue.RenderQueueType.Transparent;
systemData.renderQueueType = systemData.surfaceType == SurfaceType.Opaque ? HDRenderQueue.RenderQueueType.Opaque : HDRenderQueue.RenderQueueType.Transparent;
systemData.alphaTest = eyeMasterNode.m_AlphaTest;
systemData.sortPriority = eyeMasterNode.m_SortPriority;
systemData.doubleSidedMode = eyeMasterNode.m_DoubleSidedMode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public bool TryUpgradeFromMasterNode(IMasterNode1 masterNode, out Dictionary<Blo
systemData.surfaceType = (SurfaceType)fabricMasterNode.m_SurfaceType;
systemData.blendMode = HDSubShaderUtilities.UpgradeLegacyAlphaModeToBlendMode((int)fabricMasterNode.m_AlphaMode);
// Previous master node wasn't having any renderingPass. Assign it correctly now.
systemData.renderingPass = systemData.surfaceType == SurfaceType.Opaque ? HDRenderQueue.RenderQueueType.Opaque : HDRenderQueue.RenderQueueType.Transparent;
systemData.renderQueueType = systemData.surfaceType == SurfaceType.Opaque ? HDRenderQueue.RenderQueueType.Opaque : HDRenderQueue.RenderQueueType.Transparent;
systemData.alphaTest = fabricMasterNode.m_AlphaTest;
systemData.sortPriority = fabricMasterNode.m_SortPriority;
systemData.doubleSidedMode = fabricMasterNode.m_DoubleSidedMode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public bool TryUpgradeFromMasterNode(IMasterNode1 masterNode, out Dictionary<Blo
systemData.surfaceType = (SurfaceType)hairMasterNode.m_SurfaceType;
systemData.blendMode = HDSubShaderUtilities.UpgradeLegacyAlphaModeToBlendMode((int)hairMasterNode.m_AlphaMode);
// Previous master node wasn't having any renderingPass. Assign it correctly now.
systemData.renderingPass = systemData.surfaceType == SurfaceType.Opaque ? HDRenderQueue.RenderQueueType.Opaque : HDRenderQueue.RenderQueueType.Transparent;
systemData.renderQueueType = systemData.surfaceType == SurfaceType.Opaque ? HDRenderQueue.RenderQueueType.Opaque : HDRenderQueue.RenderQueueType.Transparent;
systemData.alphaTest = hairMasterNode.m_AlphaTest;
systemData.sortPriority = hairMasterNode.m_SortPriority;
systemData.doubleSidedMode = hairMasterNode.m_DoubleSidedMode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ static public void SetupBaseLitKeywords(Material material)
CoreUtils.SetKeyword(material, "_DISABLE_SSR", material.HasProperty(kReceivesSSR) && material.GetFloat(kReceivesSSR) == 0.0);
CoreUtils.SetKeyword(material, "_DISABLE_SSR_TRANSPARENT", material.HasProperty(kReceivesSSRTransparent) && material.GetFloat(kReceivesSSRTransparent) == 0.0);
CoreUtils.SetKeyword(material, "_ENABLE_GEOMETRIC_SPECULAR_AA", material.HasProperty(kEnableGeometricSpecularAA) && material.GetFloat(kEnableGeometricSpecularAA) == 1.0);

if (material.HasProperty(kRefractionModel))
{
var refractionModelValue = (ScreenSpaceRefraction.RefractionModel)material.GetFloat(kRefractionModel);
// We can't have refraction in pre-refraction queue and the material needs to be transparent
var canHaveRefraction = material.GetSurfaceType() == SurfaceType.Transparent && !HDRenderQueue.k_RenderQueue_PreRefraction.Contains(material.renderQueue);
CoreUtils.SetKeyword(material, "_REFRACTION_PLANE", (refractionModelValue == ScreenSpaceRefraction.RefractionModel.Box) && canHaveRefraction);
CoreUtils.SetKeyword(material, "_REFRACTION_SPHERE", (refractionModelValue == ScreenSpaceRefraction.RefractionModel.Sphere) && canHaveRefraction);
CoreUtils.SetKeyword(material, "_REFRACTION_THIN", (refractionModelValue == ScreenSpaceRefraction.RefractionModel.Thin) && canHaveRefraction);
}
}

static public void SetupStencil(Material material, bool receivesSSR, bool useSplitLighting)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,7 @@ static public void SetupMaterialKeywordsAndPass(Material material)

if (material.HasProperty(kRefractionModel))
{
var refractionModelValue = (ScreenSpaceRefraction.RefractionModel)material.GetFloat(kRefractionModel);
// We can't have refraction in pre-refraction queue and the material needs to be transparent
var canHaveRefraction = material.GetSurfaceType() == SurfaceType.Transparent && !HDRenderQueue.k_RenderQueue_PreRefraction.Contains(material.renderQueue);
CoreUtils.SetKeyword(material, "_REFRACTION_PLANE", (refractionModelValue == ScreenSpaceRefraction.RefractionModel.Box) && canHaveRefraction);
CoreUtils.SetKeyword(material, "_REFRACTION_SPHERE", (refractionModelValue == ScreenSpaceRefraction.RefractionModel.Sphere) && canHaveRefraction);
CoreUtils.SetKeyword(material, "_REFRACTION_THIN", (refractionModelValue == ScreenSpaceRefraction.RefractionModel.Thin) && canHaveRefraction);
CoreUtils.SetKeyword(material, "_TRANSMITTANCECOLORMAP", material.GetTexture(kTransmittanceColorMap) && canHaveRefraction);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void UpgradePBRMasterNode(PBRMasterNode1 pbrMasterNode, out Dictionary<BlockFiel
systemData.blendMode = HDSubShaderUtilities.UpgradeLegacyAlphaModeToBlendMode((int)pbrMasterNode.m_AlphaMode);
systemData.doubleSidedMode = pbrMasterNode.m_TwoSided ? DoubleSidedMode.Enabled : DoubleSidedMode.Disabled;
// Previous master node wasn't having any renderingPass. Assign it correctly now.
systemData.renderingPass = systemData.surfaceType == SurfaceType.Opaque ? HDRenderQueue.RenderQueueType.Opaque : HDRenderQueue.RenderQueueType.Transparent;
systemData.renderQueueType = systemData.surfaceType == SurfaceType.Opaque ? HDRenderQueue.RenderQueueType.Opaque : HDRenderQueue.RenderQueueType.Transparent;
systemData.dotsInstancing = false;
systemData.alphaTest = HDSubShaderUtilities.UpgradeLegacyAlphaClip(pbrMasterNode);
builtinData.addPrecomputedVelocity = false;
Expand Down Expand Up @@ -106,10 +106,12 @@ void UpgradeHDLitMasterNode(HDLitMasterNode1 hdLitMasterNode, out Dictionary<Blo
// Set data
systemData.surfaceType = (SurfaceType)hdLitMasterNode.m_SurfaceType;
systemData.blendMode = HDSubShaderUtilities.UpgradeLegacyAlphaModeToBlendMode((int)hdLitMasterNode.m_AlphaMode);
systemData.renderingPass = hdLitMasterNode.m_RenderingPass;
systemData.renderQueueType = HDRenderQueue.MigrateRenderQueueToHDRP10(hdLitMasterNode.m_RenderingPass);
if (systemData.renderQueueType == HDRenderQueue.RenderQueueType.PreRefraction && !hdLitMasterNode.m_DrawBeforeRefraction)
systemData.renderQueueType = HDRenderQueue.RenderQueueType.Transparent;
// Patch rendering pass in case the master node had an old configuration
if (systemData.renderingPass == HDRenderQueue.RenderQueueType.Background)
systemData.renderingPass = HDRenderQueue.RenderQueueType.Opaque;
if (systemData.renderQueueType == HDRenderQueue.RenderQueueType.Background)
systemData.renderQueueType = HDRenderQueue.RenderQueueType.Opaque;
systemData.alphaTest = hdLitMasterNode.m_AlphaTest;
systemData.sortPriority = hdLitMasterNode.m_SortPriority;
systemData.doubleSidedMode = hdLitMasterNode.m_DoubleSidedMode;
Expand Down Expand Up @@ -262,7 +264,7 @@ bool UpgradeCoatMask(HDLitMasterNode1 masterNode)
}

// Refraction
bool hasRefraction = (systemData.surfaceType == SurfaceType.Transparent && systemData.renderingPass != HDRenderQueue.RenderQueueType.PreRefraction && litData.refractionModel != ScreenSpaceRefraction.RefractionModel.None);
bool hasRefraction = (systemData.surfaceType == SurfaceType.Transparent && systemData.renderQueueType != HDRenderQueue.RenderQueueType.PreRefraction && litData.refractionModel != ScreenSpaceRefraction.RefractionModel.None);
if(hasRefraction)
{
if(!blockMap.TryGetValue(HDBlockFields.SurfaceDescription.Thickness, out _))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public HDLitData litData
};

protected override string[] templateMaterialDirectories => passTemplateMaterialDirectories;
protected override string customInspector => "Rendering.HighDefinition.LightingShaderGraphGUI";
protected override string customInspector => "Rendering.HighDefinition.LitShaderGraphGUI";
protected override GUID subTargetAssetGuid => kSubTargetSourceCodeGuid;
protected override string postDecalsInclude => "Packages/com.unity.render-pipelines.high-definition/Runtime/Material/Lit/LitDecalData.hlsl";
protected override ShaderID shaderID => HDShaderUtils.ShaderID.SG_Lit;
Expand Down Expand Up @@ -83,18 +83,30 @@ protected override SubShaderDescriptor GetRaytracingSubShaderDescriptor()
public static FieldDescriptor SpecularColor = new FieldDescriptor(kMaterial, "SpecularColor", "_MATERIAL_FEATURE_TRANSMISSION 1");

// Refraction
public static FieldDescriptor Refraction = new FieldDescriptor(string.Empty, "Refraction", "_HAS_REFRACTION 1");
public static FieldDescriptor RefractionBox = new FieldDescriptor(string.Empty, "RefractionBox", "_REFRACTION_PLANE 1");
public static FieldDescriptor RefractionSphere = new FieldDescriptor(string.Empty, "RefractionSphere", "_REFRACTION_SPHERE 1");
public static FieldDescriptor RefractionThin = new FieldDescriptor(string.Empty, "RefractionThin", "_REFRACTION_THIN 1");
public static FieldDescriptor Refraction = new FieldDescriptor(string.Empty, "Refraction", "");
public static KeywordDescriptor RefractionKeyword = new KeywordDescriptor()
{
displayName = "Refraction Model",
referenceName = "_REFRACTION",
type = KeywordType.Enum,
definition = KeywordDefinition.ShaderFeature,
scope = KeywordScope.Local,
entries = new KeywordEntry[]
{
new KeywordEntry() { displayName = "Off", referenceName = "OFF" },
new KeywordEntry() { displayName = "Plane", referenceName = "PLANE" },
new KeywordEntry() { displayName = "Sphere", referenceName = "SPHERE" },
new KeywordEntry() { displayName = "Thin", referenceName = "THIN" },
}
};

public override void GetFields(ref TargetFieldContext context)
{
base.GetFields(ref context);
AddDistortionFields(ref context);
var descs = context.blocks.Select(x => x.descriptor);

bool hasRefraction = (systemData.surfaceType == SurfaceType.Transparent && systemData.renderingPass != HDRenderQueue.RenderQueueType.PreRefraction && litData.refractionModel != ScreenSpaceRefraction.RefractionModel.None);
bool hasRefraction = (systemData.surfaceType == SurfaceType.Transparent && litData.refractionModel != ScreenSpaceRefraction.RefractionModel.None);

// Lit specific properties
context.AddField(DotsProperties, context.hasDotsProperties);
Expand All @@ -111,9 +123,6 @@ public override void GetFields(ref TargetFieldContext context)

// Refraction
context.AddField(Refraction, hasRefraction);
context.AddField(RefractionBox, hasRefraction && litData.refractionModel == ScreenSpaceRefraction.RefractionModel.Box);
context.AddField(RefractionSphere, hasRefraction && litData.refractionModel == ScreenSpaceRefraction.RefractionModel.Sphere);
context.AddField(RefractionThin, hasRefraction && litData.refractionModel == ScreenSpaceRefraction.RefractionModel.Thin);

// Misc
context.AddField(EnergyConservingSpecular, litData.energyConservingSpecular);
Expand All @@ -129,7 +138,7 @@ public override void GetFields(ref TargetFieldContext context)

public override void GetActiveBlocks(ref TargetActiveBlockContext context)
{
bool hasRefraction = (systemData.surfaceType == SurfaceType.Transparent && systemData.renderingPass != HDRenderQueue.RenderQueueType.PreRefraction && litData.refractionModel != ScreenSpaceRefraction.RefractionModel.None);
bool hasRefraction = (systemData.surfaceType == SurfaceType.Transparent && systemData.renderQueueType != HDRenderQueue.RenderQueueType.PreRefraction && litData.refractionModel != ScreenSpaceRefraction.RefractionModel.None);
bool hasDistortion = (systemData.surfaceType == SurfaceType.Transparent && builtinData.distortion);

// Vertex
Expand Down Expand Up @@ -163,6 +172,21 @@ public override void CollectShaderProperties(PropertyCollector collector, Genera
base.CollectShaderProperties(collector, generationMode);

HDSubShaderUtilities.AddRayTracingProperty(collector, litData.rayTracing);

// Refraction model property allow the material inspector to check if refraction is enabled in the shader.
collector.AddShaderProperty(new Vector1ShaderProperty{
floatType = FloatType.Enum,
hidden = true,
value = (int)litData.refractionModel,
enumNames = Enum.GetNames(typeof(ScreenSpaceRefraction.RefractionModel)).ToList(),
overrideReferenceName = kRefractionModel,
});
}

protected override void CollectPassKeywords(ref PassDescriptor pass)
{
base.CollectPassKeywords(ref pass);
pass.keywords.Add(RefractionKeyword);
}

protected override void AddInspectorPropertyBlocks(SubTargetPropertiesGUI blockList)
Expand Down
Loading