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

Function clipLine

Three.js/js/Three60.js:8218–8286  ·  view source on GitHub ↗
( s1, s2 )

Source from the content-addressed store, hash-verified

8216 }
8217
8218 function clipLine( s1, s2 ) {
8219
8220 var alpha1 = 0, alpha2 = 1,
8221
8222 // Calculate the boundary coordinate of each vertex for the near and far clip planes,
8223 // Z = -1 and Z = +1, respectively.
8224 bc1near = s1.z + s1.w,
8225 bc2near = s2.z + s2.w,
8226 bc1far = - s1.z + s1.w,
8227 bc2far = - s2.z + s2.w;
8228
8229 if ( bc1near >= 0 && bc2near >= 0 && bc1far >= 0 && bc2far >= 0 ) {
8230
8231 // Both vertices lie entirely within all clip planes.
8232 return true;
8233
8234 } else if ( ( bc1near < 0 && bc2near < 0) || (bc1far < 0 && bc2far < 0 ) ) {
8235
8236 // Both vertices lie entirely outside one of the clip planes.
8237 return false;
8238
8239 } else {
8240
8241 // The line segment spans at least one clip plane.
8242
8243 if ( bc1near < 0 ) {
8244
8245 // v1 lies outside the near plane, v2 inside
8246 alpha1 = Math.max( alpha1, bc1near / ( bc1near - bc2near ) );
8247
8248 } else if ( bc2near < 0 ) {
8249
8250 // v2 lies outside the near plane, v1 inside
8251 alpha2 = Math.min( alpha2, bc1near / ( bc1near - bc2near ) );
8252
8253 }
8254
8255 if ( bc1far < 0 ) {
8256
8257 // v1 lies outside the far plane, v2 inside
8258 alpha1 = Math.max( alpha1, bc1far / ( bc1far - bc2far ) );
8259
8260 } else if ( bc2far < 0 ) {
8261
8262 // v2 lies outside the far plane, v2 inside
8263 alpha2 = Math.min( alpha2, bc1far / ( bc1far - bc2far ) );
8264
8265 }
8266
8267 if ( alpha2 < alpha1 ) {
8268
8269 // The line segment spans two boundaries, but is outside both of them.
8270 // (This can't happen when we're only clipping against just near/far but good
8271 // to leave the check here for future usage if other clip planes are added.)
8272 return false;
8273
8274 } else {
8275

Callers 1

Three60.jsFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected