【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_0
本文链接:https://www.cnblogs.com/Xiang-gu/p/16512363.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步