I am trying to use unrealAR plugin to develop sth. I want to achieve the occlusion between real and virtual objects. One viable way is to pre-render the mesh of real objects into depth buffer and then render the virtual objects which should pass that depth buffer. I ve tried CustomDepth but it does not available for opaque material.
I achieved following effect. I want to know how to give the chair an opaque material instead of translucent. Any idea?
We really need this too. There should’t be any technical reasons to not to allow this. Custom depth is totally separate rendering pass so there isn’t any dependencies. Seems that only reason is that CustomDepth is under the scene texture which can be used for scene color and other buffers that are only available after basepass.
There is sort of a janky way to do this using decals. The custom depth can be read by a decal material, and the decal can be read by the opaque material with the dbuffer node.
Supporting this is only a few lines of code change to the engine if you are building it from source.
Patch file
To apply the patch, copy it to a file (e.g. opaquestencil.patch) then run git apply opaquestencil.patch in the engine source tree.
Engine/Shaders/Private/BasePassPixelShader.usf | 5 +++++
Engine/Shaders/Private/BasePassVertexCommon.ush | 2 ++
.../Engine/Private/Materials/HLSLMaterialTranslator.cpp | 2 +-
.../Source/Runtime/Renderer/Private/BasePassRendering.cpp | 1 +
Engine/Source/Runtime/Renderer/Private/BasePassRendering.h | 1 +
.../Runtime/Renderer/Private/CustomDepthRendering.cpp | 6 +++---
6 files changed, 13 insertions(+), 4 deletions(-)
diff --git a/Engine/Shaders/Private/BasePassPixelShader.usf b/Engine/Shaders/Private/BasePassPixelShader.usf
index f9fa4da66ed3..405eadecaf50 100644
--- a/Engine/Shaders/Private/BasePassPixelShader.usf
+++ b/Engine/Shaders/Private/BasePassPixelShader.usf
@@ -42,6 +42,11 @@
#define SubstrateStruct OpaqueBasePass.Substrate
#define LightFunctionAtlasStruct OpaqueBasePass.Shared.LightFunctionAtlas
+ #if MATERIALBLENDING_SOLID || MATERIALBLENDING_MASKED
+ #define SceneTexturesStruct OpaqueBasePass.SceneTextures
+
+ #endif // MATERIALBLENDING_SOLID
+
// Material setting to allow forward shading (including mobile) to use preintegrated GF lut for simple IBL.
#if MATERIAL_SHADINGMODEL_SINGLELAYERWATER || (FORWARD_SHADING && MATERIAL_USE_PREINTEGRATED_GF)
#define PreIntegratedGF OpaqueBasePass.PreIntegratedGFTexture
diff --git a/Engine/Shaders/Private/BasePassVertexCommon.ush b/Engine/Shaders/Private/BasePassVertexCommon.ush
index 603848cbac15..18b8509660b9 100644
--- a/Engine/Shaders/Private/BasePassVertexCommon.ush
+++ b/Engine/Shaders/Private/BasePassVertexCommon.ush
@@ -10,6 +10,8 @@
// Reroute SceneTexturesStruct uniform buffer references to the appropriate base pass uniform buffer
#if MATERIALBLENDING_TRANSLUCENT || MATERIALBLENDING_ADDITIVE || MATERIALBLENDING_MODULATE
#define SceneTexturesStruct TranslucentBasePass.SceneTextures
+#elif MATERIALBLENDING_SOLID || MATERIALBLENDING_MASKED
+ #define SceneTexturesStruct OpaqueBasePass.SceneTextures
#endif
#include "/Engine/Generated/Material.ush"
diff --git a/Engine/Source/Runtime/Engine/Private/Materials/HLSLMaterialTranslator.cpp b/Engine/Source/Runtime/Engine/Private/Materials/HLSLMaterialTranslator.cpp
index d1aa3ee53aa9..f00f28e1351a 100644
--- a/Engine/Source/Runtime/Engine/Private/Materials/HLSLMaterialTranslator.cpp
+++ b/Engine/Source/Runtime/Engine/Private/Materials/HLSLMaterialTranslator.cpp
@@ -2005,7 +2005,7 @@ void FHLSLMaterialTranslator::TranslateMaterial()
if (!MaterialShadingModels.HasShadingModel(MSM_SingleLayerWater) && IsOpaqueOrMaskedBlendMode(BlendMode))
{
// In opaque pass, none of the textures are available
- Errorf(TEXT("SceneTexture expressions cannot be used in opaque materials except if used with the Single Layer Water shading model."));
+ //Errorf(TEXT("SceneTexture expressions cannot be used in opaque materials except if used with the Single Layer Water shading model."));
}
else if (bNeedsSceneTexturePostProcessInputs)
{
diff --git a/Engine/Source/Runtime/Renderer/Private/BasePassRendering.cpp b/Engine/Source/Runtime/Renderer/Private/BasePassRendering.cpp
index bad99bd5f5bb..069795137021 100644
--- a/Engine/Source/Runtime/Renderer/Private/BasePassRendering.cpp
+++ b/Engine/Source/Runtime/Renderer/Private/BasePassRendering.cpp
@@ -820,6 +820,7 @@ TRDGUniformBufferRef<FOpaqueBasePassUniformParameters> CreateOpaqueBasePassUnifo
{
FOpaqueBasePassUniformParameters& BasePassParameters = *GraphBuilder.AllocParameters<FOpaqueBasePassUniformParameters>();
SetupSharedBasePassParameters(GraphBuilder, View, ViewIndex, bLumenGIEnabled, BasePassParameters.Shared);
+ SetupSceneTextureUniformParameters(GraphBuilder, View.GetSceneTexturesChecked(), View.FeatureLevel, ESceneTextureSetupMode::CustomDepth, BasePassParameters.SceneTextures);
const FRDGSystemTextures& SystemTextures = FRDGSystemTextures::Get(GraphBuilder);
diff --git a/Engine/Source/Runtime/Renderer/Private/BasePassRendering.h b/Engine/Source/Runtime/Renderer/Private/BasePassRendering.h
index cb560f0f00db..29d796d5c62d 100644
--- a/Engine/Source/Runtime/Renderer/Private/BasePassRendering.h
+++ b/Engine/Source/Runtime/Renderer/Private/BasePassRendering.h
@@ -89,6 +89,7 @@ END_GLOBAL_SHADER_PARAMETER_STRUCT()
BEGIN_GLOBAL_SHADER_PARAMETER_STRUCT(FOpaqueBasePassUniformParameters,)
SHADER_PARAMETER_STRUCT(FSharedBasePassUniformParameters, Shared)
+ SHADER_PARAMETER_STRUCT(FSceneTextureUniformParameters, SceneTextures)
SHADER_PARAMETER_STRUCT(FSubstrateBasePassUniformParameters, Substrate)
// Forward shading
SHADER_PARAMETER(int32, UseForwardScreenSpaceShadowMask)
diff --git a/Engine/Source/Runtime/Renderer/Private/CustomDepthRendering.cpp b/Engine/Source/Runtime/Renderer/Private/CustomDepthRendering.cpp
index 334db63787be..2d58e689f57b 100644
--- a/Engine/Source/Runtime/Renderer/Private/CustomDepthRendering.cpp
+++ b/Engine/Source/Runtime/Renderer/Private/CustomDepthRendering.cpp
@@ -14,11 +14,11 @@
static TAutoConsoleVariable<int32> CVarCustomDepthOrder(
TEXT("r.CustomDepth.Order"),
- 2,
+ 0,
TEXT("When CustomDepth (and CustomStencil) is getting rendered\n")
- TEXT(" 0: Before Base Pass (Allows samping in DBuffer pass. Can be more efficient with AsyncCompute.)\n")
+ TEXT(" 0: Default. Before Base Pass (Allows samping in DBuffer pass. Can be more efficient with AsyncCompute.)\n")
TEXT(" 1: After Base Pass\n")
- TEXT(" 2: Default (Before Base Pass if DBuffer enabled.)\n"),
+ TEXT(" 2: Before Base Pass if DBuffer enabled.\n"),
ECVF_RenderThreadSafe);
static TAutoConsoleVariable<int32> CVarCustomDepthTemporalAAJitter(
Caveats:
I have only tested this with the desktop forward renderer, although there’s no particular reason it shouldn’t work with the deferred renderer and with nanite.
If you try and use SceneTextures other than CustomDepth and CustomStencil from your opaque material, you won’t get an error message anymore. They just won’t work.