Projector.js 22 KB

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