Projector.js 14 KB

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