简体   繁体   English

Three.js 从纹理渲染深度

[英]Three.js render depth from texture

Is it possible to somehow render to depth buffer from pre-rendered texture?是否有可能以某种方式从预渲染纹理渲染到深度缓冲区?

I am pre-rendering scene like original resident evil games and I would like to apply both pre-rendered depth and color texture to screen.我正在像原始生化危机游戏一样预渲染场景,我想将预渲染的深度和颜色纹理应用到屏幕上。

I previously used technique to make simpler proxy scene for depth but I am wondering if there is a way to use precise pre rendered depth texture instead.我以前使用技术来制作更简单的深度代理场景,但我想知道是否有办法使用精确的预渲染深度纹理来代替。

three.js provides a DepthTexture class which can be used to save the depth of a rendered scene into a texture. three.js提供了一个DepthTexture class 可用于将渲染场景的深度保存到纹理中。 Typical use cases for such a texture are post processing effects like Depth-of-Field or SSAO.这种纹理的典型用例是后处理效果,如景深或 SSAO。

If you bind a depth texture to a shader, you can sample it like any other texture.如果将深度纹理绑定到着色器,则可以像任何其他纹理一样对其进行采样。 However, the sampled depth value is sometimes converted to different representations for further processing.但是,采样的深度值有时会转换为不同的表示形式以供进一步处理。 For instance you could compute the viewZ value (which is the z-distance between the rendered fragment and the camera) or convert between perspective and orthographic depth and vice versa.例如,您可以计算viewZ值(即渲染片段和相机之间的 z 距离)或在透视和正交深度之间进行转换,反之亦然。 three.js provides helper functions for such tasks. three.js为此类任务提供了帮助函数。

The official depth texture example uses these helper functions in order to visualize the scene's depth texture.官方深度纹理示例使用这些辅助函数来可视化场景的深度纹理。 The important function is:重要的 function 是:

float readDepth( sampler2D depthSampler, vec2 coord ) {
    float fragCoordZ = texture2D( depthSampler, coord ).x;
    float viewZ = perspectiveDepthToViewZ( fragCoordZ, cameraNear, cameraFar );
    return viewZToOrthographicDepth( viewZ, cameraNear, cameraFar );
}

In the example, the resulting depth value is used to compute the final color of the fragment.在示例中,生成的深度值用于计算片段的最终颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM