Projector.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author supereggbert / http://www.paulbrunt.co.uk/
  4. * @author julianwa / https://github.com/julianwa
  5. */
  6. import {
  7. BackSide,
  8. Box3,
  9. BufferGeometry,
  10. Color,
  11. DoubleSide,
  12. FaceColors,
  13. FrontSide,
  14. Frustum,
  15. Geometry,
  16. Light,
  17. Line,
  18. LineSegments,
  19. Matrix3,
  20. Matrix4,
  21. Mesh,
  22. Points,
  23. Sprite,
  24. Vector2,
  25. Vector3,
  26. Vector4,
  27. VertexColors
  28. } from "../../../build/three.module.js";
  29. var RenderableObject = function () {
  30. this.id = 0;
  31. this.object = null;
  32. this.z = 0;
  33. this.renderOrder = 0;
  34. };
  35. //
  36. var RenderableFace = function () {
  37. this.id = 0;
  38. this.v1 = new RenderableVertex();
  39. this.v2 = new RenderableVertex();
  40. this.v3 = new RenderableVertex();
  41. this.normalModel = new Vector3();
  42. this.vertexNormalsModel = [ new Vector3(), new Vector3(), new Vector3() ];
  43. this.vertexNormalsLength = 0;
  44. this.color = new Color();
  45. this.material = null;
  46. this.uvs = [ new Vector2(), new Vector2(), new Vector2() ];
  47. this.z = 0;
  48. this.renderOrder = 0;
  49. };
  50. //
  51. var RenderableVertex = function () {
  52. this.position = new Vector3();
  53. this.positionWorld = new Vector3();
  54. this.positionScreen = new Vector4();
  55. this.visible = true;
  56. };
  57. RenderableVertex.prototype.copy = function ( vertex ) {
  58. this.positionWorld.copy( vertex.positionWorld );
  59. this.positionScreen.copy( vertex.positionScreen );
  60. };
  61. //
  62. var RenderableLine = function () {
  63. this.id = 0;
  64. this.v1 = new RenderableVertex();
  65. this.v2 = new RenderableVertex();
  66. this.vertexColors = [ new Color(), new Color() ];
  67. this.material = null;
  68. this.z = 0;
  69. this.renderOrder = 0;
  70. };
  71. //
  72. var RenderableSprite = function () {
  73. this.id = 0;
  74. this.object = null;
  75. this.x = 0;
  76. this.y = 0;
  77. this.z = 0;
  78. this.rotation = 0;
  79. this.scale = new Vector2();
  80. this.material = null;
  81. this.renderOrder = 0;
  82. };
  83. //
  84. var Projector = function () {
  85. var _object, _objectCount, _objectPool = [], _objectPoolLength = 0,
  86. _vertex, _vertexCount, _vertexPool = [], _vertexPoolLength = 0,
  87. _face, _faceCount, _facePool = [], _facePoolLength = 0,
  88. _line, _lineCount, _linePool = [], _linePoolLength = 0,
  89. _sprite, _spriteCount, _spritePool = [], _spritePoolLength = 0,
  90. _renderData = { objects: [], lights: [], elements: [] },
  91. _vector3 = new Vector3(),
  92. _vector4 = new Vector4(),
  93. _clipBox = new Box3( new Vector3( - 1, - 1, - 1 ), new Vector3( 1, 1, 1 ) ),
  94. _boundingBox = new Box3(),
  95. _points3 = new Array( 3 ),
  96. _viewMatrix = new Matrix4(),
  97. _viewProjectionMatrix = new Matrix4(),
  98. _modelMatrix,
  99. _modelViewProjectionMatrix = new Matrix4(),
  100. _normalMatrix = new Matrix3(),
  101. _frustum = new Frustum(),
  102. _clippedVertex1PositionScreen = new Vector4(),
  103. _clippedVertex2PositionScreen = new Vector4();
  104. //
  105. this.projectVector = function ( vector, camera ) {
  106. console.warn( 'THREE.Projector: .projectVector() is now vector.project().' );
  107. vector.project( camera );
  108. };
  109. this.unprojectVector = function ( vector, camera ) {
  110. console.warn( 'THREE.Projector: .unprojectVector() is now vector.unproject().' );
  111. vector.unproject( camera );
  112. };
  113. this.pickingRay = function () {
  114. console.error( 'THREE.Projector: .pickingRay() is now raycaster.setFromCamera().' );
  115. };
  116. //
  117. var RenderList = function () {
  118. var normals = [];
  119. var colors = [];
  120. var uvs = [];
  121. var object = null;
  122. var normalMatrix = new Matrix3();
  123. function setObject( value ) {
  124. object = value;
  125. normalMatrix.getNormalMatrix( object.matrixWorld );
  126. normals.length = 0;
  127. colors.length = 0;
  128. uvs.length = 0;
  129. }
  130. function projectVertex( vertex ) {
  131. var position = vertex.position;
  132. var positionWorld = vertex.positionWorld;
  133. var positionScreen = vertex.positionScreen;
  134. positionWorld.copy( position ).applyMatrix4( _modelMatrix );
  135. positionScreen.copy( positionWorld ).applyMatrix4( _viewProjectionMatrix );
  136. var invW = 1 / positionScreen.w;
  137. positionScreen.x *= invW;
  138. positionScreen.y *= invW;
  139. positionScreen.z *= invW;
  140. vertex.visible = positionScreen.x >= - 1 && positionScreen.x <= 1 &&
  141. positionScreen.y >= - 1 && positionScreen.y <= 1 &&
  142. positionScreen.z >= - 1 && positionScreen.z <= 1;
  143. }
  144. function pushVertex( x, y, z ) {
  145. _vertex = getNextVertexInPool();
  146. _vertex.position.set( x, y, z );
  147. projectVertex( _vertex );
  148. }
  149. function pushNormal( x, y, z ) {
  150. normals.push( x, y, z );
  151. }
  152. function pushColor( r, g, b ) {
  153. colors.push( r, g, b );
  154. }
  155. function pushUv( x, y ) {
  156. uvs.push( x, y );
  157. }
  158. function checkTriangleVisibility( v1, v2, v3 ) {
  159. if ( v1.visible === true || v2.visible === true || v3.visible === true ) return true;
  160. _points3[ 0 ] = v1.positionScreen;
  161. _points3[ 1 ] = v2.positionScreen;
  162. _points3[ 2 ] = v3.positionScreen;
  163. return _clipBox.intersectsBox( _boundingBox.setFromPoints( _points3 ) );
  164. }
  165. function checkBackfaceCulling( v1, v2, v3 ) {
  166. return ( ( v3.positionScreen.x - v1.positionScreen.x ) *
  167. ( v2.positionScreen.y - v1.positionScreen.y ) -
  168. ( v3.positionScreen.y - v1.positionScreen.y ) *
  169. ( v2.positionScreen.x - v1.positionScreen.x ) ) < 0;
  170. }
  171. function pushLine( a, b ) {
  172. var v1 = _vertexPool[ a ];
  173. var v2 = _vertexPool[ b ];
  174. // Clip
  175. v1.positionScreen.copy( v1.position ).applyMatrix4( _modelViewProjectionMatrix );
  176. v2.positionScreen.copy( v2.position ).applyMatrix4( _modelViewProjectionMatrix );
  177. if ( clipLine( v1.positionScreen, v2.positionScreen ) === true ) {
  178. // Perform the perspective divide
  179. v1.positionScreen.multiplyScalar( 1 / v1.positionScreen.w );
  180. v2.positionScreen.multiplyScalar( 1 / v2.positionScreen.w );
  181. _line = getNextLineInPool();
  182. _line.id = object.id;
  183. _line.v1.copy( v1 );
  184. _line.v2.copy( v2 );
  185. _line.z = Math.max( v1.positionScreen.z, v2.positionScreen.z );
  186. _line.renderOrder = object.renderOrder;
  187. _line.material = object.material;
  188. if ( object.material.vertexColors === VertexColors ) {
  189. _line.vertexColors[ 0 ].fromArray( colors, a * 3 );
  190. _line.vertexColors[ 1 ].fromArray( colors, b * 3 );
  191. }
  192. _renderData.elements.push( _line );
  193. }
  194. }
  195. function pushTriangle( a, b, c, material ) {
  196. var v1 = _vertexPool[ a ];
  197. var v2 = _vertexPool[ b ];
  198. var v3 = _vertexPool[ c ];
  199. if ( checkTriangleVisibility( v1, v2, v3 ) === false ) return;
  200. if ( material.side === DoubleSide || checkBackfaceCulling( v1, v2, v3 ) === true ) {
  201. _face = getNextFaceInPool();
  202. _face.id = object.id;
  203. _face.v1.copy( v1 );
  204. _face.v2.copy( v2 );
  205. _face.v3.copy( v3 );
  206. _face.z = ( v1.positionScreen.z + v2.positionScreen.z + v3.positionScreen.z ) / 3;
  207. _face.renderOrder = object.renderOrder;
  208. // face normal
  209. _vector3.subVectors( v3.position, v2.position );
  210. _vector4.subVectors( v1.position, v2.position );
  211. _vector3.cross( _vector4 );
  212. _face.normalModel.copy( _vector3 );
  213. _face.normalModel.applyMatrix3( normalMatrix ).normalize();
  214. for ( var i = 0; i < 3; i ++ ) {
  215. var normal = _face.vertexNormalsModel[ i ];
  216. normal.fromArray( normals, arguments[ i ] * 3 );
  217. normal.applyMatrix3( normalMatrix ).normalize();
  218. var uv = _face.uvs[ i ];
  219. uv.fromArray( uvs, arguments[ i ] * 2 );
  220. }
  221. _face.vertexNormalsLength = 3;
  222. _face.material = material;
  223. if ( material.vertexColors === FaceColors || material.vertexColors === VertexColors ) {
  224. _face.color.fromArray( colors, a * 3 );
  225. }
  226. _renderData.elements.push( _face );
  227. }
  228. }
  229. return {
  230. setObject: setObject,
  231. projectVertex: projectVertex,
  232. checkTriangleVisibility: checkTriangleVisibility,
  233. checkBackfaceCulling: checkBackfaceCulling,
  234. pushVertex: pushVertex,
  235. pushNormal: pushNormal,
  236. pushColor: pushColor,
  237. pushUv: pushUv,
  238. pushLine: pushLine,
  239. pushTriangle: pushTriangle
  240. };
  241. };
  242. var renderList = new RenderList();
  243. function projectObject( object ) {
  244. if ( object.visible === false ) return;
  245. if ( object instanceof Light ) {
  246. _renderData.lights.push( object );
  247. } else if ( object instanceof Mesh || object instanceof Line || object instanceof Points ) {
  248. if ( object.material.visible === false ) return;
  249. if ( object.frustumCulled === true && _frustum.intersectsObject( object ) === false ) return;
  250. addObject( object );
  251. } else if ( object instanceof Sprite ) {
  252. if ( object.material.visible === false ) return;
  253. if ( object.frustumCulled === true && _frustum.intersectsSprite( object ) === false ) return;
  254. addObject( object );
  255. }
  256. var children = object.children;
  257. for ( var i = 0, l = children.length; i < l; i ++ ) {
  258. projectObject( children[ i ] );
  259. }
  260. }
  261. function addObject( object ) {
  262. _object = getNextObjectInPool();
  263. _object.id = object.id;
  264. _object.object = object;
  265. _vector3.setFromMatrixPosition( object.matrixWorld );
  266. _vector3.applyMatrix4( _viewProjectionMatrix );
  267. _object.z = _vector3.z;
  268. _object.renderOrder = object.renderOrder;
  269. _renderData.objects.push( _object );
  270. }
  271. this.projectScene = function ( scene, camera, sortObjects, sortElements ) {
  272. _faceCount = 0;
  273. _lineCount = 0;
  274. _spriteCount = 0;
  275. _renderData.elements.length = 0;
  276. if ( scene.autoUpdate === true ) scene.updateMatrixWorld();
  277. if ( camera.parent === null ) camera.updateMatrixWorld();
  278. _viewMatrix.copy( camera.matrixWorldInverse );
  279. _viewProjectionMatrix.multiplyMatrices( camera.projectionMatrix, _viewMatrix );
  280. _frustum.setFromProjectionMatrix( _viewProjectionMatrix );
  281. //
  282. _objectCount = 0;
  283. _renderData.objects.length = 0;
  284. _renderData.lights.length = 0;
  285. projectObject( scene );
  286. if ( sortObjects === true ) {
  287. _renderData.objects.sort( painterSort );
  288. }
  289. //
  290. var objects = _renderData.objects;
  291. for ( var o = 0, ol = objects.length; o < ol; o ++ ) {
  292. var object = objects[ o ].object;
  293. var geometry = object.geometry;
  294. renderList.setObject( object );
  295. _modelMatrix = object.matrixWorld;
  296. _vertexCount = 0;
  297. if ( object instanceof Mesh ) {
  298. if ( geometry instanceof BufferGeometry ) {
  299. var material = object.material;
  300. var isMultiMaterial = Array.isArray( material );
  301. var attributes = geometry.attributes;
  302. var groups = geometry.groups;
  303. if ( attributes.position === undefined ) continue;
  304. var positions = attributes.position.array;
  305. for ( var i = 0, l = positions.length; i < l; i += 3 ) {
  306. var x = positions[ i ];
  307. var y = positions[ i + 1 ];
  308. var z = positions[ i + 2 ];
  309. if ( material.morphTargets === true ) {
  310. var morphTargets = geometry.morphAttributes.position;
  311. var morphTargetsRelative = geometry.morphTargetsRelative;
  312. var morphInfluences = object.morphTargetInfluences;
  313. for ( var t = 0, tl = morphTargets.length; t < tl; t ++ ) {
  314. var influence = morphInfluences[ t ];
  315. if ( influence === 0 ) continue;
  316. var target = morphTargets[ t ];
  317. if ( morphTargetsRelative ) {
  318. x += target.getX( i / 3 ) * influence;
  319. y += target.getY( i / 3 ) * influence;
  320. z += target.getZ( i / 3 ) * influence;
  321. } else {
  322. x += ( target.getX( i / 3 ) - positions[ i ] ) * influence;
  323. y += ( target.getY( i / 3 ) - positions[ i + 1 ] ) * influence;
  324. z += ( target.getZ( i / 3 ) - positions[ i + 2 ] ) * influence;
  325. }
  326. }
  327. }
  328. renderList.pushVertex( x, y, z );
  329. }
  330. if ( attributes.normal !== undefined ) {
  331. var normals = attributes.normal.array;
  332. for ( var i = 0, l = normals.length; i < l; i += 3 ) {
  333. renderList.pushNormal( normals[ i ], normals[ i + 1 ], normals[ i + 2 ] );
  334. }
  335. }
  336. if ( attributes.color !== undefined ) {
  337. var colors = attributes.color.array;
  338. for ( var i = 0, l = colors.length; i < l; i += 3 ) {
  339. renderList.pushColor( colors[ i ], colors[ i + 1 ], colors[ i + 2 ] );
  340. }
  341. }
  342. if ( attributes.uv !== undefined ) {
  343. var uvs = attributes.uv.array;
  344. for ( var i = 0, l = uvs.length; i < l; i += 2 ) {
  345. renderList.pushUv( uvs[ i ], uvs[ i + 1 ] );
  346. }
  347. }
  348. if ( geometry.index !== null ) {
  349. var indices = geometry.index.array;
  350. if ( groups.length > 0 ) {
  351. for ( var g = 0; g < groups.length; g ++ ) {
  352. var group = groups[ g ];
  353. material = isMultiMaterial === true
  354. ? object.material[ group.materialIndex ]
  355. : object.material;
  356. if ( material === undefined ) continue;
  357. for ( var i = group.start, l = group.start + group.count; i < l; i += 3 ) {
  358. renderList.pushTriangle( indices[ i ], indices[ i + 1 ], indices[ i + 2 ], material );
  359. }
  360. }
  361. } else {
  362. for ( var i = 0, l = indices.length; i < l; i += 3 ) {
  363. renderList.pushTriangle( indices[ i ], indices[ i + 1 ], indices[ i + 2 ], material );
  364. }
  365. }
  366. } else {
  367. if ( groups.length > 0 ) {
  368. for ( var g = 0; g < groups.length; g ++ ) {
  369. var group = groups[ g ];
  370. material = isMultiMaterial === true
  371. ? object.material[ group.materialIndex ]
  372. : object.material;
  373. if ( material === undefined ) continue;
  374. for ( var i = group.start, l = group.start + group.count; i < l; i += 3 ) {
  375. renderList.pushTriangle( i, i + 1, i + 2, material );
  376. }
  377. }
  378. } else {
  379. for ( var i = 0, l = positions.length / 3; i < l; i += 3 ) {
  380. renderList.pushTriangle( i, i + 1, i + 2, material );
  381. }
  382. }
  383. }
  384. } else if ( geometry instanceof Geometry ) {
  385. var vertices = geometry.vertices;
  386. var faces = geometry.faces;
  387. var faceVertexUvs = geometry.faceVertexUvs[ 0 ];
  388. _normalMatrix.getNormalMatrix( _modelMatrix );
  389. var material = object.material;
  390. var isMultiMaterial = Array.isArray( material );
  391. for ( var v = 0, vl = vertices.length; v < vl; v ++ ) {
  392. var vertex = vertices[ v ];
  393. _vector3.copy( vertex );
  394. if ( material.morphTargets === true ) {
  395. var morphTargets = geometry.morphTargets;
  396. var morphInfluences = object.morphTargetInfluences;
  397. for ( var t = 0, tl = morphTargets.length; t < tl; t ++ ) {
  398. var influence = morphInfluences[ t ];
  399. if ( influence === 0 ) continue;
  400. var target = morphTargets[ t ];
  401. var targetVertex = target.vertices[ v ];
  402. _vector3.x += ( targetVertex.x - vertex.x ) * influence;
  403. _vector3.y += ( targetVertex.y - vertex.y ) * influence;
  404. _vector3.z += ( targetVertex.z - vertex.z ) * influence;
  405. }
  406. }
  407. renderList.pushVertex( _vector3.x, _vector3.y, _vector3.z );
  408. }
  409. for ( var f = 0, fl = faces.length; f < fl; f ++ ) {
  410. var face = faces[ f ];
  411. material = isMultiMaterial === true
  412. ? object.material[ face.materialIndex ]
  413. : object.material;
  414. if ( material === undefined ) continue;
  415. var side = material.side;
  416. var v1 = _vertexPool[ face.a ];
  417. var v2 = _vertexPool[ face.b ];
  418. var v3 = _vertexPool[ face.c ];
  419. if ( renderList.checkTriangleVisibility( v1, v2, v3 ) === false ) continue;
  420. var visible = renderList.checkBackfaceCulling( v1, v2, v3 );
  421. if ( side !== DoubleSide ) {
  422. if ( side === FrontSide && visible === false ) continue;
  423. if ( side === BackSide && visible === true ) continue;
  424. }
  425. _face = getNextFaceInPool();
  426. _face.id = object.id;
  427. _face.v1.copy( v1 );
  428. _face.v2.copy( v2 );
  429. _face.v3.copy( v3 );
  430. _face.normalModel.copy( face.normal );
  431. if ( visible === false && ( side === BackSide || side === DoubleSide ) ) {
  432. _face.normalModel.negate();
  433. }
  434. _face.normalModel.applyMatrix3( _normalMatrix ).normalize();
  435. var faceVertexNormals = face.vertexNormals;
  436. for ( var n = 0, nl = Math.min( faceVertexNormals.length, 3 ); n < nl; n ++ ) {
  437. var normalModel = _face.vertexNormalsModel[ n ];
  438. normalModel.copy( faceVertexNormals[ n ] );
  439. if ( visible === false && ( side === BackSide || side === DoubleSide ) ) {
  440. normalModel.negate();
  441. }
  442. normalModel.applyMatrix3( _normalMatrix ).normalize();
  443. }
  444. _face.vertexNormalsLength = faceVertexNormals.length;
  445. var vertexUvs = faceVertexUvs[ f ];
  446. if ( vertexUvs !== undefined ) {
  447. for ( var u = 0; u < 3; u ++ ) {
  448. _face.uvs[ u ].copy( vertexUvs[ u ] );
  449. }
  450. }
  451. _face.color = face.color;
  452. _face.material = material;
  453. _face.z = ( v1.positionScreen.z + v2.positionScreen.z + v3.positionScreen.z ) / 3;
  454. _face.renderOrder = object.renderOrder;
  455. _renderData.elements.push( _face );
  456. }
  457. }
  458. } else if ( object instanceof Line ) {
  459. _modelViewProjectionMatrix.multiplyMatrices( _viewProjectionMatrix, _modelMatrix );
  460. if ( geometry instanceof BufferGeometry ) {
  461. var attributes = geometry.attributes;
  462. if ( attributes.position !== undefined ) {
  463. var positions = attributes.position.array;
  464. for ( var i = 0, l = positions.length; i < l; i += 3 ) {
  465. renderList.pushVertex( positions[ i ], positions[ i + 1 ], positions[ i + 2 ] );
  466. }
  467. if ( attributes.color !== undefined ) {
  468. var colors = attributes.color.array;
  469. for ( var i = 0, l = colors.length; i < l; i += 3 ) {
  470. renderList.pushColor( colors[ i ], colors[ i + 1 ], colors[ i + 2 ] );
  471. }
  472. }
  473. if ( geometry.index !== null ) {
  474. var indices = geometry.index.array;
  475. for ( var i = 0, l = indices.length; i < l; i += 2 ) {
  476. renderList.pushLine( indices[ i ], indices[ i + 1 ] );
  477. }
  478. } else {
  479. var step = object instanceof LineSegments ? 2 : 1;
  480. for ( var i = 0, l = ( positions.length / 3 ) - 1; i < l; i += step ) {
  481. renderList.pushLine( i, i + 1 );
  482. }
  483. }
  484. }
  485. } else if ( geometry instanceof Geometry ) {
  486. var vertices = object.geometry.vertices;
  487. if ( vertices.length === 0 ) continue;
  488. v1 = getNextVertexInPool();
  489. v1.positionScreen.copy( vertices[ 0 ] ).applyMatrix4( _modelViewProjectionMatrix );
  490. var step = object instanceof LineSegments ? 2 : 1;
  491. for ( var v = 1, vl = vertices.length; v < vl; v ++ ) {
  492. v1 = getNextVertexInPool();
  493. v1.positionScreen.copy( vertices[ v ] ).applyMatrix4( _modelViewProjectionMatrix );
  494. if ( ( v + 1 ) % step > 0 ) continue;
  495. v2 = _vertexPool[ _vertexCount - 2 ];
  496. _clippedVertex1PositionScreen.copy( v1.positionScreen );
  497. _clippedVertex2PositionScreen.copy( v2.positionScreen );
  498. if ( clipLine( _clippedVertex1PositionScreen, _clippedVertex2PositionScreen ) === true ) {
  499. // Perform the perspective divide
  500. _clippedVertex1PositionScreen.multiplyScalar( 1 / _clippedVertex1PositionScreen.w );
  501. _clippedVertex2PositionScreen.multiplyScalar( 1 / _clippedVertex2PositionScreen.w );
  502. _line = getNextLineInPool();
  503. _line.id = object.id;
  504. _line.v1.positionScreen.copy( _clippedVertex1PositionScreen );
  505. _line.v2.positionScreen.copy( _clippedVertex2PositionScreen );
  506. _line.z = Math.max( _clippedVertex1PositionScreen.z, _clippedVertex2PositionScreen.z );
  507. _line.renderOrder = object.renderOrder;
  508. _line.material = object.material;
  509. if ( object.material.vertexColors === VertexColors ) {
  510. _line.vertexColors[ 0 ].copy( object.geometry.colors[ v ] );
  511. _line.vertexColors[ 1 ].copy( object.geometry.colors[ v - 1 ] );
  512. }
  513. _renderData.elements.push( _line );
  514. }
  515. }
  516. }
  517. } else if ( object instanceof Points ) {
  518. _modelViewProjectionMatrix.multiplyMatrices( _viewProjectionMatrix, _modelMatrix );
  519. if ( geometry instanceof Geometry ) {
  520. var vertices = object.geometry.vertices;
  521. for ( var v = 0, vl = vertices.length; v < vl; v ++ ) {
  522. var vertex = vertices[ v ];
  523. _vector4.set( vertex.x, vertex.y, vertex.z, 1 );
  524. _vector4.applyMatrix4( _modelViewProjectionMatrix );
  525. pushPoint( _vector4, object, camera );
  526. }
  527. } else if ( geometry instanceof BufferGeometry ) {
  528. var attributes = geometry.attributes;
  529. if ( attributes.position !== undefined ) {
  530. var positions = attributes.position.array;
  531. for ( var i = 0, l = positions.length; i < l; i += 3 ) {
  532. _vector4.set( positions[ i ], positions[ i + 1 ], positions[ i + 2 ], 1 );
  533. _vector4.applyMatrix4( _modelViewProjectionMatrix );
  534. pushPoint( _vector4, object, camera );
  535. }
  536. }
  537. }
  538. } else if ( object instanceof Sprite ) {
  539. object.modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
  540. _vector4.set( _modelMatrix.elements[ 12 ], _modelMatrix.elements[ 13 ], _modelMatrix.elements[ 14 ], 1 );
  541. _vector4.applyMatrix4( _viewProjectionMatrix );
  542. pushPoint( _vector4, object, camera );
  543. }
  544. }
  545. if ( sortElements === true ) {
  546. _renderData.elements.sort( painterSort );
  547. }
  548. return _renderData;
  549. };
  550. function pushPoint( _vector4, object, camera ) {
  551. var invW = 1 / _vector4.w;
  552. _vector4.z *= invW;
  553. if ( _vector4.z >= - 1 && _vector4.z <= 1 ) {
  554. _sprite = getNextSpriteInPool();
  555. _sprite.id = object.id;
  556. _sprite.x = _vector4.x * invW;
  557. _sprite.y = _vector4.y * invW;
  558. _sprite.z = _vector4.z;
  559. _sprite.renderOrder = object.renderOrder;
  560. _sprite.object = object;
  561. _sprite.rotation = object.rotation;
  562. _sprite.scale.x = object.scale.x * Math.abs( _sprite.x - ( _vector4.x + camera.projectionMatrix.elements[ 0 ] ) / ( _vector4.w + camera.projectionMatrix.elements[ 12 ] ) );
  563. _sprite.scale.y = object.scale.y * Math.abs( _sprite.y - ( _vector4.y + camera.projectionMatrix.elements[ 5 ] ) / ( _vector4.w + camera.projectionMatrix.elements[ 13 ] ) );
  564. _sprite.material = object.material;
  565. _renderData.elements.push( _sprite );
  566. }
  567. }
  568. // Pools
  569. function getNextObjectInPool() {
  570. if ( _objectCount === _objectPoolLength ) {
  571. var object = new RenderableObject();
  572. _objectPool.push( object );
  573. _objectPoolLength ++;
  574. _objectCount ++;
  575. return object;
  576. }
  577. return _objectPool[ _objectCount ++ ];
  578. }
  579. function getNextVertexInPool() {
  580. if ( _vertexCount === _vertexPoolLength ) {
  581. var vertex = new RenderableVertex();
  582. _vertexPool.push( vertex );
  583. _vertexPoolLength ++;
  584. _vertexCount ++;
  585. return vertex;
  586. }
  587. return _vertexPool[ _vertexCount ++ ];
  588. }
  589. function getNextFaceInPool() {
  590. if ( _faceCount === _facePoolLength ) {
  591. var face = new RenderableFace();
  592. _facePool.push( face );
  593. _facePoolLength ++;
  594. _faceCount ++;
  595. return face;
  596. }
  597. return _facePool[ _faceCount ++ ];
  598. }
  599. function getNextLineInPool() {
  600. if ( _lineCount === _linePoolLength ) {
  601. var line = new RenderableLine();
  602. _linePool.push( line );
  603. _linePoolLength ++;
  604. _lineCount ++;
  605. return line;
  606. }
  607. return _linePool[ _lineCount ++ ];
  608. }
  609. function getNextSpriteInPool() {
  610. if ( _spriteCount === _spritePoolLength ) {
  611. var sprite = new RenderableSprite();
  612. _spritePool.push( sprite );
  613. _spritePoolLength ++;
  614. _spriteCount ++;
  615. return sprite;
  616. }
  617. return _spritePool[ _spriteCount ++ ];
  618. }
  619. //
  620. function painterSort( a, b ) {
  621. if ( a.renderOrder !== b.renderOrder ) {
  622. return a.renderOrder - b.renderOrder;
  623. } else if ( a.z !== b.z ) {
  624. return b.z - a.z;
  625. } else if ( a.id !== b.id ) {
  626. return a.id - b.id;
  627. } else {
  628. return 0;
  629. }
  630. }
  631. function clipLine( s1, s2 ) {
  632. var alpha1 = 0, alpha2 = 1,
  633. // Calculate the boundary coordinate of each vertex for the near and far clip planes,
  634. // Z = -1 and Z = +1, respectively.
  635. bc1near = s1.z + s1.w,
  636. bc2near = s2.z + s2.w,
  637. bc1far = - s1.z + s1.w,
  638. bc2far = - s2.z + s2.w;
  639. if ( bc1near >= 0 && bc2near >= 0 && bc1far >= 0 && bc2far >= 0 ) {
  640. // Both vertices lie entirely within all clip planes.
  641. return true;
  642. } else if ( ( bc1near < 0 && bc2near < 0 ) || ( bc1far < 0 && bc2far < 0 ) ) {
  643. // Both vertices lie entirely outside one of the clip planes.
  644. return false;
  645. } else {
  646. // The line segment spans at least one clip plane.
  647. if ( bc1near < 0 ) {
  648. // v1 lies outside the near plane, v2 inside
  649. alpha1 = Math.max( alpha1, bc1near / ( bc1near - bc2near ) );
  650. } else if ( bc2near < 0 ) {
  651. // v2 lies outside the near plane, v1 inside
  652. alpha2 = Math.min( alpha2, bc1near / ( bc1near - bc2near ) );
  653. }
  654. if ( bc1far < 0 ) {
  655. // v1 lies outside the far plane, v2 inside
  656. alpha1 = Math.max( alpha1, bc1far / ( bc1far - bc2far ) );
  657. } else if ( bc2far < 0 ) {
  658. // v2 lies outside the far plane, v2 inside
  659. alpha2 = Math.min( alpha2, bc1far / ( bc1far - bc2far ) );
  660. }
  661. if ( alpha2 < alpha1 ) {
  662. // The line segment spans two boundaries, but is outside both of them.
  663. // (This can't happen when we're only clipping against just near/far but good
  664. // to leave the check here for future usage if other clip planes are added.)
  665. return false;
  666. } else {
  667. // Update the s1 and s2 vertices to match the clipped line segment.
  668. s1.lerp( s2, alpha1 );
  669. s2.lerp( s1, 1 - alpha2 );
  670. return true;
  671. }
  672. }
  673. }
  674. };
  675. export { RenderableObject, RenderableFace, RenderableVertex, RenderableLine, RenderableSprite, Projector };