好的,我是天空盒子的新手,并且在尝试实现RADIAL渐变天空盒时遇到了一些问题。我有一个径向渐变着色器,但是当我把它作为天空盒时,插入两种颜色之间的部分太大了,这意味着你根本无法真正告诉它的渐变。
我认为这是因为它的着色器不适用于天空盒。然后我找到了一个用于线性渐变天空盒的着色器,它作为线性渐变,但我需要它是径向的,就像从中心颜色向外。
我到处寻找,但无法找到径向渐变SKYBOX着色器。我的问题是 - 如何将线性渐变着色器转换为天空盒的径向?或者我如何使现有的径向渐变适用于天空盒?
我没有写这个,但这是线性渐变天空盒的代码:
Shader "Custom/Horizontal Skybox"
{
Properties
{
_Color1 ("Top Color", Color) = (1, 1, 1, 0)
_Color2 ("Horizon Color", Color) = (1, 1, 1, 0)
_Color3 ("Bottom Color", Color) = (1, 1, 1, 0)
_Exponent1 ("Exponent Factor for Top Half", Float) = 1.0
_Exponent2 ("Exponent Factor for Bottom Half", Float) = 1.0
_Intensity ("Intensity Amplifier", Float) = 1.0
}
CGINCLUDE
#include "UnityCG.cginc"
struct appdata
{
float4 position : POSITION;
float3 texcoord : TEXCOORD0;
};
struct v2f
{
float4 position : SV_POSITION;
float3 texcoord : TEXCOORD0;
};
half4 _Color1;
half4 _Color2;
half4 _Color3;
half _Intensity;
half _Exponent1;
half _Exponent2;
v2f vert (appdata v)
{
v2f o;
o.position = mul (UNITY_MATRIX_MVP, v.position);
o.texcoord = v.texcoord;
return o;
}
half4 frag (v2f i) : COLOR
{
float p = normalize (i.texcoord).y;
float p1 = 1.0f - pow (min (1.0f, 1.0f - p), _Exponent1);
float p3 = 1.0f - pow (min (1.0f, 1.0f + p), _Exponent2);
float p2 = 1.0f - p1 - p3;
return (_Color1 * p1 + _Color2 * p2 + _Color3 * p3) * _Intensity;
}
ENDCG
SubShader
{
Tags { "RenderType"="Background" "Queue"="Background" }
Pass
{
ZWrite Off
Cull Off
Fog { Mode Off }
CGPROGRAM
#pragma fragmentoption ARB_precision_hint_fastest
#pragma vertex vert
#pragma fragment frag
ENDCG
}
}
}
喜欢这个 -
答案 0 :(得分:2)
要创建示例图像,您根本不需要使用天空盒。
答案 1 :(得分:1)
您提供的着色器代码确实仅沿y轴绘制径向渐变。如果通过更改线
来翻转轴float p = normalize (i.texcoord).y;
到
float p = normalize (i.texcoord).x;
由于您实际上想要旋转渐变,您可以在顶点着色器中执行以下操作(1.57是pi / 2)。
v2f vert (appdata v)
{
v2f o;
float sinX = sin ( 1.57 );
float cosX = cos ( 1.57 );
float sinY = sin ( 1.57 );
float2x2 rotationMatrix = float2x2( cosX, -sinX, sinY, cosX);
o.position = mul (UNITY_MATRIX_MVP, v.position);
o.texcoord.xz = mul(v.texcoord.xz, rotationMatrix);
o.texcoord.y = v.texcoord.y;
return o;
}
完整代码:
Shader "Custom/Horizontal Skybox"
{
Properties
{
_Color1 ("Top Color", Color) = (1, 1, 1, 0)
_Color2 ("Horizon Color", Color) = (1, 1, 1, 0)
_Color3 ("Bottom Color", Color) = (1, 1, 1, 0)
_Exponent1 ("Exponent Factor for Top Half", Float) = 1.0
_Exponent2 ("Exponent Factor for Bottom Half", Float) = 1.0
_Intensity ("Intensity Amplifier", Float) = 1.0
_Angle ("Angle", Float) = 0.0
}
CGINCLUDE
#include "UnityCG.cginc"
struct appdata
{
float4 position : POSITION;
float3 texcoord : TEXCOORD0;
};
struct v2f
{
float4 position : SV_POSITION;
float3 texcoord : TEXCOORD0;
};
half4 _Color1;
half4 _Color2;
half4 _Color3;
half _Intensity;
half _Exponent1;
half _Exponent2;
half _Angle;
v2f vert (appdata v)
{
v2f o;
float sinX = sin ( _Angle );
float cosX = cos ( _Angle );
float sinY = sin ( _Angle );
float2x2 rotationMatrix = float2x2( cosX, -sinX, sinY, cosX);
o.position = mul (UNITY_MATRIX_MVP, v.position);
o.texcoord.xz = mul(v.texcoord.xz, rotationMatrix);
o.texcoord.y = v.texcoord.y;
return o;
}
half4 frag (v2f i) : COLOR
{
float p = normalize (i.texcoord).x;
float p1 = 1.0f - pow (min (1.0f, 1.0f - p), _Exponent1);
float p3 = 1.0f - pow (min (1.0f, 1.0f + p), _Exponent2);
float p2 = 1.0f - p1 - p3;
return (_Color1 * p1 + _Color2 * p2 + _Color3 * p3) * _Intensity;
}
ENDCG
SubShader
{
Tags { "RenderType"="Background" "Queue"="Background" }
Pass
{
ZWrite Off
Cull Off
Fog { Mode Off }
CGPROGRAM
#pragma fragmentoption ARB_precision_hint_fastest
#pragma vertex vert
#pragma fragment frag
ENDCG
}
}
}