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. _line, _lineCount, _linePool = [], _linePoolLength = 0,
  11. _sprite, _spriteCount, _spritePool = [], _spritePoolLength = 0,
  12. _renderData = { objects: [], sprites: [], lights: [], elements: [] },
  13. _vA = new THREE.Vector3(),
  14. _vB = new THREE.Vector3(),
  15. _vC = new THREE.Vector3(),
  16. _vector3 = new THREE.Vector3(),
  17. _vector4 = new THREE.Vector4(),
  18. _clipBox = new THREE.Box3( new THREE.Vector3( -1, -1, -1 ), new THREE.Vector3( 1, 1, 1 ) ),
  19. _boundingBox = new THREE.Box3(),
  20. _points3 = new Array( 3 ),
  21. _points4 = new Array( 4 ),
  22. _viewMatrix = new THREE.Matrix4(),
  23. _viewProjectionMatrix = new THREE.Matrix4(),
  24. _modelMatrix,
  25. _modelViewProjectionMatrix = new THREE.Matrix4(),
  26. _normalMatrix = new THREE.Matrix3(),
  27. _normalViewMatrix = new THREE.Matrix3(),
  28. _centroid = new THREE.Vector3(),
  29. _frustum = new THREE.Frustum(),
  30. _clippedVertex1PositionScreen = new THREE.Vector4(),
  31. _clippedVertex2PositionScreen = new THREE.Vector4();
  32. this.projectVector = function ( vector, camera ) {
  33. camera.matrixWorldInverse.getInverse( camera.matrixWorld );
  34. _viewProjectionMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
  35. return vector.applyProjection( _viewProjectionMatrix );
  36. };
  37. this.unprojectVector = function () {
  38. var projectionMatrixInverse = new THREE.Matrix4();
  39. return function ( vector, camera ) {
  40. projectionMatrixInverse.getInverse( camera.projectionMatrix );
  41. _viewProjectionMatrix.multiplyMatrices( camera.matrixWorld, projectionMatrixInverse );
  42. return vector.applyProjection( _viewProjectionMatrix );
  43. };
  44. }();
  45. this.pickingRay = function ( vector, camera ) {
  46. // set two vectors with opposing z values
  47. vector.z = -1.0;
  48. var 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.sub( vector ).normalize();
  53. return new THREE.Raycaster( vector, end );
  54. };
  55. var getObject = function ( object ) {
  56. _object = getNextObjectInPool();
  57. _object.id = object.id;
  58. _object.object = object;
  59. if ( object.renderDepth !== null ) {
  60. _object.z = object.renderDepth;
  61. } else {
  62. _vector3.setFromMatrixPosition( object.matrixWorld );
  63. _vector3.applyProjection( _viewProjectionMatrix );
  64. _object.z = _vector3.z;
  65. }
  66. return _object;
  67. };
  68. var projectVertex = function ( vertex ) {
  69. var position = vertex.position;
  70. var positionWorld = vertex.positionWorld;
  71. var positionScreen = vertex.positionScreen;
  72. positionWorld.copy( position ).applyMatrix4( _modelMatrix );
  73. positionScreen.copy( positionWorld ).applyMatrix4( _viewProjectionMatrix );
  74. var invW = 1 / positionScreen.w;
  75. positionScreen.x *= invW;
  76. positionScreen.y *= invW;
  77. positionScreen.z *= invW;
  78. vertex.visible = positionScreen.x >= -1 && positionScreen.x <= 1 &&
  79. positionScreen.y >= -1 && positionScreen.y <= 1 &&
  80. positionScreen.z >= -1 && positionScreen.z <= 1;
  81. };
  82. var projectObject = function ( object ) {
  83. if ( object.visible === false ) return;
  84. if ( object instanceof THREE.Light ) {
  85. _renderData.lights.push( object );
  86. } else if ( object instanceof THREE.Mesh || object instanceof THREE.Line ) {
  87. if ( object.frustumCulled === false || _frustum.intersectsObject( object ) === true ) {
  88. _renderData.objects.push( getObject( object ) );
  89. }
  90. } else if ( object instanceof THREE.Sprite ) {
  91. _renderData.sprites.push( getObject( object ) );
  92. }
  93. for ( var i = 0, l = object.children.length; i < l; i ++ ) {
  94. projectObject( object.children[ i ] );
  95. }
  96. };
  97. var projectGraph = function ( root, sortObjects ) {
  98. _objectCount = 0;
  99. _renderData.objects.length = 0;
  100. _renderData.sprites.length = 0;
  101. _renderData.lights.length = 0;
  102. projectObject( root );
  103. if ( sortObjects === true ) {
  104. _renderData.objects.sort( painterSort );
  105. }
  106. };
  107. this.projectScene = function ( scene, camera, sortObjects, sortElements ) {
  108. var visible = false,
  109. object, geometry, vertices, faces, face, faceVertexNormals, faceVertexUvs, uvs,
  110. v1, v2, v3, v4, isFaceMaterial, objectMaterials;
  111. _face3Count = 0;
  112. _lineCount = 0;
  113. _spriteCount = 0;
  114. _renderData.elements.length = 0;
  115. if ( scene.autoUpdate === true ) scene.updateMatrixWorld();
  116. if ( camera.parent === undefined ) camera.updateMatrixWorld();
  117. _viewMatrix.copy( camera.matrixWorldInverse.getInverse( camera.matrixWorld ) );
  118. _viewProjectionMatrix.multiplyMatrices( camera.projectionMatrix, _viewMatrix );
  119. _normalViewMatrix.getNormalMatrix( _viewMatrix );
  120. _frustum.setFromMatrix( _viewProjectionMatrix );
  121. projectGraph( scene, sortObjects );
  122. for ( var o = 0, ol = _renderData.objects.length; o < ol; o ++ ) {
  123. object = _renderData.objects[ o ].object;
  124. _modelMatrix = object.matrixWorld;
  125. _vertexCount = 0;
  126. if ( object instanceof THREE.Mesh ) {
  127. geometry = object.geometry;
  128. vertices = geometry.vertices;
  129. faces = geometry.faces;
  130. faceVertexUvs = geometry.faceVertexUvs;
  131. _normalMatrix.getNormalMatrix( _modelMatrix );
  132. isFaceMaterial = object.material instanceof THREE.MeshFaceMaterial;
  133. objectMaterials = isFaceMaterial === true ? object.material : null;
  134. for ( var v = 0, vl = vertices.length; v < vl; v ++ ) {
  135. _vertex = getNextVertexInPool();
  136. _vertex.position.copy( vertices[ v ] );
  137. projectVertex( _vertex );
  138. }
  139. for ( var f = 0, fl = faces.length; f < fl; f ++ ) {
  140. face = faces[ f ];
  141. var material = isFaceMaterial === true
  142. ? objectMaterials.materials[ face.materialIndex ]
  143. : object.material;
  144. if ( material === undefined ) continue;
  145. var side = material.side;
  146. v1 = _vertexPool[ face.a ];
  147. v2 = _vertexPool[ face.b ];
  148. v3 = _vertexPool[ face.c ];
  149. if ( material.morphTargets === true ) {
  150. var morphTargets = geometry.morphTargets;
  151. var morphInfluences = object.morphTargetInfluences;
  152. var v1p = v1.position;
  153. var v2p = v2.position;
  154. var v3p = v3.position;
  155. _vA.set( 0, 0, 0 );
  156. _vB.set( 0, 0, 0 );
  157. _vC.set( 0, 0, 0 );
  158. for ( var t = 0, tl = morphTargets.length; t < tl; t ++ ) {
  159. var influence = morphInfluences[ t ];
  160. if ( influence === 0 ) continue;
  161. var targets = morphTargets[ t ].vertices;
  162. _vA.x += ( targets[ face.a ].x - v1p.x ) * influence;
  163. _vA.y += ( targets[ face.a ].y - v1p.y ) * influence;
  164. _vA.z += ( targets[ face.a ].z - v1p.z ) * influence;
  165. _vB.x += ( targets[ face.b ].x - v2p.x ) * influence;
  166. _vB.y += ( targets[ face.b ].y - v2p.y ) * influence;
  167. _vB.z += ( targets[ face.b ].z - v2p.z ) * influence;
  168. _vC.x += ( targets[ face.c ].x - v3p.x ) * influence;
  169. _vC.y += ( targets[ face.c ].y - v3p.y ) * influence;
  170. _vC.z += ( targets[ face.c ].z - v3p.z ) * influence;
  171. }
  172. v1.position.add( _vA );
  173. v2.position.add( _vB );
  174. v3.position.add( _vC );
  175. projectVertex( v1 );
  176. projectVertex( v2 );
  177. projectVertex( v3 );
  178. }
  179. _points3[ 0 ] = v1.positionScreen;
  180. _points3[ 1 ] = v2.positionScreen;
  181. _points3[ 2 ] = v3.positionScreen;
  182. if ( v1.visible === true || v2.visible === true || v3.visible === true ||
  183. _clipBox.isIntersectionBox( _boundingBox.setFromPoints( _points3 ) ) ) {
  184. visible = ( ( v3.positionScreen.x - v1.positionScreen.x ) *
  185. ( v2.positionScreen.y - v1.positionScreen.y ) -
  186. ( v3.positionScreen.y - v1.positionScreen.y ) *
  187. ( v2.positionScreen.x - v1.positionScreen.x ) ) < 0;
  188. if ( side === THREE.DoubleSide || visible === ( side === THREE.FrontSide ) ) {
  189. _face = getNextFace3InPool();
  190. _face.id = object.id;
  191. _face.v1.copy( v1 );
  192. _face.v2.copy( v2 );
  193. _face.v3.copy( v3 );
  194. } else {
  195. continue;
  196. }
  197. } else {
  198. continue;
  199. }
  200. _face.normalModel.copy( face.normal );
  201. if ( visible === false && ( side === THREE.BackSide || side === THREE.DoubleSide ) ) {
  202. _face.normalModel.negate();
  203. }
  204. _face.normalModel.applyMatrix3( _normalMatrix ).normalize();
  205. _face.normalModelView.copy( _face.normalModel ).applyMatrix3( _normalViewMatrix );
  206. _face.centroidModel.copy( face.centroid ).applyMatrix4( _modelMatrix );
  207. faceVertexNormals = face.vertexNormals;
  208. for ( var n = 0, nl = Math.min( faceVertexNormals.length, 3 ); n < nl; n ++ ) {
  209. var normalModel = _face.vertexNormalsModel[ n ];
  210. normalModel.copy( faceVertexNormals[ n ] );
  211. if ( visible === false && ( side === THREE.BackSide || side === THREE.DoubleSide ) ) {
  212. normalModel.negate();
  213. }
  214. normalModel.applyMatrix3( _normalMatrix ).normalize();
  215. var normalModelView = _face.vertexNormalsModelView[ n ];
  216. normalModelView.copy( normalModel ).applyMatrix3( _normalViewMatrix );
  217. }
  218. _face.vertexNormalsLength = faceVertexNormals.length;
  219. for ( var c = 0, cl = Math.min( faceVertexUvs.length, 3 ); c < cl; c ++ ) {
  220. uvs = faceVertexUvs[ c ][ f ];
  221. if ( uvs === undefined ) continue;
  222. for ( var u = 0, ul = uvs.length; u < ul; u ++ ) {
  223. _face.uvs[ c ][ u ] = uvs[ u ];
  224. }
  225. }
  226. _face.color = face.color;
  227. _face.material = material;
  228. _centroid.copy( _face.centroidModel ).applyProjection( _viewProjectionMatrix );
  229. _face.z = _centroid.z;
  230. _renderData.elements.push( _face );
  231. }
  232. } else if ( object instanceof THREE.Line ) {
  233. _modelViewProjectionMatrix.multiplyMatrices( _viewProjectionMatrix, _modelMatrix );
  234. vertices = object.geometry.vertices;
  235. v1 = getNextVertexInPool();
  236. v1.positionScreen.copy( vertices[ 0 ] ).applyMatrix4( _modelViewProjectionMatrix );
  237. // Handle LineStrip and LinePieces
  238. var step = object.type === THREE.LinePieces ? 2 : 1;
  239. for ( v = 1, vl = vertices.length; v < vl; v ++ ) {
  240. v1 = getNextVertexInPool();
  241. v1.positionScreen.copy( vertices[ v ] ).applyMatrix4( _modelViewProjectionMatrix );
  242. if ( ( v + 1 ) % step > 0 ) continue;
  243. v2 = _vertexPool[ _vertexCount - 2 ];
  244. _clippedVertex1PositionScreen.copy( v1.positionScreen );
  245. _clippedVertex2PositionScreen.copy( v2.positionScreen );
  246. if ( clipLine( _clippedVertex1PositionScreen, _clippedVertex2PositionScreen ) === true ) {
  247. // Perform the perspective divide
  248. _clippedVertex1PositionScreen.multiplyScalar( 1 / _clippedVertex1PositionScreen.w );
  249. _clippedVertex2PositionScreen.multiplyScalar( 1 / _clippedVertex2PositionScreen.w );
  250. _line = getNextLineInPool();
  251. _line.id = object.id;
  252. _line.v1.positionScreen.copy( _clippedVertex1PositionScreen );
  253. _line.v2.positionScreen.copy( _clippedVertex2PositionScreen );
  254. _line.z = Math.max( _clippedVertex1PositionScreen.z, _clippedVertex2PositionScreen.z );
  255. _line.material = object.material;
  256. if ( object.material.vertexColors === THREE.VertexColors ) {
  257. _line.vertexColors[ 0 ].copy( object.geometry.colors[ v ] );
  258. _line.vertexColors[ 1 ].copy( object.geometry.colors[ v - 1 ] );
  259. }
  260. _renderData.elements.push( _line );
  261. }
  262. }
  263. }
  264. }
  265. for ( o = 0, ol = _renderData.sprites.length; o < ol; o++ ) {
  266. object = _renderData.sprites[ o ].object;
  267. _modelMatrix = object.matrixWorld;
  268. _vector4.set( _modelMatrix.elements[12], _modelMatrix.elements[13], _modelMatrix.elements[14], 1 );
  269. _vector4.applyMatrix4( _viewProjectionMatrix );
  270. var invW = 1 / _vector4.w;
  271. _vector4.z *= invW;
  272. if ( _vector4.z >= -1 && _vector4.z <= 1 ) {
  273. _sprite = getNextSpriteInPool();
  274. _sprite.id = object.id;
  275. _sprite.x = _vector4.x * invW;
  276. _sprite.y = _vector4.y * invW;
  277. _sprite.z = _vector4.z;
  278. _sprite.object = object;
  279. _sprite.rotation = object.rotation;
  280. _sprite.scale.x = object.scale.x * Math.abs( _sprite.x - ( _vector4.x + camera.projectionMatrix.elements[0] ) / ( _vector4.w + camera.projectionMatrix.elements[12] ) );
  281. _sprite.scale.y = object.scale.y * Math.abs( _sprite.y - ( _vector4.y + camera.projectionMatrix.elements[5] ) / ( _vector4.w + camera.projectionMatrix.elements[13] ) );
  282. _sprite.material = object.material;
  283. _renderData.elements.push( _sprite );
  284. }
  285. }
  286. if ( sortElements === true ) _renderData.elements.sort( painterSort );
  287. return _renderData;
  288. };
  289. // Pools
  290. function getNextObjectInPool() {
  291. if ( _objectCount === _objectPoolLength ) {
  292. var object = new THREE.RenderableObject();
  293. _objectPool.push( object );
  294. _objectPoolLength ++;
  295. _objectCount ++;
  296. return object;
  297. }
  298. return _objectPool[ _objectCount ++ ];
  299. }
  300. function getNextVertexInPool() {
  301. if ( _vertexCount === _vertexPoolLength ) {
  302. var vertex = new THREE.RenderableVertex();
  303. _vertexPool.push( vertex );
  304. _vertexPoolLength ++;
  305. _vertexCount ++;
  306. return vertex;
  307. }
  308. return _vertexPool[ _vertexCount ++ ];
  309. }
  310. function getNextFace3InPool() {
  311. if ( _face3Count === _face3PoolLength ) {
  312. var face = new THREE.RenderableFace3();
  313. _face3Pool.push( face );
  314. _face3PoolLength ++;
  315. _face3Count ++;
  316. return face;
  317. }
  318. return _face3Pool[ _face3Count ++ ];
  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 getNextSpriteInPool() {
  331. if ( _spriteCount === _spritePoolLength ) {
  332. var sprite = new THREE.RenderableSprite();
  333. _spritePool.push( sprite );
  334. _spritePoolLength ++;
  335. _spriteCount ++
  336. return sprite;
  337. }
  338. return _spritePool[ _spriteCount ++ ];
  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. };