UE4 CustomDepthMobile流程小记

UE4 CustomDepthMobile流程小记

原生UE opaque材质中获取CustomDepth/CustomStencil会报错

在其Compile中调用的函数中没有看到报错逻辑

UE4 CustomDepthMobile流程小记

材质节点的逻辑都没有什么问题,所以看一下报错

在HLSLMaterialTranslator::Translate中

UE4 CustomDepthMobile流程小记

UE4 CustomDepthMobile流程小记

UE4 CustomDepthMobile流程小记

修改之后 mobile流程的不透明材质可以直接获取SceneTexture::customdepth节点结果

但是PC流程会崩

UE4 CustomDepthMobile流程小记UE4 CustomDepthMobile流程小记

材质编译的时候mobile和PC流程都会走 ,deffered流程CustomDepth是在BasePass之后走的,所以拿不到,会报uniformbuffer的错。改官方引擎的话,为了防止报错材质里需要加feature switch。PC这个流程改CustomDepth的渲染顺序改动太大,不如自己新开一个自定义的深度pass。

UE4 CustomDepthMobile流程小记

(以下讨论的都是Mobile流程在opaque材质中用customdepth,非后处理材质的情况)

到这里,Mobile流程的Opaque材质中用节点获取customdepth是可用的,但马上又遇到了下一个问题,在customnode中直接写采样CustomDepthTexture的结果是不对的

UE4 CustomDepthMobile流程小记

UE4 CustomDepthMobile流程小记UE4 CustomDepthMobile流程小记

截帧查看,场景中物件勾上RenderCustomDepthPass的开关,材质中不用customdepth节点的时候,或者用自定义的custom节点中直接拿采样结果的时候,CustomDepth pass都是没有的。

那就可以确认一点,材质的SceneTexture节点一定有一些引擎相关flag/uniformbuffer相关的配置设置,是否渲CustomDepth pass不只是由模型勾选是否渲自定义深度决定的,材质的compile结果中一定有一些配置相关逻辑。

UE4 CustomDepthMobile流程小记

UE4 CustomDepthMobile流程小记

过一下流程,可以根据Template采样结果定位uniformbuffer,看和后面的分析对不对得上

UE4 CustomDepthMobile流程小记

UE4 CustomDepthMobile流程小记

再看看SceneTexture的Compile中做了哪些操作

UE4 CustomDepthMobile流程小记

UE4 CustomDepthMobile流程小记

UE4 CustomDepthMobile流程小记

UE4 CustomDepthMobile流程小记

UE4 CustomDepthMobile流程小记

UE4 CustomDepthMobile流程小记

UE4 CustomDepthMobile流程小记

UE4 CustomDepthMobile流程小记

UE4 CustomDepthMobile流程小记

UE4 CustomDepthMobile流程小记

在MobileSceneRenderer::InitViews中判断

UE4 CustomDepthMobile流程小记

UE4 CustomDepthMobile流程小记

UE4 CustomDepthMobile流程小记

该buffer包含了PC和Mobile

UE4 CustomDepthMobile流程小记

UE4 CustomDepthMobile流程小记

uniformbuffer的传参是对应上的

到这一步为止,如果我需要写一个循环采样customdepth的结果,是没法直接在材质的customnode中写采样CustomDepthTexture的。需要单独加一个自定义节点,Compile中实现SceneTexture节点配置部分相同的逻辑,然后在Template中添加自定义函数自定义采样逻辑才行。

但是,如果说我就是不想反复改代码,就想要在材质customnode里写呢。

其实是可以的,比如是否可以如下修改

UE4 CustomDepthMobile流程小记

这样修改后,原本是场景物体要勾RenderCustomDepthPass开关,并且材质节点SceneTexture走Complie逻辑后beShouldRenderCustomDepth才为true。而修改后是场景中物体只要勾了RenderCustomDepthPass开关,beShouldRenderCustomDepth就会true。这样改的好处是材质customnode中就可以直接写采样MobileSceneTextures的结果了,但是坏处是这样改不太符合管线设计的逻辑,比如这样场景中物件只要打了勾,后面材质没有到自定义深度信息,customdepthpass也是一直走的,不安全。总结就是能用,但最合理的方案还是单加上述的材质自定义节点。

本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://net2asp.com/fe9d699c08.html