| 7152 | } |
| 7153 | |
| 7154 | function clipLine( s1, s2 ) { |
| 7155 | |
| 7156 | var alpha1 = 0, alpha2 = 1, |
| 7157 | |
| 7158 | // Calculate the boundary coordinate of each vertex for the near and far clip planes, |
| 7159 | // Z = -1 and Z = +1, respectively. |
| 7160 | bc1near = s1.z + s1.w, |
| 7161 | bc2near = s2.z + s2.w, |
| 7162 | bc1far = - s1.z + s1.w, |
| 7163 | bc2far = - s2.z + s2.w; |
| 7164 | |
| 7165 | if ( bc1near >= 0 && bc2near >= 0 && bc1far >= 0 && bc2far >= 0 ) { |
| 7166 | |
| 7167 | // Both vertices lie entirely within all clip planes. |
| 7168 | return true; |
| 7169 | |
| 7170 | } else if ( ( bc1near < 0 && bc2near < 0) || (bc1far < 0 && bc2far < 0 ) ) { |
| 7171 | |
| 7172 | // Both vertices lie entirely outside one of the clip planes. |
| 7173 | return false; |
| 7174 | |
| 7175 | } else { |
| 7176 | |
| 7177 | // The line segment spans at least one clip plane. |
| 7178 | |
| 7179 | if ( bc1near < 0 ) { |
| 7180 | |
| 7181 | // v1 lies outside the near plane, v2 inside |
| 7182 | alpha1 = Math.max( alpha1, bc1near / ( bc1near - bc2near ) ); |
| 7183 | |
| 7184 | } else if ( bc2near < 0 ) { |
| 7185 | |
| 7186 | // v2 lies outside the near plane, v1 inside |
| 7187 | alpha2 = Math.min( alpha2, bc1near / ( bc1near - bc2near ) ); |
| 7188 | |
| 7189 | } |
| 7190 | |
| 7191 | if ( bc1far < 0 ) { |
| 7192 | |
| 7193 | // v1 lies outside the far plane, v2 inside |
| 7194 | alpha1 = Math.max( alpha1, bc1far / ( bc1far - bc2far ) ); |
| 7195 | |
| 7196 | } else if ( bc2far < 0 ) { |
| 7197 | |
| 7198 | // v2 lies outside the far plane, v2 inside |
| 7199 | alpha2 = Math.min( alpha2, bc1far / ( bc1far - bc2far ) ); |
| 7200 | |
| 7201 | } |
| 7202 | |
| 7203 | if ( alpha2 < alpha1 ) { |
| 7204 | |
| 7205 | // The line segment spans two boundaries, but is outside both of them. |
| 7206 | // (This can't happen when we're only clipping against just near/far but good |
| 7207 | // to leave the check here for future usage if other clip planes are added.) |
| 7208 | return false; |
| 7209 | |
| 7210 | } else { |
| 7211 | |