unity _WorldSpaceLightDir in forwardBase lightMode

现象

将方向光的旋转全部设为0,此时方向光朝向+Z,因此方向光的朝向为(0,0,1)
scene

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
Shader "Custom/Test"
{
Properties
{
}
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag

#include "UnityCG.cginc"

struct appdata
{
float4 pos : POSITION;
};

struct v2f
{
float4 pos : SV_POSITION;
float3 worldPos : TEXCOORD1;
};

v2f vert (appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.pos);
o.worldPos = mul(unity_ObjectToWorld, v.pos);
return o;
}

fixed4 frag (v2f i) : SV_Target
{
return float4(UnityWorldSpaceLightDir(i.worldPos), 1.0f);
}
ENDCG
}
}
}

此时渲染结果为一个蓝色的鸭子
TurnOff LightMode Render Result

当加上Tags { "LightMode" = "ForwardBase" }后:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
Shader "Custom/Test"
{
Properties
{
}
SubShader
{
Pass
{
Tags { "LightMode" = "ForwardBase" }

CGPROGRAM
#pragma vertex vert
#pragma fragment frag

#include "UnityCG.cginc"

struct appdata
{
float4 pos : POSITION;
};

struct v2f
{
float4 pos : SV_POSITION;
float3 worldPos : TEXCOORD1;
};

v2f vert (appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.pos);
o.worldPos = mul(unity_ObjectToWorld, v.pos);
return o;
}

fixed4 frag (v2f i) : SV_Target
{
return float4(UnityWorldSpaceLightDir(i.worldPos), 1.0f);
}
ENDCG
}
}
}

此时渲染结果为一只黑色的鸭子:
TurnOn LightMode Render Result

原因

RenderDoc剖析一下,打开ForwardBase时:
turnon forwardbase shader disassembly

关闭ForwardBase后:
turnoff forwardbase shader disassembly

可见在设置LightModeForwardBase时,CB中传入的光方向发生了反转
经过测试,当LightModeForwardAdd时,CB中传入的光方向也被反转了

unity surface shader directx 11 create device and swap chain

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×