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

时间:2018-01-28 09:21:35

标签: c# unity3d unity5

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

例如Cube如果我将在Inspector中更改Outline轮廓宽度,它将对所有三个对象也应用更改为Sphere and Cylinder。

但我希望如果我更改立方体上的轮廓宽度,它将仅为立方体更改它,如果我将更改球体的轮廓宽度,那么仅适用于球体。

Outline

这是着色器:

// 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"
}

1 个答案:

答案 0 :(得分:1)

根据您所说的内容进行有根据的猜测:您使用着色器使用了一种材料,该材质在所有三个对象之间共享。

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

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