Projector.js 14 KB

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