Projector.js 16 KB

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