Bootstrap

Unity着色器Shader根据到某点的距离显示不同颜色

Shader "Custom/trackLineShader"
{
    Properties
    {
		_Color1("Color1",Color) = (1,1,1,1)
		_Color2("Color2",Color) = (1,1,1,1)
		_Dist("Dist", Range(0, 10)) = 1
		_Center_X("Center_X", Range(0, 150)) = 0
		_Center_Y("Center_Y", Range(0, 150)) = 0
		_Center_Z("Center_Z", Range(0, 150)) = 0
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
				float3 modalPos :TEXCOORD1;
            };

			fixed4 _Color1;
			fixed4 _Color2;
			float _Dist;
			float _Center_X;
			float _Center_Y;
			float _Center_Z;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
				o.modalPos = v.vertex;
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
                fixed4 col = _Color1;

				float3 cent = float3(_Center_X, _Center_Y, _Center_Z);//靶子中心世界坐标,由外部赋值
				float3 pt = mul(i.modalPos, (float3x3)unity_ObjectToWorld);//将模型坐标换到世界坐标

				float dist = distance(pt, cent);
				if (dist > _Dist) {
					col = _Color2;
				}
                return col;
            }
            ENDCG
        }
    }
}

坐标在外部赋值

mat.SetFloat("_Center_X", center.position.x);
mat.SetFloat("_Center_Y", center.position.y);
mat.SetFloat("_Center_Z", center.position.z);

;