使用Depth Texture

使用Depth Textures:
  可以将depth信息渲染到一张texture,有些效果的制作会需要scene depth信息,此时depth texture就可以派上用场了。
  Depth Texture在不同平台上有不同的实现,并且原生的支持也不一样。
  UnityCG.cginc里面定义了一些使用depth texture的帮助宏定义:
    UNITY_TRANSFER_DEPTH(o) 计算eye space的深度值,并写入变量o(float2)。当需要渲染到一张深度贴图时,在vertex shader中使用该函数。在原生就支持depth texture的平台上,该函数啥也不做,因为Z buffer的值会被渲染。
    UNITY_OUTPUT_DEPTH(i) 根据i(float2)返回eye space深度值。当需要渲染到一张深度贴图时,在fragment shader中使用该函数。在原生就支持depth texture的平台上,该函数总是返回0。
    COMPUTE_EYEDEPTH(i) 计算eye space的深度值。在vertex shader中使用,当不渲染到depth texture,而只是获取该值时时使用该函数。
    DECODE_EYEDEPTH(i) 从depth texture i中得到高精度的eye space depth。

    Shader "Render Depth" {
        SubShader {
            Tags { "RenderType"="Opaque" }
            Pass {
                Fog { Mode Off }
        CGPROGRAM

        #pragma vertex vert
        #pragma fragment frag
        #include "UnityCG.cginc"

        struct v2f {
            float4 pos : SV_POSITION;
            float2 depth : TEXCOORD0;
        };

        v2f vert (appdata_base v) {
            v2f o;
            o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
            UNITY_TRANSFER_DEPTH(o.depth);
            return o;
        }

        half4 frag(v2f i) : COLOR {
            UNITY_OUTPUT_DEPTH(i.depth);
        }
        ENDCG
            }
        }
    }

Camera‘s Depth Texture:
  Camera能够生成一张depth texture或者depth+normals texture。可以用来实现一些后处理效果或自定义的光照模式等。
  Depth Texture可以直接来自于depth buffer,或者是基于Shader Replacement特性的一个独立的pass来实现,所以也可以自己来做这件事。
  变量:Camera.depthTextureMode
  取值:
    DepthTextureMode.Depth:一张screen-sized的depth贴图。
    DepthTextureMode.DepthNormals:
      screen-sized 32 bit(8 bit/channel)texture,包含depth和view space normals信息。
      noramls存放在R和G通道,depth使用B和A通道。
  [UnityCG.cginc]DecodeDepthNormal(float4 enc, out float depth, out float3 normal)函数可以用来从pixel value中解码出depth和normal值,返回的depth为0..1的范围。

时间: 2024-08-29 03:08:25

使用Depth Texture的相关文章

Camera’s Depth Texture

[Camera’s Depth Texture] In Unity a Camera can generate a depth or depth+normals texture. This is a minimalistic G-buffer texture that can be used for post-processing effects or to implement custom lighting models (e.g. light pre-pass). Camera actual

opengl 教程(23) shadow mapping (2)

原帖地址:http://ogldev.atspace.co.uk/www/tutorial24/tutorial24.html Background In the previous tutorial we learned the basic principle behind the shadow mapping technique and saw how to render the depth into a texture and later display it on the screen b

A Simple OpenGL Shader Example II

A Simple OpenGL Shader Example II [email protected] Abstract. The OpenGL Shading Language syntax comes from the C family of programming languages. Tokes, identifiers, semicolons, nesting with curly braces, control-flow, and many key words look like C

D3D9 优化小技巧

此篇文章主要讲一些小技巧,针对前面转载的D3D9 GPU Hacks,我们可以做的一些优化. 在做延迟渲染或者其它需要深度的地方使用INTZ格式的纹理,这样可以直接对纹理进行操作,节省了显存和带宽,这样即使在前向渲染的时候也可以获取深度,有了深度信息我们就可以做很多效果,如水的柔边,水边泡沫,景深等效果. 注:以下示例代码均摘自http://developer.amd.com/wordpress/media/2012/10/Advanced-DX9-Capabilities-for-ATI-Ra

OpenGL 阴影之Shadow Mapping和Shadow Volumes

先说下开发环境.VS2013,C++空项目,引用glut,glew.glut包含基本窗口操作,免去我们自己新建win32窗口一些操作.glew使我们能使用最新opengl的API,因winodw本身只包含opengl 1.1版本的API,根本是不能用的. 其中矩阵计算采用gitHub项目openvr中的三份文件, Vectors.h ,Matrices.h, Matrices.cpp,分别是矢量与点类,矩阵类,我们需要的一些操作,矢量的叉乘和点乘,矩阵转置,矩阵的逆,矩阵与矢量相剩等. 这里主要

Unity3D组件参考手册

Refer to the information on these pages for details on working in-depth with various aspects of Unity. 这些页面的参考信息,是有关Unity深入工作的各个方面的详细信息. The Unity Manual Guide contains sections that apply only to certain platforms. Please select which platforms you

三言两语说shader(九)钻石

这次的目标是绘制一颗闪闪发光的钻石,追求效果是越接近真实越好. 先说说为此我这几天干了些什么. 1.看了stalendp blog里那篇<钻石效果>后头的参考文献 最有价值的就是ATI在2004年GDC上作的演讲,题目就叫Drawing a Diamond.但是由于只有ppt,所以很难重现工程深入学习.思路大概是预备了一张折射CubeMap,一张带模拟色散的反射CubeMap,最后再加上耀斑混合而成. 2.试验了一些简单的镜面反射 非实时的CubeMap反射最简单没什么好说的,需要注意的是要做

OpenGL的版本历史和发展

来源请注明,本文永久地址为http://www.cnblogs.com/vertexshader/articles/2917540.html OpenGL®作为业界最为广泛使用的2D和3D图形接口标准,应用在成千上万的各式各样的计算机的程序中.从初期的崭露头角,到与Direct3D激烈竞争,后经历黯淡被Khronos接手又发扬光大,已经历经波折发展了20年.由于过去的黯淡,至今甚至仍有人站在错误的时间角度认为它是落后的--它从未停止它前进的步伐,这篇文章就来简述OpenGL的版本历史和发展. O

OpenGL的版本号历史和发展

来源请注明.本文永久地址为http://www.cnblogs.com/vertexshader/articles/2917540.html OpenGL®作为业界最为广泛使用的2D和3D图形接口标准.应用在成千上万的各式各样的计算机的程序中.从初期的崭露头角,到与Direct3D激烈竞争.后经历黯淡被Khronos接手又发扬光大.已经历经波折发展了20年. 由于过去的黯淡.至今甚至仍有人站在错误的时间角度觉得它是落后的--它从未停止它前进的步伐,这篇文章就来简述OpenGL的版本号历史和发展.