如何使用一个着色器为每个对象应用不同的值?

How can I use one shader to apply different values for each object?

在层次结构中我有 3 个游戏对象。

例如立方体,如果我要在检查器中更改轮廓宽度,它会将更改应用到所有三个对象,也应用到球体和圆柱体。

但我希望,如果我更改立方体上的轮廓宽度,它只会更改立方体的轮廓宽度,如果我更改球体的轮廓宽度,则只会更改球体的轮廓宽度。

这是着色器:

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader "Outlined/Silhouetted Diffuse" {
    Properties {
        _Color ("Main Color", Color) = (.5,.5,.5,1)
        _OutlineColor ("Outline Color", Color) = (0,0,0,1)
        _Outline ("Outline width", Range (0.0, 0.1)) = .005
        _MainTex ("Base (RGB)", 2D) = "white" { }
    }

    SubShader {
        Tags { "Queue" = "Transparent" }

        Pass {
            Name "OUTLINE"
            Tags { "LightMode" = "Always" }
            Cull Off
            ZWrite Off
            ZTest Always
            ColorMask RGB // alpha not used

            // you can choose what kind of blending mode you want for the outline
            Blend SrcAlpha OneMinusSrcAlpha // Normal
            //Blend One One // Additive
            //Blend One OneMinusDstColor // Soft Additive
            //Blend DstColor Zero // Multiplicative
            //Blend DstColor SrcColor // 2x Multiplicative

CGPROGRAM
#pragma vertex vert
#pragma fragment frag

#include "UnityCG.cginc"

        struct appdata {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
                float4 color : COLOR;
            };

            struct v2f {
                float4 pos : POSITION;
                float4 color : COLOR;
            };

            uniform float _Outline;
            uniform float4 _OutlineColor;

            v2f vert(appdata v) {
                v2f o;
                v.vertex.xyz += v.color * _Outline;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.color = _OutlineColor;
                return o;
            }

            half4 frag(v2f i) :COLOR {
                return i.color;
            }
            ENDCG
        }

        Pass {
            Name "BASE"
            ZWrite On
            ZTest LEqual
            Blend SrcAlpha OneMinusSrcAlpha
            Material {
                Diffuse [_Color]
                Ambient [_Color]
            }
            Lighting On
            SetTexture [_MainTex] {
                ConstantColor [_Color]
                Combine texture * constant
            }
            SetTexture [_MainTex] {
                Combine previous * primary DOUBLE
            }
        }
    }

    SubShader {
        Tags { "Queue" = "Transparent" }

        Pass {
            Name "OUTLINE"
            Tags { "LightMode" = "Always" }
            Cull Front
            ZWrite Off
            ZTest Always
            ColorMask RGB

            // you can choose what kind of blending mode you want for the outline
            Blend SrcAlpha OneMinusSrcAlpha // Normal
            //Blend One One // Additive
            //Blend One OneMinusDstColor // Soft Additive
            //Blend DstColor Zero // Multiplicative
            //Blend DstColor SrcColor // 2x Multiplicative

            CGPROGRAM
            #pragma vertex vert
            #pragma exclude_renderers gles xbox360 ps3
            ENDCG
            SetTexture [_MainTex] { combine primary }
        }

        Pass {
            Name "BASE"
            ZWrite On
            ZTest LEqual
            Blend SrcAlpha OneMinusSrcAlpha
            Material {
                Diffuse [_Color]
                Ambient [_Color]
            }
            Lighting On
            SetTexture [_MainTex] {
                ConstantColor [_Color]
                Combine texture * constant
            }
            SetTexture [_MainTex] {
                Combine previous * primary DOUBLE
            }
        }
    }

    Fallback "Diffuse"
}

根据您所说的有根据的猜测:您有一个 material 使用您的着色器,它在所有三个对象之间共享。

当您编辑 material 的参数时,它正在编辑共享的 material,因此更改将应用​​到使用该 material 的所有对象。

您有两种解决方案:最简单的一种是在您的项目中使用不同的着色器参数创建多个 material。另一种方法是创建一个脚本,该脚本将在运行时将您的参数更改应用到 GetComponent<Renderer>().material,因为这将创建一个与其他对象分开的 material 实例。