forked from google/model-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Renderer.js
230 lines (188 loc) · 6.32 KB
/
Renderer.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
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
/*
* Copyright 2018 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {ACESFilmicToneMapping, EventDispatcher, WebGLRenderer} from 'three';
import {IS_AR_CANDIDATE} from '../constants.js';
import {$tick} from '../model-viewer-base.js';
import {resolveDpr} from '../utils.js';
import {ARRenderer} from './ARRenderer.js';
import TextureUtils from './TextureUtils.js';
import * as WebGLUtils from './WebGLUtils.js';
const GAMMA_FACTOR = 2.2;
export const $arRenderer = Symbol('arRenderer');
/**
* Registers canvases with Canvas2DRenderingContexts and renders them
* all in the same WebGLRenderingContext, spitting out textures to apply
* to the canvases. Creates a fullscreen WebGL canvas that is not added
* to the DOM, and on each frame, renders each registered canvas on a portion
* of the WebGL canvas, and applies the texture on the registered canvas.
*
* In the future, can use ImageBitmapRenderingContext instead of
* Canvas2DRenderingContext if supported for cheaper transfering of
* the texture.
*/
export default class Renderer extends EventDispatcher {
get canRender() {
return this.renderer != null && this.context != null;
}
constructor() {
super();
const webGlOptions = {antialias: true};
// Only enable certain options when Web XR capabilities are detected:
if (IS_AR_CANDIDATE) {
Object.assign(webGlOptions, {alpha: true, preserveDrawingBuffer: true});
}
this.canvas = document.createElement('canvas');
// Need to support both 'webgl' and 'experimental-webgl' (IE11).
try {
this.context = WebGLUtils.getContext(this.canvas, webGlOptions);
// Patch the gl context's extension functions before passing
// it to three.
WebGLUtils.applyExtensionCompatibility(this.context);
this.renderer = new WebGLRenderer({
canvas: this.canvas,
context: this.context,
});
this.renderer.autoClear = false;
this.renderer.gammaOutput = true;
this.renderer.gammaFactor = GAMMA_FACTOR;
this.renderer.physicallyCorrectLights = true;
this.renderer.setPixelRatio(resolveDpr());
// ACESFilmicToneMapping appears to be the most "saturated",
// and similar to Filament's gltf-viewer.
this.renderer.toneMapping = ACESFilmicToneMapping;
} catch (error) {
this.context = null;
console.warn(error);
}
this[$arRenderer] = ARRenderer.fromInlineRenderer(this);
this.textureUtils = this.canRender ?
new TextureUtils(this.renderer, {pmremSamples: 128}) :
null;
this.scenes = new Set();
this.scenesRendered = 0;
this.setRendererSize(1, 1);
this.lastTick = performance.now();
}
set exposure(exposure) {
if (!this.canRender) {
return;
}
const exposureIsNumber =
typeof exposure === 'number' && !self.isNaN(exposure);
this.renderer.toneMappingExposure = exposureIsNumber ? exposure : 1.0;
}
get exposure() {
if (!this.canRender) {
return null;
}
return this.renderer.toneMappingExposure;
}
setRendererSize(width, height) {
if (this.canRender) {
this.renderer.setSize(width, height, false);
}
this.width = width;
this.height = height;
}
registerScene(scene) {
this.scenes.add(scene);
if (this.canRender && this.scenes.size > 0) {
this.renderer.setAnimationLoop((time) => this.render(time));
}
}
unregisterScene(scene) {
this.scenes.delete(scene);
if (this.canRender && this.scenes.size === 0) {
this.renderer.setAnimationLoop(null);
}
}
async supportsPresentation() {
return this.canRender && this[$arRenderer].supportsPresentation();
}
get presentedScene() {
return this[$arRenderer].presentedScene;
}
async present(scene) {
try {
return await this[$arRenderer].present(scene);
} catch (error) {
this[$arRenderer].stopPresenting();
throw error;
} finally {
// NOTE(cdata): Setting width and height to 0 will have the effect of
// invoking a `setSize` the next time we render in this renderer
this.width = this.height = 0;
}
}
stopPresenting() {
return this[$arRenderer].stopPresenting();
}
get isPresenting() {
return this[$arRenderer] != null && this[$arRenderer].isPresenting;
}
render(t) {
if (!this.canRender || this.isPresenting) {
return;
}
this.scenesRendered = 0;
const delta = t - this.lastTick;
const dpr = resolveDpr();
if (dpr !== this.renderer.getPixelRatio()) {
this.renderer.setPixelRatio(dpr);
}
for (let scene of this.scenes) {
const {element, width, height, context} = scene;
element[$tick](t, delta);
if (!scene.isVisible || !scene.isDirty || scene.paused) {
continue;
}
const camera = scene.getCamera();
if (width > this.width || height > this.height) {
const maxWidth = Math.max(width, this.width);
const maxHeight = Math.max(height, this.height);
this.setRendererSize(maxWidth, maxHeight, false);
}
// Need to set the render target in order to prevent
// clearing the depth from a different buffer -- possibly
// from something in
this.renderer.setRenderTarget(null);
this.renderer.clearDepth();
this.renderer.setViewport(0, 0, width, height);
this.renderer.render(scene, camera);
const widthDPR = width * dpr;
const heightDPR = height * dpr;
context.drawImage(
this.renderer.domElement,
0,
0,
widthDPR,
heightDPR,
0,
0,
widthDPR,
heightDPR);
scene.isDirty = false;
this.scenesRendered++;
}
this.lastTick = t;
}
dispose() {
super.dispose();
if (this.textureUtils != null) {
this.textureUtils.dispose();
}
this.textureUtils = null;
}
}