-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
render-pass-bloom.js
162 lines (130 loc) · 4.56 KB
/
render-pass-bloom.js
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
import {
Color,
Texture,
BlendState,
RenderTarget,
RenderPass,
FILTER_LINEAR, ADDRESS_CLAMP_TO_EDGE
} from "playcanvas";
import { RenderPassDownsample } from "./render-pass-downsample.js";
import { RenderPassUpsample } from "./render-pass-upsample.js";
// based on https://learnopengl.com/Guest-Articles/2022/Phys.-Based-Bloom
class RenderPassBloom extends RenderPass {
bloomTexture;
lastMipLevel = 1;
bloomRenderTarget;
textureFormat;
renderTargets = [];
constructor(device, sourceTexture, format) {
super(device);
this._sourceTexture = sourceTexture;
this.textureFormat = format;
this.bloomRenderTarget = this.createRenderTarget(0);
this.bloomTexture = this.bloomRenderTarget.colorBuffer;
}
destroy() {
this.destroyRenderPasses();
this.destroyRenderTargets();
}
destroyRenderTargets(startIndex = 0) {
for (let i = startIndex; i < this.renderTargets.length; i++) {
const rt = this.renderTargets[i];
rt.destroyTextureBuffers();
rt.destroy();
}
this.renderTargets.length = 0;
}
destroyRenderPasses() {
for (let i = 0; i < this.beforePasses.length; i++) {
this.beforePasses[i].destroy();
}
this.beforePasses.length = 0;
}
createRenderTarget(index) {
return new RenderTarget({
depth: false,
colorBuffer: new Texture(this.device, {
name: `BloomTexture${index}`,
width: 1,
height: 1,
format: this.textureFormat,
mipmaps: false,
minFilter: FILTER_LINEAR,
magFilter: FILTER_LINEAR,
addressU: ADDRESS_CLAMP_TO_EDGE,
addressV: ADDRESS_CLAMP_TO_EDGE
})
});
}
createRenderTargets(count) {
for (let i = 0; i < count; i++) {
const rt = i === 0 ? this.bloomRenderTarget : this.createRenderTarget(i);
this.renderTargets.push(rt);
}
}
// number of levels till hitting min size
calcMipLevels(width, height, minSize) {
const min = Math.min(width, height);
return Math.floor(Math.log2(min) - Math.log2(minSize));
}
createRenderPasses(numPasses) {
const device = this.device;
// progressive downscale
let passSourceTexture = this._sourceTexture;
for (let i = 0; i < numPasses; i++) {
const pass = new RenderPassDownsample(device, passSourceTexture);
const rt = this.renderTargets[i];
pass.init(rt, {
resizeSource: passSourceTexture,
scaleX: 0.5,
scaleY: 0.5
});
pass.setClearColor(Color.BLACK); // clear when down-scaling
this.beforePasses.push(pass);
passSourceTexture = rt.colorBuffer;
}
// progressive upscale
passSourceTexture = this.renderTargets[numPasses - 1].colorBuffer;
for (let i = numPasses - 2; i >= 0; i--) {
const pass = new RenderPassUpsample(device, passSourceTexture);
const rt = this.renderTargets[i];
pass.init(rt);
pass.blendState = BlendState.ADDBLEND; // blend when up-scaling
this.beforePasses.push(pass);
passSourceTexture = rt.colorBuffer;
}
}
onDisable() {
// resize down the persistent render target
this.renderTargets[0]?.resize(1, 1);
// release the rest
this.destroyRenderPasses();
this.destroyRenderTargets(1);
}
set sourceTexture(value) {
this._sourceTexture = value;
if (this.beforePasses.length > 0) {
const firstPass = this.beforePasses[0];
// change resize source
firstPass.options.resizeSource = value;
// change downsample source
firstPass.sourceTexture = value;
}
}
get sourceTexture() {
return this._sourceTexture;
}
frameUpdate() {
super.frameUpdate();
// create an appropriate amount of render passes
let numPasses = this.calcMipLevels(this._sourceTexture.width, this._sourceTexture.height, 2 ** this.lastMipLevel);
numPasses = Math.max(1, numPasses);
if (this.renderTargets.length !== numPasses) {
this.destroyRenderPasses();
this.destroyRenderTargets(1);
this.createRenderTargets(numPasses);
this.createRenderPasses(numPasses);
}
}
}
export { RenderPassBloom };