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