Projector.js 20 KB

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