Projector.js 13 KB

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