UNPKG

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