File size: 5,329 Bytes
b797337 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | import { Vector3, Vector2, Triangle, DoubleSide, BackSide, REVISION } from 'three';
const IS_GT_REVISION_169 = parseInt( REVISION ) >= 169;
const IS_LT_REVISION_161 = parseInt( REVISION ) <= 161;
// Ripped and modified From THREE.js Mesh raycast
// https://github.com/mrdoob/three.js/blob/0aa87c999fe61e216c1133fba7a95772b503eddf/src/objects/Mesh.js#L115
const _vA = /* @__PURE__ */ new Vector3();
const _vB = /* @__PURE__ */ new Vector3();
const _vC = /* @__PURE__ */ new Vector3();
const _uvA = /* @__PURE__ */ new Vector2();
const _uvB = /* @__PURE__ */ new Vector2();
const _uvC = /* @__PURE__ */ new Vector2();
const _normalA = /* @__PURE__ */ new Vector3();
const _normalB = /* @__PURE__ */ new Vector3();
const _normalC = /* @__PURE__ */ new Vector3();
const _intersectionPoint = /* @__PURE__ */ new Vector3();
function checkIntersection( ray, pA, pB, pC, point, side, near, far ) {
let intersect;
if ( side === BackSide ) {
intersect = ray.intersectTriangle( pC, pB, pA, true, point );
} else {
intersect = ray.intersectTriangle( pA, pB, pC, side !== DoubleSide, point );
}
if ( intersect === null ) return null;
const distance = ray.origin.distanceTo( point );
if ( distance < near || distance > far ) return null;
return {
distance: distance,
point: point.clone(),
};
}
function checkBufferGeometryIntersection( ray, position, normal, uv, uv1, a, b, c, side, near, far ) {
_vA.fromBufferAttribute( position, a );
_vB.fromBufferAttribute( position, b );
_vC.fromBufferAttribute( position, c );
const intersection = checkIntersection( ray, _vA, _vB, _vC, _intersectionPoint, side, near, far );
if ( intersection ) {
if ( uv ) {
_uvA.fromBufferAttribute( uv, a );
_uvB.fromBufferAttribute( uv, b );
_uvC.fromBufferAttribute( uv, c );
intersection.uv = new Vector2();
const res = Triangle.getInterpolation( _intersectionPoint, _vA, _vB, _vC, _uvA, _uvB, _uvC, intersection.uv );
if ( ! IS_GT_REVISION_169 ) {
intersection.uv = res;
}
}
if ( uv1 ) {
_uvA.fromBufferAttribute( uv1, a );
_uvB.fromBufferAttribute( uv1, b );
_uvC.fromBufferAttribute( uv1, c );
intersection.uv1 = new Vector2();
const res = Triangle.getInterpolation( _intersectionPoint, _vA, _vB, _vC, _uvA, _uvB, _uvC, intersection.uv1 );
if ( ! IS_GT_REVISION_169 ) {
intersection.uv1 = res;
}
if ( IS_LT_REVISION_161 ) {
intersection.uv2 = intersection.uv1;
}
}
if ( normal ) {
_normalA.fromBufferAttribute( normal, a );
_normalB.fromBufferAttribute( normal, b );
_normalC.fromBufferAttribute( normal, c );
intersection.normal = new Vector3();
const res = Triangle.getInterpolation( _intersectionPoint, _vA, _vB, _vC, _normalA, _normalB, _normalC, intersection.normal );
if ( intersection.normal.dot( ray.direction ) > 0 ) {
intersection.normal.multiplyScalar( - 1 );
}
if ( ! IS_GT_REVISION_169 ) {
intersection.normal = res;
}
}
const face = {
a: a,
b: b,
c: c,
normal: new Vector3(),
materialIndex: 0
};
Triangle.getNormal( _vA, _vB, _vC, face.normal );
intersection.face = face;
intersection.faceIndex = a;
if ( IS_GT_REVISION_169 ) {
const barycoord = new Vector3();
Triangle.getBarycoord( _intersectionPoint, _vA, _vB, _vC, barycoord );
intersection.barycoord = barycoord;
}
}
return intersection;
}
function getSide( materialOrSide ) {
return materialOrSide && materialOrSide.isMaterial ? materialOrSide.side : materialOrSide;
}
// https://github.com/mrdoob/three.js/blob/0aa87c999fe61e216c1133fba7a95772b503eddf/src/objects/Mesh.js#L258
export function intersectTri( geometry, materialOrSide, ray, tri, intersections, near, far ) {
const triOffset = tri * 3;
let a = triOffset + 0;
let b = triOffset + 1;
let c = triOffset + 2;
const { index, groups } = geometry;
if ( geometry.index ) {
a = index.getX( a );
b = index.getX( b );
c = index.getX( c );
}
const { position, normal, uv, uv1 } = geometry.attributes;
if ( Array.isArray( materialOrSide ) ) {
// check which groups a triangle is present in and run the intersections
// TODO: we shouldn't need to run and intersection test multiple times
const firstIndex = tri * 3;
for ( let i = 0, l = groups.length; i < l; i ++ ) {
const { start, count, materialIndex } = groups[ i ];
if ( firstIndex >= start && firstIndex < start + count ) {
const side = getSide( materialOrSide[ materialIndex ] );
const intersection = checkBufferGeometryIntersection( ray, position, normal, uv, uv1, a, b, c, side, near, far );
if ( intersection ) {
intersection.faceIndex = tri;
intersection.face.materialIndex = materialIndex;
if ( intersections ) {
intersections.push( intersection );
} else {
return intersection;
}
}
}
}
} else {
// run the intersection for the single material
const side = getSide( materialOrSide );
const intersection = checkBufferGeometryIntersection( ray, position, normal, uv, uv1, a, b, c, side, near, far );
if ( intersection ) {
intersection.faceIndex = tri;
intersection.face.materialIndex = 0;
if ( intersections ) {
intersections.push( intersection );
} else {
return intersection;
}
}
}
return null;
}
|