Projector.js 21 KB

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