-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDX11 Framework.hlsl
242 lines (193 loc) · 7.53 KB
/
DX11 Framework.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//--------------------------------------------------------------------------------------
// File: DX11 Framework.hlsl
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
// Shader vars
//--------------------------------------------------------------------------------------
Texture2D texDiffuse : register(t0);
Texture2D texSpec : register(t1);
SamplerState sampLinear : register(s0);
struct Light
{
float4 Color;
// -------------- 16 bytes
float3 Direction;
int LightType;
// -------------- 16 bytes
float3 Position;
float Attenuation;
// -------------- 16 bytes
float SpotAngle;
float3 _padding;
// -------------- 16 bytes
};
struct Fog
{
float4 Color;
// -------------- 16 bytes
float Start;
float Range;
float2 _padding;
// -------------- 16 bytes
};
//--------------------------------------------------------------------------------------
// Constant Buffer Variables
//--------------------------------------------------------------------------------------
cbuffer ConstantBuffer : register( b0 )
{
matrix World;
// ----------- 64 bytes
matrix View;
// ----------- 64 bytes
matrix Projection;
// ----------- 64 bytes
float4 ambientLight;
// ----------- 16 bytes
Fog fog;
// ----------- 32 bytes
Light lights[20];
// ----------- 1280 bytes
float4 AmbMat;
// ----------- 16 bytes
float4 DiffMat;
// ----------- 16 bytes
float4 SpecMat;
// ----------- 16 bytes
float3 EyePosW;
bool hasAlbedoTexture;
// ----------- 16 bytes
bool hasSpecularMapTexture;
int numLights;
float SpecularPower;
float _padding;
// ----------- 16 bytes
}
// Functions
float4 Diffuse(Light light, float3 directionToLight, float3 surfaceNormal)
{
float4 potentialDiff = light.Color * DiffMat;
float diffPercent = max(dot(directionToLight, surfaceNormal), 0);
return potentialDiff * diffPercent;
}
// Blinn-Phong specular
float4 Specular(Light light, float3 viewDirection, float3 directionToLight, float3 surfaceNormal, float2 texCoord)
{
float4 potentialSpecular;
if (hasSpecularMapTexture)
potentialSpecular = light.Color * texSpec.Sample(sampLinear, texCoord);
else
potentialSpecular = light.Color * SpecMat;
float3 halfwayVector = normalize(directionToLight + viewDirection);
float normalDotHalfway = max(0, dot(surfaceNormal, halfwayVector));
return potentialSpecular * pow(normalDotHalfway, SpecularPower);
}
float Attenuation(Light light, float distance)
{
return 1.0f / (1.0f + pow(light.Attenuation * distance, 2.0f));
}
//--------------------------------------------------------------------------------------
struct VS_OUTPUT
{
float4 Pos : SV_POSITION;
float3 PosW : POSITION0;
float3 NormalW : NORMAL0;
float2 TexCoord : TEXCOORD0;
};
//--------------------------------------------------------------------------------------
// Vertex Shader
//--------------------------------------------------------------------------------------
VS_OUTPUT VS(float3 Pos : POSITION, float3 Normal : NORMAL, float2 texcoord : TEXCOORD0)
{
float4 pos4 = float4(Pos, 1.0f);
VS_OUTPUT output = (VS_OUTPUT)0;
output.Pos = mul( pos4, World);
output.PosW = output.Pos;
output.Pos = mul( output.Pos, View );
output.Pos = mul( output.Pos, Projection );
// Normalise normals
Normal = normalize(Normal);
float3 NormalW = mul(Normal, World);
NormalW = normalize(NormalW);
output.NormalW = NormalW;
output.TexCoord = texcoord;
return output;
}
//--------------------------------------------------------------------------------------
// Pixel Shader
//--------------------------------------------------------------------------------------
float4 PS(VS_OUTPUT input) : SV_Target
{
float4 litColor;
float4 ambient = float4(1.0, 1.0, 1.0, 1.0);
float4 diffuse = float4(0.0, 0.0, 0.0, 0.0);
float4 specular = float4(0.0, 0.0, 0.0, 0.0);
// Get direction from pixel to viewpoint
float3 viewerDir = normalize(EyePosW - input.PosW);
// Ambient Lighting
ambient = ambientLight * AmbMat;
// Lights
for (int i = 0; i < numLights; i++)
{
switch (lights[i].LightType)
{
case 0: // Directional Light
{
diffuse += Diffuse(lights[i], normalize(lights[i].Direction), input.NormalW);
specular += Specular(lights[i], viewerDir, normalize(lights[i].Direction), input.NormalW, input.TexCoord);
}
break;
case 1: // Point Light
{
float3 lightVector = lights[i].Position - input.PosW;
float distanceToLight = length(lightVector);
lightVector = normalize(lightVector);
float attenuation = Attenuation(lights[i], distanceToLight);
diffuse += Diffuse(lights[i], lightVector, input.NormalW) * attenuation;
specular += Specular(lights[i], viewerDir, lightVector, input.NormalW, input.TexCoord) * attenuation;
}
break;
case 2: // Spot Light
{
// Get direction of the light to the current pixel's world position
float3 lightVector = lights[i].Position - input.PosW;
float distanceToLight = length(lightVector);
lightVector = normalize(lightVector);
// Falloff as light vector gets close to maximum angle to normal
// Most code is derived (not directly copied) from here: https://www.3dgep.com/texturing-lighting-directx-11/#Spotlight_Cone
float minCos = cos(radians(lights[i].SpotAngle));
float maxCos = (minCos + 1.0f) / 2.0f;
float cosAngle = dot(normalize(lights[i].Direction), -lightVector);
float spotIntensity = smoothstep(minCos, maxCos, cosAngle);
float attenuation = Attenuation(lights[i], distanceToLight);
diffuse += Diffuse(lights[i], lightVector, input.NormalW) * attenuation * spotIntensity;
specular += Specular(lights[i], viewerDir, lightVector, input.NormalW, input.TexCoord) * attenuation * spotIntensity;
}
break;
}
}
// Modulate with late add. See verse Frank Luna 8.6 of the Bible to remind yourself what this means.
litColor = ambient + diffuse;
// Texturing
// Account for having no texture available by multiplying by texture first and only using the texture
// if it has one.
if (hasAlbedoTexture)
{
float4 textureColor = texDiffuse.Sample(sampLinear, input.TexCoord);
litColor *= textureColor;
// Translucency
litColor.a = textureColor.a * DiffMat.a;
}
else
{
// Translucency
litColor.a = DiffMat.a;
}
litColor.rgb += specular.rgb;
// The fog is coming
float distanceToCamera = distance(input.PosW, EyePosW);
float fogLerp = saturate((distanceToCamera - fog.Start) / fog.Range);
litColor = lerp(litColor, fog.Color, fogLerp);
return litColor;
}