Version: 6000.3
语言: 中文
预览 3D 纹理
通过脚本创建 3D 纹理

在着色器中对 3D 纹理进行采样

在自定义着色器在 GPU 上运行的程序。更多信息
请参阅术语表
,您可以使用以下内容:

  • 3Dmaterial 属性声明以添加 3D 纹理属性。 例如_MainTex ("Example 3D texture", 3D) = "white" {};
  • sampler3D采样器状态,以将 3D 纹理用作着色器输入。 例如sampler3D _MainTex;
  • tex3DHLSL 方法对 3D 纹理进行采样。 例如float4 color = tex3D(_MainTex, float3(0.5f, 0.5f, 0.5f));

下面是一个着色器示例,该着色器使用光线行进对 3D 纹理进行采样并将其渲染为半透明体积。

要使用该示例,请执行以下步骤:

  1. 在 项目(Project) 窗口中,使用 添加>着色器(Add Shader) > 无光照着色器(Unlit Shader) 创建新的基本着色器。
  2. 将着色器代码替换为示例代码。
  3. 项目(Project) 窗口中右键点击着色器,然后选择 创建>材质(Create Material) 以从着色器创建新材质。
  4. 导入或创建 3D 纹理。例如,使用在脚本中创建 3D 纹理页面中的示例代码。
  5. 将 3D 纹理资源从 Project 窗口拖动到材质的 Texture 3D 属性。
  6. 将材质附加到游戏对象Unity 场景中的基本对象,可以表示角色、道具、风景、相机、航路点等。游戏对象的功能由附加到它的组件定义。更多信息
    请参阅术语表
    .
Shader "Unlit/VolumeShader"
{
    Properties
    {
        _MainTex ("Texture", 3D) = "white" {}
        _Alpha ("Alpha", float) = 0.02
        _StepSize ("Step Size", float) = 0.01
    }
    SubShader
    {
        Tags { "Queue" = "Transparent" "RenderType" = "Transparent" }
        Blend One OneMinusSrcAlpha
        LOD 100

        Pass
        {
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            // Maximum number of raymarching samples
            #define MAX_STEP_COUNT 128

            // Allowed floating point inaccuracy
            #define EPSILON 0.00001f

            struct appdata
            {
                float4 vertex : POSITION;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
                float3 objectVertex : TEXCOORD0;
                float3 vectorToSurface : TEXCOORD1;
            };

            sampler3D _MainTex;
            float4 _MainTex_ST;
            float _Alpha;
            float _StepSize;

            v2f vert (appdata v)
            {
                v2f o;

                // Vertex in object space. This is the starting point for the raymarching.
                o.objectVertex = v.vertex;

                // Calculate vector from camera to vertex in world space
                float3 worldVertex = mul(unity_ObjectToWorld, v.vertex).xyz;
                o.vectorToSurface = worldVertex - _WorldSpaceCameraPos;

                o.vertex = UnityObjectToClipPos(v.vertex);
                return o;
            }

            float4 BlendUnder(float4 color, float4 newColor)
            {
                color.rgb += (1.0 - color.a) * newColor.a * newColor.rgb;
                color.a += (1.0 - color.a) * newColor.a;
                return color;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                // Start raymarching at the front surface of the object
                float3 rayOrigin = i.objectVertex;

                // Use vector from camera to object surface to get ray direction
                float3 rayDirection = mul(unity_WorldToObject, float4(normalize(i.vectorToSurface), 1));

                float4 color = float4(0, 0, 0, 0);
                float3 samplePosition = rayOrigin;

                // Raymarch through object space
                for (int i = 0; i < MAX_STEP_COUNT; i++)
                {
                    // Accumulate color only within unit cube bounds
                    if(max(abs(samplePosition.x), max(abs(samplePosition.y), abs(samplePosition.z))) < 0.5f + EPSILON)
                    {
                        float4 sampledColor = tex3D(_MainTex, samplePosition + float3(0.5f, 0.5f, 0.5f));
                        sampledColor.a *= _Alpha;
                        color = BlendUnder(color, sampledColor);
                        samplePosition += rayDirection * _StepSize;
                    }
                }

                return color;
            }
            ENDHLSL
        }
    }
}


如果将示例着色器与“在脚本中创建 3D 纹理”页中的 3D 纹理一起使用,则 Unity 将呈现该卷。

其他资源

预览 3D 纹理
通过脚本创建 3D 纹理