Version: Unity 6.2 Alpha (6000.2)
Language : English
Example of a Surface Shader that supports GPU Instancing in the Built-In Render Pipeline
Example of changing per-instance data at runtime in the Built-In Render Pipeline

Example of a shader that supports GPU Instancing in the Built-In Render Pipeline

The following example demonstrates how to create an instanced vertex and fragment shaderA program that runs on the GPU. More info
See in Glossary
with different color values for each instance. Unlike the surface shaderA streamlined way of writing shaders for the Built-in Render Pipeline. More info
See in Glossary
, when you create the vertex and fragment shader you must use UNITY_SETUP_INSTANCE_ID to manually set up an instance ID.

Shader "Custom/SimplestInstancedShader"
{
    Properties
    {
        _Color ("Color", Color) = (1, 1, 1, 1)
    }

    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_instancing
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                UNITY_VERTEX_INPUT_INSTANCE_ID
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
                UNITY_VERTEX_INPUT_INSTANCE_ID // use this to access instanced properties in the fragment shader.
            };

            UNITY_INSTANCING_BUFFER_START(Props)
            UNITY_DEFINE_INSTANCED_PROP(float4, _Color)
            UNITY_INSTANCING_BUFFER_END(Props)

            v2f vert(appdata v)
            {
                v2f o;

                UNITY_SETUP_INSTANCE_ID(v);
                UNITY_TRANSFER_INSTANCE_ID(v, o);
                o.vertex = UnityObjectToClipPos(v.vertex);
                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                UNITY_SETUP_INSTANCE_ID(i);
                return UNITY_ACCESS_INSTANCED_PROP(Props, _Color);
            }
            ENDCG
        }
    }
}

对文档有任何疑问,请移步至开发者社区提问,我们将尽快为您解答
Example of a Surface Shader that supports GPU Instancing in the Built-In Render Pipeline
Example of changing per-instance data at runtime in the Built-In Render Pipeline
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961