Projector.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author supereggbert / http://www.paulbrunt.co.uk/
  4. * @author julianwa / https://github.com/julianwa
  5. */
  6. THREE.Projector = function () {
  7. var _object, _objectCount, _objectPool = [], _objectPoolLength = 0,
  8. _vertex, _vertexCount, _vertexPool = [], _vertexPoolLength = 0,
  9. _face, _face3Count, _face3Pool = [], _face3PoolLength = 0,
  10. _line, _lineCount, _linePool = [], _linePoolLength = 0,
  11. _sprite, _spriteCount, _spritePool = [], _spritePoolLength = 0,
  12. _renderData = { objects: [], lights: [], elements: [] },
  13. _vA = new THREE.Vector3(),
  14. _vB = new THREE.Vector3(),
  15. _vC = new THREE.Vector3(),
  16. _vector3 = new THREE.Vector3(),
  17. _vector4 = new THREE.Vector4(),
  18. _clipBox = new THREE.Box3( new THREE.Vector3( -1, -1, -1 ), new THREE.Vector3( 1, 1, 1 ) ),
  19. _boundingBox = new THREE.Box3(),
  20. _points3 = new Array( 3 ),
  21. _points4 = new Array( 4 ),
  22. _viewMatrix = new THREE.Matrix4(),
  23. _viewProjectionMatrix = new THREE.Matrix4(),
  24. _modelMatrix,
  25. _modelViewProjectionMatrix = new THREE.Matrix4(),
  26. _normalMatrix = new THREE.Matrix3(),
  27. _centroid = new THREE.Vector3(),
  28. _frustum = new THREE.Frustum(),
  29. _clippedVertex1PositionScreen = new THREE.Vector4(),
  30. _clippedVertex2PositionScreen = new THREE.Vector4();
  31. this.projectVector = function ( vector, camera ) {
  32. camera.matrixWorldInverse.getInverse( camera.matrixWorld );
  33. _viewProjectionMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
  34. return vector.applyProjection( _viewProjectionMatrix );
  35. };
  36. this.unprojectVector = function () {
  37. var projectionMatrixInverse = new THREE.Matrix4();
  38. return function ( vector, camera ) {
  39. projectionMatrixInverse.getInverse( camera.projectionMatrix );
  40. _viewProjectionMatrix.multiplyMatrices( camera.matrixWorld, projectionMatrixInverse );
  41. return vector.applyProjection( _viewProjectionMatrix );
  42. };
  43. }();
  44. this.pickingRay = function ( vector, camera ) {
  45. // set two vectors with opposing z values
  46. vector.z = -1.0;
  47. var end = new THREE.Vector3( vector.x, vector.y, 1.0 );
  48. this.unprojectVector( vector, camera );
  49. this.unprojectVector( end, camera );
  50. // find direction from vector to end
  51. end.sub( vector ).normalize();
  52. return new THREE.Raycaster( vector, end );
  53. };
  54. var getObject = function ( object ) {
  55. _object = getNextObjectInPool();
  56. _object.id = object.id;
  57. _object.object = object;
  58. if ( object.renderDepth !== null ) {
  59. _object.z = object.renderDepth;
  60. } else {
  61. _vector3.setFromMatrixPosition( object.matrixWorld );
  62. _vector3.applyProjection( _viewProjectionMatrix );
  63. _object.z = _vector3.z;
  64. }
  65. return _object;
  66. };
  67. var projectObject = function ( object ) {
  68. if ( object.visible === false ) return;
  69. if ( object instanceof THREE.Light ) {
  70. _renderData.lights.push( object );
  71. } else if ( object instanceof THREE.Mesh || object instanceof THREE.Line || object instanceof THREE.Sprite ) {
  72. if ( object.frustumCulled === false || _frustum.intersectsObject( object ) === true ) {
  73. _renderData.objects.push( getObject( object ) );
  74. }
  75. }
  76. for ( var i = 0, l = object.children.length; i < l; i ++ ) {
  77. projectObject( object.children[ i ] );
  78. }
  79. };
  80. var projectGraph = function ( root, sortObjects ) {
  81. _objectCount = 0;
  82. _renderData.objects.length = 0;
  83. _renderData.lights.length = 0;
  84. projectObject( root );
  85. if ( sortObjects === true ) {
  86. _renderData.objects.sort( painterSort );
  87. }
  88. };
  89. var RenderList = function () {
  90. var normals = [];
  91. var object = null;
  92. var normalMatrix = new THREE.Matrix3();
  93. var setObject = function ( value ) {
  94. object = value;
  95. normalMatrix.getNormalMatrix( object.matrixWorld );
  96. normals.length = 0;
  97. };
  98. var projectVertex = function ( vertex ) {
  99. var position = vertex.position;
  100. var positionWorld = vertex.positionWorld;
  101. var positionScreen = vertex.positionScreen;
  102. positionWorld.copy( position ).applyMatrix4( _modelMatrix );
  103. positionScreen.copy( positionWorld ).applyMatrix4( _viewProjectionMatrix );
  104. var invW = 1 / positionScreen.w;
  105. positionScreen.x *= invW;
  106. positionScreen.y *= invW;
  107. positionScreen.z *= invW;
  108. vertex.visible = positionScreen.x >= -1 && positionScreen.x <= 1 &&
  109. positionScreen.y >= -1 && positionScreen.y <= 1 &&
  110. positionScreen.z >= -1 && positionScreen.z <= 1;
  111. };
  112. var pushVertex = function ( x, y, z ) {
  113. _vertex = getNextVertexInPool();
  114. _vertex.position.set( x, y, z );
  115. projectVertex( _vertex );
  116. };
  117. var pushNormal = function ( x, y, z ) {
  118. normals.push( x, y, z );
  119. };
  120. var checkTriangleVisibility = function ( v1, v2, v3 ) {
  121. _points3[ 0 ] = v1.positionScreen;
  122. _points3[ 1 ] = v2.positionScreen;
  123. _points3[ 2 ] = v3.positionScreen;
  124. if ( v1.visible === true || v2.visible === true || v3.visible === true ||
  125. _clipBox.isIntersectionBox( _boundingBox.setFromPoints( _points3 ) ) ) {
  126. return ( ( v3.positionScreen.x - v1.positionScreen.x ) *
  127. ( v2.positionScreen.y - v1.positionScreen.y ) -
  128. ( v3.positionScreen.y - v1.positionScreen.y ) *
  129. ( v2.positionScreen.x - v1.positionScreen.x ) ) < 0;
  130. }
  131. return false;
  132. };
  133. var pushLine = function ( a, b ) {
  134. var v1 = _vertexPool[ a ];
  135. var v2 = _vertexPool[ b ];
  136. _line = getNextLineInPool();
  137. _line.id = object.id;
  138. _line.v1.copy( v1 );
  139. _line.v2.copy( v2 );
  140. _face.z = ( v1.positionScreen.z + v2.positionScreen.z ) / 2;
  141. _line.material = object.material;
  142. _renderData.elements.push( _line );
  143. };
  144. var pushTriangle = function ( a, b, c ) {
  145. var v1 = _vertexPool[ a ];
  146. var v2 = _vertexPool[ b ];
  147. var v3 = _vertexPool[ c ];
  148. if ( checkTriangleVisibility( v1, v2, v3 ) === true ) {
  149. _face = getNextFace3InPool();
  150. _face.id = object.id;
  151. _face.v1.copy( v1 );
  152. _face.v2.copy( v2 );
  153. _face.v3.copy( v3 );
  154. _face.z = ( v1.positionScreen.z + v2.positionScreen.z + v3.positionScreen.z ) / 3;
  155. for ( var i = 0; i < 3; i ++ ) {
  156. var offset = arguments[ i ] * 3;
  157. var normal = _face.vertexNormalsModel[ i ];
  158. normal.set( normals[ offset + 0 ], normals[ offset + 1 ], normals[ offset + 2 ] );
  159. normal.applyMatrix3( normalMatrix ).normalize();
  160. }
  161. _face.vertexNormalsLength = 3;
  162. _face.material = object.material;
  163. _renderData.elements.push( _face );
  164. }
  165. };
  166. return {
  167. setObject: setObject,
  168. projectVertex: projectVertex,
  169. checkTriangleVisibility: checkTriangleVisibility,
  170. pushVertex: pushVertex,
  171. pushNormal: pushNormal,
  172. pushLine: pushLine,
  173. pushTriangle: pushTriangle
  174. }
  175. };
  176. var renderList = new RenderList();
  177. this.projectScene = function ( scene, camera, sortObjects, sortElements ) {
  178. var object, geometry, vertices, faces, face, faceVertexNormals, faceVertexUvs, uvs,
  179. isFaceMaterial, objectMaterials;
  180. _face3Count = 0;
  181. _lineCount = 0;
  182. _spriteCount = 0;
  183. _renderData.elements.length = 0;
  184. if ( scene.autoUpdate === true ) scene.updateMatrixWorld();
  185. if ( camera.parent === undefined ) camera.updateMatrixWorld();
  186. _viewMatrix.copy( camera.matrixWorldInverse.getInverse( camera.matrixWorld ) );
  187. _viewProjectionMatrix.multiplyMatrices( camera.projectionMatrix, _viewMatrix );
  188. _frustum.setFromMatrix( _viewProjectionMatrix );
  189. projectGraph( scene, sortObjects );
  190. for ( var o = 0, ol = _renderData.objects.length; o < ol; o ++ ) {
  191. object = _renderData.objects[ o ].object;
  192. geometry = object.geometry;
  193. renderList.setObject( object );
  194. _modelMatrix = object.matrixWorld;
  195. _vertexCount = 0;
  196. if ( object instanceof THREE.Mesh ) {
  197. if ( geometry instanceof THREE.BufferGeometry ) {
  198. var attributes = geometry.attributes;
  199. if ( attributes.position !== undefined ) {
  200. var positions = attributes.position.array;
  201. for ( var i = 0, l = positions.length; i < l; i += 3 ) {
  202. renderList.pushVertex( positions[ i ], positions[ i + 1 ], positions[ i + 2 ] );
  203. }
  204. var normals = attributes.normal.array;
  205. for ( var i = 0, l = normals.length; i < l; i += 3 ) {
  206. renderList.pushNormal( normals[ i ], normals[ i + 1 ], normals[ i + 2 ] );
  207. }
  208. if ( attributes.index !== undefined ) {
  209. var indices = attributes.index.array;
  210. for ( var i = 0, l = indices.length; i < l; i += 3 ) {
  211. renderList.pushTriangle( indices[ i ], indices[ i + 1 ], indices[ i + 2 ] );
  212. }
  213. } else {
  214. for ( var i = 0, l = positions.length / 3; i < l; i += 3 ) {
  215. renderList.pushTriangle( i, i + 1, i + 2 );
  216. }
  217. }
  218. }
  219. } else if ( geometry instanceof THREE.Geometry2 ) {
  220. vertices = geometry.vertices;
  221. for ( var i = 0, j = 0, l = vertices.length; i < l; i += 9, j += 3 ) {
  222. renderList.pushVertex( vertices[ i + 0 ], vertices[ i + 1 ], vertices[ i + 2 ] );
  223. renderList.pushVertex( vertices[ i + 3 ], vertices[ i + 4 ], vertices[ i + 5 ] );
  224. renderList.pushVertex( vertices[ i + 6 ], vertices[ i + 7 ], vertices[ i + 8 ] );
  225. renderList.pushTriangle( j + 0, j + 1, j + 2 );
  226. }
  227. } else if ( geometry instanceof THREE.Geometry ) {
  228. vertices = geometry.vertices;
  229. faces = geometry.faces;
  230. faceVertexUvs = geometry.faceVertexUvs;
  231. _normalMatrix.getNormalMatrix( _modelMatrix );
  232. isFaceMaterial = object.material instanceof THREE.MeshFaceMaterial;
  233. objectMaterials = isFaceMaterial === true ? object.material : null;
  234. for ( var v = 0, vl = vertices.length; v < vl; v ++ ) {
  235. var vertex = vertices[ v ];
  236. renderList.pushVertex( vertex.x, vertex.y, vertex.z );
  237. }
  238. for ( var f = 0, fl = faces.length; f < fl; f ++ ) {
  239. face = faces[ f ];
  240. var material = isFaceMaterial === true
  241. ? objectMaterials.materials[ face.materialIndex ]
  242. : object.material;
  243. if ( material === undefined ) continue;
  244. var side = material.side;
  245. var v1 = _vertexPool[ face.a ];
  246. var v2 = _vertexPool[ face.b ];
  247. var v3 = _vertexPool[ face.c ];
  248. if ( material.morphTargets === true ) {
  249. var morphTargets = geometry.morphTargets;
  250. var morphInfluences = object.morphTargetInfluences;
  251. var v1p = v1.position;
  252. var v2p = v2.position;
  253. var v3p = v3.position;
  254. _vA.set( 0, 0, 0 );
  255. _vB.set( 0, 0, 0 );
  256. _vC.set( 0, 0, 0 );
  257. for ( var t = 0, tl = morphTargets.length; t < tl; t ++ ) {
  258. var influence = morphInfluences[ t ];
  259. if ( influence === 0 ) continue;
  260. var targets = morphTargets[ t ].vertices;
  261. _vA.x += ( targets[ face.a ].x - v1p.x ) * influence;
  262. _vA.y += ( targets[ face.a ].y - v1p.y ) * influence;
  263. _vA.z += ( targets[ face.a ].z - v1p.z ) * influence;
  264. _vB.x += ( targets[ face.b ].x - v2p.x ) * influence;
  265. _vB.y += ( targets[ face.b ].y - v2p.y ) * influence;
  266. _vB.z += ( targets[ face.b ].z - v2p.z ) * influence;
  267. _vC.x += ( targets[ face.c ].x - v3p.x ) * influence;
  268. _vC.y += ( targets[ face.c ].y - v3p.y ) * influence;
  269. _vC.z += ( targets[ face.c ].z - v3p.z ) * influence;
  270. }
  271. v1.position.add( _vA );
  272. v2.position.add( _vB );
  273. v3.position.add( _vC );
  274. renderList.projectVertex( v1 );
  275. renderList.projectVertex( v2 );
  276. renderList.projectVertex( v3 );
  277. }
  278. var visible = renderList.checkTriangleVisibility( v1, v2, v3 );
  279. if ( visible === ( side === THREE.BackSide ) ) continue;
  280. _face = getNextFace3InPool();
  281. _face.id = object.id;
  282. _face.v1.copy( v1 );
  283. _face.v2.copy( v2 );
  284. _face.v3.copy( v3 );
  285. _face.normalModel.copy( face.normal );
  286. if ( visible === false && ( side === THREE.BackSide || side === THREE.DoubleSide ) ) {
  287. _face.normalModel.negate();
  288. }
  289. _face.normalModel.applyMatrix3( _normalMatrix ).normalize();
  290. _face.centroidModel.copy( face.centroid ).applyMatrix4( _modelMatrix );
  291. faceVertexNormals = face.vertexNormals;
  292. for ( var n = 0, nl = Math.min( faceVertexNormals.length, 3 ); n < nl; n ++ ) {
  293. var normalModel = _face.vertexNormalsModel[ n ];
  294. normalModel.copy( faceVertexNormals[ n ] );
  295. if ( visible === false && ( side === THREE.BackSide || side === THREE.DoubleSide ) ) {
  296. normalModel.negate();
  297. }
  298. normalModel.applyMatrix3( _normalMatrix ).normalize();
  299. }
  300. _face.vertexNormalsLength = faceVertexNormals.length;
  301. for ( var c = 0, cl = Math.min( faceVertexUvs.length, 3 ); c < cl; c ++ ) {
  302. uvs = faceVertexUvs[ c ][ f ];
  303. if ( uvs === undefined ) continue;
  304. for ( var u = 0, ul = uvs.length; u < ul; u ++ ) {
  305. _face.uvs[ c ][ u ] = uvs[ u ];
  306. }
  307. }
  308. _face.color = face.color;
  309. _face.material = material;
  310. _centroid.copy( _face.centroidModel ).applyProjection( _viewProjectionMatrix );
  311. _face.z = _centroid.z;
  312. _renderData.elements.push( _face );
  313. }
  314. }
  315. } else if ( object instanceof THREE.Line ) {
  316. if ( geometry instanceof THREE.BufferGeometry ) {
  317. var attributes = geometry.attributes;
  318. if ( attributes.position !== undefined ) {
  319. var positions = attributes.position.array;
  320. for ( var i = 0, l = positions.length; i < l; i += 3 ) {
  321. renderList.pushVertex( positions[ i ], positions[ i + 1 ], positions[ i + 2 ] );
  322. }
  323. if ( attributes.index !== undefined ) {
  324. var indices = attributes.index.array;
  325. for ( var i = 0, l = indices.length; i < l; i += 2 ) {
  326. renderList.pushLine( indices[ i ], indices[ i + 1 ] );
  327. }
  328. } else {
  329. for ( var i = 0, l = ( positions.length / 3 ) - 1; i < l; i ++ ) {
  330. renderList.pushLine( i, i + 1 );
  331. }
  332. }
  333. }
  334. } else if ( geometry instanceof THREE.Geometry ) {
  335. _modelViewProjectionMatrix.multiplyMatrices( _viewProjectionMatrix, _modelMatrix );
  336. vertices = object.geometry.vertices;
  337. v1 = getNextVertexInPool();
  338. v1.positionScreen.copy( vertices[ 0 ] ).applyMatrix4( _modelViewProjectionMatrix );
  339. // Handle LineStrip and LinePieces
  340. var step = object.type === THREE.LinePieces ? 2 : 1;
  341. for ( v = 1, vl = vertices.length; v < vl; v ++ ) {
  342. v1 = getNextVertexInPool();
  343. v1.positionScreen.copy( vertices[ v ] ).applyMatrix4( _modelViewProjectionMatrix );
  344. if ( ( v + 1 ) % step > 0 ) continue;
  345. v2 = _vertexPool[ _vertexCount - 2 ];
  346. _clippedVertex1PositionScreen.copy( v1.positionScreen );
  347. _clippedVertex2PositionScreen.copy( v2.positionScreen );
  348. if ( clipLine( _clippedVertex1PositionScreen, _clippedVertex2PositionScreen ) === true ) {
  349. // Perform the perspective divide
  350. _clippedVertex1PositionScreen.multiplyScalar( 1 / _clippedVertex1PositionScreen.w );
  351. _clippedVertex2PositionScreen.multiplyScalar( 1 / _clippedVertex2PositionScreen.w );
  352. _line = getNextLineInPool();
  353. _line.id = object.id;
  354. _line.v1.positionScreen.copy( _clippedVertex1PositionScreen );
  355. _line.v2.positionScreen.copy( _clippedVertex2PositionScreen );
  356. _line.z = Math.max( _clippedVertex1PositionScreen.z, _clippedVertex2PositionScreen.z );
  357. _line.material = object.material;
  358. if ( object.material.vertexColors === THREE.VertexColors ) {
  359. _line.vertexColors[ 0 ].copy( object.geometry.colors[ v ] );
  360. _line.vertexColors[ 1 ].copy( object.geometry.colors[ v - 1 ] );
  361. }
  362. _renderData.elements.push( _line );
  363. }
  364. }
  365. }
  366. } else if ( object instanceof THREE.Sprite ) {
  367. _modelMatrix = object.matrixWorld;
  368. _vector4.set( _modelMatrix.elements[12], _modelMatrix.elements[13], _modelMatrix.elements[14], 1 );
  369. _vector4.applyMatrix4( _viewProjectionMatrix );
  370. var invW = 1 / _vector4.w;
  371. _vector4.z *= invW;
  372. if ( _vector4.z >= -1 && _vector4.z <= 1 ) {
  373. _sprite = getNextSpriteInPool();
  374. _sprite.id = object.id;
  375. _sprite.x = _vector4.x * invW;
  376. _sprite.y = _vector4.y * invW;
  377. _sprite.z = _vector4.z;
  378. _sprite.object = object;
  379. _sprite.rotation = object.rotation;
  380. _sprite.scale.x = object.scale.x * Math.abs( _sprite.x - ( _vector4.x + camera.projectionMatrix.elements[0] ) / ( _vector4.w + camera.projectionMatrix.elements[12] ) );
  381. _sprite.scale.y = object.scale.y * Math.abs( _sprite.y - ( _vector4.y + camera.projectionMatrix.elements[5] ) / ( _vector4.w + camera.projectionMatrix.elements[13] ) );
  382. _sprite.material = object.material;
  383. _renderData.elements.push( _sprite );
  384. }
  385. }
  386. }
  387. if ( sortElements === true ) _renderData.elements.sort( painterSort );
  388. return _renderData;
  389. };
  390. // Pools
  391. function getNextObjectInPool() {
  392. if ( _objectCount === _objectPoolLength ) {
  393. var object = new THREE.RenderableObject();
  394. _objectPool.push( object );
  395. _objectPoolLength ++;
  396. _objectCount ++;
  397. return object;
  398. }
  399. return _objectPool[ _objectCount ++ ];
  400. }
  401. function getNextVertexInPool() {
  402. if ( _vertexCount === _vertexPoolLength ) {
  403. var vertex = new THREE.RenderableVertex();
  404. _vertexPool.push( vertex );
  405. _vertexPoolLength ++;
  406. _vertexCount ++;
  407. return vertex;
  408. }
  409. return _vertexPool[ _vertexCount ++ ];
  410. }
  411. function getNextFace3InPool() {
  412. if ( _face3Count === _face3PoolLength ) {
  413. var face = new THREE.RenderableFace3();
  414. _face3Pool.push( face );
  415. _face3PoolLength ++;
  416. _face3Count ++;
  417. return face;
  418. }
  419. return _face3Pool[ _face3Count ++ ];
  420. }
  421. function getNextLineInPool() {
  422. if ( _lineCount === _linePoolLength ) {
  423. var line = new THREE.RenderableLine();
  424. _linePool.push( line );
  425. _linePoolLength ++;
  426. _lineCount ++
  427. return line;
  428. }
  429. return _linePool[ _lineCount ++ ];
  430. }
  431. function getNextSpriteInPool() {
  432. if ( _spriteCount === _spritePoolLength ) {
  433. var sprite = new THREE.RenderableSprite();
  434. _spritePool.push( sprite );
  435. _spritePoolLength ++;
  436. _spriteCount ++
  437. return sprite;
  438. }
  439. return _spritePool[ _spriteCount ++ ];
  440. }
  441. //
  442. function painterSort( a, b ) {
  443. if ( a.z !== b.z ) {
  444. return b.z - a.z;
  445. } else if ( a.id !== b.id ) {
  446. return a.id - b.id;
  447. } else {
  448. return 0;
  449. }
  450. }
  451. function clipLine( s1, s2 ) {
  452. var alpha1 = 0, alpha2 = 1,
  453. // Calculate the boundary coordinate of each vertex for the near and far clip planes,
  454. // Z = -1 and Z = +1, respectively.
  455. bc1near = s1.z + s1.w,
  456. bc2near = s2.z + s2.w,
  457. bc1far = - s1.z + s1.w,
  458. bc2far = - s2.z + s2.w;
  459. if ( bc1near >= 0 && bc2near >= 0 && bc1far >= 0 && bc2far >= 0 ) {
  460. // Both vertices lie entirely within all clip planes.
  461. return true;
  462. } else if ( ( bc1near < 0 && bc2near < 0) || (bc1far < 0 && bc2far < 0 ) ) {
  463. // Both vertices lie entirely outside one of the clip planes.
  464. return false;
  465. } else {
  466. // The line segment spans at least one clip plane.
  467. if ( bc1near < 0 ) {
  468. // v1 lies outside the near plane, v2 inside
  469. alpha1 = Math.max( alpha1, bc1near / ( bc1near - bc2near ) );
  470. } else if ( bc2near < 0 ) {
  471. // v2 lies outside the near plane, v1 inside
  472. alpha2 = Math.min( alpha2, bc1near / ( bc1near - bc2near ) );
  473. }
  474. if ( bc1far < 0 ) {
  475. // v1 lies outside the far plane, v2 inside
  476. alpha1 = Math.max( alpha1, bc1far / ( bc1far - bc2far ) );
  477. } else if ( bc2far < 0 ) {
  478. // v2 lies outside the far plane, v2 inside
  479. alpha2 = Math.min( alpha2, bc1far / ( bc1far - bc2far ) );
  480. }
  481. if ( alpha2 < alpha1 ) {
  482. // The line segment spans two boundaries, but is outside both of them.
  483. // (This can't happen when we're only clipping against just near/far but good
  484. // to leave the check here for future usage if other clip planes are added.)
  485. return false;
  486. } else {
  487. // Update the s1 and s2 vertices to match the clipped line segment.
  488. s1.lerp( s2, alpha1 );
  489. s2.lerp( s1, 1 - alpha2 );
  490. return true;
  491. }
  492. }
  493. }
  494. };