Projector.js 20 KB

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