Cg入门20:Fragment shader - 片段级模型动态变色(实现汽车动态换漆)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/aa4790139/article/details/50964736
Unity 一个面片的最大顶点数为65524,所以大于这个数,请拆分成多个面片
1.获取汽车x轴的最大值和最小值[-2.5,2.5]+R

surfaceShader 语法

surf :surface shader的方法名
vert :使用顶点程序方法名

surface shader 方法不能有返回值


源代码:
Shader "Sbin/SurfaceShader" {
	Properties {
		_Glossiness ("Smoothness", Range(0,1)) = 0.5
		_Metallic ("Metallic", Range(0,1)) = 0.0

		_UpColor("UpColor",color) = (1,0,0,1)
		_DownColor("DownColor",color) = (0,1,0,1)
		_Center("Center",range(-0.7,0.7)) = 0
		_R("R",range(0,0.5)) = 0.2
	}
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200
		
		CGPROGRAM
		#pragma surface surf Standard vertex:vert fullforwardshadows
		//说明:pragma surface method1 Standard vertex:method2	fullforwardshadows
		
		#pragma target 3.0

		sampler2D _MainTex;

		struct Input {
			float2 uv_MainTex;
			float x:TEXCOORD0;
		};

		half _Glossiness;
		half _Metallic;

		float4 _UpColor;
		float4 _DownColor;
		float _Center; 
		float _R; 

		void vert(inout appdata_full v,out Input o){
			o.uv_MainTex = v.texcoord.xy;
			o.x = v.vertex.x;
		}

		void surf (Input IN, inout SurfaceOutputStandard o) {
			fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
			o.Albedo = c.rgb;
			o.Metallic = _Metallic;
			o.Smoothness = _Glossiness;
			o.Alpha = c.a;

			float d = IN.x - _Center;//融合带
			float s = abs(d);
			d = d/s;//正负值分别描述上半部分和下半部分,取值1和-1

			float f = s/_R;	//范围>1:表示上下部分;范围<1:表示融合带
			f = saturate(f);
			d *= f;//表示全部[-1,1];范围>1:表示上部分;范围<1:表示融合带;范围<-1:表示下部分
				
			d = d/2+0.5;//将范围控制到[0,1],因为颜色值返回就是[0,1]
			o.Albedo += lerp(_UpColor,_DownColor,d);
		}
		ENDCG
	}
	FallBack "Diffuse"
}

效果:

猜你喜欢

转载自blog.csdn.net/aa4790139/article/details/50964736