MCPcopy
hub / github.com/stemkoski/stemkoski.github.com / clipLine

Function clipLine

Three.js/js/Three59dev.js:7759–7827  ·  view source on GitHub ↗
( s1, s2 )

Source from the content-addressed store, hash-verified

7757 }
7758
7759 function clipLine( s1, s2 ) {
7760
7761 var alpha1 = 0, alpha2 = 1,
7762
7763 // Calculate the boundary coordinate of each vertex for the near and far clip planes,
7764 // Z = -1 and Z = +1, respectively.
7765 bc1near = s1.z + s1.w,
7766 bc2near = s2.z + s2.w,
7767 bc1far = - s1.z + s1.w,
7768 bc2far = - s2.z + s2.w;
7769
7770 if ( bc1near >= 0 && bc2near >= 0 && bc1far >= 0 && bc2far >= 0 ) {
7771
7772 // Both vertices lie entirely within all clip planes.
7773 return true;
7774
7775 } else if ( ( bc1near < 0 && bc2near < 0) || (bc1far < 0 && bc2far < 0 ) ) {
7776
7777 // Both vertices lie entirely outside one of the clip planes.
7778 return false;
7779
7780 } else {
7781
7782 // The line segment spans at least one clip plane.
7783
7784 if ( bc1near < 0 ) {
7785
7786 // v1 lies outside the near plane, v2 inside
7787 alpha1 = Math.max( alpha1, bc1near / ( bc1near - bc2near ) );
7788
7789 } else if ( bc2near < 0 ) {
7790
7791 // v2 lies outside the near plane, v1 inside
7792 alpha2 = Math.min( alpha2, bc1near / ( bc1near - bc2near ) );
7793
7794 }
7795
7796 if ( bc1far < 0 ) {
7797
7798 // v1 lies outside the far plane, v2 inside
7799 alpha1 = Math.max( alpha1, bc1far / ( bc1far - bc2far ) );
7800
7801 } else if ( bc2far < 0 ) {
7802
7803 // v2 lies outside the far plane, v2 inside
7804 alpha2 = Math.min( alpha2, bc1far / ( bc1far - bc2far ) );
7805
7806 }
7807
7808 if ( alpha2 < alpha1 ) {
7809
7810 // The line segment spans two boundaries, but is outside both of them.
7811 // (This can't happen when we're only clipping against just near/far but good
7812 // to leave the check here for future usage if other clip planes are added.)
7813 return false;
7814
7815 } else {
7816

Callers 1

Three59dev.jsFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected