Projector.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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. var invW = 1 / _vertex.positionScreen.w;
  122. _vertex.positionScreen.x *= invW;
  123. _vertex.positionScreen.y *= invW;
  124. _vertex.positionScreen.z *= invW;
  125. _vertex.visible = ! ( _vertex.positionScreen.x < -1 || _vertex.positionScreen.x > 1 ||
  126. _vertex.positionScreen.y < -1 || _vertex.positionScreen.y > 1 ||
  127. _vertex.positionScreen.z < -1 || _vertex.positionScreen.z > 1 );
  128. }
  129. for ( f = 0, fl = faces.length; f < fl; f ++ ) {
  130. face = faces[ f ];
  131. var material = isFaceMaterial === true
  132. ? objectMaterials.materials[ face.materialIndex ]
  133. : object.material;
  134. if ( material === undefined ) continue;
  135. var side = material.side;
  136. if ( face instanceof THREE.Face3 ) {
  137. v1 = _vertexPool[ face.a ];
  138. v2 = _vertexPool[ face.b ];
  139. v3 = _vertexPool[ face.c ];
  140. _points3[ 0 ] = v1.positionScreen;
  141. _points3[ 1 ] = v2.positionScreen;
  142. _points3[ 2 ] = v3.positionScreen;
  143. if ( v1.visible === true || v2.visible === true || v3.visible === true ||
  144. _clipBox.isIntersectionBox( _boundingBox.setFromPoints( _points3 ) ) ) {
  145. visible = ( ( v3.positionScreen.x - v1.positionScreen.x ) * ( v2.positionScreen.y - v1.positionScreen.y ) -
  146. ( v3.positionScreen.y - v1.positionScreen.y ) * ( v2.positionScreen.x - v1.positionScreen.x ) ) < 0;
  147. if ( side === THREE.DoubleSide || visible === ( side === THREE.FrontSide ) ) {
  148. _face = getNextFace3InPool();
  149. _face.id = object.id;
  150. _face.v1.copy( v1 );
  151. _face.v2.copy( v2 );
  152. _face.v3.copy( v3 );
  153. } else {
  154. continue;
  155. }
  156. } else {
  157. continue;
  158. }
  159. } else if ( face instanceof THREE.Face4 ) {
  160. v1 = _vertexPool[ face.a ];
  161. v2 = _vertexPool[ face.b ];
  162. v3 = _vertexPool[ face.c ];
  163. v4 = _vertexPool[ face.d ];
  164. _points4[ 0 ] = v1.positionScreen;
  165. _points4[ 1 ] = v2.positionScreen;
  166. _points4[ 2 ] = v3.positionScreen;
  167. _points4[ 3 ] = v4.positionScreen;
  168. if ( v1.visible === true || v2.visible === true || v3.visible === true || v4.visible === true ||
  169. _clipBox.isIntersectionBox( _boundingBox.setFromPoints( _points4 ) ) ) {
  170. visible = ( v4.positionScreen.x - v1.positionScreen.x ) * ( v2.positionScreen.y - v1.positionScreen.y ) -
  171. ( v4.positionScreen.y - v1.positionScreen.y ) * ( v2.positionScreen.x - v1.positionScreen.x ) < 0 ||
  172. ( v2.positionScreen.x - v3.positionScreen.x ) * ( v4.positionScreen.y - v3.positionScreen.y ) -
  173. ( v2.positionScreen.y - v3.positionScreen.y ) * ( v4.positionScreen.x - v3.positionScreen.x ) < 0;
  174. if ( side === THREE.DoubleSide || visible === ( side === THREE.FrontSide ) ) {
  175. _face = getNextFace4InPool();
  176. _face.id = object.id;
  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.normalModel.copy( face.normal );
  189. if ( visible === false && ( side === THREE.BackSide || side === THREE.DoubleSide ) ) {
  190. _face.normalModel.negate();
  191. }
  192. _face.normalModel.applyMatrix3( _normalMatrix ).normalize();
  193. _face.normalModelView.copy( _face.normalModel ).applyMatrix3( _normalViewMatrix );
  194. _face.centroidModel.copy( face.centroid ).applyMatrix4( _modelMatrix );
  195. faceVertexNormals = face.vertexNormals;
  196. for ( n = 0, nl = faceVertexNormals.length; n < nl; n ++ ) {
  197. var normalModel = _face.vertexNormalsModel[ n ];
  198. normalModel.copy( faceVertexNormals[ n ] );
  199. if ( visible === false && ( side === THREE.BackSide || side === THREE.DoubleSide ) ) {
  200. normalModel.negate();
  201. }
  202. normalModel.applyMatrix3( _normalMatrix ).normalize();
  203. var normalModelView = _face.vertexNormalsModelView[ n ];
  204. normalModelView.copy( normalModel ).applyMatrix3( _normalViewMatrix );
  205. }
  206. _face.vertexNormalsLength = faceVertexNormals.length;
  207. for ( c = 0, cl = faceVertexUvs.length; c < cl; c ++ ) {
  208. uvs = faceVertexUvs[ c ][ f ];
  209. if ( uvs === undefined ) continue;
  210. for ( u = 0, ul = uvs.length; u < ul; u ++ ) {
  211. _face.uvs[ c ][ u ] = uvs[ u ];
  212. }
  213. }
  214. _face.color = face.color;
  215. _face.material = material;
  216. _centroid.copy( _face.centroidModel ).applyProjection( _viewProjectionMatrix );
  217. _face.z = _centroid.z;
  218. _renderData.elements.push( _face );
  219. }
  220. } else if ( object instanceof THREE.Line ) {
  221. _modelViewProjectionMatrix.multiplyMatrices( _viewProjectionMatrix, _modelMatrix );
  222. vertices = object.geometry.vertices;
  223. v1 = getNextVertexInPool();
  224. v1.positionScreen.copy( vertices[ 0 ] ).applyMatrix4( _modelViewProjectionMatrix );
  225. // Handle LineStrip and LinePieces
  226. var step = object.type === THREE.LinePieces ? 2 : 1;
  227. for ( v = 1, vl = vertices.length; v < vl; v ++ ) {
  228. v1 = getNextVertexInPool();
  229. v1.positionScreen.copy( vertices[ v ] ).applyMatrix4( _modelViewProjectionMatrix );
  230. if ( ( v + 1 ) % step > 0 ) continue;
  231. v2 = _vertexPool[ _vertexCount - 2 ];
  232. _clippedVertex1PositionScreen.copy( v1.positionScreen );
  233. _clippedVertex2PositionScreen.copy( v2.positionScreen );
  234. if ( clipLine( _clippedVertex1PositionScreen, _clippedVertex2PositionScreen ) === true ) {
  235. // Perform the perspective divide
  236. _clippedVertex1PositionScreen.multiplyScalar( 1 / _clippedVertex1PositionScreen.w );
  237. _clippedVertex2PositionScreen.multiplyScalar( 1 / _clippedVertex2PositionScreen.w );
  238. _line = getNextLineInPool();
  239. _line.id = object.id;
  240. _line.v1.positionScreen.copy( _clippedVertex1PositionScreen );
  241. _line.v2.positionScreen.copy( _clippedVertex2PositionScreen );
  242. _line.z = Math.max( _clippedVertex1PositionScreen.z, _clippedVertex2PositionScreen.z );
  243. _line.material = object.material;
  244. if ( object.material.vertexColors === THREE.VertexColors ) {
  245. _line.vertexColors[ 0 ].copy( object.geometry.colors[ v ] );
  246. _line.vertexColors[ 1 ].copy( object.geometry.colors[ v - 1 ] );
  247. }
  248. _renderData.elements.push( _line );
  249. }
  250. }
  251. }
  252. }
  253. for ( o = 0, ol = _renderData.sprites.length; o < ol; o++ ) {
  254. object = _renderData.sprites[ o ].object;
  255. _modelMatrix = object.matrixWorld;
  256. if ( object instanceof THREE.Particle ) {
  257. _vector4.set( _modelMatrix.elements[12], _modelMatrix.elements[13], _modelMatrix.elements[14], 1 );
  258. _vector4.applyMatrix4( _viewProjectionMatrix );
  259. var invW = 1 / _vector4.w;
  260. _vector4.z *= invW;
  261. if ( _vector4.z > 0 && _vector4.z < 1 ) {
  262. _particle = getNextParticleInPool();
  263. _particle.id = object.id;
  264. _particle.x = _vector4.x * invW;
  265. _particle.y = _vector4.y * invW;
  266. _particle.z = _vector4.z;
  267. _particle.object = object;
  268. _particle.rotation = object.rotation.z;
  269. _particle.scale.x = object.scale.x * Math.abs( _particle.x - ( _vector4.x + camera.projectionMatrix.elements[0] ) / ( _vector4.w + camera.projectionMatrix.elements[12] ) );
  270. _particle.scale.y = object.scale.y * Math.abs( _particle.y - ( _vector4.y + camera.projectionMatrix.elements[5] ) / ( _vector4.w + camera.projectionMatrix.elements[13] ) );
  271. _particle.material = object.material;
  272. _renderData.elements.push( _particle );
  273. }
  274. }
  275. }
  276. if ( sortElements === true ) _renderData.elements.sort( painterSort );
  277. return _renderData;
  278. };
  279. // Pools
  280. function getNextObjectInPool() {
  281. if ( _objectCount === _objectPoolLength ) {
  282. var object = new THREE.RenderableObject();
  283. _objectPool.push( object );
  284. _objectPoolLength ++;
  285. _objectCount ++;
  286. return object;
  287. }
  288. return _objectPool[ _objectCount ++ ];
  289. }
  290. function getNextVertexInPool() {
  291. if ( _vertexCount === _vertexPoolLength ) {
  292. var vertex = new THREE.RenderableVertex();
  293. _vertexPool.push( vertex );
  294. _vertexPoolLength ++;
  295. _vertexCount ++;
  296. return vertex;
  297. }
  298. return _vertexPool[ _vertexCount ++ ];
  299. }
  300. function getNextFace3InPool() {
  301. if ( _face3Count === _face3PoolLength ) {
  302. var face = new THREE.RenderableFace3();
  303. _face3Pool.push( face );
  304. _face3PoolLength ++;
  305. _face3Count ++;
  306. return face;
  307. }
  308. return _face3Pool[ _face3Count ++ ];
  309. }
  310. function getNextFace4InPool() {
  311. if ( _face4Count === _face4PoolLength ) {
  312. var face = new THREE.RenderableFace4();
  313. _face4Pool.push( face );
  314. _face4PoolLength ++;
  315. _face4Count ++;
  316. return face;
  317. }
  318. return _face4Pool[ _face4Count ++ ];
  319. }
  320. function getNextLineInPool() {
  321. if ( _lineCount === _linePoolLength ) {
  322. var line = new THREE.RenderableLine();
  323. _linePool.push( line );
  324. _linePoolLength ++;
  325. _lineCount ++
  326. return line;
  327. }
  328. return _linePool[ _lineCount ++ ];
  329. }
  330. function getNextParticleInPool() {
  331. if ( _particleCount === _particlePoolLength ) {
  332. var particle = new THREE.RenderableParticle();
  333. _particlePool.push( particle );
  334. _particlePoolLength ++;
  335. _particleCount ++
  336. return particle;
  337. }
  338. return _particlePool[ _particleCount ++ ];
  339. }
  340. //
  341. function painterSort( a, b ) {
  342. if ( a.z !== b.z ) {
  343. return b.z - a.z;
  344. } else if ( a.id !== b.id ) {
  345. return a.id - b.id;
  346. } else {
  347. return 0;
  348. }
  349. }
  350. function clipLine( s1, s2 ) {
  351. var alpha1 = 0, alpha2 = 1,
  352. // Calculate the boundary coordinate of each vertex for the near and far clip planes,
  353. // Z = -1 and Z = +1, respectively.
  354. bc1near = s1.z + s1.w,
  355. bc2near = s2.z + s2.w,
  356. bc1far = - s1.z + s1.w,
  357. bc2far = - s2.z + s2.w;
  358. if ( bc1near >= 0 && bc2near >= 0 && bc1far >= 0 && bc2far >= 0 ) {
  359. // Both vertices lie entirely within all clip planes.
  360. return true;
  361. } else if ( ( bc1near < 0 && bc2near < 0) || (bc1far < 0 && bc2far < 0 ) ) {
  362. // Both vertices lie entirely outside one of the clip planes.
  363. return false;
  364. } else {
  365. // The line segment spans at least one clip plane.
  366. if ( bc1near < 0 ) {
  367. // v1 lies outside the near plane, v2 inside
  368. alpha1 = Math.max( alpha1, bc1near / ( bc1near - bc2near ) );
  369. } else if ( bc2near < 0 ) {
  370. // v2 lies outside the near plane, v1 inside
  371. alpha2 = Math.min( alpha2, bc1near / ( bc1near - bc2near ) );
  372. }
  373. if ( bc1far < 0 ) {
  374. // v1 lies outside the far plane, v2 inside
  375. alpha1 = Math.max( alpha1, bc1far / ( bc1far - bc2far ) );
  376. } else if ( bc2far < 0 ) {
  377. // v2 lies outside the far plane, v2 inside
  378. alpha2 = Math.min( alpha2, bc1far / ( bc1far - bc2far ) );
  379. }
  380. if ( alpha2 < alpha1 ) {
  381. // The line segment spans two boundaries, but is outside both of them.
  382. // (This can't happen when we're only clipping against just near/far but good
  383. // to leave the check here for future usage if other clip planes are added.)
  384. return false;
  385. } else {
  386. // Update the s1 and s2 vertices to match the clipped line segment.
  387. s1.lerp( s2, alpha1 );
  388. s2.lerp( s1, 1 - alpha2 );
  389. return true;
  390. }
  391. }
  392. }
  393. };
粤ICP备19079148号