Projector.js 20 KB

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