Projector.js 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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.Points ) {
  444. _modelViewProjectionMatrix.multiplyMatrices( _viewProjectionMatrix, _modelMatrix );
  445. if ( geometry instanceof THREE.Geometry ) {
  446. var vertices = object.geometry.vertices;
  447. for ( var v = 0, vl = vertices.length; v < vl; v ++ ) {
  448. var vertex = vertices[ v ];
  449. _vector4.set( vertex.x, vertex.y, vertex.z, 1 );
  450. _vector4.applyMatrix4( _modelViewProjectionMatrix );
  451. pushPoint( _vector4, object, camera );
  452. }
  453. }
  454. } else if ( object instanceof THREE.Sprite ) {
  455. _vector4.set( _modelMatrix.elements[ 12 ], _modelMatrix.elements[ 13 ], _modelMatrix.elements[ 14 ], 1 );
  456. _vector4.applyMatrix4( _viewProjectionMatrix );
  457. pushPoint( _vector4, object, camera );
  458. }
  459. }
  460. if ( sortElements === true ) {
  461. _renderData.elements.sort( painterSort );
  462. }
  463. return _renderData;
  464. };
  465. function pushPoint( _vector4, object, camera ) {
  466. var invW = 1 / _vector4.w;
  467. _vector4.z *= invW;
  468. if ( _vector4.z >= - 1 && _vector4.z <= 1 ) {
  469. _sprite = getNextSpriteInPool();
  470. _sprite.id = object.id;
  471. _sprite.x = _vector4.x * invW;
  472. _sprite.y = _vector4.y * invW;
  473. _sprite.z = _vector4.z;
  474. _sprite.renderOrder = object.renderOrder;
  475. _sprite.object = object;
  476. _sprite.rotation = object.rotation;
  477. _sprite.scale.x = object.scale.x * Math.abs( _sprite.x - ( _vector4.x + camera.projectionMatrix.elements[ 0 ] ) / ( _vector4.w + camera.projectionMatrix.elements[ 12 ] ) );
  478. _sprite.scale.y = object.scale.y * Math.abs( _sprite.y - ( _vector4.y + camera.projectionMatrix.elements[ 5 ] ) / ( _vector4.w + camera.projectionMatrix.elements[ 13 ] ) );
  479. _sprite.material = object.material;
  480. _renderData.elements.push( _sprite );
  481. }
  482. }
  483. // Pools
  484. function getNextObjectInPool() {
  485. if ( _objectCount === _objectPoolLength ) {
  486. var object = new THREE.RenderableObject();
  487. _objectPool.push( object );
  488. _objectPoolLength ++;
  489. _objectCount ++;
  490. return object;
  491. }
  492. return _objectPool[ _objectCount ++ ];
  493. }
  494. function getNextVertexInPool() {
  495. if ( _vertexCount === _vertexPoolLength ) {
  496. var vertex = new THREE.RenderableVertex();
  497. _vertexPool.push( vertex );
  498. _vertexPoolLength ++;
  499. _vertexCount ++;
  500. return vertex;
  501. }
  502. return _vertexPool[ _vertexCount ++ ];
  503. }
  504. function getNextFaceInPool() {
  505. if ( _faceCount === _facePoolLength ) {
  506. var face = new THREE.RenderableFace();
  507. _facePool.push( face );
  508. _facePoolLength ++;
  509. _faceCount ++;
  510. return face;
  511. }
  512. return _facePool[ _faceCount ++ ];
  513. }
  514. function getNextLineInPool() {
  515. if ( _lineCount === _linePoolLength ) {
  516. var line = new THREE.RenderableLine();
  517. _linePool.push( line );
  518. _linePoolLength ++;
  519. _lineCount ++;
  520. return line;
  521. }
  522. return _linePool[ _lineCount ++ ];
  523. }
  524. function getNextSpriteInPool() {
  525. if ( _spriteCount === _spritePoolLength ) {
  526. var sprite = new THREE.RenderableSprite();
  527. _spritePool.push( sprite );
  528. _spritePoolLength ++;
  529. _spriteCount ++;
  530. return sprite;
  531. }
  532. return _spritePool[ _spriteCount ++ ];
  533. }
  534. //
  535. function painterSort( a, b ) {
  536. if ( a.renderOrder !== b.renderOrder ) {
  537. return a.renderOrder - b.renderOrder;
  538. } else if ( a.z !== b.z ) {
  539. return b.z - a.z;
  540. } else if ( a.id !== b.id ) {
  541. return a.id - b.id;
  542. } else {
  543. return 0;
  544. }
  545. }
  546. function clipLine( s1, s2 ) {
  547. var alpha1 = 0, alpha2 = 1,
  548. // Calculate the boundary coordinate of each vertex for the near and far clip planes,
  549. // Z = -1 and Z = +1, respectively.
  550. bc1near = s1.z + s1.w,
  551. bc2near = s2.z + s2.w,
  552. bc1far = - s1.z + s1.w,
  553. bc2far = - s2.z + s2.w;
  554. if ( bc1near >= 0 && bc2near >= 0 && bc1far >= 0 && bc2far >= 0 ) {
  555. // Both vertices lie entirely within all clip planes.
  556. return true;
  557. } else if ( ( bc1near < 0 && bc2near < 0 ) || ( bc1far < 0 && bc2far < 0 ) ) {
  558. // Both vertices lie entirely outside one of the clip planes.
  559. return false;
  560. } else {
  561. // The line segment spans at least one clip plane.
  562. if ( bc1near < 0 ) {
  563. // v1 lies outside the near plane, v2 inside
  564. alpha1 = Math.max( alpha1, bc1near / ( bc1near - bc2near ) );
  565. } else if ( bc2near < 0 ) {
  566. // v2 lies outside the near plane, v1 inside
  567. alpha2 = Math.min( alpha2, bc1near / ( bc1near - bc2near ) );
  568. }
  569. if ( bc1far < 0 ) {
  570. // v1 lies outside the far plane, v2 inside
  571. alpha1 = Math.max( alpha1, bc1far / ( bc1far - bc2far ) );
  572. } else if ( bc2far < 0 ) {
  573. // v2 lies outside the far plane, v2 inside
  574. alpha2 = Math.min( alpha2, bc1far / ( bc1far - bc2far ) );
  575. }
  576. if ( alpha2 < alpha1 ) {
  577. // The line segment spans two boundaries, but is outside both of them.
  578. // (This can't happen when we're only clipping against just near/far but good
  579. // to leave the check here for future usage if other clip planes are added.)
  580. return false;
  581. } else {
  582. // Update the s1 and s2 vertices to match the clipped line segment.
  583. s1.lerp( s2, alpha1 );
  584. s2.lerp( s1, 1 - alpha2 );
  585. return true;
  586. }
  587. }
  588. }
  589. };