由于着色器 Unity3D 中的错误,我的网格上没有阴影
No shadows on my mesh becuase of a mistake in my shader Unity3D
我正在写一个卡通水着色器,我希望它被定向光照亮。我让它工作了,但我认为法线有问题,因为波浪之间没有阴影。我希望有人能发现我犯的错误并提前谢谢你。
Shader "Custom/NoobShader_03" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_Scale ("Wave Scale", float) = 0.6
_Frequency ("Frequency", float) = 1
_Speed ("Speed", float) = 0.5
_Scale2 ("Wave Scale", float) = 0.6
_Frequency2 ("Frequency", float) = 1
_Speed2 ("Speed", float) = 1
}
SubShader {
Pass{
Tags { "LightMode" = "ForwardBase"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
float4 _Color;
float _Scale;
float _Frequency;
float _Speed;
float _Scale2;
float _Frequency2;
float _Speed2;
float4 _LightColor0;
struct VertexOutput
{
float4 pos : SV_POSITION;
float3 nor : NORMAL;
float4 col : COLOR;
};
struct VertexInput
{
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct FragmentOutput
{
float4 color : COLOR;
};
VertexOutput vert (VertexInput i)
{
VertexOutput VOUT;
float randomNum = 1 / _Time;
float newPos = _Scale * sin(_Time.w * _Speed + i.vertex.x * _Frequency);
float newPos2 = _Scale2 * cos(_Time.w * _Speed2 + i.vertex.z * _Frequency2);
//i.vertex.y += newPos;
float4 tempPos = i.vertex;
tempPos.y += newPos * newPos2;
VOUT.pos = mul(UNITY_MATRIX_MVP,tempPos);
VOUT.nor = float3(mul(UNITY_MATRIX_MVP,tempPos).xyz);
float3 normalDirection = normalize( mul( float4( VOUT.nor, 0.0 ), _World2Object).xyz);
float3 lightDirection;
float atten = 1.0;
lightDirection = normalize(_WorldSpaceLightPos0.xyz);
float3 diffuseRefflection = atten * _LightColor0.xyz * _Color.rgb * max( 0.0, dot(normalDirection, lightDirection));
VOUT.col = float4(diffuseRefflection, 0.0);
return VOUT;
}
FragmentOutput frag(VertexOutput v)
{
FragmentOutput FOUT;
FOUT.color = v.col + _Color;
return FOUT;
}
ENDCG
}
}
FallBack "Diffuse"
}
简单回答一下你的法线
float3 normalDirection = normalize( mul( float4( VOUT.nor, 0.0 ), _World2Object).xyz);
法线在本地 aka 对象中给出 space。您需要转换到世界 space。你的乘法顺序是错误的。应该是:
float3 normalDirection = normalize( mul( _World2Object, float4( VOUT.nor, 0.0 )).xyz);
相当于:
float3 normalDirection = normalize( mul( float4( VOUT.nor, 0.0 ), _Object2World).xyz);
通常将变换矩阵放在要变换的向量之前。
我正在写一个卡通水着色器,我希望它被定向光照亮。我让它工作了,但我认为法线有问题,因为波浪之间没有阴影。我希望有人能发现我犯的错误并提前谢谢你。
Shader "Custom/NoobShader_03" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_Scale ("Wave Scale", float) = 0.6
_Frequency ("Frequency", float) = 1
_Speed ("Speed", float) = 0.5
_Scale2 ("Wave Scale", float) = 0.6
_Frequency2 ("Frequency", float) = 1
_Speed2 ("Speed", float) = 1
}
SubShader {
Pass{
Tags { "LightMode" = "ForwardBase"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
float4 _Color;
float _Scale;
float _Frequency;
float _Speed;
float _Scale2;
float _Frequency2;
float _Speed2;
float4 _LightColor0;
struct VertexOutput
{
float4 pos : SV_POSITION;
float3 nor : NORMAL;
float4 col : COLOR;
};
struct VertexInput
{
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct FragmentOutput
{
float4 color : COLOR;
};
VertexOutput vert (VertexInput i)
{
VertexOutput VOUT;
float randomNum = 1 / _Time;
float newPos = _Scale * sin(_Time.w * _Speed + i.vertex.x * _Frequency);
float newPos2 = _Scale2 * cos(_Time.w * _Speed2 + i.vertex.z * _Frequency2);
//i.vertex.y += newPos;
float4 tempPos = i.vertex;
tempPos.y += newPos * newPos2;
VOUT.pos = mul(UNITY_MATRIX_MVP,tempPos);
VOUT.nor = float3(mul(UNITY_MATRIX_MVP,tempPos).xyz);
float3 normalDirection = normalize( mul( float4( VOUT.nor, 0.0 ), _World2Object).xyz);
float3 lightDirection;
float atten = 1.0;
lightDirection = normalize(_WorldSpaceLightPos0.xyz);
float3 diffuseRefflection = atten * _LightColor0.xyz * _Color.rgb * max( 0.0, dot(normalDirection, lightDirection));
VOUT.col = float4(diffuseRefflection, 0.0);
return VOUT;
}
FragmentOutput frag(VertexOutput v)
{
FragmentOutput FOUT;
FOUT.color = v.col + _Color;
return FOUT;
}
ENDCG
}
}
FallBack "Diffuse"
}
简单回答一下你的法线
float3 normalDirection = normalize( mul( float4( VOUT.nor, 0.0 ), _World2Object).xyz);
法线在本地 aka 对象中给出 space。您需要转换到世界 space。你的乘法顺序是错误的。应该是:
float3 normalDirection = normalize( mul( _World2Object, float4( VOUT.nor, 0.0 )).xyz);
相当于:
float3 normalDirection = normalize( mul( float4( VOUT.nor, 0.0 ), _Object2World).xyz);
通常将变换矩阵放在要变换的向量之前。