UNPKG

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