Renderer.js 20 KB

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