Projector.js 16 KB

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