Projector.js 15 KB

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