-
Notifications
You must be signed in to change notification settings - Fork 4
/
DecalPass.hlsl
112 lines (86 loc) · 2.18 KB
/
DecalPass.hlsl
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
struct VSInput
{
float3 position : POSITION;
float3 normal : NORMAL;
float3 tangent : TANGENT;
float2 uv : TEXCOORD;
};
struct PSInput
{
float3 normal : NORMAL;
float3 tangent : TANGENT;
float2 uv : TEXCOORD0;
float3 worldPos : TEXCOORD1;
float4 position : SV_POSITION;
};
cbuffer GPrepassCB : register(b0)
{
float4x4 ViewProjection;
float4x4 InvViewProjection;
float4 CameraPosition;
float4 RTSize;
float MipBias;
float3 pad;
};
cbuffer perModelInstanceCB : register(b1)
{
float4x4 World;
float4x4 ToObject;
float AlbedoID;
float NormalID;
float Roughness;
float Metalness;
float4 Colour;
float2 UVscale;
float2 NormalScale;
float Emissive;
float FurShellsID;
float2 perModelInstanceCB_pad;
};
Texture2D<float> depthBuffer : register(t0);
Texture2D<float4> Textures[] : register(t1);
SamplerState SamplerLinear : register(s0);
PSInput VSMain(VSInput input)
{
PSInput result;
result.normal = mul((float3x3)World, input.normal.xyz);
result.tangent = mul((float3x3)World, input.tangent.xyz);
result.position = mul(World, float4(input.position.xyz, 1));
result.worldPos = result.position.xyz;
result.position = mul(ViewProjection, result.position);
result.uv = UVscale * input.uv;
return result;
}
struct PSOutput
{
float4 colour : SV_Target0;
float4 normal : SV_Target1;
};
PSOutput PSMain(PSInput input)
{
PSOutput output = (PSOutput)0;
float3 n = 0;
float4 albedo = Colour;
float depth = depthBuffer[input.position.xy].x;
float2 uv = input.position.xy * RTSize.zw;
float4 clipPos = float4(2 * uv - 1, depth, 1);
clipPos.y = -clipPos.y;
float4 worldPos = mul(InvViewProjection, clipPos);
worldPos.xyz /= worldPos.w;
float4 objectSpacePos = mul(ToObject, float4(worldPos.xyz, 1));
clip(0.5 - abs(objectSpacePos.xyz));
if (AlbedoID >= 0)
{
albedo = Textures[AlbedoID].SampleBias(SamplerLinear, input.uv, MipBias).rgba;
}
clip(albedo.a - 0.5f);
if (NormalID >= 0)
{
n = Textures[NormalID].SampleBias(SamplerLinear, input.uv, MipBias).rgb * 2 - 1;
}
output.colour.rgb = albedo.rgb;
output.colour.a = 1;
output.normal.xyz = normalize(input.normal.xyz + float3(NormalScale * n.xy, 0));
output.normal.w = 1;
return output;
}