Version: 2020.2
渲染纹理
电影纹理 (Movie Textures)

自定义渲染纹理

Custom Render Textures are a special type of texture that allows you to update a texture with a shader. They are an extension to Render Textures. You can use Custom Render Textures to create complex simulations like caustics, ripple simulation for rain effects and liquid splatters.

Custom Render Textures provides a scripting and Shader framework to help with more complicated configuration like partial or multi-pass updates or varying update frequency. To use this framework you need to assign a Material to the Custom Render Texture asset. To do this:

  1. Create a new Custom Render Texture asset
  2. Assign a Material to it in the inspector window in the Material property

This Material updates the content of the texture according to its parameters. You can assign any kind of Material to a Custom Render Texture, including a Material that is assigned to another Custom Render Texture.

渲染管线兼容性

The table below describes the compatibility between the Custom Render Textures feature and each render pipeline:

功能 内置渲染管线 通用渲染管线 (URP) 高清渲染管线 (HDRP) 自定义可编程渲染管线 (SRP)
自定义渲染纹理 是 (1) 是 (1) 是 (1) 是 (1)

注意:

  1. Currently, you can’t create Materials that update and initialize Custom Render Textures in Shader Graph. You can only create Unity shaders that operate on Custom Render Textures by writing ShaderLab code.

属性

The Custom Render Textures inspector window displays many of the same properties as the Render Texture inspector, and some properties specific to Custom Render Textures.

渲染纹理:

属性: 功能:
Dimension 渲染纹理的尺寸。
     2D Makes the Render Texture two dimensional.
     Cube 使渲染纹理成为立方体贴图。
     3D Makes the Render Texture three dimensional.
Size 渲染纹理的大小(以像素为单位)。
Color Format The Format of the Render Texture.
sRGB (Color Render Texture) 启用可允许渲染纹理使用 sRGB 读/写转换(只读)。
Enable Mip Maps Enable to allow the Render Texture to use Mip Maps.
Auto generate Mip Maps 启用此属性可自动生成 MipMap。
Wrap Mode 定义纹理平铺时的行为方式。
     Repeat 纹理以瓦片形式重复自身。
     Clamp 纹理的边缘被拉伸。
Filter Mode 定义当纹理在通过 3D 变换拉伸时 Unity 如何过滤纹理。
     Point 纹理在靠近时变为块状。
     Bilinear 纹理在靠近时变得模糊。
     Trilinear 与 Bilinear 类似,但纹理也在不同的 Mip 级别之间模糊。
Aniso Level 以大角度查看纹理时提高纹理质量。适用于地板和地面纹理。

自定义纹理:

这些属性是自定义渲染纹理独有的。自定义纹理参数分为三个类别:

  • Material:定义用于更新纹理的着色器。

  • Initialization: Controls how the texture is initialized before the shader performs any updates.

  • Update: Controls how the shader updates the texture.

属性: 功能:
Material The Material used to update the Custom Render Texture
     Shader Pass The Shader Pass used to update the Custom Render Texture. The combo box shows all passes available in your Material.
Initialization Mode Unity 初始化纹理的频率。
     OnLoad Unity 在创建时初始化纹理一次。
     Realtime Unity 在每一帧初始化纹理。
     OnDemand Unity 通过脚本按需初始化纹理。
Source Unity 纹理如何初始化纹理。
     Texture and Color A texture multiplied by a color initializes the texture.
         Initialization Color Defines the color that initializes the Custom Render Texture. If you also provide an Initialization Texture, the multiplication of the color and the texture initializes the the Custom Render Texture.
         Initialization Texture Defines the texture that initializes the Custom Render Texture. If you also provide an Initialization Color, the multiplication of the color and the texture initializes the the Custom Render Texture.
     Material A Material initialises the texture.
         Initialization Material Defines the Material that initializes the Custom Render Texture.
Update Mode The rate at which the shader updates the Custom Render Texture..
     OnLoad 着色器在创建时更新纹理一次。
     Realtime 着色器在每一帧更新纹理。
     OnDemand 着色器通过脚本按需更新纹理。
Period The period in seconds at which Unity updates a real-time texture. A value of 0.0 updates every frame. This property is only available in real time.
Double Buffered 对纹理进行双缓冲。每次更新都会交换两个缓冲区。这样可以在着色器中读取先前更新的结果。
Wrap Update Zones 启用此属性允许部分更新区域包围纹理的边框。
Cubemap Faces (仅限立方体贴图)通过一系列开关允许用户对每个立方体贴图面启用/禁用更新。
Update Zone Space 在此坐标系统中定义更新区域。
     Normalized 所有坐标和大小都在 0 到 1 之间,左上角从 (0, 0) 开始。
     Pixel 所有坐标和大小都以像素宽度和高度所限制的像素表示。左上角从 (0, 0) 开始。
Update Zone List List of update zones for the texture (see below for more details).

将自定义渲染纹理导出到文件:

You can export Custom Render Textures to a PNG or EXR file (depending on the texture format) via the contextual “Export” menu.

更新区域 (Update Zones):

When Unity updates the Custom Render Texture, the Material updates the whole texture at once by default. The Custom Texture allows you to define zones of partial update. You can use this to define as many zones as you want and the order in which the zones are processed.

This is useful for various purposes. For example, you could have multiple small zones to splat water drops on the texture and then do a full pass to simulate the ripples. This can also be used as an optimization when you know that you don’t need to update the full texture.

Update zones have their own set of properties. The Update Zone Space is visible in the display. Coordinates depend on the Dimension of the texture: 2D for 2D and Cube textures, or 3D for 3D textures.

属性: 功能:
Center 更新区域中心的坐标。
Size 更新区域的大小。
Rotation 更新区域的方向(以度为单位,3D 纹理中不可用)。
Shader Pass 定义用于此更新区域的着色器通道。如果将此属性设置为默认值,则此更新区域会使用在检视面板主要部分中定义的着色器通道。否则,它会使用提供的着色器通道。
Swap (Double Buffer) (Only for Double Buffered textures) If Swap is enabled, Unity swaps the buffers before processing this update zone.

双缓冲自定义纹理

Custom Render Textures can be double-buffered. This means that inside one Custom Render Texture there are two textures which Unity can swap after each update. This allows you to read the result of the last update while writing a new result in the Custom Render Texture. This is particularly useful if the shader needs to use the content already written in the texture but cannot mix the values with classic blend modes. This is also required if the shaders have to sample different pixels of the preceding result.

Performance warning: Double buffering currently involves a copy of the texture at each swap which can lead to a drop in performance depending on the frequency at which it is done and the resolution of the texture.

链接自定义渲染纹理

Custom Render Textures require a Material to be updated. This Material can have textures as an input. This means that you can use a Custom Render Texture as an input to generate another one. This way, you can chain up several Custom Textures to generate a more complex multi-step simulation. The system will correctly handle all the dependencies so that the different updates happen in the right order.

为自定义渲染纹理编写着色器

Updating a Custom Texture is like performing 2D post processing in a Render Texture. To help you write your custom texture shaders, here is a small framework with utility functions and built-in helper variables.

The following shader example fills the texture with a color multiplied by a color:

Shader "CustomRenderTexture/Simple"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        _Tex("InputTex", 2D) = "white" {}
     }

     SubShader
     {
        Lighting Off
        Blend One Zero

        Pass
        {
            CGPROGRAM
            #include "UnityCustomRenderTexture.cginc"
            #pragma vertex CustomRenderTextureVertexShader
            #pragma fragment frag
            #pragma target 3.0

            float4      _Color;
            sampler2D   _Tex;

            float4 frag(v2f_customrendertexture IN) : COLOR
            {
                return _Color * tex2D(_Tex, IN.localTexcoord.xy);
            }
            ENDCG
            }
    }
}

为自定义纹理编写着色器时,唯一必需的步骤是:

  • #include "UnityCustomRenderTexture.cginc"

  • 使用提供的顶点着色器 CustomRenderTextureVertexShader

  • Use the provided input structure v2f_customrendertexture for the pixel shader

Here is another example of a shader used in an initialization Material:

Shader "CustomRenderTexture/CustomTextureInit"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        _Tex("InputTex", 2D) = "white" {}
    }

    SubShader
    {
        Lighting Off
        Blend One Zero

        Pass
        {
            CGPROGRAM
            #include "UnityCustomRenderTexture.cginc"

            #pragma vertex InitCustomRenderTextureVertexShader
            #pragma fragment frag
            #pragma target 3.0

            float4      _Color;
            sampler2D   _Tex;

            float4 frag(v2f_init_customrendertexture IN) : COLOR
            {
                return _Color * tex2D(_Tex, IN.texcoord.xy);
            }
            ENDCG
        }
    }
}

与更新着色器的情况下相同,唯一必需的步骤如下:

* #include “UnityCustomRenderTexture.cginc”

  • 使用提供的顶点着色器 InitCustomRenderTextureVertexShader

  • 为像素着色器使用提供的输入结构 v2f_init_customrendertexture

This example shader includes a set of built-in values. This includes Global values, values from the v2f_customrendertexture structure and values from the v2f_init_customrendertexture structure.

The input values from the v2f_customrendertexture structure:

名称 Type
localTexcoord float3 相对于当前正在处理的更新区域的纹理坐标。
globalTexcoord float3 相对于自定义渲染纹理本身的纹理坐标
primitiveID uint 当前正在处理的更新区域的索引。
direction float3 对于立方体自定义渲染纹理,立方体贴图内当前像素的方向。

The input values from the v2f_init_customrendertexture structure:

名称 Type
texcoord float3 相对于自定义渲染纹理本身的纹理坐标。

全局值:

名称 Type
_CustomRenderTextureWidth float 自定义纹理的宽度(以像素为单位)
_CustomRenderTextureHeight float 自定义纹理的高度(以像素为单位)
_CustomRenderTextureDepth float 自定义纹理的深度(以像素为单位,仅适用于 3D 纹理,否则将始终等于 1)。
_CustomRenderTextureCubeFace float 仅适用于立方体贴图:正在处理的当前立方体贴图面的索引 (-X, +X, -Y, +Y, -Z, +Z)。
_CustomRenderTexture3DSlice float 仅适用于 3D 纹理:正在处理的当前 3D 切片的索引。
_SelfTexture2D Sampler2D 仅适用于双缓冲纹理:该纹理包含上次交换之前的上次更新的结果。
_SelfTextureCube SamplerCUBE 仅适用于双缓冲纹理:该纹理包含上次交换之前的上次更新的结果。
_SelfTexture3D Sampler3D 仅适用于双缓冲纹理:该纹理包含上次交换之前的上次更新的结果。

通过脚本控制自定义渲染纹理

Most of the functionalities described here can be accessed via the Scripting API. Changing Material parameters, update frequency, update zones, requesting an update etc, can all be done with the script.

Any update requested for the Custom Texture will happen at a very specific time at the beginning of the frame with the current state of the Custom Texture. This guarantees that any Material using this texture will have the up-to-date result.

这意味着以下模式:

customRenderTexture.updateZones = updateZones1;
customRenderTexture.Update();
customRenderTexture.updateZones = updateZones2;
customRenderTexture.Update();

Will not yield the “expected” result of one update done with the first array of update zones and then a second update with the other array. This will perform two updates with the second array.

Essentially, any property modified will only be active in the next frame.


  • 2017–05–18 页面已发布

  • Unity 2017.1 中的新功能 NewIn20171

渲染纹理
电影纹理 (Movie Textures)
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961