Projector.js 21 KB

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