/
postProcessRenderEffect.ts
259 lines (226 loc) · 8.32 KB
/
postProcessRenderEffect.ts
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
import type { Nullable } from "../../types";
import { Tools } from "../../Misc/tools";
import type { Camera } from "../../Cameras/camera";
import type { PostProcess } from "../../PostProcesses/postProcess";
import type { Engine } from "../../Engines/engine";
/**
* This represents a set of one or more post processes in Babylon.
* A post process can be used to apply a shader to a texture after it is rendered.
* @example https://doc.babylonjs.com/features/featuresDeepDive/postProcesses/postProcessRenderPipeline
*/
export class PostProcessRenderEffect {
private _postProcesses: { [Key: string]: Array<PostProcess> };
private _getPostProcesses: () => Nullable<PostProcess | Array<PostProcess>>;
private _singleInstance: boolean;
private _cameras: { [key: string]: Nullable<Camera> };
private _indicesForCamera: { [key: string]: number[] };
/**
* Name of the effect
* @internal
*/
public _name: string;
/**
* Instantiates a post process render effect.
* A post process can be used to apply a shader to a texture after it is rendered.
* @param engine The engine the effect is tied to
* @param name The name of the effect
* @param getPostProcesses A function that returns a set of post processes which the effect will run in order to be run.
* @param singleInstance False if this post process can be run on multiple cameras. (default: true)
*/
constructor(engine: Engine, name: string, getPostProcesses: () => Nullable<PostProcess | Array<PostProcess>>, singleInstance?: boolean) {
this._name = name;
this._singleInstance = singleInstance || true;
this._getPostProcesses = getPostProcesses;
this._cameras = {};
this._indicesForCamera = {};
this._postProcesses = {};
}
/**
* Checks if all the post processes in the effect are supported.
*/
public get isSupported(): boolean {
for (const index in this._postProcesses) {
if (Object.prototype.hasOwnProperty.call(this._postProcesses, index)) {
const pps = this._postProcesses[index];
for (let ppIndex = 0; ppIndex < pps.length; ppIndex++) {
if (!pps[ppIndex].isSupported) {
return false;
}
}
}
}
return true;
}
/**
* Updates the current state of the effect
* @internal
*/
public _update(): void {}
/**
* Attaches the effect on cameras
* @param cameras The camera to attach to.
* @internal
*/
public _attachCameras(cameras: Camera): void;
/**
* Attaches the effect on cameras
* @param cameras The camera to attach to.
* @internal
*/
public _attachCameras(cameras: Camera[]): void;
/**
* Attaches the effect on cameras
* @param cameras The camera to attach to.
* @internal
*/
public _attachCameras(cameras: any): void {
let cameraKey;
const cams = Tools.MakeArray(cameras || this._cameras);
if (!cams) {
return;
}
for (let i = 0; i < cams.length; i++) {
const camera = cams[i];
if (!camera) {
continue;
}
const cameraName = camera.name;
if (this._singleInstance) {
cameraKey = 0;
} else {
cameraKey = cameraName;
}
if (!this._postProcesses[cameraKey]) {
const postProcess = this._getPostProcesses();
if (postProcess) {
this._postProcesses[cameraKey] = Array.isArray(postProcess) ? postProcess : [postProcess];
}
}
if (!this._indicesForCamera[cameraName]) {
this._indicesForCamera[cameraName] = [];
}
this._postProcesses[cameraKey].forEach((postProcess: PostProcess) => {
const index = camera.attachPostProcess(postProcess);
this._indicesForCamera[cameraName].push(index);
});
if (!this._cameras[cameraName]) {
this._cameras[cameraName] = camera;
}
}
}
/**
* Detaches the effect on cameras
* @param cameras The camera to detach from.
* @internal
*/
public _detachCameras(cameras: Camera): void;
/**
* Detaches the effect on cameras
* @param cameras The camera to detach from.
* @internal
*/
public _detachCameras(cameras: Camera[]): void;
/**
* Detaches the effect on cameras
* @param cameras The camera to detach from.
* @internal
*/
public _detachCameras(cameras: any): void {
const cams = Tools.MakeArray(cameras || this._cameras);
if (!cams) {
return;
}
for (let i = 0; i < cams.length; i++) {
const camera: Camera = cams[i];
const cameraName: string = camera.name;
const postProcesses = this._postProcesses[this._singleInstance ? 0 : cameraName];
if (postProcesses) {
postProcesses.forEach((postProcess: PostProcess) => {
camera.detachPostProcess(postProcess);
});
}
if (this._cameras[cameraName]) {
this._cameras[cameraName] = null;
}
}
}
/**
* Enables the effect on given cameras
* @param cameras The camera to enable.
* @internal
*/
public _enable(cameras: Camera): void;
/**
* Enables the effect on given cameras
* @param cameras The camera to enable.
* @internal
*/
public _enable(cameras: Nullable<Camera[]>): void;
/**
* Enables the effect on given cameras
* @param cameras The camera to enable.
* @internal
*/
public _enable(cameras: any): void {
const cams: Nullable<Array<Camera>> = Tools.MakeArray(cameras || this._cameras);
if (!cams) {
return;
}
for (let i = 0; i < cams.length; i++) {
const camera = cams[i];
const cameraName = camera.name;
for (let j = 0; j < this._indicesForCamera[cameraName].length; j++) {
if (camera._postProcesses[this._indicesForCamera[cameraName][j]] === undefined || camera._postProcesses[this._indicesForCamera[cameraName][j]] === null) {
this._postProcesses[this._singleInstance ? 0 : cameraName].forEach((postProcess) => {
cams![i].attachPostProcess(postProcess, this._indicesForCamera[cameraName][j]);
});
}
}
}
}
/**
* Disables the effect on the given cameras
* @param cameras The camera to disable.
* @internal
*/
public _disable(cameras: Camera): void;
/**
* Disables the effect on the given cameras
* @param cameras The camera to disable.
* @internal
*/
public _disable(cameras: Nullable<Camera[]>): void;
/**
* Disables the effect on the given cameras
* @param cameras The camera to disable.
* @internal
*/
public _disable(cameras: any): void {
const cams: Nullable<Array<Camera>> = Tools.MakeArray(cameras || this._cameras);
if (!cams) {
return;
}
for (let i = 0; i < cams.length; i++) {
const camera = cams[i];
const cameraName = camera.name;
this._postProcesses[this._singleInstance ? 0 : cameraName].forEach((postProcess) => {
camera.detachPostProcess(postProcess);
});
}
}
/**
* Gets a list of the post processes contained in the effect.
* @param camera The camera to get the post processes on.
* @returns The list of the post processes in the effect.
*/
public getPostProcesses(camera?: Camera): Nullable<Array<PostProcess>> {
if (this._singleInstance) {
return this._postProcesses[0];
} else {
if (!camera) {
return null;
}
return this._postProcesses[camera.name];
}
}
}