Projector.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /**
  2. * @author mr.doob / 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 = [],
  8. _vertex, _vertexCount, _vertexPool = [],
  9. _face, _face3Count, _face3Pool = [], _face4Count, _face4Pool = [],
  10. _line, _lineCount, _linePool = [],
  11. _particle, _particleCount, _particlePool = [],
  12. _objectList = [], _renderList = [],
  13. _vector3 = new THREE.Vector4(),
  14. _vector4 = new THREE.Vector4(),
  15. _projScreenMatrix = new THREE.Matrix4(),
  16. _projScreenObjectMatrix = new THREE.Matrix4(),
  17. _frustum = [
  18. new THREE.Vector4(),
  19. new THREE.Vector4(),
  20. new THREE.Vector4(),
  21. new THREE.Vector4(),
  22. new THREE.Vector4(),
  23. new THREE.Vector4()
  24. ],
  25. _clippedVertex1PositionScreen = new THREE.Vector4(),
  26. _clippedVertex2PositionScreen = new THREE.Vector4(),
  27. _face3VertexNormals;
  28. this.projectVector = function ( vector, camera ) {
  29. _projScreenMatrix.multiply( camera.projectionMatrix, camera.matrixWorldInverse );
  30. _projScreenMatrix.multiplyVector3( vector );
  31. return vector;
  32. };
  33. this.unprojectVector = function ( vector, camera ) {
  34. _projScreenMatrix.multiply( camera.matrixWorld, THREE.Matrix4.makeInvert( camera.projectionMatrix ) );
  35. _projScreenMatrix.multiplyVector3( vector );
  36. return vector;
  37. };
  38. /**
  39. * Translates a 2D point from NDC to a THREE.Ray
  40. * that can be used for picking.
  41. * @vector - THREE.Vector3 that represents 2D point
  42. * @camera - THREE.Camera
  43. */
  44. this.pickingRay = function ( vector, camera ) {
  45. var end, ray, t;
  46. // set two vectors with opposing z values
  47. vector.z = -1.0;
  48. end = new THREE.Vector3( vector.x, vector.y, 1.0 );
  49. this.unprojectVector( vector, camera );
  50. this.unprojectVector( end, camera );
  51. // find direction from vector to end
  52. end.subSelf( vector ).normalize();
  53. return new THREE.Ray( vector, end );
  54. };
  55. this.projectObjects = function ( scene, camera, sort ) {
  56. var o, ol, objects, object, matrix;
  57. _objectList.length = 0;
  58. _objectCount = 0;
  59. objects = scene.objects;
  60. for ( o = 0, ol = objects.length; o < ol; o ++ ) {
  61. object = objects[ o ];
  62. if ( !object.visible || ( object instanceof THREE.Mesh && ( object.frustumCulled && !isInFrustum( object ) ) ) ) continue;
  63. _object = getNextObjectInPool();
  64. _vector3.copy( object.position );
  65. _projScreenMatrix.multiplyVector3( _vector3 );
  66. _object.object = object;
  67. _object.z = _vector3.z;
  68. _objectList.push( _object );
  69. }
  70. sort && _objectList.sort( painterSort );
  71. return _objectList;
  72. };
  73. // TODO: Rename to projectElements?
  74. this.projectScene = function ( scene, camera, sort ) {
  75. var near = camera.near, far = camera.far,
  76. o, ol, v, vl, f, fl, n, nl, c, cl, u, ul, objects, object,
  77. objectMatrix, objectMatrixRotation, objectMaterials, objectOverdraw,
  78. geometry, vertices, vertex, vertexPositionScreen,
  79. faces, face, faceVertexNormals, normal, faceVertexUvs, uvs,
  80. v1, v2, v3, v4;
  81. _renderList.length = 0;
  82. _face3Count = 0;
  83. _face4Count = 0;
  84. _lineCount = 0;
  85. _particleCount = 0;
  86. camera.matrixAutoUpdate && camera.update( undefined, true );
  87. scene.update( undefined, false, camera );
  88. _projScreenMatrix.multiply( camera.projectionMatrix, camera.matrixWorldInverse );
  89. computeFrustum( _projScreenMatrix );
  90. objects = this.projectObjects( scene, camera, true );
  91. for ( o = 0, ol = objects.length; o < ol; o++ ) {
  92. object = objects[ o ].object;
  93. if ( !object.visible ) continue;
  94. objectMatrix = object.matrixWorld;
  95. objectMatrixRotation = object.matrixRotationWorld;
  96. objectMaterials = object.materials;
  97. objectOverdraw = object.overdraw;
  98. _vertexCount = 0;
  99. if ( object instanceof THREE.Mesh ) {
  100. geometry = object.geometry;
  101. vertices = geometry.vertices;
  102. faces = geometry.faces;
  103. faceVertexUvs = geometry.faceVertexUvs;
  104. for ( v = 0, vl = vertices.length; v < vl; v ++ ) {
  105. _vertex = getNextVertexInPool();
  106. _vertex.positionWorld.copy( vertices[ v ].position );
  107. objectMatrix.multiplyVector3( _vertex.positionWorld );
  108. _vertex.positionScreen.copy( _vertex.positionWorld );
  109. _projScreenMatrix.multiplyVector4( _vertex.positionScreen );
  110. _vertex.positionScreen.x /= _vertex.positionScreen.w;
  111. _vertex.positionScreen.y /= _vertex.positionScreen.w;
  112. _vertex.visible = _vertex.positionScreen.z > near && _vertex.positionScreen.z < far;
  113. }
  114. for ( f = 0, fl = faces.length; f < fl; f ++ ) {
  115. face = faces[ f ];
  116. if ( face instanceof THREE.Face3 ) {
  117. v1 = _vertexPool[ face.a ];
  118. v2 = _vertexPool[ face.b ];
  119. v3 = _vertexPool[ face.c ];
  120. if ( v1.visible && v2.visible && v3.visible &&
  121. ( object.doubleSided || ( object.flipSided !=
  122. ( v3.positionScreen.x - v1.positionScreen.x ) * ( v2.positionScreen.y - v1.positionScreen.y ) -
  123. ( v3.positionScreen.y - v1.positionScreen.y ) * ( v2.positionScreen.x - v1.positionScreen.x ) < 0 ) ) ) {
  124. _face = getNextFace3InPool();
  125. _face.v1.copy( v1 );
  126. _face.v2.copy( v2 );
  127. _face.v3.copy( v3 );
  128. } else {
  129. continue;
  130. }
  131. } else if ( face instanceof THREE.Face4 ) {
  132. v1 = _vertexPool[ face.a ];
  133. v2 = _vertexPool[ face.b ];
  134. v3 = _vertexPool[ face.c ];
  135. v4 = _vertexPool[ face.d ];
  136. if ( v1.visible && v2.visible && v3.visible && v4.visible &&
  137. ( object.doubleSided || ( object.flipSided !=
  138. ( ( v4.positionScreen.x - v1.positionScreen.x ) * ( v2.positionScreen.y - v1.positionScreen.y ) -
  139. ( v4.positionScreen.y - v1.positionScreen.y ) * ( v2.positionScreen.x - v1.positionScreen.x ) < 0 ||
  140. ( v2.positionScreen.x - v3.positionScreen.x ) * ( v4.positionScreen.y - v3.positionScreen.y ) -
  141. ( v2.positionScreen.y - v3.positionScreen.y ) * ( v4.positionScreen.x - v3.positionScreen.x ) < 0 ) ) ) ) {
  142. _face = getNextFace4InPool();
  143. _face.v1.copy( v1 );
  144. _face.v2.copy( v2 );
  145. _face.v3.copy( v3 );
  146. _face.v4.copy( v4 );
  147. } else {
  148. continue;
  149. }
  150. }
  151. _face.normalWorld.copy( face.normal );
  152. objectMatrixRotation.multiplyVector3( _face.normalWorld );
  153. _face.centroidWorld.copy( face.centroid );
  154. objectMatrix.multiplyVector3( _face.centroidWorld );
  155. _face.centroidScreen.copy( _face.centroidWorld );
  156. _projScreenMatrix.multiplyVector3( _face.centroidScreen );
  157. faceVertexNormals = face.vertexNormals;
  158. for ( n = 0, nl = faceVertexNormals.length; n < nl; n ++ ) {
  159. normal = _face.vertexNormalsWorld[ n ];
  160. normal.copy( faceVertexNormals[ n ] );
  161. objectMatrixRotation.multiplyVector3( normal );
  162. }
  163. for ( c = 0, cl = faceVertexUvs.length; c < cl; c ++ ) {
  164. uvs = faceVertexUvs[ c ][ f ];
  165. if ( !uvs ) continue;
  166. for ( u = 0, ul = uvs.length; u < ul; u ++ ) {
  167. _face.uvs[ c ][ u ] = uvs[ u ];
  168. }
  169. }
  170. _face.meshMaterials = objectMaterials;
  171. _face.faceMaterials = face.materials;
  172. _face.overdraw = objectOverdraw;
  173. _face.z = _face.centroidScreen.z;
  174. _renderList.push( _face );
  175. }
  176. } else if ( object instanceof THREE.Line ) {
  177. _projScreenObjectMatrix.multiply( _projScreenMatrix, objectMatrix );
  178. vertices = object.geometry.vertices;
  179. v1 = getNextVertexInPool();
  180. v1.positionScreen.copy( vertices[ 0 ].position );
  181. _projScreenObjectMatrix.multiplyVector4( v1.positionScreen );
  182. for ( v = 1, vl = vertices.length; v < vl; v++ ) {
  183. v1 = getNextVertexInPool();
  184. v1.positionScreen.copy( vertices[ v ].position );
  185. _projScreenObjectMatrix.multiplyVector4( v1.positionScreen );
  186. v2 = _vertexPool[ _vertexCount - 2 ];
  187. _clippedVertex1PositionScreen.copy( v1.positionScreen );
  188. _clippedVertex2PositionScreen.copy( v2.positionScreen );
  189. if ( clipLine( _clippedVertex1PositionScreen, _clippedVertex2PositionScreen ) ) {
  190. // Perform the perspective divide
  191. _clippedVertex1PositionScreen.multiplyScalar( 1 / _clippedVertex1PositionScreen.w );
  192. _clippedVertex2PositionScreen.multiplyScalar( 1 / _clippedVertex2PositionScreen.w );
  193. _line = getNextLineInPool();
  194. _line.v1.positionScreen.copy( _clippedVertex1PositionScreen );
  195. _line.v2.positionScreen.copy( _clippedVertex2PositionScreen );
  196. _line.z = Math.max( _clippedVertex1PositionScreen.z, _clippedVertex2PositionScreen.z );
  197. _line.materials = object.materials;
  198. _renderList.push( _line );
  199. }
  200. }
  201. } else if ( object instanceof THREE.Particle ) {
  202. _vector4.set( object.matrixWorld.n14, object.matrixWorld.n24, object.matrixWorld.n34, 1 );
  203. _projScreenMatrix.multiplyVector4( _vector4 );
  204. _vector4.z /= _vector4.w;
  205. if ( _vector4.z > 0 && _vector4.z < 1 ) {
  206. _particle = getNextParticleInPool();
  207. _particle.x = _vector4.x / _vector4.w;
  208. _particle.y = _vector4.y / _vector4.w;
  209. _particle.z = _vector4.z;
  210. _particle.rotation = object.rotation.z;
  211. _particle.scale.x = object.scale.x * Math.abs( _particle.x - ( _vector4.x + camera.projectionMatrix.n11 ) / ( _vector4.w + camera.projectionMatrix.n14 ) );
  212. _particle.scale.y = object.scale.y * Math.abs( _particle.y - ( _vector4.y + camera.projectionMatrix.n22 ) / ( _vector4.w + camera.projectionMatrix.n24 ) );
  213. _particle.materials = object.materials;
  214. _renderList.push( _particle );
  215. }
  216. }
  217. }
  218. sort && _renderList.sort( painterSort );
  219. return _renderList;
  220. };
  221. // Pools
  222. function getNextObjectInPool() {
  223. var object = _objectPool[ _objectCount ] = _objectPool[ _objectCount ] || new THREE.RenderableObject();
  224. _objectCount ++;
  225. return object;
  226. }
  227. function getNextVertexInPool() {
  228. var vertex = _vertexPool[ _vertexCount ] = _vertexPool[ _vertexCount ] || new THREE.RenderableVertex();
  229. _vertexCount ++;
  230. return vertex;
  231. }
  232. function getNextFace3InPool() {
  233. var face = _face3Pool[ _face3Count ] = _face3Pool[ _face3Count ] || new THREE.RenderableFace3();
  234. _face3Count ++;
  235. return face;
  236. }
  237. function getNextFace4InPool() {
  238. var face = _face4Pool[ _face4Count ] = _face4Pool[ _face4Count ] || new THREE.RenderableFace4();
  239. _face4Count ++;
  240. return face;
  241. }
  242. function getNextLineInPool() {
  243. var line = _linePool[ _lineCount ] = _linePool[ _lineCount ] || new THREE.RenderableLine();
  244. _lineCount ++;
  245. return line;
  246. }
  247. function getNextParticleInPool() {
  248. var particle = _particlePool[ _particleCount ] = _particlePool[ _particleCount ] || new THREE.RenderableParticle();
  249. _particleCount ++;
  250. return particle;
  251. }
  252. //
  253. function painterSort( a, b ) {
  254. return b.z - a.z;
  255. }
  256. function computeFrustum( m ) {
  257. _frustum[ 0 ].set( m.n41 - m.n11, m.n42 - m.n12, m.n43 - m.n13, m.n44 - m.n14 );
  258. _frustum[ 1 ].set( m.n41 + m.n11, m.n42 + m.n12, m.n43 + m.n13, m.n44 + m.n14 );
  259. _frustum[ 2 ].set( m.n41 + m.n21, m.n42 + m.n22, m.n43 + m.n23, m.n44 + m.n24 );
  260. _frustum[ 3 ].set( m.n41 - m.n21, m.n42 - m.n22, m.n43 - m.n23, m.n44 - m.n24 );
  261. _frustum[ 4 ].set( m.n41 - m.n31, m.n42 - m.n32, m.n43 - m.n33, m.n44 - m.n34 );
  262. _frustum[ 5 ].set( m.n41 + m.n31, m.n42 + m.n32, m.n43 + m.n33, m.n44 + m.n34 );
  263. for ( var i = 0; i < 6; i ++ ) {
  264. var plane = _frustum[ i ];
  265. plane.divideScalar( Math.sqrt( plane.x * plane.x + plane.y * plane.y + plane.z * plane.z ) );
  266. }
  267. }
  268. function isInFrustum( object ) {
  269. var distance, matrix = object.matrixWorld,
  270. radius = - object.geometry.boundingSphere.radius * Math.max( object.scale.x, Math.max( object.scale.y, object.scale.z ) );
  271. for ( var i = 0; i < 6; i ++ ) {
  272. distance = _frustum[ i ].x * matrix.n14 + _frustum[ i ].y * matrix.n24 + _frustum[ i ].z * matrix.n34 + _frustum[ i ].w;
  273. if ( distance <= radius ) return false;
  274. }
  275. return true;
  276. };
  277. function clipLine( s1, s2 ) {
  278. var alpha1 = 0, alpha2 = 1,
  279. // Calculate the boundary coordinate of each vertex for the near and far clip planes,
  280. // Z = -1 and Z = +1, respectively.
  281. bc1near = s1.z + s1.w,
  282. bc2near = s2.z + s2.w,
  283. bc1far = - s1.z + s1.w,
  284. bc2far = - s2.z + s2.w;
  285. if ( bc1near >= 0 && bc2near >= 0 && bc1far >= 0 && bc2far >= 0 ) {
  286. // Both vertices lie entirely within all clip planes.
  287. return true;
  288. } else if ( ( bc1near < 0 && bc2near < 0) || (bc1far < 0 && bc2far < 0 ) ) {
  289. // Both vertices lie entirely outside one of the clip planes.
  290. return false;
  291. } else {
  292. // The line segment spans at least one clip plane.
  293. if ( bc1near < 0 ) {
  294. // v1 lies outside the near plane, v2 inside
  295. alpha1 = Math.max( alpha1, bc1near / ( bc1near - bc2near ) );
  296. } else if ( bc2near < 0 ) {
  297. // v2 lies outside the near plane, v1 inside
  298. alpha2 = Math.min( alpha2, bc1near / ( bc1near - bc2near ) );
  299. }
  300. if ( bc1far < 0 ) {
  301. // v1 lies outside the far plane, v2 inside
  302. alpha1 = Math.max( alpha1, bc1far / ( bc1far - bc2far ) );
  303. } else if ( bc2far < 0 ) {
  304. // v2 lies outside the far plane, v2 inside
  305. alpha2 = Math.min( alpha2, bc1far / ( bc1far - bc2far ) );
  306. }
  307. if ( alpha2 < alpha1 ) {
  308. // The line segment spans two boundaries, but is outside both of them.
  309. // (This can't happen when we're only clipping against just near/far but good
  310. // to leave the check here for future usage if other clip planes are added.)
  311. return false;
  312. } else {
  313. // Update the s1 and s2 vertices to match the clipped line segment.
  314. s1.lerpSelf( s2, alpha1 );
  315. s2.lerpSelf( s1, 1 - alpha2 );
  316. return true;
  317. }
  318. }
  319. }
  320. };