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

Function clipLine

Three.js/js/Three66.js:8768–8836  ·  view source on GitHub ↗
( s1, s2 )

Source from the content-addressed store, hash-verified

8766 }
8767
8768 function clipLine( s1, s2 ) {
8769
8770 var alpha1 = 0, alpha2 = 1,
8771
8772 // Calculate the boundary coordinate of each vertex for the near and far clip planes,
8773 // Z = -1 and Z = +1, respectively.
8774 bc1near = s1.z + s1.w,
8775 bc2near = s2.z + s2.w,
8776 bc1far = - s1.z + s1.w,
8777 bc2far = - s2.z + s2.w;
8778
8779 if ( bc1near >= 0 && bc2near >= 0 && bc1far >= 0 && bc2far >= 0 ) {
8780
8781 // Both vertices lie entirely within all clip planes.
8782 return true;
8783
8784 } else if ( ( bc1near < 0 && bc2near < 0) || (bc1far < 0 && bc2far < 0 ) ) {
8785
8786 // Both vertices lie entirely outside one of the clip planes.
8787 return false;
8788
8789 } else {
8790
8791 // The line segment spans at least one clip plane.
8792
8793 if ( bc1near < 0 ) {
8794
8795 // v1 lies outside the near plane, v2 inside
8796 alpha1 = Math.max( alpha1, bc1near / ( bc1near - bc2near ) );
8797
8798 } else if ( bc2near < 0 ) {
8799
8800 // v2 lies outside the near plane, v1 inside
8801 alpha2 = Math.min( alpha2, bc1near / ( bc1near - bc2near ) );
8802
8803 }
8804
8805 if ( bc1far < 0 ) {
8806
8807 // v1 lies outside the far plane, v2 inside
8808 alpha1 = Math.max( alpha1, bc1far / ( bc1far - bc2far ) );
8809
8810 } else if ( bc2far < 0 ) {
8811
8812 // v2 lies outside the far plane, v2 inside
8813 alpha2 = Math.min( alpha2, bc1far / ( bc1far - bc2far ) );
8814
8815 }
8816
8817 if ( alpha2 < alpha1 ) {
8818
8819 // The line segment spans two boundaries, but is outside both of them.
8820 // (This can't happen when we're only clipping against just near/far but good
8821 // to leave the check here for future usage if other clip planes are added.)
8822 return false;
8823
8824 } else {
8825

Callers 1

Three66.jsFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected