CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/assets/threejs/r73/Projector.js
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
/**
7
* @author mrdoob / http://mrdoob.com/
8
* @author supereggbert / http://www.paulbrunt.co.uk/
9
* @author julianwa / https://github.com/julianwa
10
*/
11
12
THREE.RenderableObject = function () {
13
14
this.id = 0;
15
16
this.object = null;
17
this.z = 0;
18
this.renderOrder = 0;
19
20
};
21
22
//
23
24
THREE.RenderableFace = function () {
25
26
this.id = 0;
27
28
this.v1 = new THREE.RenderableVertex();
29
this.v2 = new THREE.RenderableVertex();
30
this.v3 = new THREE.RenderableVertex();
31
32
this.normalModel = new THREE.Vector3();
33
34
this.vertexNormalsModel = [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ];
35
this.vertexNormalsLength = 0;
36
37
this.color = new THREE.Color();
38
this.material = null;
39
this.uvs = [ new THREE.Vector2(), new THREE.Vector2(), new THREE.Vector2() ];
40
41
this.z = 0;
42
this.renderOrder = 0;
43
44
};
45
46
//
47
48
THREE.RenderableVertex = function () {
49
50
this.position = new THREE.Vector3();
51
this.positionWorld = new THREE.Vector3();
52
this.positionScreen = new THREE.Vector4();
53
54
this.visible = true;
55
56
};
57
58
THREE.RenderableVertex.prototype.copy = function ( vertex ) {
59
60
this.positionWorld.copy( vertex.positionWorld );
61
this.positionScreen.copy( vertex.positionScreen );
62
63
};
64
65
//
66
67
THREE.RenderableLine = function () {
68
69
this.id = 0;
70
71
this.v1 = new THREE.RenderableVertex();
72
this.v2 = new THREE.RenderableVertex();
73
74
this.vertexColors = [ new THREE.Color(), new THREE.Color() ];
75
this.material = null;
76
77
this.z = 0;
78
this.renderOrder = 0;
79
80
};
81
82
//
83
84
THREE.RenderableSprite = function () {
85
86
this.id = 0;
87
88
this.object = null;
89
90
this.x = 0;
91
this.y = 0;
92
this.z = 0;
93
94
this.rotation = 0;
95
this.scale = new THREE.Vector2();
96
97
this.material = null;
98
this.renderOrder = 0;
99
100
};
101
102
//
103
104
THREE.Projector = function () {
105
106
var _object, _objectCount, _objectPool = [], _objectPoolLength = 0,
107
_vertex, _vertexCount, _vertexPool = [], _vertexPoolLength = 0,
108
_face, _faceCount, _facePool = [], _facePoolLength = 0,
109
_line, _lineCount, _linePool = [], _linePoolLength = 0,
110
_sprite, _spriteCount, _spritePool = [], _spritePoolLength = 0,
111
112
_renderData = { objects: [], lights: [], elements: [] },
113
114
_vector3 = new THREE.Vector3(),
115
_vector4 = new THREE.Vector4(),
116
117
_clipBox = new THREE.Box3( new THREE.Vector3( - 1, - 1, - 1 ), new THREE.Vector3( 1, 1, 1 ) ),
118
_boundingBox = new THREE.Box3(),
119
_points3 = new Array( 3 ),
120
_points4 = new Array( 4 ),
121
122
_viewMatrix = new THREE.Matrix4(),
123
_viewProjectionMatrix = new THREE.Matrix4(),
124
125
_modelMatrix,
126
_modelViewProjectionMatrix = new THREE.Matrix4(),
127
128
_normalMatrix = new THREE.Matrix3(),
129
130
_frustum = new THREE.Frustum(),
131
132
_clippedVertex1PositionScreen = new THREE.Vector4(),
133
_clippedVertex2PositionScreen = new THREE.Vector4();
134
135
//
136
137
this.projectVector = function ( vector, camera ) {
138
139
console.warn( 'THREE.Projector: .projectVector() is now vector.project().' );
140
vector.project( camera );
141
142
};
143
144
this.unprojectVector = function ( vector, camera ) {
145
146
console.warn( 'THREE.Projector: .unprojectVector() is now vector.unproject().' );
147
vector.unproject( camera );
148
149
};
150
151
this.pickingRay = function ( vector, camera ) {
152
153
console.error( 'THREE.Projector: .pickingRay() is now raycaster.setFromCamera().' );
154
155
};
156
157
//
158
159
var RenderList = function () {
160
161
var normals = [];
162
var uvs = [];
163
164
var object = null;
165
var material = null;
166
167
var normalMatrix = new THREE.Matrix3();
168
169
var setObject = function ( value ) {
170
171
object = value;
172
material = object.material;
173
174
normalMatrix.getNormalMatrix( object.matrixWorld );
175
176
normals.length = 0;
177
uvs.length = 0;
178
179
};
180
181
var projectVertex = function ( vertex ) {
182
183
var position = vertex.position;
184
var positionWorld = vertex.positionWorld;
185
var positionScreen = vertex.positionScreen;
186
187
positionWorld.copy( position ).applyMatrix4( _modelMatrix );
188
positionScreen.copy( positionWorld ).applyMatrix4( _viewProjectionMatrix );
189
190
var invW = 1 / positionScreen.w;
191
192
positionScreen.x *= invW;
193
positionScreen.y *= invW;
194
positionScreen.z *= invW;
195
196
vertex.visible = positionScreen.x >= - 1 && positionScreen.x <= 1 &&
197
positionScreen.y >= - 1 && positionScreen.y <= 1 &&
198
positionScreen.z >= - 1 && positionScreen.z <= 1;
199
200
};
201
202
var pushVertex = function ( x, y, z ) {
203
204
_vertex = getNextVertexInPool();
205
_vertex.position.set( x, y, z );
206
207
projectVertex( _vertex );
208
209
};
210
211
var pushNormal = function ( x, y, z ) {
212
213
normals.push( x, y, z );
214
215
};
216
217
var pushUv = function ( x, y ) {
218
219
uvs.push( x, y );
220
221
};
222
223
var checkTriangleVisibility = function ( v1, v2, v3 ) {
224
225
if ( v1.visible === true || v2.visible === true || v3.visible === true ) return true;
226
227
_points3[ 0 ] = v1.positionScreen;
228
_points3[ 1 ] = v2.positionScreen;
229
_points3[ 2 ] = v3.positionScreen;
230
231
return _clipBox.isIntersectionBox( _boundingBox.setFromPoints( _points3 ) );
232
233
};
234
235
var checkBackfaceCulling = function ( v1, v2, v3 ) {
236
237
return ( ( v3.positionScreen.x - v1.positionScreen.x ) *
238
( v2.positionScreen.y - v1.positionScreen.y ) -
239
( v3.positionScreen.y - v1.positionScreen.y ) *
240
( v2.positionScreen.x - v1.positionScreen.x ) ) < 0;
241
242
};
243
244
var pushLine = function ( a, b ) {
245
246
var v1 = _vertexPool[ a ];
247
var v2 = _vertexPool[ b ];
248
249
_line = getNextLineInPool();
250
251
_line.id = object.id;
252
_line.v1.copy( v1 );
253
_line.v2.copy( v2 );
254
_line.z = ( v1.positionScreen.z + v2.positionScreen.z ) / 2;
255
_line.renderOrder = object.renderOrder;
256
257
_line.material = object.material;
258
259
_renderData.elements.push( _line );
260
261
};
262
263
var pushTriangle = function ( a, b, c ) {
264
265
var v1 = _vertexPool[ a ];
266
var v2 = _vertexPool[ b ];
267
var v3 = _vertexPool[ c ];
268
269
if ( checkTriangleVisibility( v1, v2, v3 ) === false ) return;
270
271
if ( material.side === THREE.DoubleSide || checkBackfaceCulling( v1, v2, v3 ) === true ) {
272
273
_face = getNextFaceInPool();
274
275
_face.id = object.id;
276
_face.v1.copy( v1 );
277
_face.v2.copy( v2 );
278
_face.v3.copy( v3 );
279
_face.z = ( v1.positionScreen.z + v2.positionScreen.z + v3.positionScreen.z ) / 3;
280
_face.renderOrder = object.renderOrder;
281
282
// use first vertex normal as face normal
283
284
_face.normalModel.fromArray( normals, a * 3 );
285
_face.normalModel.applyMatrix3( normalMatrix ).normalize();
286
287
for ( var i = 0; i < 3; i ++ ) {
288
289
var normal = _face.vertexNormalsModel[ i ];
290
normal.fromArray( normals, arguments[ i ] * 3 );
291
normal.applyMatrix3( normalMatrix ).normalize();
292
293
var uv = _face.uvs[ i ];
294
uv.fromArray( uvs, arguments[ i ] * 2 );
295
296
}
297
298
_face.vertexNormalsLength = 3;
299
300
_face.material = object.material;
301
302
_renderData.elements.push( _face );
303
304
}
305
306
};
307
308
return {
309
setObject: setObject,
310
projectVertex: projectVertex,
311
checkTriangleVisibility: checkTriangleVisibility,
312
checkBackfaceCulling: checkBackfaceCulling,
313
pushVertex: pushVertex,
314
pushNormal: pushNormal,
315
pushUv: pushUv,
316
pushLine: pushLine,
317
pushTriangle: pushTriangle
318
};
319
320
};
321
322
var renderList = new RenderList();
323
324
this.projectScene = function ( scene, camera, sortObjects, sortElements ) {
325
326
_faceCount = 0;
327
_lineCount = 0;
328
_spriteCount = 0;
329
330
_renderData.elements.length = 0;
331
332
if ( scene.autoUpdate === true ) scene.updateMatrixWorld();
333
if ( camera.parent === null ) camera.updateMatrixWorld();
334
335
_viewMatrix.copy( camera.matrixWorldInverse.getInverse( camera.matrixWorld ) );
336
_viewProjectionMatrix.multiplyMatrices( camera.projectionMatrix, _viewMatrix );
337
338
_frustum.setFromMatrix( _viewProjectionMatrix );
339
340
//
341
342
_objectCount = 0;
343
344
_renderData.objects.length = 0;
345
_renderData.lights.length = 0;
346
347
scene.traverseVisible( function ( object ) {
348
349
if ( object instanceof THREE.Light ) {
350
351
_renderData.lights.push( object );
352
353
} else if ( object instanceof THREE.Mesh || object instanceof THREE.Line || object instanceof THREE.Sprite ) {
354
355
var material = object.material;
356
357
if ( material.visible === false ) return;
358
359
if ( object.frustumCulled === false || _frustum.intersectsObject( object ) === true ) {
360
361
_object = getNextObjectInPool();
362
_object.id = object.id;
363
_object.object = object;
364
365
_vector3.setFromMatrixPosition( object.matrixWorld );
366
_vector3.applyProjection( _viewProjectionMatrix );
367
_object.z = _vector3.z;
368
_object.renderOrder = object.renderOrder;
369
370
_renderData.objects.push( _object );
371
372
}
373
374
}
375
376
} );
377
378
if ( sortObjects === true ) {
379
380
_renderData.objects.sort( painterSort );
381
382
}
383
384
//
385
386
for ( var o = 0, ol = _renderData.objects.length; o < ol; o ++ ) {
387
388
var object = _renderData.objects[ o ].object;
389
var geometry = object.geometry;
390
391
renderList.setObject( object );
392
393
_modelMatrix = object.matrixWorld;
394
395
_vertexCount = 0;
396
397
if ( object instanceof THREE.Mesh ) {
398
399
if ( geometry instanceof THREE.BufferGeometry ) {
400
401
var attributes = geometry.attributes;
402
var groups = geometry.groups;
403
404
if ( attributes.position === undefined ) continue;
405
406
var positions = attributes.position.array;
407
408
for ( var i = 0, l = positions.length; i < l; i += 3 ) {
409
410
renderList.pushVertex( positions[ i ], positions[ i + 1 ], positions[ i + 2 ] );
411
412
}
413
414
if ( attributes.normal !== undefined ) {
415
416
var normals = attributes.normal.array;
417
418
for ( var i = 0, l = normals.length; i < l; i += 3 ) {
419
420
renderList.pushNormal( normals[ i ], normals[ i + 1 ], normals[ i + 2 ] );
421
422
}
423
424
}
425
426
if ( attributes.uv !== undefined ) {
427
428
var uvs = attributes.uv.array;
429
430
for ( var i = 0, l = uvs.length; i < l; i += 2 ) {
431
432
renderList.pushUv( uvs[ i ], uvs[ i + 1 ] );
433
434
}
435
436
}
437
438
if ( geometry.index !== null ) {
439
440
var indices = geometry.index.array;
441
442
if ( groups.length > 0 ) {
443
444
for ( var o = 0; o < groups.length; o ++ ) {
445
446
var group = groups[ o ];
447
448
for ( var i = group.start, l = group.start + group.count; i < l; i += 3 ) {
449
450
renderList.pushTriangle( indices[ i ], indices[ i + 1 ], indices[ i + 2 ] );
451
452
}
453
454
}
455
456
} else {
457
458
for ( var i = 0, l = indices.length; i < l; i += 3 ) {
459
460
renderList.pushTriangle( indices[ i ], indices[ i + 1 ], indices[ i + 2 ] );
461
462
}
463
464
}
465
466
} else {
467
468
for ( var i = 0, l = positions.length / 3; i < l; i += 3 ) {
469
470
renderList.pushTriangle( i, i + 1, i + 2 );
471
472
}
473
474
}
475
476
} else if ( geometry instanceof THREE.Geometry ) {
477
478
var vertices = geometry.vertices;
479
var faces = geometry.faces;
480
var faceVertexUvs = geometry.faceVertexUvs[ 0 ];
481
482
_normalMatrix.getNormalMatrix( _modelMatrix );
483
484
var material = object.material;
485
486
var isFaceMaterial = material instanceof THREE.MeshFaceMaterial;
487
var objectMaterials = isFaceMaterial === true ? object.material : null;
488
489
for ( var v = 0, vl = vertices.length; v < vl; v ++ ) {
490
491
var vertex = vertices[ v ];
492
493
_vector3.copy( vertex );
494
495
if ( material.morphTargets === true ) {
496
497
var morphTargets = geometry.morphTargets;
498
var morphInfluences = object.morphTargetInfluences;
499
500
for ( var t = 0, tl = morphTargets.length; t < tl; t ++ ) {
501
502
var influence = morphInfluences[ t ];
503
504
if ( influence === 0 ) continue;
505
506
var target = morphTargets[ t ];
507
var targetVertex = target.vertices[ v ];
508
509
_vector3.x += ( targetVertex.x - vertex.x ) * influence;
510
_vector3.y += ( targetVertex.y - vertex.y ) * influence;
511
_vector3.z += ( targetVertex.z - vertex.z ) * influence;
512
513
}
514
515
}
516
517
renderList.pushVertex( _vector3.x, _vector3.y, _vector3.z );
518
519
}
520
521
for ( var f = 0, fl = faces.length; f < fl; f ++ ) {
522
523
var face = faces[ f ];
524
525
material = isFaceMaterial === true
526
? objectMaterials.materials[ face.materialIndex ]
527
: object.material;
528
529
if ( material === undefined ) continue;
530
531
var side = material.side;
532
533
var v1 = _vertexPool[ face.a ];
534
var v2 = _vertexPool[ face.b ];
535
var v3 = _vertexPool[ face.c ];
536
537
if ( renderList.checkTriangleVisibility( v1, v2, v3 ) === false ) continue;
538
539
var visible = renderList.checkBackfaceCulling( v1, v2, v3 );
540
541
if ( side !== THREE.DoubleSide ) {
542
543
if ( side === THREE.FrontSide && visible === false ) continue;
544
if ( side === THREE.BackSide && visible === true ) continue;
545
546
}
547
548
_face = getNextFaceInPool();
549
550
_face.id = object.id;
551
_face.v1.copy( v1 );
552
_face.v2.copy( v2 );
553
_face.v3.copy( v3 );
554
555
_face.normalModel.copy( face.normal );
556
557
if ( visible === false && ( side === THREE.BackSide || side === THREE.DoubleSide ) ) {
558
559
_face.normalModel.negate();
560
561
}
562
563
_face.normalModel.applyMatrix3( _normalMatrix ).normalize();
564
565
var faceVertexNormals = face.vertexNormals;
566
567
for ( var n = 0, nl = Math.min( faceVertexNormals.length, 3 ); n < nl; n ++ ) {
568
569
var normalModel = _face.vertexNormalsModel[ n ];
570
normalModel.copy( faceVertexNormals[ n ] );
571
572
if ( visible === false && ( side === THREE.BackSide || side === THREE.DoubleSide ) ) {
573
574
normalModel.negate();
575
576
}
577
578
normalModel.applyMatrix3( _normalMatrix ).normalize();
579
580
}
581
582
_face.vertexNormalsLength = faceVertexNormals.length;
583
584
var vertexUvs = faceVertexUvs[ f ];
585
586
if ( vertexUvs !== undefined ) {
587
588
for ( var u = 0; u < 3; u ++ ) {
589
590
_face.uvs[ u ].copy( vertexUvs[ u ] );
591
592
}
593
594
}
595
596
_face.color = face.color;
597
_face.material = material;
598
599
_face.z = ( v1.positionScreen.z + v2.positionScreen.z + v3.positionScreen.z ) / 3;
600
_face.renderOrder = object.renderOrder;
601
602
_renderData.elements.push( _face );
603
604
}
605
606
}
607
608
} else if ( object instanceof THREE.Line ) {
609
610
if ( geometry instanceof THREE.BufferGeometry ) {
611
612
var attributes = geometry.attributes;
613
614
if ( attributes.position !== undefined ) {
615
616
var positions = attributes.position.array;
617
618
for ( var i = 0, l = positions.length; i < l; i += 3 ) {
619
620
renderList.pushVertex( positions[ i ], positions[ i + 1 ], positions[ i + 2 ] );
621
622
}
623
624
if ( geometry.index !== null ) {
625
626
var indices = geometry.index.array;
627
628
for ( var i = 0, l = indices.length; i < l; i += 2 ) {
629
630
renderList.pushLine( indices[ i ], indices[ i + 1 ] );
631
632
}
633
634
} else {
635
636
var step = object instanceof THREE.LineSegments ? 2 : 1;
637
638
for ( var i = 0, l = ( positions.length / 3 ) - 1; i < l; i += step ) {
639
640
renderList.pushLine( i, i + 1 );
641
642
}
643
644
}
645
646
}
647
648
} else if ( geometry instanceof THREE.Geometry ) {
649
650
_modelViewProjectionMatrix.multiplyMatrices( _viewProjectionMatrix, _modelMatrix );
651
652
var vertices = object.geometry.vertices;
653
654
if ( vertices.length === 0 ) continue;
655
656
v1 = getNextVertexInPool();
657
v1.positionScreen.copy( vertices[ 0 ] ).applyMatrix4( _modelViewProjectionMatrix );
658
659
var step = object instanceof THREE.LineSegments ? 2 : 1;
660
661
for ( var v = 1, vl = vertices.length; v < vl; v ++ ) {
662
663
v1 = getNextVertexInPool();
664
v1.positionScreen.copy( vertices[ v ] ).applyMatrix4( _modelViewProjectionMatrix );
665
666
if ( ( v + 1 ) % step > 0 ) continue;
667
668
v2 = _vertexPool[ _vertexCount - 2 ];
669
670
_clippedVertex1PositionScreen.copy( v1.positionScreen );
671
_clippedVertex2PositionScreen.copy( v2.positionScreen );
672
673
if ( clipLine( _clippedVertex1PositionScreen, _clippedVertex2PositionScreen ) === true ) {
674
675
// Perform the perspective divide
676
_clippedVertex1PositionScreen.multiplyScalar( 1 / _clippedVertex1PositionScreen.w );
677
_clippedVertex2PositionScreen.multiplyScalar( 1 / _clippedVertex2PositionScreen.w );
678
679
_line = getNextLineInPool();
680
681
_line.id = object.id;
682
_line.v1.positionScreen.copy( _clippedVertex1PositionScreen );
683
_line.v2.positionScreen.copy( _clippedVertex2PositionScreen );
684
685
_line.z = Math.max( _clippedVertex1PositionScreen.z, _clippedVertex2PositionScreen.z );
686
_line.renderOrder = object.renderOrder;
687
688
_line.material = object.material;
689
690
if ( object.material.vertexColors === THREE.VertexColors ) {
691
692
_line.vertexColors[ 0 ].copy( object.geometry.colors[ v ] );
693
_line.vertexColors[ 1 ].copy( object.geometry.colors[ v - 1 ] );
694
695
}
696
697
_renderData.elements.push( _line );
698
699
}
700
701
}
702
703
}
704
705
} else if ( object instanceof THREE.Sprite ) {
706
707
_vector4.set( _modelMatrix.elements[ 12 ], _modelMatrix.elements[ 13 ], _modelMatrix.elements[ 14 ], 1 );
708
_vector4.applyMatrix4( _viewProjectionMatrix );
709
710
var invW = 1 / _vector4.w;
711
712
_vector4.z *= invW;
713
714
if ( _vector4.z >= - 1 && _vector4.z <= 1 ) {
715
716
_sprite = getNextSpriteInPool();
717
_sprite.id = object.id;
718
_sprite.x = _vector4.x * invW;
719
_sprite.y = _vector4.y * invW;
720
_sprite.z = _vector4.z;
721
_sprite.renderOrder = object.renderOrder;
722
_sprite.object = object;
723
724
_sprite.rotation = object.rotation;
725
726
_sprite.scale.x = object.scale.x * Math.abs( _sprite.x - ( _vector4.x + camera.projectionMatrix.elements[ 0 ] ) / ( _vector4.w + camera.projectionMatrix.elements[ 12 ] ) );
727
_sprite.scale.y = object.scale.y * Math.abs( _sprite.y - ( _vector4.y + camera.projectionMatrix.elements[ 5 ] ) / ( _vector4.w + camera.projectionMatrix.elements[ 13 ] ) );
728
729
_sprite.material = object.material;
730
731
_renderData.elements.push( _sprite );
732
733
}
734
735
}
736
737
}
738
739
if ( sortElements === true ) {
740
741
_renderData.elements.sort( painterSort );
742
743
}
744
745
return _renderData;
746
747
};
748
749
// Pools
750
751
function getNextObjectInPool() {
752
753
if ( _objectCount === _objectPoolLength ) {
754
755
var object = new THREE.RenderableObject();
756
_objectPool.push( object );
757
_objectPoolLength ++;
758
_objectCount ++;
759
return object;
760
761
}
762
763
return _objectPool[ _objectCount ++ ];
764
765
}
766
767
function getNextVertexInPool() {
768
769
if ( _vertexCount === _vertexPoolLength ) {
770
771
var vertex = new THREE.RenderableVertex();
772
_vertexPool.push( vertex );
773
_vertexPoolLength ++;
774
_vertexCount ++;
775
return vertex;
776
777
}
778
779
return _vertexPool[ _vertexCount ++ ];
780
781
}
782
783
function getNextFaceInPool() {
784
785
if ( _faceCount === _facePoolLength ) {
786
787
var face = new THREE.RenderableFace();
788
_facePool.push( face );
789
_facePoolLength ++;
790
_faceCount ++;
791
return face;
792
793
}
794
795
return _facePool[ _faceCount ++ ];
796
797
798
}
799
800
function getNextLineInPool() {
801
802
if ( _lineCount === _linePoolLength ) {
803
804
var line = new THREE.RenderableLine();
805
_linePool.push( line );
806
_linePoolLength ++;
807
_lineCount ++;
808
return line;
809
810
}
811
812
return _linePool[ _lineCount ++ ];
813
814
}
815
816
function getNextSpriteInPool() {
817
818
if ( _spriteCount === _spritePoolLength ) {
819
820
var sprite = new THREE.RenderableSprite();
821
_spritePool.push( sprite );
822
_spritePoolLength ++;
823
_spriteCount ++;
824
return sprite;
825
826
}
827
828
return _spritePool[ _spriteCount ++ ];
829
830
}
831
832
//
833
834
function painterSort( a, b ) {
835
836
if ( a.renderOrder !== b.renderOrder ) {
837
838
return a.renderOrder - b.renderOrder;
839
840
} else if ( a.z !== b.z ) {
841
842
return b.z - a.z;
843
844
} else if ( a.id !== b.id ) {
845
846
return a.id - b.id;
847
848
} else {
849
850
return 0;
851
852
}
853
854
}
855
856
function clipLine( s1, s2 ) {
857
858
var alpha1 = 0, alpha2 = 1,
859
860
// Calculate the boundary coordinate of each vertex for the near and far clip planes,
861
// Z = -1 and Z = +1, respectively.
862
bc1near = s1.z + s1.w,
863
bc2near = s2.z + s2.w,
864
bc1far = - s1.z + s1.w,
865
bc2far = - s2.z + s2.w;
866
867
if ( bc1near >= 0 && bc2near >= 0 && bc1far >= 0 && bc2far >= 0 ) {
868
869
// Both vertices lie entirely within all clip planes.
870
return true;
871
872
} else if ( ( bc1near < 0 && bc2near < 0 ) || ( bc1far < 0 && bc2far < 0 ) ) {
873
874
// Both vertices lie entirely outside one of the clip planes.
875
return false;
876
877
} else {
878
879
// The line segment spans at least one clip plane.
880
881
if ( bc1near < 0 ) {
882
883
// v1 lies outside the near plane, v2 inside
884
alpha1 = Math.max( alpha1, bc1near / ( bc1near - bc2near ) );
885
886
} else if ( bc2near < 0 ) {
887
888
// v2 lies outside the near plane, v1 inside
889
alpha2 = Math.min( alpha2, bc1near / ( bc1near - bc2near ) );
890
891
}
892
893
if ( bc1far < 0 ) {
894
895
// v1 lies outside the far plane, v2 inside
896
alpha1 = Math.max( alpha1, bc1far / ( bc1far - bc2far ) );
897
898
} else if ( bc2far < 0 ) {
899
900
// v2 lies outside the far plane, v2 inside
901
alpha2 = Math.min( alpha2, bc1far / ( bc1far - bc2far ) );
902
903
}
904
905
if ( alpha2 < alpha1 ) {
906
907
// The line segment spans two boundaries, but is outside both of them.
908
// (This can't happen when we're only clipping against just near/far but good
909
// to leave the check here for future usage if other clip planes are added.)
910
return false;
911
912
} else {
913
914
// Update the s1 and s2 vertices to match the clipped line segment.
915
s1.lerp( s2, alpha1 );
916
s2.lerp( s1, 1 - alpha2 );
917
918
return true;
919
920
}
921
922
}
923
924
}
925
926
};
927
928