今天讲解shader中的颜色color.
颜色是由rgb混合而成的,color = float4(r,g,b,a)这是颜色的公式!
我们想要自己调色,只需要改变这些颜色的值,就可以实现颜色有自己控制了。
首先我们可以输出我们的顶点颜色,看看当前的界面怎么显示!
Shader "Unlit/CubeShader"
{
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f
{
float4 color : TEXCOORD0;
float4 pos : SV_POSITION;
};
v2f vert (float4 vertex:SV_POSITION)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, vertex);
//顶点颜色由于直角坐标系的圆点在立方体的几何中心,所以我们要为(x,y,z)坐标加上0.5,因为立方体的坐标处于-0.5到0.5之间,而我们需要它在0到1之间。
o.color = vertex + float4(0.5,0.5,0.5,0.0);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
return i.color;
}
ENDCG
}
}
}
然后我们写一个可以自己控制颜色的shader,你可以自己调整rgba值,组合千万种颜色
Shader "Unlit/Cubebrightness"
{
Properties{
_Red("Red",Range(0,1)) = 0.5
_Green("Green",Range(0,1)) = 0.5
_Blue("Blue",Range(0,1)) = 0.5
_Alpha("Alpha",Range(0,1)) = 1
_Brightness("Brightness",Range(1,5))=1
}
SubShader
{
Tags{"Queue" = "Transparent" "RenderType" = "Transparent"}
Pass
{
Cull Off
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float _Red;
float _Green;
float _Blue;
float _Alpha;
float _Brightness;
struct v2f
{
float4 color : TEXCOORD0;
float4 pos : SV_POSITION;
};
v2f vert (float4 vertex:SV_POSITION)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, vertex);
//调整显示亮度通过rgb值的大小控制
o.color = float4(_Red,_Green,_Blue,_Alpha)*_Brightness;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
return i.color;
}
ENDCG
}
}
}
最后我们上最复杂的,也是解释最多的
饱和度:一般我们的公式 gray = 0.2125*tex.r + 0.7154*tex.g + 0.0721*tex.b 这个公式可以得到灰度的一个系数,然后用这个灰度组合一个graycolor=float(gray,gray,gray)最后用插值lerp求得这个颜色和最终颜色的中间值。
对比度:avgcolor=float3(0.5,0.5,0.5);对比度最弱的颜色与最终的颜色插值
亮度:是直接提高每个颜色的色值,所以在基础颜色上面乘以一个系数就可以了
以下是代码:
Shader "Unlit/任务13解释平面软件中的调色在shader中的实现"
{
Properties
{
_DiffuseTex("漫反射贴图",2D) = "white"{}
_DiffuseColor("漫反射颜色",Color) = (1,1,1,1)
_BumpMap("法线贴图",2D) = "bump"{}
_BumpScaleXY("法线缩放",Float) = 1
_BumpScaleZ("法线缩放",Float) = 1
_Shininess("光泽度",Float) = 1
_SpecularLightColor("高光颜色",Color) = (1,1,1,1)
_Brightness("亮度",Range(0,1)) = 0.5
_Saturation("饱和度",Float) = 0.5
_Contrast("对比度",Float) = 0.5
_R("R",Range(0,1)) = 0.5
_G("G",Range(0,1)) = 0.5
_B("B",Range(0,1)) = 0.5
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
struct v2f
{
float2 uv : TEXCOORD0;
float3 worldBinmornal:TEXCOORD1;
float3 worldNormal:TEXCOORD2;
float3 worldTangent:TEXCOORD3;
float3 worldPos:TEXCOORD4;
float4 pos : SV_POSITION;
};
float4 _DiffuseColor;
float _Brightness;
float _Saturation;
float _Contrast;
float _R;
float _G;
float _B;
sampler2D _BumpMap; float4 _BumpMap_ST;
sampler2D _DiffuseTex; float4 _DiffuseTex_ST;
float _BumpScaleXY;
float _BumpScaleZ;
float4 _SpecularLightColor;
float _Shininess;
v2f vert (appdata_full v)
{
v2f o;
o.uv = v.texcoord;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.worldNormal = mul(_World2Object,float4(v.normal,0.0));
o.worldTangent = mul(_Object2World,v.tangent);
o.worldBinmornal = cross(o.worldTangent,o.worldNormal) * v.tangent.w;
o.worldPos = mul(_Object2World,v.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
float3x3 worldRotation = float3x3(normalize(i.worldTangent),normalize(i.worldBinmornal),normalize(i.worldNormal));
float3 normal = normalize(UnpackNormal(tex2D(_BumpMap,i.uv)));
normal.xy *= _BumpScaleXY;
normal.z *= _BumpScaleZ;
normal = mul(worldRotation,normal);
float3 worldlightdir = normalize(_WorldSpaceLightPos0.xyz);
float3 worldViewdir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos);
float3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
float4 albedo = tex2D(_DiffuseTex,i.uv);
float3 diffuse = _LightColor0.rgb * _DiffuseColor.rgb * max(0,dot( worldlightdir,normal)) * _Brightness * albedo.xyz;
float3 specular = _LightColor0.rgb * _SpecularLightColor.rgb * pow(max(0,dot( normalize(reflect(-worldlightdir,normal)),worldViewdir)),_Shininess);
float3 avgcolor = float3(0.5,0.5,0.5);
float4 fincolor = float4(ambient + diffuse + specular,1.0);
fincolor.x += _R;
fincolor.y += _G;
fincolor.z += _B;
float gray = 0.2125 * albedo.r + 0.7154 * albedo.g + 0.0721 * albedo.b;
float3 grayColor = fixed3(gray, gray, gray);
fincolor.rgb = lerp(grayColor, fincolor.rgb, _Saturation);
fincolor.rgb = lerp(avgcolor,fincolor.rgb,_Contrast);
return float4(fincolor);
}
ENDCG
}
}
}
下面是效果:
上面属于个人意见,如有错误欢迎指正,也欢迎交流!!! 601408323我的shader学习群,欢迎一起来学习!