Projector.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. ( function () {
  2. class RenderableObject {
  3. constructor() {
  4. this.id = 0;
  5. this.object = null;
  6. this.z = 0;
  7. this.renderOrder = 0;
  8. }
  9. }
  10. //
  11. class RenderableFace {
  12. constructor() {
  13. this.id = 0;
  14. this.v1 = new RenderableVertex();
  15. this.v2 = new RenderableVertex();
  16. this.v3 = new RenderableVertex();
  17. this.normalModel = new THREE.Vector3();
  18. this.vertexNormalsModel = [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ];
  19. this.vertexNormalsLength = 0;
  20. this.color = new THREE.Color();
  21. this.material = null;
  22. this.uvs = [ new THREE.Vector2(), new THREE.Vector2(), new THREE.Vector2() ];
  23. this.z = 0;
  24. this.renderOrder = 0;
  25. }
  26. }
  27. //
  28. class RenderableVertex {
  29. constructor() {
  30. this.position = new THREE.Vector3();
  31. this.positionWorld = new THREE.Vector3();
  32. this.positionScreen = new THREE.Vector4();
  33. this.visible = true;
  34. }
  35. copy( vertex ) {
  36. this.positionWorld.copy( vertex.positionWorld );
  37. this.positionScreen.copy( vertex.positionScreen );
  38. }
  39. }
  40. //
  41. class RenderableLine {
  42. constructor() {
  43. this.id = 0;
  44. this.v1 = new RenderableVertex();
  45. this.v2 = new RenderableVertex();
  46. this.vertexColors = [ new THREE.Color(), new THREE.Color() ];
  47. this.material = null;
  48. this.z = 0;
  49. this.renderOrder = 0;
  50. }
  51. }
  52. //
  53. class RenderableSprite {
  54. constructor() {
  55. this.id = 0;
  56. this.object = null;
  57. this.x = 0;
  58. this.y = 0;
  59. this.z = 0;
  60. this.rotation = 0;
  61. this.scale = new THREE.Vector2();
  62. this.material = null;
  63. this.renderOrder = 0;
  64. }
  65. }
  66. //
  67. class Projector {
  68. constructor() {
  69. let _object,
  70. _objectCount,
  71. _objectPoolLength = 0,
  72. _vertex,
  73. _vertexCount,
  74. _vertexPoolLength = 0,
  75. _face,
  76. _faceCount,
  77. _facePoolLength = 0,
  78. _line,
  79. _lineCount,
  80. _linePoolLength = 0,
  81. _sprite,
  82. _spriteCount,
  83. _spritePoolLength = 0,
  84. _modelMatrix;
  85. const _renderData = {
  86. objects: [],
  87. lights: [],
  88. elements: []
  89. },
  90. _vector3 = new THREE.Vector3(),
  91. _vector4 = new THREE.Vector4(),
  92. _clipBox = new THREE.Box3( new THREE.Vector3( - 1, - 1, - 1 ), new THREE.Vector3( 1, 1, 1 ) ),
  93. _boundingBox = new THREE.Box3(),
  94. _points3 = new Array( 3 ),
  95. _viewMatrix = new THREE.Matrix4(),
  96. _viewProjectionMatrix = new THREE.Matrix4(),
  97. _modelViewProjectionMatrix = new THREE.Matrix4(),
  98. _frustum = new THREE.Frustum(),
  99. _objectPool = [],
  100. _vertexPool = [],
  101. _facePool = [],
  102. _linePool = [],
  103. _spritePool = [];
  104. //
  105. function RenderList() {
  106. const normals = [];
  107. const colors = [];
  108. const uvs = [];
  109. let object = null;
  110. const normalMatrix = new THREE.Matrix3();
  111. function setObject( value ) {
  112. object = value;
  113. normalMatrix.getNormalMatrix( object.matrixWorld );
  114. normals.length = 0;
  115. colors.length = 0;
  116. uvs.length = 0;
  117. }
  118. function projectVertex( vertex ) {
  119. const position = vertex.position;
  120. const positionWorld = vertex.positionWorld;
  121. const positionScreen = vertex.positionScreen;
  122. positionWorld.copy( position ).applyMatrix4( _modelMatrix );
  123. positionScreen.copy( positionWorld ).applyMatrix4( _viewProjectionMatrix );
  124. const invW = 1 / positionScreen.w;
  125. positionScreen.x *= invW;
  126. positionScreen.y *= invW;
  127. positionScreen.z *= invW;
  128. vertex.visible = positionScreen.x >= - 1 && positionScreen.x <= 1 && positionScreen.y >= - 1 && positionScreen.y <= 1 && positionScreen.z >= - 1 && positionScreen.z <= 1;
  129. }
  130. function pushVertex( x, y, z ) {
  131. _vertex = getNextVertexInPool();
  132. _vertex.position.set( x, y, z );
  133. projectVertex( _vertex );
  134. }
  135. function pushNormal( x, y, z ) {
  136. normals.push( x, y, z );
  137. }
  138. function pushColor( r, g, b ) {
  139. colors.push( r, g, b );
  140. }
  141. function pushUv( x, y ) {
  142. uvs.push( x, y );
  143. }
  144. function checkTriangleVisibility( v1, v2, v3 ) {
  145. if ( v1.visible === true || v2.visible === true || v3.visible === true ) return true;
  146. _points3[ 0 ] = v1.positionScreen;
  147. _points3[ 1 ] = v2.positionScreen;
  148. _points3[ 2 ] = v3.positionScreen;
  149. return _clipBox.intersectsBox( _boundingBox.setFromPoints( _points3 ) );
  150. }
  151. function checkBackfaceCulling( v1, v2, v3 ) {
  152. return ( v3.positionScreen.x - v1.positionScreen.x ) * ( v2.positionScreen.y - v1.positionScreen.y ) - ( v3.positionScreen.y - v1.positionScreen.y ) * ( v2.positionScreen.x - v1.positionScreen.x ) < 0;
  153. }
  154. function pushLine( a, b ) {
  155. const v1 = _vertexPool[ a ];
  156. const v2 = _vertexPool[ b ];
  157. // Clip
  158. v1.positionScreen.copy( v1.position ).applyMatrix4( _modelViewProjectionMatrix );
  159. v2.positionScreen.copy( v2.position ).applyMatrix4( _modelViewProjectionMatrix );
  160. if ( clipLine( v1.positionScreen, v2.positionScreen ) === true ) {
  161. // Perform the perspective divide
  162. v1.positionScreen.multiplyScalar( 1 / v1.positionScreen.w );
  163. v2.positionScreen.multiplyScalar( 1 / v2.positionScreen.w );
  164. _line = getNextLineInPool();
  165. _line.id = object.id;
  166. _line.v1.copy( v1 );
  167. _line.v2.copy( v2 );
  168. _line.z = Math.max( v1.positionScreen.z, v2.positionScreen.z );
  169. _line.renderOrder = object.renderOrder;
  170. _line.material = object.material;
  171. if ( object.material.vertexColors ) {
  172. _line.vertexColors[ 0 ].fromArray( colors, a * 3 );
  173. _line.vertexColors[ 1 ].fromArray( colors, b * 3 );
  174. }
  175. _renderData.elements.push( _line );
  176. }
  177. }
  178. function pushTriangle( a, b, c, material ) {
  179. const v1 = _vertexPool[ a ];
  180. const v2 = _vertexPool[ b ];
  181. const v3 = _vertexPool[ c ];
  182. if ( checkTriangleVisibility( v1, v2, v3 ) === false ) return;
  183. if ( material.side === THREE.DoubleSide || checkBackfaceCulling( v1, v2, v3 ) === true ) {
  184. _face = getNextFaceInPool();
  185. _face.id = object.id;
  186. _face.v1.copy( v1 );
  187. _face.v2.copy( v2 );
  188. _face.v3.copy( v3 );
  189. _face.z = ( v1.positionScreen.z + v2.positionScreen.z + v3.positionScreen.z ) / 3;
  190. _face.renderOrder = object.renderOrder;
  191. // face normal
  192. _vector3.subVectors( v3.position, v2.position );
  193. _vector4.subVectors( v1.position, v2.position );
  194. _vector3.cross( _vector4 );
  195. _face.normalModel.copy( _vector3 );
  196. _face.normalModel.applyMatrix3( normalMatrix ).normalize();
  197. for ( let i = 0; i < 3; i ++ ) {
  198. const normal = _face.vertexNormalsModel[ i ];
  199. normal.fromArray( normals, arguments[ i ] * 3 );
  200. normal.applyMatrix3( normalMatrix ).normalize();
  201. const uv = _face.uvs[ i ];
  202. uv.fromArray( uvs, arguments[ i ] * 2 );
  203. }
  204. _face.vertexNormalsLength = 3;
  205. _face.material = material;
  206. if ( material.vertexColors ) {
  207. _face.color.fromArray( colors, a * 3 );
  208. }
  209. _renderData.elements.push( _face );
  210. }
  211. }
  212. return {
  213. setObject: setObject,
  214. projectVertex: projectVertex,
  215. checkTriangleVisibility: checkTriangleVisibility,
  216. checkBackfaceCulling: checkBackfaceCulling,
  217. pushVertex: pushVertex,
  218. pushNormal: pushNormal,
  219. pushColor: pushColor,
  220. pushUv: pushUv,
  221. pushLine: pushLine,
  222. pushTriangle: pushTriangle
  223. };
  224. }
  225. const renderList = new RenderList();
  226. function projectObject( object ) {
  227. if ( object.visible === false ) return;
  228. if ( object.isLight ) {
  229. _renderData.lights.push( object );
  230. } else if ( object.isMesh || object.isLine || object.isPoints ) {
  231. if ( object.material.visible === false ) return;
  232. if ( object.frustumCulled === true && _frustum.intersectsObject( object ) === false ) return;
  233. addObject( object );
  234. } else if ( object.isSprite ) {
  235. if ( object.material.visible === false ) return;
  236. if ( object.frustumCulled === true && _frustum.intersectsSprite( object ) === false ) return;
  237. addObject( object );
  238. }
  239. const children = object.children;
  240. for ( let i = 0, l = children.length; i < l; i ++ ) {
  241. projectObject( children[ i ] );
  242. }
  243. }
  244. function addObject( object ) {
  245. _object = getNextObjectInPool();
  246. _object.id = object.id;
  247. _object.object = object;
  248. _vector3.setFromMatrixPosition( object.matrixWorld );
  249. _vector3.applyMatrix4( _viewProjectionMatrix );
  250. _object.z = _vector3.z;
  251. _object.renderOrder = object.renderOrder;
  252. _renderData.objects.push( _object );
  253. }
  254. this.projectScene = function ( scene, camera, sortObjects, sortElements ) {
  255. _faceCount = 0;
  256. _lineCount = 0;
  257. _spriteCount = 0;
  258. _renderData.elements.length = 0;
  259. if ( scene.matrixWorldAutoUpdate === true ) scene.updateMatrixWorld();
  260. if ( camera.parent === null && camera.matrixWorldAutoUpdate === true ) camera.updateMatrixWorld();
  261. _viewMatrix.copy( camera.matrixWorldInverse );
  262. _viewProjectionMatrix.multiplyMatrices( camera.projectionMatrix, _viewMatrix );
  263. _frustum.setFromProjectionMatrix( _viewProjectionMatrix );
  264. //
  265. _objectCount = 0;
  266. _renderData.objects.length = 0;
  267. _renderData.lights.length = 0;
  268. projectObject( scene );
  269. if ( sortObjects === true ) {
  270. _renderData.objects.sort( painterSort );
  271. }
  272. //
  273. const objects = _renderData.objects;
  274. for ( let o = 0, ol = objects.length; o < ol; o ++ ) {
  275. const object = objects[ o ].object;
  276. const geometry = object.geometry;
  277. renderList.setObject( object );
  278. _modelMatrix = object.matrixWorld;
  279. _vertexCount = 0;
  280. if ( object.isMesh ) {
  281. let material = object.material;
  282. const isMultiMaterial = Array.isArray( material );
  283. const attributes = geometry.attributes;
  284. const groups = geometry.groups;
  285. if ( attributes.position === undefined ) continue;
  286. const positions = attributes.position.array;
  287. for ( let i = 0, l = positions.length; i < l; i += 3 ) {
  288. let x = positions[ i ];
  289. let y = positions[ i + 1 ];
  290. let z = positions[ i + 2 ];
  291. const morphTargets = geometry.morphAttributes.position;
  292. if ( morphTargets !== undefined ) {
  293. const morphTargetsRelative = geometry.morphTargetsRelative;
  294. const morphInfluences = object.morphTargetInfluences;
  295. for ( let t = 0, tl = morphTargets.length; t < tl; t ++ ) {
  296. const influence = morphInfluences[ t ];
  297. if ( influence === 0 ) continue;
  298. const target = morphTargets[ t ];
  299. if ( morphTargetsRelative ) {
  300. x += target.getX( i / 3 ) * influence;
  301. y += target.getY( i / 3 ) * influence;
  302. z += target.getZ( i / 3 ) * influence;
  303. } else {
  304. x += ( target.getX( i / 3 ) - positions[ i ] ) * influence;
  305. y += ( target.getY( i / 3 ) - positions[ i + 1 ] ) * influence;
  306. z += ( target.getZ( i / 3 ) - positions[ i + 2 ] ) * influence;
  307. }
  308. }
  309. }
  310. renderList.pushVertex( x, y, z );
  311. }
  312. if ( attributes.normal !== undefined ) {
  313. const normals = attributes.normal.array;
  314. for ( let i = 0, l = normals.length; i < l; i += 3 ) {
  315. renderList.pushNormal( normals[ i ], normals[ i + 1 ], normals[ i + 2 ] );
  316. }
  317. }
  318. if ( attributes.color !== undefined ) {
  319. const colors = attributes.color.array;
  320. for ( let i = 0, l = colors.length; i < l; i += 3 ) {
  321. renderList.pushColor( colors[ i ], colors[ i + 1 ], colors[ i + 2 ] );
  322. }
  323. }
  324. if ( attributes.uv !== undefined ) {
  325. const uvs = attributes.uv.array;
  326. for ( let i = 0, l = uvs.length; i < l; i += 2 ) {
  327. renderList.pushUv( uvs[ i ], uvs[ i + 1 ] );
  328. }
  329. }
  330. if ( geometry.index !== null ) {
  331. const indices = geometry.index.array;
  332. if ( groups.length > 0 ) {
  333. for ( let g = 0; g < groups.length; g ++ ) {
  334. const group = groups[ g ];
  335. material = isMultiMaterial === true ? object.material[ group.materialIndex ] : object.material;
  336. if ( material === undefined ) continue;
  337. for ( let i = group.start, l = group.start + group.count; i < l; i += 3 ) {
  338. renderList.pushTriangle( indices[ i ], indices[ i + 1 ], indices[ i + 2 ], material );
  339. }
  340. }
  341. } else {
  342. for ( let i = 0, l = indices.length; i < l; i += 3 ) {
  343. renderList.pushTriangle( indices[ i ], indices[ i + 1 ], indices[ i + 2 ], material );
  344. }
  345. }
  346. } else {
  347. if ( groups.length > 0 ) {
  348. for ( let g = 0; g < groups.length; g ++ ) {
  349. const group = groups[ g ];
  350. material = isMultiMaterial === true ? object.material[ group.materialIndex ] : object.material;
  351. if ( material === undefined ) continue;
  352. for ( let i = group.start, l = group.start + group.count; i < l; i += 3 ) {
  353. renderList.pushTriangle( i, i + 1, i + 2, material );
  354. }
  355. }
  356. } else {
  357. for ( let i = 0, l = positions.length / 3; i < l; i += 3 ) {
  358. renderList.pushTriangle( i, i + 1, i + 2, material );
  359. }
  360. }
  361. }
  362. } else if ( object.isLine ) {
  363. _modelViewProjectionMatrix.multiplyMatrices( _viewProjectionMatrix, _modelMatrix );
  364. const attributes = geometry.attributes;
  365. if ( attributes.position !== undefined ) {
  366. const positions = attributes.position.array;
  367. for ( let i = 0, l = positions.length; i < l; i += 3 ) {
  368. renderList.pushVertex( positions[ i ], positions[ i + 1 ], positions[ i + 2 ] );
  369. }
  370. if ( attributes.color !== undefined ) {
  371. const colors = attributes.color.array;
  372. for ( let i = 0, l = colors.length; i < l; i += 3 ) {
  373. renderList.pushColor( colors[ i ], colors[ i + 1 ], colors[ i + 2 ] );
  374. }
  375. }
  376. if ( geometry.index !== null ) {
  377. const indices = geometry.index.array;
  378. for ( let i = 0, l = indices.length; i < l; i += 2 ) {
  379. renderList.pushLine( indices[ i ], indices[ i + 1 ] );
  380. }
  381. } else {
  382. const step = object.isLineSegments ? 2 : 1;
  383. for ( let i = 0, l = positions.length / 3 - 1; i < l; i += step ) {
  384. renderList.pushLine( i, i + 1 );
  385. }
  386. }
  387. }
  388. } else if ( object.isPoints ) {
  389. _modelViewProjectionMatrix.multiplyMatrices( _viewProjectionMatrix, _modelMatrix );
  390. const attributes = geometry.attributes;
  391. if ( attributes.position !== undefined ) {
  392. const positions = attributes.position.array;
  393. for ( let i = 0, l = positions.length; i < l; i += 3 ) {
  394. _vector4.set( positions[ i ], positions[ i + 1 ], positions[ i + 2 ], 1 );
  395. _vector4.applyMatrix4( _modelViewProjectionMatrix );
  396. pushPoint( _vector4, object, camera );
  397. }
  398. }
  399. } else if ( object.isSprite ) {
  400. object.modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
  401. _vector4.set( _modelMatrix.elements[ 12 ], _modelMatrix.elements[ 13 ], _modelMatrix.elements[ 14 ], 1 );
  402. _vector4.applyMatrix4( _viewProjectionMatrix );
  403. pushPoint( _vector4, object, camera );
  404. }
  405. }
  406. if ( sortElements === true ) {
  407. _renderData.elements.sort( painterSort );
  408. }
  409. return _renderData;
  410. };
  411. function pushPoint( _vector4, object, camera ) {
  412. const invW = 1 / _vector4.w;
  413. _vector4.z *= invW;
  414. if ( _vector4.z >= - 1 && _vector4.z <= 1 ) {
  415. _sprite = getNextSpriteInPool();
  416. _sprite.id = object.id;
  417. _sprite.x = _vector4.x * invW;
  418. _sprite.y = _vector4.y * invW;
  419. _sprite.z = _vector4.z;
  420. _sprite.renderOrder = object.renderOrder;
  421. _sprite.object = object;
  422. _sprite.rotation = object.rotation;
  423. _sprite.scale.x = object.scale.x * Math.abs( _sprite.x - ( _vector4.x + camera.projectionMatrix.elements[ 0 ] ) / ( _vector4.w + camera.projectionMatrix.elements[ 12 ] ) );
  424. _sprite.scale.y = object.scale.y * Math.abs( _sprite.y - ( _vector4.y + camera.projectionMatrix.elements[ 5 ] ) / ( _vector4.w + camera.projectionMatrix.elements[ 13 ] ) );
  425. _sprite.material = object.material;
  426. _renderData.elements.push( _sprite );
  427. }
  428. }
  429. // Pools
  430. function getNextObjectInPool() {
  431. if ( _objectCount === _objectPoolLength ) {
  432. const object = new RenderableObject();
  433. _objectPool.push( object );
  434. _objectPoolLength ++;
  435. _objectCount ++;
  436. return object;
  437. }
  438. return _objectPool[ _objectCount ++ ];
  439. }
  440. function getNextVertexInPool() {
  441. if ( _vertexCount === _vertexPoolLength ) {
  442. const vertex = new RenderableVertex();
  443. _vertexPool.push( vertex );
  444. _vertexPoolLength ++;
  445. _vertexCount ++;
  446. return vertex;
  447. }
  448. return _vertexPool[ _vertexCount ++ ];
  449. }
  450. function getNextFaceInPool() {
  451. if ( _faceCount === _facePoolLength ) {
  452. const face = new RenderableFace();
  453. _facePool.push( face );
  454. _facePoolLength ++;
  455. _faceCount ++;
  456. return face;
  457. }
  458. return _facePool[ _faceCount ++ ];
  459. }
  460. function getNextLineInPool() {
  461. if ( _lineCount === _linePoolLength ) {
  462. const line = new RenderableLine();
  463. _linePool.push( line );
  464. _linePoolLength ++;
  465. _lineCount ++;
  466. return line;
  467. }
  468. return _linePool[ _lineCount ++ ];
  469. }
  470. function getNextSpriteInPool() {
  471. if ( _spriteCount === _spritePoolLength ) {
  472. const sprite = new RenderableSprite();
  473. _spritePool.push( sprite );
  474. _spritePoolLength ++;
  475. _spriteCount ++;
  476. return sprite;
  477. }
  478. return _spritePool[ _spriteCount ++ ];
  479. }
  480. //
  481. function painterSort( a, b ) {
  482. if ( a.renderOrder !== b.renderOrder ) {
  483. return a.renderOrder - b.renderOrder;
  484. } else if ( a.z !== b.z ) {
  485. return b.z - a.z;
  486. } else if ( a.id !== b.id ) {
  487. return a.id - b.id;
  488. } else {
  489. return 0;
  490. }
  491. }
  492. function clipLine( s1, s2 ) {
  493. let alpha1 = 0,
  494. alpha2 = 1;
  495. // Calculate the boundary coordinate of each vertex for the near and far clip planes,
  496. // Z = -1 and Z = +1, respectively.
  497. const bc1near = s1.z + s1.w,
  498. bc2near = s2.z + s2.w,
  499. bc1far = - s1.z + s1.w,
  500. bc2far = - s2.z + s2.w;
  501. if ( bc1near >= 0 && bc2near >= 0 && bc1far >= 0 && bc2far >= 0 ) {
  502. // Both vertices lie entirely within all clip planes.
  503. return true;
  504. } else if ( bc1near < 0 && bc2near < 0 || bc1far < 0 && bc2far < 0 ) {
  505. // Both vertices lie entirely outside one of the clip planes.
  506. return false;
  507. } else {
  508. // The line segment spans at least one clip plane.
  509. if ( bc1near < 0 ) {
  510. // v1 lies outside the near plane, v2 inside
  511. alpha1 = Math.max( alpha1, bc1near / ( bc1near - bc2near ) );
  512. } else if ( bc2near < 0 ) {
  513. // v2 lies outside the near plane, v1 inside
  514. alpha2 = Math.min( alpha2, bc1near / ( bc1near - bc2near ) );
  515. }
  516. if ( bc1far < 0 ) {
  517. // v1 lies outside the far plane, v2 inside
  518. alpha1 = Math.max( alpha1, bc1far / ( bc1far - bc2far ) );
  519. } else if ( bc2far < 0 ) {
  520. // v2 lies outside the far plane, v2 inside
  521. alpha2 = Math.min( alpha2, bc1far / ( bc1far - bc2far ) );
  522. }
  523. if ( alpha2 < alpha1 ) {
  524. // The line segment spans two boundaries, but is outside both of them.
  525. // (This can't happen when we're only clipping against just near/far but good
  526. // to leave the check here for future usage if other clip planes are added.)
  527. return false;
  528. } else {
  529. // Update the s1 and s2 vertices to match the clipped line segment.
  530. s1.lerp( s2, alpha1 );
  531. s2.lerp( s1, 1 - alpha2 );
  532. return true;
  533. }
  534. }
  535. }
  536. }
  537. }
  538. THREE.Projector = Projector;
  539. THREE.RenderableFace = RenderableFace;
  540. THREE.RenderableLine = RenderableLine;
  541. THREE.RenderableObject = RenderableObject;
  542. THREE.RenderableSprite = RenderableSprite;
  543. THREE.RenderableVertex = RenderableVertex;
  544. } )();