-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFinalPass.ts
More file actions
122 lines (97 loc) · 3.5 KB
/
FinalPass.ts
File metadata and controls
122 lines (97 loc) · 3.5 KB
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
import {fromValues as Vec2} from 'gl-vec2';
import {create as Mat4} from 'gl-mat4';
import {setUniforms, hexToVec3} from 'gl-utils';
import {DrawParams, DepthTest, CullingMode} from 'gl-utils/DrawParams';
import {PassExecuteParams} from './structs';
export class FinalPass {
execute (params: PassExecuteParams) {
const {cfg, device, frameRes, viewport, camera} = params;
const {gl} = device;
const shader = frameRes.finalShader;
shader.use(gl);
const dp = new DrawParams();
dp.depth.test = DepthTest.AlwaysPass;
dp.depth.write = false;
dp.culling = CullingMode.None;
device.setState(dp);
device.setBackbufferAsRenderTarget();
gl.viewport(0.0, 0.0, viewport.width, viewport.height);
setUniforms(device, shader, {
'u_viewport': Vec2(viewport.width, viewport.height),
'u_displayMode': cfg.displayMode,
'u_gamma': cfg.postfx.gamma,
'u_nearAndFar': Vec2(camera.settings.zNear, camera.settings.zFar),
// textures:
'u_tonemappedTex': frameRes.tonemappingResultTex,
'u_linearDepthTex': frameRes.linearDepthTex,
'u_normalsTex': frameRes.forwardNormalsTex,
'u_ssaoTex': frameRes.ssaoTex,
// fxaa
'u_subpixel': cfg.postfx.subpixel,
'u_edgeThreshold': cfg.postfx.useFxaa ? cfg.postfx.edgeThreshold : 0.0,
'u_edgeThresholdMin': cfg.postfx.edgeThresholdMin,
}, true);
device.renderFullscreenQuad();
if (cfg.showDebugPositions) {
this.debugPositions(params);
}
if (cfg.shadows.showDebugView) {
this.debugShadowDepths(params);
}
}
private debugShadowDepths (params: PassExecuteParams) {
const {device, frameRes, viewport} = params;
const {gl} = device;
const MAX_WIDTH = 200, PADDING = 5;
const dbgSingleViewDim = Math.floor(Math.min(viewport.width / 3, MAX_WIDTH));
const shader = frameRes.dbgShadowsShader;
shader.use(gl);
const dp = new DrawParams();
dp.depth.test = DepthTest.AlwaysPass;
dp.depth.write = false;
dp.culling = CullingMode.None;
device.setState(dp);
const depthTex = [
frameRes.shadowDepthTex,
frameRes.sssDepthTex,
];
depthTex.forEach((tex, i) => {
gl.viewport(
i * dbgSingleViewDim + i * PADDING, 0,
dbgSingleViewDim, dbgSingleViewDim
);
setUniforms(device, shader, {
'u_depthTex': tex,
}, true);
device.renderFullscreenQuad(true);
});
}
private debugPositions (params: PassExecuteParams) {
const {cfg, device, frameRes, camera} = params;
const {gl} = device;
const shader = frameRes.dbgSphereShader;
shader.use(gl);
const dp = new DrawParams();
dp.depth.test = DepthTest.AlwaysPass;
dp.depth.write = false;
dp.culling = CullingMode.None;
device.setState(dp);
const spheres = [
{position: cfg.sphericalToCartesian(cfg.light0), color: cfg.light0.color},
{position: cfg.sphericalToCartesian(cfg.light1), color: cfg.light1.color},
{position: cfg.sphericalToCartesian(cfg.light2), color: cfg.light2.color},
{position: cfg.sphericalToCartesian(cfg.shadows.directionalLight), color: hexToVec3('#404040')},
{position: cfg.sphericalToCartesian(cfg.lightSSS), color: hexToVec3('#de875d')},
];
const vp = camera.getMVP(Mat4());
spheres.forEach(sphereDef => {
setUniforms(device, shader, {
'u_position': sphereDef.position,
'u_scale': 1.0,
'u_color': sphereDef.color,
'u_VP': vp,
}, true);
device.renderDebugSphere();
});
}
}