-
-
Notifications
You must be signed in to change notification settings - Fork 36.4k
Expand file tree
/
Copy pathWebGLRenderer.js
More file actions
3596 lines (2336 loc) · 103 KB
/
WebGLRenderer.js
File metadata and controls
3596 lines (2336 loc) · 103 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {
REVISION,
BackSide,
FrontSide,
DoubleSide,
HalfFloatType,
UnsignedByteType,
NoToneMapping,
LinearMipmapLinearFilter,
SRGBColorSpace,
LinearSRGBColorSpace,
RGBAIntegerFormat,
RGIntegerFormat,
RedIntegerFormat,
UnsignedIntType,
UnsignedShortType,
UnsignedInt248Type,
UnsignedShort4444Type,
UnsignedShort5551Type,
WebGLCoordinateSystem
} from '../constants.js';
import { Color } from '../math/Color.js';
import { Frustum } from '../math/Frustum.js';
import { Matrix4 } from '../math/Matrix4.js';
import { Vector3 } from '../math/Vector3.js';
import { Vector4 } from '../math/Vector4.js';
import { WebGLAnimation } from './webgl/WebGLAnimation.js';
import { WebGLAttributes } from './webgl/WebGLAttributes.js';
import { WebGLBackground } from './webgl/WebGLBackground.js';
import { WebGLBindingStates } from './webgl/WebGLBindingStates.js';
import { WebGLBufferRenderer } from './webgl/WebGLBufferRenderer.js';
import { WebGLCapabilities } from './webgl/WebGLCapabilities.js';
import { WebGLClipping } from './webgl/WebGLClipping.js';
import { WebGLCubeMaps } from './webgl/WebGLCubeMaps.js';
import { WebGLCubeUVMaps } from './webgl/WebGLCubeUVMaps.js';
import { WebGLExtensions } from './webgl/WebGLExtensions.js';
import { WebGLGeometries } from './webgl/WebGLGeometries.js';
import { WebGLIndexedBufferRenderer } from './webgl/WebGLIndexedBufferRenderer.js';
import { WebGLInfo } from './webgl/WebGLInfo.js';
import { WebGLMorphtargets } from './webgl/WebGLMorphtargets.js';
import { WebGLObjects } from './webgl/WebGLObjects.js';
import { WebGLOutput } from './webgl/WebGLOutput.js';
import { WebGLPrograms } from './webgl/WebGLPrograms.js';
import { WebGLProperties } from './webgl/WebGLProperties.js';
import { WebGLRenderLists } from './webgl/WebGLRenderLists.js';
import { WebGLRenderStates } from './webgl/WebGLRenderStates.js';
import { WebGLRenderTarget } from './WebGLRenderTarget.js';
import { WebGLShadowMap } from './webgl/WebGLShadowMap.js';
import { WebGLState } from './webgl/WebGLState.js';
import { WebGLTextures } from './webgl/WebGLTextures.js';
import { WebGLUniforms } from './webgl/WebGLUniforms.js';
import { WebGLUtils } from './webgl/WebGLUtils.js';
import { WebXRManager } from './webxr/WebXRManager.js';
import { WebGLMaterials } from './webgl/WebGLMaterials.js';
import { WebGLUniformsGroups } from './webgl/WebGLUniformsGroups.js';
import { createCanvasElement, probeAsync, warnOnce, error, warn, log } from '../utils.js';
import { ColorManagement } from '../math/ColorManagement.js';
import { getDFGLUT } from './shaders/DFGLUTData.js';
/**
* This renderer uses WebGL 2 to display scenes.
*
* WebGL 1 is not supported since `r163`.
*/
class WebGLRenderer {
/**
* Constructs a new WebGL renderer.
*
* @param {WebGLRenderer~Options} [parameters] - The configuration parameter.
*/
constructor( parameters = {} ) {
const {
canvas = createCanvasElement(),
context = null,
depth = true,
stencil = false,
alpha = false,
antialias = false,
premultipliedAlpha = true,
preserveDrawingBuffer = false,
powerPreference = 'default',
failIfMajorPerformanceCaveat = false,
reversedDepthBuffer = false,
outputBufferType = UnsignedByteType,
} = parameters;
/**
* This flag can be used for type testing.
*
* @type {boolean}
* @readonly
* @default true
*/
this.isWebGLRenderer = true;
let _alpha;
if ( context !== null ) {
if ( typeof WebGLRenderingContext !== 'undefined' && context instanceof WebGLRenderingContext ) {
throw new Error( 'THREE.WebGLRenderer: WebGL 1 is not supported since r163.' );
}
_alpha = context.getContextAttributes().alpha;
} else {
_alpha = alpha;
}
const _outputBufferType = outputBufferType;
const INTEGER_FORMATS = new Set( [
RGBAIntegerFormat,
RGIntegerFormat,
RedIntegerFormat
] );
const UNSIGNED_TYPES = new Set( [
UnsignedByteType,
UnsignedIntType,
UnsignedShortType,
UnsignedInt248Type,
UnsignedShort4444Type,
UnsignedShort5551Type
] );
const uintClearColor = new Uint32Array( 4 );
const intClearColor = new Int32Array( 4 );
let currentRenderList = null;
let currentRenderState = null;
// render() can be called from within a callback triggered by another render.
// We track this so that the nested render call gets its list and state isolated from the parent render call.
const renderListStack = [];
const renderStateStack = [];
// internal render target for non-UnsignedByteType color buffer
let output = null;
// public properties
/**
* A canvas where the renderer draws its output.This is automatically created by the renderer
* in the constructor (if not provided already); you just need to add it to your page like so:
* ```js
* document.body.appendChild( renderer.domElement );
* ```
*
* @type {HTMLCanvasElement|OffscreenCanvas}
*/
this.domElement = canvas;
/**
* A object with debug configuration settings.
*
* - `checkShaderErrors`: If it is `true`, defines whether material shader programs are
* checked for errors during compilation and linkage process. It may be useful to disable
* this check in production for performance gain. It is strongly recommended to keep these
* checks enabled during development. If the shader does not compile and link - it will not
* work and associated material will not render.
* - `onShaderError(gl, program, glVertexShader,glFragmentShader)`: A callback function that
* can be used for custom error reporting. The callback receives the WebGL context, an instance
* of WebGLProgram as well two instances of WebGLShader representing the vertex and fragment shader.
* Assigning a custom function disables the default error reporting.
*
* @type {Object}
*/
this.debug = {
/**
* Enables error checking and reporting when shader programs are being compiled.
* @type {boolean}
*/
checkShaderErrors: true,
/**
* Callback for custom error reporting.
* @type {?Function}
*/
onShaderError: null
};
// clearing
/**
* Whether the renderer should automatically clear its output before rendering a frame or not.
*
* @type {boolean}
* @default true
*/
this.autoClear = true;
/**
* If {@link WebGLRenderer#autoClear} set to `true`, whether the renderer should clear
* the color buffer or not.
*
* @type {boolean}
* @default true
*/
this.autoClearColor = true;
/**
* If {@link WebGLRenderer#autoClear} set to `true`, whether the renderer should clear
* the depth buffer or not.
*
* @type {boolean}
* @default true
*/
this.autoClearDepth = true;
/**
* If {@link WebGLRenderer#autoClear} set to `true`, whether the renderer should clear
* the stencil buffer or not.
*
* @type {boolean}
* @default true
*/
this.autoClearStencil = true;
// scene graph
/**
* Whether the renderer should sort objects or not.
*
* Note: Sorting is used to attempt to properly render objects that have some
* degree of transparency. By definition, sorting objects may not work in all
* cases. Depending on the needs of application, it may be necessary to turn
* off sorting and use other methods to deal with transparency rendering e.g.
* manually determining each object's rendering order.
*
* @type {boolean}
* @default true
*/
this.sortObjects = true;
// user-defined clipping
/**
* User-defined clipping planes specified in world space. These planes apply globally.
* Points in space whose dot product with the plane is negative are cut away.
*
* @type {Array<Plane>}
*/
this.clippingPlanes = [];
/**
* Whether the renderer respects object-level clipping planes or not.
*
* @type {boolean}
* @default false
*/
this.localClippingEnabled = false;
// tone mapping
/**
* The tone mapping technique of the renderer.
*
* @type {(NoToneMapping|LinearToneMapping|ReinhardToneMapping|CineonToneMapping|ACESFilmicToneMapping|CustomToneMapping|AgXToneMapping|NeutralToneMapping)}
* @default NoToneMapping
*/
this.toneMapping = NoToneMapping;
/**
* Exposure level of tone mapping.
*
* @type {number}
* @default 1
*/
this.toneMappingExposure = 1.0;
// transmission
/**
* The normalized resolution scale for the transmission render target, measured in percentage
* of viewport dimensions. Lowering this value can result in significant performance improvements
* when using {@link MeshPhysicalMaterial#transmission}.
*
* @type {number}
* @default 1
*/
this.transmissionResolutionScale = 1.0;
// internal properties
const _this = this;
let _isContextLost = false;
// internal state cache
this._outputColorSpace = SRGBColorSpace;
let _currentActiveCubeFace = 0;
let _currentActiveMipmapLevel = 0;
let _currentRenderTarget = null;
let _currentMaterialId = - 1;
let _currentCamera = null;
const _currentViewport = new Vector4();
const _currentScissor = new Vector4();
let _currentScissorTest = null;
const _currentClearColor = new Color( 0x000000 );
let _currentClearAlpha = 0;
//
let _width = canvas.width;
let _height = canvas.height;
let _pixelRatio = 1;
let _opaqueSort = null;
let _transparentSort = null;
const _viewport = new Vector4( 0, 0, _width, _height );
const _scissor = new Vector4( 0, 0, _width, _height );
let _scissorTest = false;
// frustum
const _frustum = new Frustum();
// clipping
let _clippingEnabled = false;
let _localClippingEnabled = false;
// camera matrices cache
const _projScreenMatrix = new Matrix4();
const _vector3 = new Vector3();
const _vector4 = new Vector4();
const _emptyScene = { background: null, fog: null, environment: null, overrideMaterial: null, isScene: true };
let _renderBackground = false;
function getTargetPixelRatio() {
return _currentRenderTarget === null ? _pixelRatio : 1;
}
// initialize
let _gl = context;
function getContext( contextName, contextAttributes ) {
return canvas.getContext( contextName, contextAttributes );
}
try {
const contextAttributes = {
alpha: true,
depth,
stencil,
antialias,
premultipliedAlpha,
preserveDrawingBuffer,
powerPreference,
failIfMajorPerformanceCaveat,
};
// OffscreenCanvas does not have setAttribute, see #22811
if ( 'setAttribute' in canvas ) canvas.setAttribute( 'data-engine', `three.js r${REVISION}` );
// event listeners must be registered before WebGL context is created, see #12753
canvas.addEventListener( 'webglcontextlost', onContextLost, false );
canvas.addEventListener( 'webglcontextrestored', onContextRestore, false );
canvas.addEventListener( 'webglcontextcreationerror', onContextCreationError, false );
if ( _gl === null ) {
const contextName = 'webgl2';
_gl = getContext( contextName, contextAttributes );
if ( _gl === null ) {
if ( getContext( contextName ) ) {
throw new Error( 'Error creating WebGL context with your selected attributes.' );
} else {
throw new Error( 'Error creating WebGL context.' );
}
}
}
} catch ( e ) {
error( 'WebGLRenderer: ' + e.message );
throw e;
}
let extensions, capabilities, state, info;
let properties, textures, cubemaps, cubeuvmaps, attributes, geometries, objects;
let programCache, materials, renderLists, renderStates, clipping, shadowMap;
let background, morphtargets, bufferRenderer, indexedBufferRenderer;
let utils, bindingStates, uniformsGroups;
function initGLContext() {
extensions = new WebGLExtensions( _gl );
extensions.init();
utils = new WebGLUtils( _gl, extensions );
capabilities = new WebGLCapabilities( _gl, extensions, parameters, utils );
state = new WebGLState( _gl, extensions );
if ( capabilities.reversedDepthBuffer && reversedDepthBuffer ) {
state.buffers.depth.setReversed( true );
}
info = new WebGLInfo( _gl );
properties = new WebGLProperties();
textures = new WebGLTextures( _gl, extensions, state, properties, capabilities, utils, info );
cubemaps = new WebGLCubeMaps( _this );
cubeuvmaps = new WebGLCubeUVMaps( _this );
attributes = new WebGLAttributes( _gl );
bindingStates = new WebGLBindingStates( _gl, attributes );
geometries = new WebGLGeometries( _gl, attributes, info, bindingStates );
objects = new WebGLObjects( _gl, geometries, attributes, bindingStates, info );
morphtargets = new WebGLMorphtargets( _gl, capabilities, textures );
clipping = new WebGLClipping( properties );
programCache = new WebGLPrograms( _this, cubemaps, cubeuvmaps, extensions, capabilities, bindingStates, clipping );
materials = new WebGLMaterials( _this, properties );
renderLists = new WebGLRenderLists();
renderStates = new WebGLRenderStates( extensions );
background = new WebGLBackground( _this, cubemaps, cubeuvmaps, state, objects, _alpha, premultipliedAlpha );
shadowMap = new WebGLShadowMap( _this, objects, capabilities );
uniformsGroups = new WebGLUniformsGroups( _gl, info, capabilities, state );
bufferRenderer = new WebGLBufferRenderer( _gl, extensions, info );
indexedBufferRenderer = new WebGLIndexedBufferRenderer( _gl, extensions, info );
info.programs = programCache.programs;
/**
* Holds details about the capabilities of the current rendering context.
*
* @name WebGLRenderer#capabilities
* @type {WebGLRenderer~Capabilities}
*/
_this.capabilities = capabilities;
/**
* Provides methods for retrieving and testing WebGL extensions.
*
* - `get(extensionName:string)`: Used to check whether a WebGL extension is supported
* and return the extension object if available.
* - `has(extensionName:string)`: returns `true` if the extension is supported.
*
* @name WebGLRenderer#extensions
* @type {Object}
*/
_this.extensions = extensions;
/**
* Used to track properties of other objects like native WebGL objects.
*
* @name WebGLRenderer#properties
* @type {Object}
*/
_this.properties = properties;
/**
* Manages the render lists of the renderer.
*
* @name WebGLRenderer#renderLists
* @type {Object}
*/
_this.renderLists = renderLists;
/**
* Interface for managing shadows.
*
* @name WebGLRenderer#shadowMap
* @type {WebGLRenderer~ShadowMap}
*/
_this.shadowMap = shadowMap;
/**
* Interface for managing the WebGL state.
*
* @name WebGLRenderer#state
* @type {Object}
*/
_this.state = state;
/**
* Holds a series of statistical information about the GPU memory
* and the rendering process. Useful for debugging and monitoring.
*
* By default these data are reset at each render call but when having
* multiple render passes per frame (e.g. when using post processing) it can
* be preferred to reset with a custom pattern. First, set `autoReset` to
* `false`.
* ```js
* renderer.info.autoReset = false;
* ```
* Call `reset()` whenever you have finished to render a single frame.
* ```js
* renderer.info.reset();
* ```
*
* @name WebGLRenderer#info
* @type {WebGLRenderer~Info}
*/
_this.info = info;
}
initGLContext();
// initialize internal render target for non-UnsignedByteType color buffer
if ( _outputBufferType !== UnsignedByteType ) {
output = new WebGLOutput( _outputBufferType, canvas.width, canvas.height, depth, stencil );
}
// xr
const xr = new WebXRManager( _this, _gl );
/**
* A reference to the XR manager.
*
* @type {WebXRManager}
*/
this.xr = xr;
/**
* Returns the rendering context.
*
* @return {WebGL2RenderingContext} The rendering context.
*/
this.getContext = function () {
return _gl;
};
/**
* Returns the rendering context attributes.
*
* @return {WebGLContextAttributes} The rendering context attributes.
*/
this.getContextAttributes = function () {
return _gl.getContextAttributes();
};
/**
* Simulates a loss of the WebGL context. This requires support for the `WEBGL_lose_context` extension.
*/
this.forceContextLoss = function () {
const extension = extensions.get( 'WEBGL_lose_context' );
if ( extension ) extension.loseContext();
};
/**
* Simulates a restore of the WebGL context. This requires support for the `WEBGL_lose_context` extension.
*/
this.forceContextRestore = function () {
const extension = extensions.get( 'WEBGL_lose_context' );
if ( extension ) extension.restoreContext();
};
/**
* Returns the pixel ratio.
*
* @return {number} The pixel ratio.
*/
this.getPixelRatio = function () {
return _pixelRatio;
};
/**
* Sets the given pixel ratio and resizes the canvas if necessary.
*
* @param {number} value - The pixel ratio.
*/
this.setPixelRatio = function ( value ) {
if ( value === undefined ) return;
_pixelRatio = value;
this.setSize( _width, _height, false );
};
/**
* Returns the renderer's size in logical pixels. This method does not honor the pixel ratio.
*
* @param {Vector2} target - The method writes the result in this target object.
* @return {Vector2} The renderer's size in logical pixels.
*/
this.getSize = function ( target ) {
return target.set( _width, _height );
};
/**
* Resizes the output canvas to (width, height) with device pixel ratio taken
* into account, and also sets the viewport to fit that size, starting in (0,
* 0). Setting `updateStyle` to false prevents any style changes to the output canvas.
*
* @param {number} width - The width in logical pixels.
* @param {number} height - The height in logical pixels.
* @param {boolean} [updateStyle=true] - Whether to update the `style` attribute of the canvas or not.
*/
this.setSize = function ( width, height, updateStyle = true ) {
if ( xr.isPresenting ) {
warn( 'WebGLRenderer: Can\'t change size while VR device is presenting.' );
return;
}
_width = width;
_height = height;
canvas.width = Math.floor( width * _pixelRatio );
canvas.height = Math.floor( height * _pixelRatio );
if ( updateStyle === true ) {
canvas.style.width = width + 'px';
canvas.style.height = height + 'px';
}
if ( output !== null ) {
output.setSize( canvas.width, canvas.height );
}
this.setViewport( 0, 0, width, height );
};
/**
* Returns the drawing buffer size in physical pixels. This method honors the pixel ratio.
*
* @param {Vector2} target - The method writes the result in this target object.
* @return {Vector2} The drawing buffer size.
*/
this.getDrawingBufferSize = function ( target ) {
return target.set( _width * _pixelRatio, _height * _pixelRatio ).floor();
};
/**
* This method allows to define the drawing buffer size by specifying
* width, height and pixel ratio all at once. The size of the drawing
* buffer is computed with this formula:
* ```js
* size.x = width * pixelRatio;
* size.y = height * pixelRatio;
* ```
*
* @param {number} width - The width in logical pixels.
* @param {number} height - The height in logical pixels.
* @param {number} pixelRatio - The pixel ratio.
*/
this.setDrawingBufferSize = function ( width, height, pixelRatio ) {
_width = width;
_height = height;
_pixelRatio = pixelRatio;
canvas.width = Math.floor( width * pixelRatio );
canvas.height = Math.floor( height * pixelRatio );
this.setViewport( 0, 0, width, height );
};
/**
* Sets the post-processing effects to be applied after rendering.
*
* @param {Array} effects - An array of post-processing effects.
*/
this.setEffects = function ( effects ) {
if ( _outputBufferType === UnsignedByteType ) {
console.error( 'THREE.WebGLRenderer: setEffects() requires outputBufferType set to HalfFloatType or FloatType.' );
return;
}
if ( effects ) {
for ( let i = 0; i < effects.length; i ++ ) {
if ( effects[ i ].isOutputPass === true ) {
console.warn( 'THREE.WebGLRenderer: OutputPass is not needed in setEffects(). Tone mapping and color space conversion are applied automatically.' );
break;
}
}
}
output.setEffects( effects || [] );
};
/**
* Returns the current viewport definition.
*
* @param {Vector2} target - The method writes the result in this target object.
* @return {Vector2} The current viewport definition.
*/
this.getCurrentViewport = function ( target ) {
return target.copy( _currentViewport );
};
/**
* Returns the viewport definition.
*
* @param {Vector4} target - The method writes the result in this target object.
* @return {Vector4} The viewport definition.
*/
this.getViewport = function ( target ) {
return target.copy( _viewport );
};
/**
* Sets the viewport to render from `(x, y)` to `(x + width, y + height)`.
*
* @param {number | Vector4} x - The horizontal coordinate for the lower left corner of the viewport origin in logical pixel unit.
* Or alternatively a four-component vector specifying all the parameters of the viewport.
* @param {number} y - The vertical coordinate for the lower left corner of the viewport origin in logical pixel unit.
* @param {number} width - The width of the viewport in logical pixel unit.
* @param {number} height - The height of the viewport in logical pixel unit.
*/
this.setViewport = function ( x, y, width, height ) {
if ( x.isVector4 ) {
_viewport.set( x.x, x.y, x.z, x.w );
} else {
_viewport.set( x, y, width, height );
}
state.viewport( _currentViewport.copy( _viewport ).multiplyScalar( _pixelRatio ).round() );
};
/**
* Returns the scissor region.
*
* @param {Vector4} target - The method writes the result in this target object.
* @return {Vector4} The scissor region.
*/
this.getScissor = function ( target ) {
return target.copy( _scissor );
};
/**
* Sets the scissor region to render from `(x, y)` to `(x + width, y + height)`.
*
* @param {number | Vector4} x - The horizontal coordinate for the lower left corner of the scissor region origin in logical pixel unit.
* Or alternatively a four-component vector specifying all the parameters of the scissor region.
* @param {number} y - The vertical coordinate for the lower left corner of the scissor region origin in logical pixel unit.
* @param {number} width - The width of the scissor region in logical pixel unit.
* @param {number} height - The height of the scissor region in logical pixel unit.
*/
this.setScissor = function ( x, y, width, height ) {
if ( x.isVector4 ) {
_scissor.set( x.x, x.y, x.z, x.w );
} else {
_scissor.set( x, y, width, height );
}
state.scissor( _currentScissor.copy( _scissor ).multiplyScalar( _pixelRatio ).round() );
};
/**
* Returns `true` if the scissor test is enabled.
*
* @return {boolean} Whether the scissor test is enabled or not.
*/
this.getScissorTest = function () {
return _scissorTest;
};
/**
* Enable or disable the scissor test. When this is enabled, only the pixels
* within the defined scissor area will be affected by further renderer
* actions.
*
* @param {boolean} boolean - Whether the scissor test is enabled or not.
*/
this.setScissorTest = function ( boolean ) {
state.setScissorTest( _scissorTest = boolean );
};
/**
* Sets a custom opaque sort function for the render lists. Pass `null`
* to use the default `painterSortStable` function.
*
* @param {?Function} method - The opaque sort function.
*/
this.setOpaqueSort = function ( method ) {
_opaqueSort = method;
};
/**
* Sets a custom transparent sort function for the render lists. Pass `null`
* to use the default `reversePainterSortStable` function.
*
* @param {?Function} method - The opaque sort function.
*/
this.setTransparentSort = function ( method ) {
_transparentSort = method;
};
// Clearing
/**
* Returns the clear color.
*
* @param {Color} target - The method writes the result in this target object.
* @return {Color} The clear color.
*/
this.getClearColor = function ( target ) {
return target.copy( background.getClearColor() );
};
/**
* Sets the clear color and alpha.
*
* @param {Color} color - The clear color.
* @param {number} [alpha=1] - The clear alpha.
*/
this.setClearColor = function () {
background.setClearColor( ...arguments );
};
/**
* Returns the clear alpha. Ranges within `[0,1]`.
*
* @return {number} The clear alpha.
*/
this.getClearAlpha = function () {
return background.getClearAlpha();
};
/**
* Sets the clear alpha.
*
* @param {number} alpha - The clear alpha.
*/
this.setClearAlpha = function () {
background.setClearAlpha( ...arguments );
};
/**
* Tells the renderer to clear its color, depth or stencil drawing buffer(s).
* This method initializes the buffers to the current clear color values.
*
* @param {boolean} [color=true] - Whether the color buffer should be cleared or not.
* @param {boolean} [depth=true] - Whether the depth buffer should be cleared or not.
* @param {boolean} [stencil=true] - Whether the stencil buffer should be cleared or not.
*/
this.clear = function ( color = true, depth = true, stencil = true ) {
let bits = 0;
if ( color ) {
// check if we're trying to clear an integer target
let isIntegerFormat = false;
if ( _currentRenderTarget !== null ) {
const targetFormat = _currentRenderTarget.texture.format;
isIntegerFormat = INTEGER_FORMATS.has( targetFormat );
}
// use the appropriate clear functions to clear the target if it's a signed
// or unsigned integer target
if ( isIntegerFormat ) {
const targetType = _currentRenderTarget.texture.type;
const isUnsignedType = UNSIGNED_TYPES.has( targetType );
const clearColor = background.getClearColor();
const a = background.getClearAlpha();
const r = clearColor.r;
const g = clearColor.g;
const b = clearColor.b;
if ( isUnsignedType ) {
uintClearColor[ 0 ] = r;
uintClearColor[ 1 ] = g;
uintClearColor[ 2 ] = b;
uintClearColor[ 3 ] = a;
_gl.clearBufferuiv( _gl.COLOR, 0, uintClearColor );
} else {
intClearColor[ 0 ] = r;
intClearColor[ 1 ] = g;
intClearColor[ 2 ] = b;
intClearColor[ 3 ] = a;
_gl.clearBufferiv( _gl.COLOR, 0, intClearColor );
}
} else {
bits |= _gl.COLOR_BUFFER_BIT;
}
}
if ( depth ) {