UNPKG

16.5 kBTypeScriptView Raw
1import { Scene } from './../scenes/Scene';
2import { Camera } from './../cameras/Camera';
3import { WebGLExtensions } from './webgl/WebGLExtensions';
4import { WebGLInfo } from './webgl/WebGLInfo';
5import { WebGLShadowMap } from './webgl/WebGLShadowMap';
6import { WebGLCapabilities } from './webgl/WebGLCapabilities';
7import { WebGLProperties } from './webgl/WebGLProperties';
8import { WebGLProgram } from './webgl/WebGLProgram';
9import { RenderTarget, WebGLRenderLists } from './webgl/WebGLRenderLists';
10import { WebGLState } from './webgl/WebGLState';
11import { Vector2 } from './../math/Vector2';
12import { Vector4 } from './../math/Vector4';
13import { Color } from './../math/Color';
14import { WebGLRenderTarget } from './WebGLRenderTarget';
15import { Object3D } from './../core/Object3D';
16import { Material } from './../materials/Material';
17import { ToneMapping, ShadowMapType, CullFace, TextureEncoding } from '../constants';
18import { WebXRManager } from '../renderers/webxr/WebXRManager';
19import { BufferGeometry } from './../core/BufferGeometry';
20import { Texture } from '../textures/Texture';
21import { DataTexture3D } from '../textures/DataTexture3D';
22import { XRAnimationLoopCallback } from './webxr/WebXR';
23import { Vector3 } from '../math/Vector3';
24import { Box3 } from '../math/Box3';
25import { DataTexture2DArray } from '../textures/DataTexture2DArray';
26import { ColorRepresentation } from '../utils';
27
28export interface Renderer {
29 domElement: HTMLCanvasElement;
30
31 render(scene: Object3D, camera: Camera): void;
32 setSize(width: number, height: number, updateStyle?: boolean): void;
33}
34
35/** This is only available in worker JS contexts, not the DOM. */
36// tslint:disable-next-line:no-empty-interface
37export interface OffscreenCanvas extends EventTarget {}
38
39export interface WebGLRendererParameters {
40 /**
41 * A Canvas where the renderer draws its output.
42 */
43 canvas?: HTMLCanvasElement | OffscreenCanvas | undefined;
44
45 /**
46 * A WebGL Rendering Context.
47 * (https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext)
48 * Default is null
49 */
50 context?: WebGLRenderingContext | undefined;
51
52 /**
53 * shader precision. Can be "highp", "mediump" or "lowp".
54 */
55 precision?: string | undefined;
56
57 /**
58 * default is false.
59 */
60 alpha?: boolean | undefined;
61
62 /**
63 * default is true.
64 */
65 premultipliedAlpha?: boolean | undefined;
66
67 /**
68 * default is false.
69 */
70 antialias?: boolean | undefined;
71
72 /**
73 * default is true.
74 */
75 stencil?: boolean | undefined;
76
77 /**
78 * default is false.
79 */
80 preserveDrawingBuffer?: boolean | undefined;
81
82 /**
83 * Can be "high-performance", "low-power" or "default"
84 */
85 powerPreference?: string | undefined;
86
87 /**
88 * default is true.
89 */
90 depth?: boolean | undefined;
91
92 /**
93 * default is false.
94 */
95 logarithmicDepthBuffer?: boolean | undefined;
96
97 /**
98 * default is false.
99 */
100 failIfMajorPerformanceCaveat?: boolean | undefined;
101}
102
103export interface WebGLDebug {
104 /**
105 * Enables error checking and reporting when shader programs are being compiled.
106 */
107 checkShaderErrors: boolean;
108}
109
110/**
111 * The WebGL renderer displays your beautifully crafted scenes using WebGL, if your device supports it.
112 * This renderer has way better performance than CanvasRenderer.
113 *
114 * see {@link https://github.com/mrdoob/three.js/blob/master/src/renderers/WebGLRenderer.js|src/renderers/WebGLRenderer.js}
115 */
116export class WebGLRenderer implements Renderer {
117 /**
118 * parameters is an optional object with properties defining the renderer's behaviour.
119 * The constructor also accepts no parameters at all.
120 * In all cases, it will assume sane defaults when parameters are missing.
121 */
122 constructor(parameters?: WebGLRendererParameters);
123
124 /**
125 * A Canvas where the renderer draws its output.
126 * This is automatically created by the renderer in the constructor (if not provided already); you just need to add it to your page.
127 * @default document.createElementNS( 'http://www.w3.org/1999/xhtml', 'canvas' )
128 */
129 domElement: HTMLCanvasElement;
130
131 /**
132 * The HTML5 Canvas's 'webgl' context obtained from the canvas where the renderer will draw.
133 */
134 context: WebGLRenderingContext;
135
136 /**
137 * Defines whether the renderer should automatically clear its output before rendering.
138 * @default true
139 */
140 autoClear: boolean;
141
142 /**
143 * If autoClear is true, defines whether the renderer should clear the color buffer. Default is true.
144 * @default true
145 */
146 autoClearColor: boolean;
147
148 /**
149 * If autoClear is true, defines whether the renderer should clear the depth buffer. Default is true.
150 * @default true
151 */
152 autoClearDepth: boolean;
153
154 /**
155 * If autoClear is true, defines whether the renderer should clear the stencil buffer. Default is true.
156 * @default true
157 */
158 autoClearStencil: boolean;
159
160 /**
161 * Debug configurations.
162 * @default { checkShaderErrors: true }
163 */
164 debug: WebGLDebug;
165
166 /**
167 * Defines whether the renderer should sort objects. Default is true.
168 * @default true
169 */
170 sortObjects: boolean;
171
172 /**
173 * @default []
174 */
175 clippingPlanes: any[];
176
177 /**
178 * @default false
179 */
180 localClippingEnabled: boolean;
181
182 extensions: WebGLExtensions;
183
184 /**
185 * Default is LinearEncoding.
186 * @default THREE.LinearEncoding
187 */
188 outputEncoding: TextureEncoding;
189
190 /**
191 * @default false
192 */
193 physicallyCorrectLights: boolean;
194
195 /**
196 * @default THREE.NoToneMapping
197 */
198 toneMapping: ToneMapping;
199
200 /**
201 * @default 1
202 */
203 toneMappingExposure: number;
204
205 info: WebGLInfo;
206
207 shadowMap: WebGLShadowMap;
208
209 pixelRatio: number;
210
211 capabilities: WebGLCapabilities;
212 properties: WebGLProperties;
213 renderLists: WebGLRenderLists;
214 state: WebGLState;
215
216 xr: WebXRManager;
217
218 /**
219 * Return the WebGL context.
220 */
221 getContext(): WebGLRenderingContext;
222 getContextAttributes(): any;
223 forceContextLoss(): void;
224
225 /**
226 * @deprecated Use {@link WebGLCapabilities#getMaxAnisotropy .capabilities.getMaxAnisotropy()} instead.
227 */
228 getMaxAnisotropy(): number;
229
230 /**
231 * @deprecated Use {@link WebGLCapabilities#precision .capabilities.precision} instead.
232 */
233 getPrecision(): string;
234
235 getPixelRatio(): number;
236 setPixelRatio(value: number): void;
237
238 getDrawingBufferSize(target: Vector2): Vector2;
239 setDrawingBufferSize(width: number, height: number, pixelRatio: number): void;
240
241 getSize(target: Vector2): Vector2;
242
243 /**
244 * Resizes the output canvas to (width, height), and also sets the viewport to fit that size, starting in (0, 0).
245 */
246 setSize(width: number, height: number, updateStyle?: boolean): void;
247
248 getCurrentViewport(target: Vector4): Vector4;
249
250 /**
251 * Copies the viewport into target.
252 */
253 getViewport(target: Vector4): Vector4;
254
255 /**
256 * Sets the viewport to render from (x, y) to (x + width, y + height).
257 * (x, y) is the lower-left corner of the region.
258 */
259 setViewport(x: Vector4 | number, y?: number, width?: number, height?: number): void;
260
261 /**
262 * Copies the scissor area into target.
263 */
264 getScissor(target: Vector4): Vector4;
265
266 /**
267 * Sets the scissor area from (x, y) to (x + width, y + height).
268 */
269 setScissor(x: Vector4 | number, y?: number, width?: number, height?: number): void;
270
271 /**
272 * Returns true if scissor test is enabled; returns false otherwise.
273 */
274 getScissorTest(): boolean;
275
276 /**
277 * Enable the scissor test. When this is enabled, only the pixels within the defined scissor area will be affected by further renderer actions.
278 */
279 setScissorTest(enable: boolean): void;
280
281 /**
282 * Sets the custom opaque sort function for the WebGLRenderLists. Pass null to use the default painterSortStable function.
283 */
284 setOpaqueSort(method: (a: any, b: any) => number): void;
285
286 /**
287 * Sets the custom transparent sort function for the WebGLRenderLists. Pass null to use the default reversePainterSortStable function.
288 */
289 setTransparentSort(method: (a: any, b: any) => number): void;
290
291 /**
292 * Returns a THREE.Color instance with the current clear color.
293 */
294 getClearColor(target: Color): Color;
295
296 /**
297 * Sets the clear color, using color for the color and alpha for the opacity.
298 */
299 setClearColor(color: ColorRepresentation, alpha?: number): void;
300
301 /**
302 * Returns a float with the current clear alpha. Ranges from 0 to 1.
303 */
304 getClearAlpha(): number;
305
306 setClearAlpha(alpha: number): void;
307
308 /**
309 * Tells the renderer to clear its color, depth or stencil drawing buffer(s).
310 * Arguments default to true
311 */
312 clear(color?: boolean, depth?: boolean, stencil?: boolean): void;
313
314 clearColor(): void;
315 clearDepth(): void;
316 clearStencil(): void;
317 clearTarget(renderTarget: WebGLRenderTarget, color: boolean, depth: boolean, stencil: boolean): void;
318
319 /**
320 * @deprecated Use {@link WebGLState#reset .state.reset()} instead.
321 */
322 resetGLState(): void;
323 dispose(): void;
324
325 renderBufferImmediate(object: Object3D, program: WebGLProgram): void;
326
327 renderBufferDirect(
328 camera: Camera,
329 scene: Scene,
330 geometry: BufferGeometry,
331 material: Material,
332 object: Object3D,
333 geometryGroup: any,
334 ): void;
335
336 /**
337 * A build in function that can be used instead of requestAnimationFrame. For WebXR projects this function must be used.
338 * @param callback The function will be called every available frame. If `null` is passed it will stop any already ongoing animation.
339 */
340 setAnimationLoop(callback: XRAnimationLoopCallback | null): void;
341
342 /**
343 * @deprecated Use {@link WebGLRenderer#setAnimationLoop .setAnimationLoop()} instead.
344 */
345 animate(callback: () => void): void;
346
347 /**
348 * Compiles all materials in the scene with the camera. This is useful to precompile shaders before the first rendering.
349 */
350 compile(scene: Object3D, camera: Camera): void;
351
352 /**
353 * Render a scene or an object using a camera.
354 * The render is done to a previously specified {@link WebGLRenderTarget#renderTarget .renderTarget} set by calling
355 * {@link WebGLRenderer#setRenderTarget .setRenderTarget} or to the canvas as usual.
356 *
357 * By default render buffers are cleared before rendering but you can prevent this by setting the property
358 * {@link WebGLRenderer#autoClear autoClear} to false. If you want to prevent only certain buffers being cleared
359 * you can set either the {@link WebGLRenderer#autoClearColor autoClearColor},
360 * {@link WebGLRenderer#autoClearStencil autoClearStencil} or {@link WebGLRenderer#autoClearDepth autoClearDepth}
361 * properties to false. To forcibly clear one ore more buffers call {@link WebGLRenderer#clear .clear}.
362 */
363 render(scene: Object3D, camera: Camera): void;
364
365 /**
366 * Returns the current active cube face.
367 */
368 getActiveCubeFace(): number;
369
370 /**
371 * Returns the current active mipmap level.
372 */
373 getActiveMipmapLevel(): number;
374
375 /**
376 * Returns the current render target. If no render target is set, null is returned.
377 */
378 getRenderTarget(): RenderTarget | null;
379
380 /**
381 * @deprecated Use {@link WebGLRenderer#getRenderTarget .getRenderTarget()} instead.
382 */
383 getCurrentRenderTarget(): RenderTarget | null;
384
385 /**
386 * Sets the active render target.
387 *
388 * @param renderTarget The {@link WebGLRenderTarget renderTarget} that needs to be activated. When `null` is given, the canvas is set as the active render target instead.
389 * @param activeCubeFace Specifies the active cube side (PX 0, NX 1, PY 2, NY 3, PZ 4, NZ 5) of {@link WebGLCubeRenderTarget}.
390 * @param activeMipmapLevel Specifies the active mipmap level.
391 */
392 setRenderTarget(renderTarget: RenderTarget | null, activeCubeFace?: number, activeMipmapLevel?: number): void;
393
394 readRenderTargetPixels(
395 renderTarget: RenderTarget,
396 x: number,
397 y: number,
398 width: number,
399 height: number,
400 buffer: any,
401 activeCubeFaceIndex?: number,
402 ): void;
403
404 /**
405 * Copies a region of the currently bound framebuffer into the selected mipmap level of the selected texture.
406 * This region is defined by the size of the destination texture's mip level, offset by the input position.
407 *
408 * @param position Specifies the pixel offset from which to copy out of the framebuffer.
409 * @param texture Specifies the destination texture.
410 * @param level Specifies the destination mipmap level of the texture.
411 */
412 copyFramebufferToTexture(position: Vector2, texture: Texture, level?: number): void;
413
414 /**
415 * Copies srcTexture to the specified level of dstTexture, offset by the input position.
416 *
417 * @param position Specifies the pixel offset into the dstTexture where the copy will occur.
418 * @param srcTexture Specifies the source texture.
419 * @param dstTexture Specifies the destination texture.
420 * @param level Specifies the destination mipmap level of the texture.
421 */
422 copyTextureToTexture(position: Vector2, srcTexture: Texture, dstTexture: Texture, level?: number): void;
423
424 /**
425 * Copies the pixels of a texture in the bounds sourceBox in the desination texture starting from the given position.
426 * @param sourceBox Specifies the bounds
427 * @param position Specifies the pixel offset into the dstTexture where the copy will occur.
428 * @param srcTexture Specifies the source texture.
429 * @param dstTexture Specifies the destination texture.
430 * @param level Specifies the destination mipmap level of the texture.
431 */
432 copyTextureToTexture3D(
433 sourceBox: Box3,
434 position: Vector3,
435 srcTexture: Texture,
436 dstTexture: DataTexture3D | DataTexture2DArray,
437 level?: number,
438 ): void;
439
440 /**
441 * Initializes the given texture. Can be used to preload a texture rather than waiting until first render (which can cause noticeable lags due to decode and GPU upload overhead).
442 *
443 * @param texture The texture to Initialize.
444 */
445 initTexture(texture: Texture): void;
446
447 /**
448 * Can be used to reset the internal WebGL state.
449 */
450 resetState(): void;
451
452 /**
453 * @deprecated
454 */
455 gammaFactor: number;
456
457 /**
458 * @deprecated Use {@link WebGLRenderer#xr .xr} instead.
459 */
460 vr: boolean;
461
462 /**
463 * @deprecated Use {@link WebGLShadowMap#enabled .shadowMap.enabled} instead.
464 */
465 shadowMapEnabled: boolean;
466
467 /**
468 * @deprecated Use {@link WebGLShadowMap#type .shadowMap.type} instead.
469 */
470 shadowMapType: ShadowMapType;
471
472 /**
473 * @deprecated Use {@link WebGLShadowMap#cullFace .shadowMap.cullFace} instead.
474 */
475 shadowMapCullFace: CullFace;
476
477 /**
478 * @deprecated Use {@link WebGLExtensions#get .extensions.get( 'OES_texture_float' )} instead.
479 */
480 supportsFloatTextures(): any;
481
482 /**
483 * @deprecated Use {@link WebGLExtensions#get .extensions.get( 'OES_texture_half_float' )} instead.
484 */
485 supportsHalfFloatTextures(): any;
486
487 /**
488 * @deprecated Use {@link WebGLExtensions#get .extensions.get( 'OES_standard_derivatives' )} instead.
489 */
490 supportsStandardDerivatives(): any;
491
492 /**
493 * @deprecated Use {@link WebGLExtensions#get .extensions.get( 'WEBGL_compressed_texture_s3tc' )} instead.
494 */
495 supportsCompressedTextureS3TC(): any;
496
497 /**
498 * @deprecated Use {@link WebGLExtensions#get .extensions.get( 'WEBGL_compressed_texture_pvrtc' )} instead.
499 */
500 supportsCompressedTexturePVRTC(): any;
501
502 /**
503 * @deprecated Use {@link WebGLExtensions#get .extensions.get( 'EXT_blend_minmax' )} instead.
504 */
505 supportsBlendMinMax(): any;
506
507 /**
508 * @deprecated Use {@link WebGLCapabilities#vertexTextures .capabilities.vertexTextures} instead.
509 */
510 supportsVertexTextures(): any;
511
512 /**
513 * @deprecated Use {@link WebGLExtensions#get .extensions.get( 'ANGLE_instanced_arrays' )} instead.
514 */
515 supportsInstancedArrays(): any;
516
517 /**
518 * @deprecated Use {@link WebGLRenderer#setScissorTest .setScissorTest()} instead.
519 */
520 enableScissorTest(boolean: any): any;
521}
522
\No newline at end of file