Projector.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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.Projector = function() {
  7. var _object, _objectCount, _objectPool = [], _objectPoolLength = 0,
  8. _vertex, _vertexCount, _vertexPool = [], _vertexPoolLength = 0,
  9. _face, _face3Count, _face3Pool = [], _face3PoolLength = 0,
  10. _face4Count, _face4Pool = [], _face4PoolLength = 0,
  11. _line, _lineCount, _linePool = [], _linePoolLength = 0,
  12. _particle, _particleCount, _particlePool = [], _particlePoolLength = 0,
  13. _renderData = { objects: [], sprites: [], lights: [], elements: [] },
  14. _vector3 = new THREE.Vector3(),
  15. _vector4 = new THREE.Vector4(),
  16. _viewMatrix = new THREE.Matrix4(),
  17. _viewProjectionMatrix = new THREE.Matrix4(),
  18. _modelMatrix,
  19. _modelViewProjectionMatrix = new THREE.Matrix4(),
  20. _normalMatrix = new THREE.Matrix3(),
  21. _normalViewMatrix = new THREE.Matrix3(),
  22. _centroid = new THREE.Vector3(),
  23. _frustum = new THREE.Frustum(),
  24. _clippedVertex1PositionScreen = new THREE.Vector4(),
  25. _clippedVertex2PositionScreen = new THREE.Vector4(),
  26. _face3VertexNormals;
  27. this.projectVector = function ( vector, camera ) {
  28. camera.matrixWorldInverse.getInverse( camera.matrixWorld );
  29. _viewProjectionMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
  30. return vector.applyMatrix4( _viewProjectionMatrix );
  31. };
  32. this.unprojectVector = function ( vector, camera ) {
  33. camera.projectionMatrixInverse.getInverse( camera.projectionMatrix );
  34. _viewProjectionMatrix.multiplyMatrices( camera.matrixWorld, camera.projectionMatrixInverse );
  35. return vector.applyMatrix4( _viewProjectionMatrix );
  36. };
  37. this.pickingRay = function ( vector, camera ) {
  38. // set two vectors with opposing z values
  39. vector.z = -1.0;
  40. var end = new THREE.Vector3( vector.x, vector.y, 1.0 );
  41. this.unprojectVector( vector, camera );
  42. this.unprojectVector( end, camera );
  43. // find direction from vector to end
  44. end.sub( vector ).normalize();
  45. return new THREE.Raycaster( vector, end );
  46. };
  47. var projectGraph = function ( root, sortObjects ) {
  48. _objectCount = 0;
  49. _renderData.objects.length = 0;
  50. _renderData.sprites.length = 0;
  51. _renderData.lights.length = 0;
  52. var projectObject = function ( parent ) {
  53. for ( var c = 0, cl = parent.children.length; c < cl; c ++ ) {
  54. var object = parent.children[ c ];
  55. if ( object.visible === false ) continue;
  56. if ( object instanceof THREE.Light ) {
  57. _renderData.lights.push( object );
  58. } else if ( object instanceof THREE.Mesh || object instanceof THREE.Line ) {
  59. if ( object.frustumCulled === false || _frustum.intersectsObject( object ) === true ) {
  60. _object = getNextObjectInPool();
  61. _object.object = object;
  62. if ( object.renderDepth !== null ) {
  63. _object.z = object.renderDepth;
  64. } else {
  65. _vector3.copy( object.matrixWorld.getPosition() );
  66. _vector3.applyMatrix4( _viewProjectionMatrix );
  67. _object.z = _vector3.z;
  68. }
  69. _renderData.objects.push( _object );
  70. }
  71. } else if ( object instanceof THREE.Sprite || object instanceof THREE.Particle ) {
  72. _object = getNextObjectInPool();
  73. _object.object = object;
  74. // TODO: Find an elegant and performant solution and remove this dupe code.
  75. if ( object.renderDepth !== null ) {
  76. _object.z = object.renderDepth;
  77. } else {
  78. _vector3.copy( object.matrixWorld.getPosition() );
  79. _vector3.applyMatrix4( _viewProjectionMatrix );
  80. _object.z = _vector3.z;
  81. }
  82. _renderData.sprites.push( _object );
  83. } else {
  84. _object = getNextObjectInPool();
  85. _object.object = object;
  86. if ( object.renderDepth !== null ) {
  87. _object.z = object.renderDepth;
  88. } else {
  89. _vector3.copy( object.matrixWorld.getPosition() );
  90. _vector3.applyMatrix4( _viewProjectionMatrix );
  91. _object.z = _vector3.z;
  92. }
  93. _renderData.objects.push( _object );
  94. }
  95. projectObject( object );
  96. }
  97. };
  98. projectObject( root );
  99. if ( sortObjects === true ) _renderData.objects.sort( painterSort );
  100. return _renderData;
  101. };
  102. this.projectScene = function ( scene, camera, sortObjects, sortElements ) {
  103. var visible = false,
  104. o, ol, v, vl, f, fl, n, nl, c, cl, u, ul, object,
  105. geometry, vertices, vertex, vertexPositionScreen,
  106. faces, face, faceVertexNormals, faceVertexUvs, uvs,
  107. v1, v2, v3, v4, isFaceMaterial, objectMaterials;
  108. _face3Count = 0;
  109. _face4Count = 0;
  110. _lineCount = 0;
  111. _particleCount = 0;
  112. _renderData.elements.length = 0;
  113. scene.updateMatrixWorld();
  114. if ( camera.parent === undefined ) camera.updateMatrixWorld();
  115. _viewMatrix.copy( camera.matrixWorldInverse.getInverse( camera.matrixWorld ) );
  116. _viewProjectionMatrix.multiplyMatrices( camera.projectionMatrix, _viewMatrix );
  117. _normalViewMatrix.getInverse( _viewMatrix );
  118. _normalViewMatrix.transpose();
  119. _frustum.setFromMatrix( _viewProjectionMatrix );
  120. _renderData = projectGraph( scene, sortObjects );
  121. for ( o = 0, ol = _renderData.objects.length; o < ol; o ++ ) {
  122. object = _renderData.objects[ o ].object;
  123. _modelMatrix = object.matrixWorld;
  124. _vertexCount = 0;
  125. if ( object instanceof THREE.Mesh ) {
  126. geometry = object.geometry;
  127. vertices = geometry.vertices;
  128. faces = geometry.faces;
  129. faceVertexUvs = geometry.faceVertexUvs;
  130. _normalMatrix.getInverse( _modelMatrix );
  131. _normalMatrix.transpose();
  132. isFaceMaterial = object.material instanceof THREE.MeshFaceMaterial;
  133. objectMaterials = isFaceMaterial === true ? object.material : null;
  134. for ( v = 0, vl = vertices.length; v < vl; v ++ ) {
  135. _vertex = getNextVertexInPool();
  136. _vertex.positionWorld.copy( vertices[ v ] );
  137. _vertex.positionWorld.applyMatrix4( _modelMatrix );
  138. _vertex.positionScreen.copy( _vertex.positionWorld );
  139. _vertex.positionScreen.applyMatrix4( _viewProjectionMatrix );
  140. _vertex.positionScreen.x /= _vertex.positionScreen.w;
  141. _vertex.positionScreen.y /= _vertex.positionScreen.w;
  142. _vertex.positionScreen.z /= _vertex.positionScreen.w;
  143. _vertex.visible = ! ( _vertex.positionScreen.x < -1 || _vertex.positionScreen.x > 1 ||
  144. _vertex.positionScreen.y < -1 || _vertex.positionScreen.y > 1 ||
  145. _vertex.positionScreen.z < -1 || _vertex.positionScreen.z > 1 );
  146. }
  147. for ( f = 0, fl = faces.length; f < fl; f ++ ) {
  148. face = faces[ f ];
  149. var material = isFaceMaterial === true
  150. ? objectMaterials.materials[ face.materialIndex ]
  151. : object.material;
  152. if ( material === undefined ) continue;
  153. var side = material.side;
  154. if ( face instanceof THREE.Face3 ) {
  155. v1 = _vertexPool[ face.a ];
  156. v2 = _vertexPool[ face.b ];
  157. v3 = _vertexPool[ face.c ];
  158. if ( v1.visible === true || v2.visible === true || v3.visible === true ) {
  159. visible = ( ( v3.positionScreen.x - v1.positionScreen.x ) * ( v2.positionScreen.y - v1.positionScreen.y ) -
  160. ( v3.positionScreen.y - v1.positionScreen.y ) * ( v2.positionScreen.x - v1.positionScreen.x ) ) < 0;
  161. if ( side === THREE.DoubleSide || visible === ( side === THREE.FrontSide ) ) {
  162. _face = getNextFace3InPool();
  163. _face.v1.copy( v1 );
  164. _face.v2.copy( v2 );
  165. _face.v3.copy( v3 );
  166. } else {
  167. continue;
  168. }
  169. } else {
  170. continue;
  171. }
  172. } else if ( face instanceof THREE.Face4 ) {
  173. v1 = _vertexPool[ face.a ];
  174. v2 = _vertexPool[ face.b ];
  175. v3 = _vertexPool[ face.c ];
  176. v4 = _vertexPool[ face.d ];
  177. if ( v1.visible === true || v2.visible === true || v3.visible === true || v4.visible === true ) {
  178. visible = ( v4.positionScreen.x - v1.positionScreen.x ) * ( v2.positionScreen.y - v1.positionScreen.y ) -
  179. ( v4.positionScreen.y - v1.positionScreen.y ) * ( v2.positionScreen.x - v1.positionScreen.x ) < 0 ||
  180. ( v2.positionScreen.x - v3.positionScreen.x ) * ( v4.positionScreen.y - v3.positionScreen.y ) -
  181. ( v2.positionScreen.y - v3.positionScreen.y ) * ( v4.positionScreen.x - v3.positionScreen.x ) < 0;
  182. if ( side === THREE.DoubleSide || visible === ( side === THREE.FrontSide ) ) {
  183. _face = getNextFace4InPool();
  184. _face.v1.copy( v1 );
  185. _face.v2.copy( v2 );
  186. _face.v3.copy( v3 );
  187. _face.v4.copy( v4 );
  188. } else {
  189. continue;
  190. }
  191. } else {
  192. continue;
  193. }
  194. }
  195. _face.normalModel.copy( face.normal );
  196. if ( visible === false && ( side === THREE.BackSide || side === THREE.DoubleSide ) ) {
  197. _face.normalModel.negate();
  198. }
  199. _face.normalModel.applyMatrix3( _normalMatrix );
  200. _face.normalModel.normalize();
  201. _face.normalModelView.copy( _face.normalModel );
  202. _face.normalModelView.applyMatrix3( _normalViewMatrix );
  203. _face.centroidModel.copy( face.centroid );
  204. _face.centroidModel.applyMatrix4( _modelMatrix );
  205. faceVertexNormals = face.vertexNormals;
  206. for ( n = 0, nl = faceVertexNormals.length; n < nl; n ++ ) {
  207. var normalModel = _face.vertexNormalsModel[ n ];
  208. normalModel.copy( faceVertexNormals[ n ] );
  209. if ( visible === false && ( side === THREE.BackSide || side === THREE.DoubleSide ) ) {
  210. normalModel.negate();
  211. }
  212. normalModel.applyMatrix3( _normalMatrix );
  213. normalModel.normalize();
  214. var normalModelView = _face.vertexNormalsModelView[ n ];
  215. normalModelView.copy( normalModel );
  216. normalModelView.applyMatrix3( _normalViewMatrix );
  217. }
  218. _face.vertexNormalsLength = faceVertexNormals.length;
  219. for ( c = 0, cl = faceVertexUvs.length; c < cl; c ++ ) {
  220. uvs = faceVertexUvs[ c ][ f ];
  221. if ( uvs === undefined ) continue;
  222. for ( u = 0, ul = uvs.length; u < ul; u ++ ) {
  223. _face.uvs[ c ][ u ] = uvs[ u ];
  224. }
  225. }
  226. _face.color = face.color;
  227. _face.material = material;
  228. _centroid.copy( _face.centroidModel )
  229. _centroid.applyMatrix4( _viewProjectionMatrix );
  230. _face.z = _centroid.z;
  231. _renderData.elements.push( _face );
  232. }
  233. } else if ( object instanceof THREE.Line ) {
  234. _modelViewProjectionMatrix.multiplyMatrices( _viewProjectionMatrix, _modelMatrix );
  235. vertices = object.geometry.vertices;
  236. v1 = getNextVertexInPool();
  237. v1.positionScreen.copy( vertices[ 0 ] );
  238. v1.positionScreen.applyMatrix4( _modelViewProjectionMatrix );
  239. // Handle LineStrip and LinePieces
  240. var step = object.type === THREE.LinePieces ? 2 : 1;
  241. for ( v = 1, vl = vertices.length; v < vl; v ++ ) {
  242. v1 = getNextVertexInPool();
  243. v1.positionScreen.copy( vertices[ v ] );
  244. v1.positionScreen.applyMatrix4( _modelViewProjectionMatrix );
  245. if ( ( v + 1 ) % step > 0 ) continue;
  246. v2 = _vertexPool[ _vertexCount - 2 ];
  247. _clippedVertex1PositionScreen.copy( v1.positionScreen );
  248. _clippedVertex2PositionScreen.copy( v2.positionScreen );
  249. if ( clipLine( _clippedVertex1PositionScreen, _clippedVertex2PositionScreen ) === true ) {
  250. // Perform the perspective divide
  251. _clippedVertex1PositionScreen.multiplyScalar( 1 / _clippedVertex1PositionScreen.w );
  252. _clippedVertex2PositionScreen.multiplyScalar( 1 / _clippedVertex2PositionScreen.w );
  253. _line = getNextLineInPool();
  254. _line.v1.positionScreen.copy( _clippedVertex1PositionScreen );
  255. _line.v2.positionScreen.copy( _clippedVertex2PositionScreen );
  256. _line.z = Math.max( _clippedVertex1PositionScreen.z, _clippedVertex2PositionScreen.z );
  257. _line.material = object.material;
  258. _renderData.elements.push( _line );
  259. }
  260. }
  261. }
  262. }
  263. for ( o = 0, ol = _renderData.sprites.length; o < ol; o++ ) {
  264. object = _renderData.sprites[ o ].object;
  265. _modelMatrix = object.matrixWorld;
  266. if ( object instanceof THREE.Particle ) {
  267. _vector4.set( _modelMatrix.elements[12], _modelMatrix.elements[13], _modelMatrix.elements[14], 1 );
  268. _vector4.applyMatrix4( _viewProjectionMatrix );
  269. _vector4.z /= _vector4.w;
  270. if ( _vector4.z > 0 && _vector4.z < 1 ) {
  271. _particle = getNextParticleInPool();
  272. _particle.object = object;
  273. _particle.x = _vector4.x / _vector4.w;
  274. _particle.y = _vector4.y / _vector4.w;
  275. _particle.z = _vector4.z;
  276. _particle.rotation = object.rotation.z;
  277. _particle.scale.x = object.scale.x * Math.abs( _particle.x - ( _vector4.x + camera.projectionMatrix.elements[0] ) / ( _vector4.w + camera.projectionMatrix.elements[12] ) );
  278. _particle.scale.y = object.scale.y * Math.abs( _particle.y - ( _vector4.y + camera.projectionMatrix.elements[5] ) / ( _vector4.w + camera.projectionMatrix.elements[13] ) );
  279. _particle.material = object.material;
  280. _renderData.elements.push( _particle );
  281. }
  282. }
  283. }
  284. if ( sortElements === true ) _renderData.elements.sort( painterSort );
  285. return _renderData;
  286. };
  287. // Pools
  288. function getNextObjectInPool() {
  289. if ( _objectCount === _objectPoolLength ) {
  290. var object = new THREE.RenderableObject();
  291. _objectPool.push( object );
  292. _objectPoolLength ++;
  293. _objectCount ++;
  294. return object;
  295. }
  296. return _objectPool[ _objectCount ++ ];
  297. }
  298. function getNextVertexInPool() {
  299. if ( _vertexCount === _vertexPoolLength ) {
  300. var vertex = new THREE.RenderableVertex();
  301. _vertexPool.push( vertex );
  302. _vertexPoolLength ++;
  303. _vertexCount ++;
  304. return vertex;
  305. }
  306. return _vertexPool[ _vertexCount ++ ];
  307. }
  308. function getNextFace3InPool() {
  309. if ( _face3Count === _face3PoolLength ) {
  310. var face = new THREE.RenderableFace3();
  311. _face3Pool.push( face );
  312. _face3PoolLength ++;
  313. _face3Count ++;
  314. return face;
  315. }
  316. return _face3Pool[ _face3Count ++ ];
  317. }
  318. function getNextFace4InPool() {
  319. if ( _face4Count === _face4PoolLength ) {
  320. var face = new THREE.RenderableFace4();
  321. _face4Pool.push( face );
  322. _face4PoolLength ++;
  323. _face4Count ++;
  324. return face;
  325. }
  326. return _face4Pool[ _face4Count ++ ];
  327. }
  328. function getNextLineInPool() {
  329. if ( _lineCount === _linePoolLength ) {
  330. var line = new THREE.RenderableLine();
  331. _linePool.push( line );
  332. _linePoolLength ++;
  333. _lineCount ++
  334. return line;
  335. }
  336. return _linePool[ _lineCount ++ ];
  337. }
  338. function getNextParticleInPool() {
  339. if ( _particleCount === _particlePoolLength ) {
  340. var particle = new THREE.RenderableParticle();
  341. _particlePool.push( particle );
  342. _particlePoolLength ++;
  343. _particleCount ++
  344. return particle;
  345. }
  346. return _particlePool[ _particleCount ++ ];
  347. }
  348. //
  349. function painterSort( a, b ) {
  350. return b.z - a.z;
  351. }
  352. function clipLine( s1, s2 ) {
  353. var alpha1 = 0, alpha2 = 1,
  354. // Calculate the boundary coordinate of each vertex for the near and far clip planes,
  355. // Z = -1 and Z = +1, respectively.
  356. bc1near = s1.z + s1.w,
  357. bc2near = s2.z + s2.w,
  358. bc1far = - s1.z + s1.w,
  359. bc2far = - s2.z + s2.w;
  360. if ( bc1near >= 0 && bc2near >= 0 && bc1far >= 0 && bc2far >= 0 ) {
  361. // Both vertices lie entirely within all clip planes.
  362. return true;
  363. } else if ( ( bc1near < 0 && bc2near < 0) || (bc1far < 0 && bc2far < 0 ) ) {
  364. // Both vertices lie entirely outside one of the clip planes.
  365. return false;
  366. } else {
  367. // The line segment spans at least one clip plane.
  368. if ( bc1near < 0 ) {
  369. // v1 lies outside the near plane, v2 inside
  370. alpha1 = Math.max( alpha1, bc1near / ( bc1near - bc2near ) );
  371. } else if ( bc2near < 0 ) {
  372. // v2 lies outside the near plane, v1 inside
  373. alpha2 = Math.min( alpha2, bc1near / ( bc1near - bc2near ) );
  374. }
  375. if ( bc1far < 0 ) {
  376. // v1 lies outside the far plane, v2 inside
  377. alpha1 = Math.max( alpha1, bc1far / ( bc1far - bc2far ) );
  378. } else if ( bc2far < 0 ) {
  379. // v2 lies outside the far plane, v2 inside
  380. alpha2 = Math.min( alpha2, bc1far / ( bc1far - bc2far ) );
  381. }
  382. if ( alpha2 < alpha1 ) {
  383. // The line segment spans two boundaries, but is outside both of them.
  384. // (This can't happen when we're only clipping against just near/far but good
  385. // to leave the check here for future usage if other clip planes are added.)
  386. return false;
  387. } else {
  388. // Update the s1 and s2 vertices to match the clipped line segment.
  389. s1.lerp( s2, alpha1 );
  390. s2.lerp( s1, 1 - alpha2 );
  391. return true;
  392. }
  393. }
  394. }
  395. };