【Unity Shader学习】颜色过渡

Shader "Sbin/Frag_Color"
{
	Properties
	{
// 主颜色
		_FirstColor("FirstColor",color) = (1,1,1,1)
// 次颜色
		_SecondColor("SecondColor",color) = (1,1,1,1)
// 不同颜色的分界中心
		_Center("Center",range(-0.51,0.51)) = 0
// 颜色过渡半径
		_Radius("Radius",range(0,0.2)) = 0.1
	}

	SubShader
	{
	pass
	{
		CGPROGRAM

		#pragma vertex vert
		#pragma fragment frag
		#include "unitycg.cginc"

// 变量定义
		float4 _FirstColor;
		float4 _SecondColor;
		float _Center;
		float _Radius;

		struct v2f
		{
			float4 pos:POSITION;
			float y : TEXCOORD0; // 物体顶点y值
			float4 col:COLOR;
		};

// 顶点函数
		v2f vert(appdata_base v)
		{
			v2f o;
			o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
			o.y = v.vertex.y;
			return o;
		}

// 片段函数
		fixed4 frag(v2f IN):COLOR
		{
// 当像素点处于渐变区以上
			if (IN.y >= _Center + _Radius)
			{
// 以第一种颜色进行填充
				return _FirstColor;
			}
// 当像素点处于渐变区上半部分
			else if (IN.y > _Center && IN.y < _Center + _Radius)
			{
// 插值实现渐变融合
				float d = IN.y - _Center;
				d = (1 - d / _Radius) - 0.5;
				d = saturate(d);
				return lerp(_FirstColor,_SecondColor,d);
			}
			else if (IN.y <= _Center - _Radius)
			{
				return _SecondColor;
			}
			else
			{
				float d = IN.y - _Center;
				d = (1 - d / _Radius) - 0.5;
				d = saturate(d);
				return lerp(_FirstColor, _SecondColor, d);
			}
		}

		ENDCG
	}
	}
}

效果截图:

 

心得:

1.在顶点程序和片段程序中都能够实现给物体显示不同的颜色,在顶点程序中能够实现不同颜色之间的过渡,但无法实现不同颜色各占不同的比重。而在片段程序中能够通过代码实现不同颜色之间的过渡和不同颜色占有不同的比重。

2.当变量被赋予COLOR语义时则其所有值将被强制转为正值。

用lerp函数能够插值计算物体的颜色

Shader "Sbin/SurfaceShader" {
	Properties {
		_FirstColor("FirstColor",color) = (1,1,1,1)
		_SecondColor("SecondColor",color) = (1,1,1,1)
		_Center("Center",range(-0.7,0.7)) = 0
		_Radius("Radius",range(0,0.2)) = 0.1
// 光滑度
		_Glossiness ("Smoothness", Range(0,1)) = 0.5
// 金属度
		_Metallic ("Metallic", Range(0,1)) = 0.0
	}
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200 
		
		CGPROGRAM
		// 基于物理的标准照明模型,使所有光线类型有阴影
		#pragma surface surf Standard vertex:vert 

		// 使用着色器模型3目标,以获得更好的照明效果
		#pragma target 3.0

		sampler2D _MainTex;


		float4 _FirstColor;
		float4 _SecondColor;
		float _Center;
		float _Radius;

		struct Input {
			float2 uv_MainTex;
			float z;
		};

		half _Glossiness;
		half _Metallic;


		UNITY_INSTANCING_CBUFFER_START(Props)
		UNITY_INSTANCING_CBUFFER_END

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

		void surf (Input IN, inout SurfaceOutputStandard o) {
			// Albedo comes from a texture tinted by color
			fixed4 c = tex2D (_MainTex, IN.uv_MainTex) ;
			o.Albedo = c.rgb;
			// Metallic and smoothness come from slider variables
			o.Metallic = _Metallic;
			o.Smoothness = _Glossiness;
			o.Alpha = c.a;

			float d = IN.z - _Center;
			float  s = abs(d);
			d = d / s;
			float f = s / _Radius;
			f = saturate(f);
			d *= f;
			d = d / 2 + 0.5;
			o.Albedo *= lerp(_FirstColor,_SecondColor,d);
		}
		ENDCG
	}
	FallBack "Diffuse"
}

效果截图:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值