Projector.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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() has been removed.' );
  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. if ( object.renderDepth !== null ) {
  219. _object.z = object.renderDepth;
  220. } else {
  221. _vector3.setFromMatrixPosition( object.matrixWorld );
  222. _vector3.applyProjection( _viewProjectionMatrix );
  223. _object.z = _vector3.z;
  224. }
  225. _renderData.objects.push( _object );
  226. }
  227. }
  228. } );
  229. if ( sortObjects === true ) {
  230. _renderData.objects.sort( painterSort );
  231. }
  232. //
  233. for ( var o = 0, ol = _renderData.objects.length; o < ol; o ++ ) {
  234. var object = _renderData.objects[ o ].object;
  235. var geometry = object.geometry;
  236. renderList.setObject( object );
  237. _modelMatrix = object.matrixWorld;
  238. _vertexCount = 0;
  239. if ( object instanceof THREE.Mesh ) {
  240. if ( geometry instanceof THREE.BufferGeometry ) {
  241. var attributes = geometry.attributes;
  242. var offsets = geometry.offsets;
  243. if ( attributes.position === undefined ) continue;
  244. var positions = attributes.position.array;
  245. for ( var i = 0, l = positions.length; i < l; i += 3 ) {
  246. renderList.pushVertex( positions[ i ], positions[ i + 1 ], positions[ i + 2 ] );
  247. }
  248. if ( attributes.normal !== undefined ) {
  249. var normals = attributes.normal.array;
  250. for ( var i = 0, l = normals.length; i < l; i += 3 ) {
  251. renderList.pushNormal( normals[ i ], normals[ i + 1 ], normals[ i + 2 ] );
  252. }
  253. }
  254. if ( attributes.uv !== undefined ) {
  255. var uvs = attributes.uv.array;
  256. for ( var i = 0, l = uvs.length; i < l; i += 2 ) {
  257. renderList.pushUv( uvs[ i ], uvs[ i + 1 ] );
  258. }
  259. }
  260. if ( attributes.index !== undefined ) {
  261. var indices = attributes.index.array;
  262. if ( offsets.length > 0 ) {
  263. for ( var o = 0; o < offsets.length; o ++ ) {
  264. var offset = offsets[ o ];
  265. var index = offset.index;
  266. for ( var i = offset.start, l = offset.start + offset.count; i < l; i += 3 ) {
  267. renderList.pushTriangle( indices[ i ] + index, indices[ i + 1 ] + index, indices[ i + 2 ] + index );
  268. }
  269. }
  270. } else {
  271. for ( var i = 0, l = indices.length; i < l; i += 3 ) {
  272. renderList.pushTriangle( indices[ i ], indices[ i + 1 ], indices[ i + 2 ] );
  273. }
  274. }
  275. } else {
  276. for ( var i = 0, l = positions.length / 3; i < l; i += 3 ) {
  277. renderList.pushTriangle( i, i + 1, i + 2 );
  278. }
  279. }
  280. } else if ( geometry instanceof THREE.Geometry ) {
  281. var vertices = geometry.vertices;
  282. var faces = geometry.faces;
  283. var faceVertexUvs = geometry.faceVertexUvs[ 0 ];
  284. _normalMatrix.getNormalMatrix( _modelMatrix );
  285. var isFaceMaterial = object.material instanceof THREE.MeshFaceMaterial;
  286. var objectMaterials = isFaceMaterial === true ? object.material : null;
  287. for ( var v = 0, vl = vertices.length; v < vl; v ++ ) {
  288. var vertex = vertices[ v ];
  289. renderList.pushVertex( vertex.x, vertex.y, vertex.z );
  290. }
  291. for ( var f = 0, fl = faces.length; f < fl; f ++ ) {
  292. var face = faces[ f ];
  293. var material = isFaceMaterial === true
  294. ? objectMaterials.materials[ face.materialIndex ]
  295. : object.material;
  296. if ( material === undefined ) continue;
  297. var side = material.side;
  298. var v1 = _vertexPool[ face.a ];
  299. var v2 = _vertexPool[ face.b ];
  300. var v3 = _vertexPool[ face.c ];
  301. if ( material.morphTargets === true ) {
  302. var morphTargets = geometry.morphTargets;
  303. var morphInfluences = object.morphTargetInfluences;
  304. var v1p = v1.position;
  305. var v2p = v2.position;
  306. var v3p = v3.position;
  307. _vA.set( 0, 0, 0 );
  308. _vB.set( 0, 0, 0 );
  309. _vC.set( 0, 0, 0 );
  310. for ( var t = 0, tl = morphTargets.length; t < tl; t ++ ) {
  311. var influence = morphInfluences[ t ];
  312. if ( influence === 0 ) continue;
  313. var targets = morphTargets[ t ].vertices;
  314. _vA.x += ( targets[ face.a ].x - v1p.x ) * influence;
  315. _vA.y += ( targets[ face.a ].y - v1p.y ) * influence;
  316. _vA.z += ( targets[ face.a ].z - v1p.z ) * influence;
  317. _vB.x += ( targets[ face.b ].x - v2p.x ) * influence;
  318. _vB.y += ( targets[ face.b ].y - v2p.y ) * influence;
  319. _vB.z += ( targets[ face.b ].z - v2p.z ) * influence;
  320. _vC.x += ( targets[ face.c ].x - v3p.x ) * influence;
  321. _vC.y += ( targets[ face.c ].y - v3p.y ) * influence;
  322. _vC.z += ( targets[ face.c ].z - v3p.z ) * influence;
  323. }
  324. v1.position.add( _vA );
  325. v2.position.add( _vB );
  326. v3.position.add( _vC );
  327. renderList.projectVertex( v1 );
  328. renderList.projectVertex( v2 );
  329. renderList.projectVertex( v3 );
  330. }
  331. if ( renderList.checkTriangleVisibility( v1, v2, v3 ) === false ) continue;
  332. var visible = renderList.checkBackfaceCulling( v1, v2, v3 );
  333. if ( side !== THREE.DoubleSide ) {
  334. if ( side === THREE.FrontSide && visible === false ) continue;
  335. if ( side === THREE.BackSide && visible === true ) continue;
  336. }
  337. _face = getNextFaceInPool();
  338. _face.id = object.id;
  339. _face.v1.copy( v1 );
  340. _face.v2.copy( v2 );
  341. _face.v3.copy( v3 );
  342. _face.normalModel.copy( face.normal );
  343. if ( visible === false && ( side === THREE.BackSide || side === THREE.DoubleSide ) ) {
  344. _face.normalModel.negate();
  345. }
  346. _face.normalModel.applyMatrix3( _normalMatrix ).normalize();
  347. var faceVertexNormals = face.vertexNormals;
  348. for ( var n = 0, nl = Math.min( faceVertexNormals.length, 3 ); n < nl; n ++ ) {
  349. var normalModel = _face.vertexNormalsModel[ n ];
  350. normalModel.copy( faceVertexNormals[ n ] );
  351. if ( visible === false && ( side === THREE.BackSide || side === THREE.DoubleSide ) ) {
  352. normalModel.negate();
  353. }
  354. normalModel.applyMatrix3( _normalMatrix ).normalize();
  355. }
  356. _face.vertexNormalsLength = faceVertexNormals.length;
  357. var vertexUvs = faceVertexUvs[ f ];
  358. if ( vertexUvs !== undefined ) {
  359. for ( var u = 0; u < 3; u ++ ) {
  360. _face.uvs[ u ].copy( vertexUvs[ u ] );
  361. }
  362. }
  363. _face.color = face.color;
  364. _face.material = material;
  365. _face.z = ( v1.positionScreen.z + v2.positionScreen.z + v3.positionScreen.z ) / 3;
  366. _renderData.elements.push( _face );
  367. }
  368. }
  369. } else if ( object instanceof THREE.Line ) {
  370. if ( geometry instanceof THREE.BufferGeometry ) {
  371. var attributes = geometry.attributes;
  372. if ( attributes.position !== undefined ) {
  373. var positions = attributes.position.array;
  374. for ( var i = 0, l = positions.length; i < l; i += 3 ) {
  375. renderList.pushVertex( positions[ i ], positions[ i + 1 ], positions[ i + 2 ] );
  376. }
  377. if ( attributes.index !== undefined ) {
  378. var indices = attributes.index.array;
  379. for ( var i = 0, l = indices.length; i < l; i += 2 ) {
  380. renderList.pushLine( indices[ i ], indices[ i + 1 ] );
  381. }
  382. } else {
  383. var step = object.type === THREE.LinePieces ? 2 : 1;
  384. for ( var i = 0, l = ( positions.length / 3 ) - 1; i < l; i += step ) {
  385. renderList.pushLine( i, i + 1 );
  386. }
  387. }
  388. }
  389. } else if ( geometry instanceof THREE.Geometry ) {
  390. _modelViewProjectionMatrix.multiplyMatrices( _viewProjectionMatrix, _modelMatrix );
  391. var vertices = object.geometry.vertices;
  392. if ( vertices.length === 0 ) continue;
  393. v1 = getNextVertexInPool();
  394. v1.positionScreen.copy( vertices[ 0 ] ).applyMatrix4( _modelViewProjectionMatrix );
  395. // Handle LineStrip and LinePieces
  396. var step = object.type === THREE.LinePieces ? 2 : 1;
  397. for ( var v = 1, vl = vertices.length; v < vl; v ++ ) {
  398. v1 = getNextVertexInPool();
  399. v1.positionScreen.copy( vertices[ v ] ).applyMatrix4( _modelViewProjectionMatrix );
  400. if ( ( v + 1 ) % step > 0 ) continue;
  401. v2 = _vertexPool[ _vertexCount - 2 ];
  402. _clippedVertex1PositionScreen.copy( v1.positionScreen );
  403. _clippedVertex2PositionScreen.copy( v2.positionScreen );
  404. if ( clipLine( _clippedVertex1PositionScreen, _clippedVertex2PositionScreen ) === true ) {
  405. // Perform the perspective divide
  406. _clippedVertex1PositionScreen.multiplyScalar( 1 / _clippedVertex1PositionScreen.w );
  407. _clippedVertex2PositionScreen.multiplyScalar( 1 / _clippedVertex2PositionScreen.w );
  408. _line = getNextLineInPool();
  409. _line.id = object.id;
  410. _line.v1.positionScreen.copy( _clippedVertex1PositionScreen );
  411. _line.v2.positionScreen.copy( _clippedVertex2PositionScreen );
  412. _line.z = Math.max( _clippedVertex1PositionScreen.z, _clippedVertex2PositionScreen.z );
  413. _line.material = object.material;
  414. if ( object.material.vertexColors === THREE.VertexColors ) {
  415. _line.vertexColors[ 0 ].copy( object.geometry.colors[ v ] );
  416. _line.vertexColors[ 1 ].copy( object.geometry.colors[ v - 1 ] );
  417. }
  418. _renderData.elements.push( _line );
  419. }
  420. }
  421. }
  422. } else if ( object instanceof THREE.Sprite ) {
  423. _vector4.set( _modelMatrix.elements[ 12 ], _modelMatrix.elements[ 13 ], _modelMatrix.elements[ 14 ], 1 );
  424. _vector4.applyMatrix4( _viewProjectionMatrix );
  425. var invW = 1 / _vector4.w;
  426. _vector4.z *= invW;
  427. if ( _vector4.z >= - 1 && _vector4.z <= 1 ) {
  428. _sprite = getNextSpriteInPool();
  429. _sprite.id = object.id;
  430. _sprite.x = _vector4.x * invW;
  431. _sprite.y = _vector4.y * invW;
  432. _sprite.z = _vector4.z;
  433. _sprite.object = object;
  434. _sprite.rotation = object.rotation;
  435. _sprite.scale.x = object.scale.x * Math.abs( _sprite.x - ( _vector4.x + camera.projectionMatrix.elements[ 0 ] ) / ( _vector4.w + camera.projectionMatrix.elements[ 12 ] ) );
  436. _sprite.scale.y = object.scale.y * Math.abs( _sprite.y - ( _vector4.y + camera.projectionMatrix.elements[ 5 ] ) / ( _vector4.w + camera.projectionMatrix.elements[ 13 ] ) );
  437. _sprite.material = object.material;
  438. _renderData.elements.push( _sprite );
  439. }
  440. }
  441. }
  442. if ( sortElements === true ) {
  443. _renderData.elements.sort( painterSort );
  444. }
  445. return _renderData;
  446. };
  447. // Pools
  448. function getNextObjectInPool() {
  449. if ( _objectCount === _objectPoolLength ) {
  450. var object = new THREE.RenderableObject();
  451. _objectPool.push( object );
  452. _objectPoolLength ++;
  453. _objectCount ++;
  454. return object;
  455. }
  456. return _objectPool[ _objectCount ++ ];
  457. }
  458. function getNextVertexInPool() {
  459. if ( _vertexCount === _vertexPoolLength ) {
  460. var vertex = new THREE.RenderableVertex();
  461. _vertexPool.push( vertex );
  462. _vertexPoolLength ++;
  463. _vertexCount ++;
  464. return vertex;
  465. }
  466. return _vertexPool[ _vertexCount ++ ];
  467. }
  468. function getNextFaceInPool() {
  469. if ( _faceCount === _facePoolLength ) {
  470. var face = new THREE.RenderableFace();
  471. _facePool.push( face );
  472. _facePoolLength ++;
  473. _faceCount ++;
  474. return face;
  475. }
  476. return _facePool[ _faceCount ++ ];
  477. }
  478. function getNextLineInPool() {
  479. if ( _lineCount === _linePoolLength ) {
  480. var line = new THREE.RenderableLine();
  481. _linePool.push( line );
  482. _linePoolLength ++;
  483. _lineCount ++
  484. return line;
  485. }
  486. return _linePool[ _lineCount ++ ];
  487. }
  488. function getNextSpriteInPool() {
  489. if ( _spriteCount === _spritePoolLength ) {
  490. var sprite = new THREE.RenderableSprite();
  491. _spritePool.push( sprite );
  492. _spritePoolLength ++;
  493. _spriteCount ++
  494. return sprite;
  495. }
  496. return _spritePool[ _spriteCount ++ ];
  497. }
  498. //
  499. function painterSort( a, b ) {
  500. if ( a.z !== b.z ) {
  501. return b.z - a.z;
  502. } else if ( a.id !== b.id ) {
  503. return a.id - b.id;
  504. } else {
  505. return 0;
  506. }
  507. }
  508. function clipLine( s1, s2 ) {
  509. var alpha1 = 0, alpha2 = 1,
  510. // Calculate the boundary coordinate of each vertex for the near and far clip planes,
  511. // Z = -1 and Z = +1, respectively.
  512. bc1near = s1.z + s1.w,
  513. bc2near = s2.z + s2.w,
  514. bc1far = - s1.z + s1.w,
  515. bc2far = - s2.z + s2.w;
  516. if ( bc1near >= 0 && bc2near >= 0 && bc1far >= 0 && bc2far >= 0 ) {
  517. // Both vertices lie entirely within all clip planes.
  518. return true;
  519. } else if ( ( bc1near < 0 && bc2near < 0 ) || ( bc1far < 0 && bc2far < 0 ) ) {
  520. // Both vertices lie entirely outside one of the clip planes.
  521. return false;
  522. } else {
  523. // The line segment spans at least one clip plane.
  524. if ( bc1near < 0 ) {
  525. // v1 lies outside the near plane, v2 inside
  526. alpha1 = Math.max( alpha1, bc1near / ( bc1near - bc2near ) );
  527. } else if ( bc2near < 0 ) {
  528. // v2 lies outside the near plane, v1 inside
  529. alpha2 = Math.min( alpha2, bc1near / ( bc1near - bc2near ) );
  530. }
  531. if ( bc1far < 0 ) {
  532. // v1 lies outside the far plane, v2 inside
  533. alpha1 = Math.max( alpha1, bc1far / ( bc1far - bc2far ) );
  534. } else if ( bc2far < 0 ) {
  535. // v2 lies outside the far plane, v2 inside
  536. alpha2 = Math.min( alpha2, bc1far / ( bc1far - bc2far ) );
  537. }
  538. if ( alpha2 < alpha1 ) {
  539. // The line segment spans two boundaries, but is outside both of them.
  540. // (This can't happen when we're only clipping against just near/far but good
  541. // to leave the check here for future usage if other clip planes are added.)
  542. return false;
  543. } else {
  544. // Update the s1 and s2 vertices to match the clipped line segment.
  545. s1.lerp( s2, alpha1 );
  546. s2.lerp( s1, 1 - alpha2 );
  547. return true;
  548. }
  549. }
  550. }
  551. };