| 8262 | } |
| 8263 | |
| 8264 | function clipLine( s1, s2 ) { |
| 8265 | |
| 8266 | var alpha1 = 0, alpha2 = 1, |
| 8267 | |
| 8268 | // Calculate the boundary coordinate of each vertex for the near and far clip planes, |
| 8269 | // Z = -1 and Z = +1, respectively. |
| 8270 | bc1near = s1.z + s1.w, |
| 8271 | bc2near = s2.z + s2.w, |
| 8272 | bc1far = - s1.z + s1.w, |
| 8273 | bc2far = - s2.z + s2.w; |
| 8274 | |
| 8275 | if ( bc1near >= 0 && bc2near >= 0 && bc1far >= 0 && bc2far >= 0 ) { |
| 8276 | |
| 8277 | // Both vertices lie entirely within all clip planes. |
| 8278 | return true; |
| 8279 | |
| 8280 | } else if ( ( bc1near < 0 && bc2near < 0) || (bc1far < 0 && bc2far < 0 ) ) { |
| 8281 | |
| 8282 | // Both vertices lie entirely outside one of the clip planes. |
| 8283 | return false; |
| 8284 | |
| 8285 | } else { |
| 8286 | |
| 8287 | // The line segment spans at least one clip plane. |
| 8288 | |
| 8289 | if ( bc1near < 0 ) { |
| 8290 | |
| 8291 | // v1 lies outside the near plane, v2 inside |
| 8292 | alpha1 = Math.max( alpha1, bc1near / ( bc1near - bc2near ) ); |
| 8293 | |
| 8294 | } else if ( bc2near < 0 ) { |
| 8295 | |
| 8296 | // v2 lies outside the near plane, v1 inside |
| 8297 | alpha2 = Math.min( alpha2, bc1near / ( bc1near - bc2near ) ); |
| 8298 | |
| 8299 | } |
| 8300 | |
| 8301 | if ( bc1far < 0 ) { |
| 8302 | |
| 8303 | // v1 lies outside the far plane, v2 inside |
| 8304 | alpha1 = Math.max( alpha1, bc1far / ( bc1far - bc2far ) ); |
| 8305 | |
| 8306 | } else if ( bc2far < 0 ) { |
| 8307 | |
| 8308 | // v2 lies outside the far plane, v2 inside |
| 8309 | alpha2 = Math.min( alpha2, bc1far / ( bc1far - bc2far ) ); |
| 8310 | |
| 8311 | } |
| 8312 | |
| 8313 | if ( alpha2 < alpha1 ) { |
| 8314 | |
| 8315 | // The line segment spans two boundaries, but is outside both of them. |
| 8316 | // (This can't happen when we're only clipping against just near/far but good |
| 8317 | // to leave the check here for future usage if other clip planes are added.) |
| 8318 | return false; |
| 8319 | |
| 8320 | } else { |
| 8321 | |