-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeferredShaders.cs
285 lines (279 loc) · 14.8 KB
/
DeferredShaders.cs
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
using System;
namespace _3D_Renderer
{
#nullable enable
class DeferredShaderStaticMethods
{
public static Colour? GetScreenSpaceReflection(Arr2D<Colour> CurrentFrame, Arr2D<double> DepthBuffer, (Arr2D<Vec3D> Position,
Arr2D<Vec3D> Normal, Arr2D<Colour> Albedo, Arr2D<double> Specular, Arr2D<Colour> Emmissive,
Arr2D<Mesh> MeshRef) DeferredBuffer, int x, int y, int[] Resolution,
Camera CameraObj, double MaxReflectDist, double ScreenSpaceStep, double Tolerance, int Iterations, double Strength, double Bias)
{
Vec3D PixelPos = DeferredBuffer.Position[x, y];
Vec3D PixelNormal = DeferredBuffer.Normal[x, y];
Vec3D CamToPixelVector = (PixelPos - CameraObj.Position).Normalise();
Vec3D ReflectionVector = CamToPixelVector - PixelNormal.ScalarMult(2 * Vec3D.DotProduct(PixelNormal, CamToPixelVector));
ReflectionVector = ReflectionVector.Normalise().ScalarMult(MaxReflectDist);
//Already know screen space pixel maps
Vec2D ScreenSpaceReflectionStart = new Vec2D(x, y);
Vec3D ReflectionEnd = PixelPos + ReflectionVector;
Vec3D CameraSpaceReflectionEnd = Matrix.Multiply(CameraObj.GetCameraSpaceTransform(Resolution), ReflectionEnd.PositionMatrix(4)).ToVec3D();
if (CameraSpaceReflectionEnd.Z < CameraObj.MinZ)
{
Vec3D ReflectionStart = PixelPos;
Vec3D CameraSpaceReflectionStart = Matrix.Multiply(CameraObj.GetCameraSpaceTransform(Resolution), ReflectionStart.PositionMatrix(4)).ToVec3D();
Vec3D CameraSpaceReflectionVec = CameraSpaceReflectionEnd - CameraSpaceReflectionStart;
//As reflection vector at normal distances goes out of screen-space, need to find the position vector of the reflection as it hits MinZ
CameraSpaceReflectionEnd = CameraSpaceReflectionStart + CameraSpaceReflectionVec.ScalarMult((CameraObj.MinZ - CameraSpaceReflectionStart.Z) /
CameraSpaceReflectionVec.Z);
}
Vec2D ScreenSpaceReflectionEnd = new Vec2D(CameraSpaceReflectionEnd.X / CameraSpaceReflectionEnd.Z,
CameraSpaceReflectionEnd.Y / CameraSpaceReflectionEnd.Z);
ScreenSpaceReflectionEnd += new Vec2D(Resolution[0] / 2, Resolution[1] / 2);
double StartDepth = DepthBuffer[x, y];
double EndDepth = CameraSpaceReflectionEnd.Z;
double MinDepth = StartDepth;
double MaxDepth = EndDepth;
// First pass, find approx intersection
double ScreenSpaceReflectDist = (ScreenSpaceReflectionEnd - ScreenSpaceReflectionStart).RectilinearDist();
double LerpStepSize = ScreenSpaceStep / ScreenSpaceReflectDist;
bool ReflectionFound = false;
Vec2D Prev = ScreenSpaceReflectionStart;
Vec2D Current = ScreenSpaceReflectionStart;
double ReflectDepth = StartDepth;
for (double LerpFactor = 0; LerpFactor <= 1; LerpFactor += LerpStepSize)
{
Prev = Current;
MinDepth = ReflectDepth;
Current = Vec2D.Lerp(ScreenSpaceReflectionStart, ScreenSpaceReflectionEnd, LerpFactor);
int ReflectX = (int)Math.Round(Current.X);
int ReflectY = (int)Math.Round(Current.Y);
if (ReflectX < 0 || ReflectX > Resolution[0] - 1 || ReflectY < 0 || ReflectY > Resolution[1] - 1)
{
//Reflection has gone outside the bounds of the screen
break;
}
else
{
double Depth = DepthBuffer[ReflectX, ReflectY];
//Perspective correct interpolation
ReflectDepth = StartDepth * EndDepth / Double.Lerp(StartDepth, EndDepth, 1 - LerpFactor);
if (Depth != 0 && ReflectDepth - Depth > Bias && ReflectDepth - Depth < Tolerance + Bias)
{
ReflectionFound = true;
MaxDepth = ReflectDepth;
break;
}
}
}
if (!ReflectionFound)
{
// No reflection
return null;
}
else
{
// Second pass, find more accurate intersection
Vec2D Min = Prev;
Vec2D Max = Current;
double Depth;
for (int _ = 0; _ < Iterations; _++)
{
Current = Vec2D.Lerp(Min, Max, 0.5);
int ReflectX = (int)Math.Round(Current.X);
int ReflectY = (int)Math.Round(Current.Y);
Depth = DepthBuffer[ReflectX, ReflectY];
//Perspective correct interpolation
ReflectDepth = MinDepth * MaxDepth / Double.Lerp(MinDepth, MaxDepth, 0.5);
if (Depth != 0 && ReflectDepth - Depth > 0 && ReflectDepth - Depth < Tolerance)
{
Max = Current;
MaxDepth = ReflectDepth;
}
else
{
Min = Current;
MinDepth = ReflectDepth;
}
}
int FinalX = (int)Math.Round(Max.X);
int FinalY = (int)Math.Round(Max.Y);
Depth = DepthBuffer[FinalX, FinalY];
//Perspective correct interpolation
ReflectDepth = MaxDepth;
return CurrentFrame[FinalX, FinalY].ScalarMult(Strength * DeferredBuffer.Specular[x, y]
//Multiply by 1 - how close the reflection and pixel to camera vector as in this case it is much more likely the reflection is of something not visible in screenspace
* (1 - Math.Max(-Vec3D.DotProduct(CamToPixelVector, ReflectionVector.Normalise()), 0))
//Multiply by how 1 - the proportion in tolerance to avoid pixels far away from the actual reflection location being given as high intensity
* (1 - (ReflectDepth - Depth) / Tolerance)
//Multiply by 1-distance to reflection divided by the maximum distance so reflections fade out with distance instead of abruptly stopping
* (1 - (DeferredBuffer.Position[FinalX, FinalY] - PixelPos).Magnitude() / MaxReflectDist)
);
}
}
}
interface IDeferredShader
{
public Colour PerPixel(Arr2D<Colour> CurrentFrame, Arr2D<double> DepthBuffer, (Arr2D<Vec3D> Position,
Arr2D<Vec3D> Normal, Arr2D<Colour> Albedo, Arr2D<double> Specular, Arr2D<Colour> Emmissive,
Arr2D<Mesh> MeshRef) DeferredBuffer, int x, int y, int[] Resolution,
Camera CameraObj)
{
return CurrentFrame[x, y];
}
}
class ScreenSpaceDielectricReflectionShader : IDeferredShader
{
public double MaxReflectDist { get; set; }
public double ScreenSpaceStep { get; set; }
public int Iterations { get; set; }
public double Tolerance { get; set; }
public double Strength { get; set; }
public double Bias { get; set; }
public ScreenSpaceDielectricReflectionShader(double Strength = 1, double MaxReflectDist = 3, double ScreenSpaceStep = 8, int Iterations = 3, double Tolerance = 0.4, double Bias = 0.001)
{
this.MaxReflectDist = MaxReflectDist;
this.ScreenSpaceStep = ScreenSpaceStep;
this.Iterations = Iterations;
this.Tolerance = Tolerance;
this.Bias = Bias;
this.Strength = Strength;
}
public Colour PerPixel(Arr2D<Colour> CurrentFrame, Arr2D<double> DepthBuffer, (Arr2D<Vec3D> Position,
Arr2D<Vec3D> Normal, Arr2D<Colour> Albedo, Arr2D<double> Specular, Arr2D<Colour> Emmissive,
Arr2D<Mesh> MeshRef) DeferredBuffer, int x, int y, int[] Resolution,
Camera CameraObj)
{
Colour? ReflectCol = DeferredShaderStaticMethods.GetScreenSpaceReflection(CurrentFrame, DepthBuffer, DeferredBuffer,
x, y, Resolution, CameraObj, MaxReflectDist, ScreenSpaceStep, Tolerance, Iterations, Strength, Bias);
if (ReflectCol != null)
{
return CurrentFrame[x, y] + ReflectCol.GetValueOrDefault();
}
else
{
return CurrentFrame[x, y];
}
}
}
class ScreenSpaceDielectricReflectionShaderWithCubeMapFallback : IDeferredShader
{
public double MaxReflectDist { get; set; }
public double ScreenSpaceStep { get; set; }
public int Iterations { get; set; }
public double Tolerance { get; set; }
public double Strength { get; set; }
public double Bias { get; set; }
public ScreenSpaceDielectricReflectionShaderWithCubeMapFallback(double Strength = 1, double MaxReflectDist = 4, double ScreenSpaceStep = 8, int Iterations = 3,
double Tolerance = 0.4, double Bias = 0.001)
{
this.MaxReflectDist = MaxReflectDist;
this.ScreenSpaceStep = ScreenSpaceStep;
this.Iterations = Iterations;
this.Tolerance = Tolerance;
this.Bias = Bias;
this.Strength = Strength;
}
public Colour PerPixel(Arr2D<Colour> CurrentFrame, Arr2D<double> DepthBuffer, (Arr2D<Vec3D> Position,
Arr2D<Vec3D> Normal, Arr2D<Colour> Albedo, Arr2D<double> Specular, Arr2D<Colour> Emmissive,
Arr2D<Mesh> MeshRef) DeferredBuffer, int x, int y, int[] Resolution,
Camera CameraObj)
{
Colour? ReflectCol = DeferredShaderStaticMethods.GetScreenSpaceReflection(CurrentFrame, DepthBuffer, DeferredBuffer,
x, y, Resolution, CameraObj, MaxReflectDist, ScreenSpaceStep, Tolerance, Iterations, Strength, Bias);
CubeMap? CubeMapReflection = DeferredBuffer.MeshRef[x, y].CubeMapReflection;
if (ReflectCol != null)
{
return CurrentFrame[x, y] + ReflectCol.GetValueOrDefault();
}
else if (CubeMapReflection != null)
{
Vec3D PixelToCamVector = (CameraObj.Position - DeferredBuffer.Position[x, y]).Normalise();
Vec3D ReflectionVector = PixelToCamVector - DeferredBuffer.Normal[x, y].ScalarMult(2 * Vec3D.DotProduct(DeferredBuffer.Normal[x, y], PixelToCamVector));
Colour ReflectionCol = CubeMapReflection.GetReflectionAlbedo(ReflectionVector);
return CurrentFrame[x, y] + ReflectionCol.ScalarMult(DeferredBuffer.Specular[x, y] * Strength);
}
else
{
return CurrentFrame[x, y];
}
}
}
class ScreenSpaceMetallicReflectionShader : IDeferredShader
{
public double MaxReflectDist { get; set; }
public double ScreenSpaceStep { get; set; }
public int Iterations { get; set; }
public double Tolerance { get; set; }
public double Strength { get; set; }
public double Bias { get; set; }
public ScreenSpaceMetallicReflectionShader(double Strength = 1, double MaxReflectDist = 4, double ScreenSpaceStep = 8, int Iterations = 3, double Tolerance = 0.4, double Bias = 0.001)
{
this.MaxReflectDist = MaxReflectDist;
this.ScreenSpaceStep = ScreenSpaceStep;
this.Iterations = Iterations;
this.Tolerance = Tolerance;
this.Bias = Bias;
this.Strength = Strength;
}
public Colour PerPixel(Arr2D<Colour> CurrentFrame, Arr2D<double> DepthBuffer, (Arr2D<Vec3D> Position,
Arr2D<Vec3D> Normal, Arr2D<Colour> Albedo, Arr2D<double> Specular, Arr2D<Colour> Emmissive,
Arr2D<Mesh> MeshRef) DeferredBuffer, int x, int y, int[] Resolution,
Camera CameraObj)
{
Colour? ReflectCol = DeferredShaderStaticMethods.GetScreenSpaceReflection(CurrentFrame, DepthBuffer, DeferredBuffer,
x, y, Resolution, CameraObj, MaxReflectDist, ScreenSpaceStep, Tolerance, Iterations, Strength, Bias);
if (ReflectCol != null)
{
return CurrentFrame[x, y] + Colour.ComponentWiseProd(DeferredBuffer.Albedo[x, y], ReflectCol.GetValueOrDefault());
}
else
{
return CurrentFrame[x, y];
}
}
}
class ScreenSpaceMetallicReflectionShaderWithCubeMapFallback : IDeferredShader
{
public double MaxReflectDist { get; set; }
public double ScreenSpaceStep { get; set; }
public int Iterations { get; set; }
public double Tolerance { get; set; }
public double Strength { get; set; }
public double Bias { get; set; }
public ScreenSpaceMetallicReflectionShaderWithCubeMapFallback(double Strength = 1, double MaxReflectDist = 4, double ScreenSpaceStep = 8, int Iterations = 3,
double Tolerance = 0.4, double Bias = 0.001)
{
this.MaxReflectDist = MaxReflectDist;
this.ScreenSpaceStep = ScreenSpaceStep;
this.Iterations = Iterations;
this.Tolerance = Tolerance;
this.Bias = Bias;
this.Strength = Strength;
}
public Colour PerPixel(Arr2D<Colour> CurrentFrame, Arr2D<double> DepthBuffer, (Arr2D<Vec3D> Position,
Arr2D<Vec3D> Normal, Arr2D<Colour> Albedo, Arr2D<double> Specular, Arr2D<Colour> Emmissive,
Arr2D<Mesh> MeshRef) DeferredBuffer, int x, int y, int[] Resolution,
Camera CameraObj)
{
Colour? ReflectCol = DeferredShaderStaticMethods.GetScreenSpaceReflection(CurrentFrame, DepthBuffer, DeferredBuffer,
x, y, Resolution, CameraObj, MaxReflectDist, ScreenSpaceStep, Tolerance, Iterations, Strength, Bias);
CubeMap? CubeMapReflection = DeferredBuffer.MeshRef[x, y].CubeMapReflection;
if (ReflectCol != null)
{
return CurrentFrame[x, y] + Colour.ComponentWiseProd(DeferredBuffer.Albedo[x, y], ReflectCol.GetValueOrDefault());
}
else if (CubeMapReflection != null)
{
Vec3D PixelToCamVector = (CameraObj.Position - DeferredBuffer.Position[x, y]).Normalise();
Vec3D ReflectionVector = PixelToCamVector - DeferredBuffer.Normal[x, y].ScalarMult(2 * Vec3D.DotProduct(DeferredBuffer.Normal[x, y], PixelToCamVector));
Colour ReflectionCol = CubeMapReflection.GetReflectionAlbedo(ReflectionVector);
return CurrentFrame[x, y] + Colour.ComponentWiseProd(DeferredBuffer.Albedo[x, y], ReflectionCol.ScalarMult(DeferredBuffer.Specular[x, y] * Strength));
}
else
{
return CurrentFrame[x, y];
}
}
}
}