Projector.js 20 KB

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