如何在着色器程序中使颜色变亮?
How to lighten color in shader program?
我正在尝试在我的 Unity 游戏中实现这种外观:
我喜欢山的颜色随着高度的增加而变浅。
我对游戏开发还是个新手,虽然我了解着色器的作用,但在实践中尝试使用它们时遇到了麻烦。
我知道我需要在我的表面着色器中做这样的事情:
float4 mountainColor = lerp(_BottomColor,_TopColor,IN.vertex.z);
...根据 z 值在较深的颜色和较浅的颜色之间进行 lerp。
但我不确定如何实用地在着色器中使颜色变亮。我没有使用顶点颜色,颜色来自纹理。任何 help/pointers 将不胜感激。
编辑:
所以,呃,我意识到我只需要乘以 rgb 值就可以使它变亮或变暗。
问题是,如果我只是这样做:
o.Albedo = c.rgb * 1.5;
...是的,它变亮了,但它也稍微改变了色调并变得过度饱和。
我该如何解决这个问题?
这是我的代码:
Shader "Custom/Altitude Gradient" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows vertex:vert
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
float3 localPos;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
void vert(inout appdata_full v, out Input o) {
UNITY_INITIALIZE_OUTPUT(Input, o);
o.localPos = v.vertex.xyz;
}
void surf (Input IN, inout SurfaceOutputStandard o) {
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = lerp(c.rgb * 0.5, 1, IN.localPos.y);
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
它足以满足我的需求。我将不得不继续 fiddle 使用它来获得我想要的确切外观,但基础就在这里。
我正在尝试在我的 Unity 游戏中实现这种外观:
我喜欢山的颜色随着高度的增加而变浅。
我对游戏开发还是个新手,虽然我了解着色器的作用,但在实践中尝试使用它们时遇到了麻烦。
我知道我需要在我的表面着色器中做这样的事情:
float4 mountainColor = lerp(_BottomColor,_TopColor,IN.vertex.z);
...根据 z 值在较深的颜色和较浅的颜色之间进行 lerp。
但我不确定如何实用地在着色器中使颜色变亮。我没有使用顶点颜色,颜色来自纹理。任何 help/pointers 将不胜感激。
编辑:
所以,呃,我意识到我只需要乘以 rgb 值就可以使它变亮或变暗。
问题是,如果我只是这样做:
o.Albedo = c.rgb * 1.5;
...是的,它变亮了,但它也稍微改变了色调并变得过度饱和。
我该如何解决这个问题?
这是我的代码:
Shader "Custom/Altitude Gradient" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows vertex:vert
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
float3 localPos;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
void vert(inout appdata_full v, out Input o) {
UNITY_INITIALIZE_OUTPUT(Input, o);
o.localPos = v.vertex.xyz;
}
void surf (Input IN, inout SurfaceOutputStandard o) {
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = lerp(c.rgb * 0.5, 1, IN.localPos.y);
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
它足以满足我的需求。我将不得不继续 fiddle 使用它来获得我想要的确切外观,但基础就在这里。