Spaces:
Running
Running
File size: 10,511 Bytes
2b7aae2 | 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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 | import { Vector3 } from '../math/Vector3.js';
import { Vector2 } from '../math/Vector2.js';
import { Sphere } from '../math/Sphere.js';
import { Ray } from '../math/Ray.js';
import { Matrix4 } from '../math/Matrix4.js';
import { Object3D } from '../core/Object3D.js';
import { Triangle } from '../math/Triangle.js';
import { DoubleSide, BackSide } from '../constants.js';
import { MeshBasicMaterial } from '../materials/MeshBasicMaterial.js';
import { BufferGeometry } from '../core/BufferGeometry.js';
const _inverseMatrix = /*@__PURE__*/ new Matrix4();
const _ray = /*@__PURE__*/ new Ray();
const _sphere = /*@__PURE__*/ new Sphere();
const _vA = /*@__PURE__*/ new Vector3();
const _vB = /*@__PURE__*/ new Vector3();
const _vC = /*@__PURE__*/ new Vector3();
const _tempA = /*@__PURE__*/ new Vector3();
const _tempB = /*@__PURE__*/ new Vector3();
const _tempC = /*@__PURE__*/ new Vector3();
const _morphA = /*@__PURE__*/ new Vector3();
const _morphB = /*@__PURE__*/ new Vector3();
const _morphC = /*@__PURE__*/ new Vector3();
const _uvA = /*@__PURE__*/ new Vector2();
const _uvB = /*@__PURE__*/ new Vector2();
const _uvC = /*@__PURE__*/ new Vector2();
const _intersectionPoint = /*@__PURE__*/ new Vector3();
const _intersectionPointWorld = /*@__PURE__*/ new Vector3();
class Mesh extends Object3D {
constructor(geometry = new BufferGeometry(), material = new MeshBasicMaterial()) {
super();
this.type = 'Mesh';
this.geometry = geometry;
this.material = material;
this.updateMorphTargets();
}
copy(source) {
super.copy(source);
if (source.morphTargetInfluences !== undefined) {
this.morphTargetInfluences = source.morphTargetInfluences.slice();
}
if (source.morphTargetDictionary !== undefined) {
this.morphTargetDictionary = Object.assign({}, source.morphTargetDictionary);
}
this.material = source.material;
this.geometry = source.geometry;
return this;
}
updateMorphTargets() {
const geometry = this.geometry;
if (geometry.isBufferGeometry) {
const morphAttributes = geometry.morphAttributes;
const keys = Object.keys(morphAttributes);
if (keys.length > 0) {
const morphAttribute = morphAttributes[keys[0]];
if (morphAttribute !== undefined) {
this.morphTargetInfluences = [];
this.morphTargetDictionary = {};
for (let m = 0, ml = morphAttribute.length; m < ml; m++) {
const name = morphAttribute[m].name || String(m);
this.morphTargetInfluences.push(0);
this.morphTargetDictionary[name] = m;
}
}
}
} else {
const morphTargets = geometry.morphTargets;
if (morphTargets !== undefined && morphTargets.length > 0) {
console.error('THREE.Mesh.updateMorphTargets() no longer supports THREE.Geometry. Use THREE.BufferGeometry instead.');
}
}
}
raycast(raycaster, intersects) {
const geometry = this.geometry;
const material = this.material;
const matrixWorld = this.matrixWorld;
if (material === undefined) return;
// Checking boundingSphere distance to ray
if (geometry.boundingSphere === null) geometry.computeBoundingSphere();
_sphere.copy(geometry.boundingSphere);
_sphere.applyMatrix4(matrixWorld);
if (raycaster.ray.intersectsSphere(_sphere) === false) return;
//
_inverseMatrix.copy(matrixWorld).invert();
_ray.copy(raycaster.ray).applyMatrix4(_inverseMatrix);
// Check boundingBox before continuing
if (geometry.boundingBox !== null) {
if (_ray.intersectsBox(geometry.boundingBox) === false) return;
}
let intersection;
if (geometry.isBufferGeometry) {
const index = geometry.index;
const position = geometry.attributes.position;
const morphPosition = geometry.morphAttributes.position;
const morphTargetsRelative = geometry.morphTargetsRelative;
const uv = geometry.attributes.uv;
const uv2 = geometry.attributes.uv2;
const groups = geometry.groups;
const drawRange = geometry.drawRange;
if (index !== null) {
// indexed buffer geometry
if (Array.isArray(material)) {
for (let i = 0, il = groups.length; i < il; i++) {
const group = groups[i];
const groupMaterial = material[group.materialIndex];
const start = Math.max(group.start, drawRange.start);
const end = Math.min(index.count, Math.min(group.start + group.count, drawRange.start + drawRange.count));
for (let j = start, jl = end; j < jl; j += 3) {
const a = index.getX(j);
const b = index.getX(j + 1);
const c = index.getX(j + 2);
intersection = checkBufferGeometryIntersection(
this,
groupMaterial,
raycaster,
_ray,
position,
morphPosition,
morphTargetsRelative,
uv,
uv2,
a,
b,
c
);
if (intersection) {
intersection.faceIndex = Math.floor(j / 3); // triangle number in indexed buffer semantics
intersection.face.materialIndex = group.materialIndex;
intersects.push(intersection);
}
}
}
} else {
const start = Math.max(0, drawRange.start);
const end = Math.min(index.count, drawRange.start + drawRange.count);
for (let i = start, il = end; i < il; i += 3) {
const a = index.getX(i);
const b = index.getX(i + 1);
const c = index.getX(i + 2);
intersection = checkBufferGeometryIntersection(
this,
material,
raycaster,
_ray,
position,
morphPosition,
morphTargetsRelative,
uv,
uv2,
a,
b,
c
);
if (intersection) {
intersection.faceIndex = Math.floor(i / 3); // triangle number in indexed buffer semantics
intersects.push(intersection);
}
}
}
} else if (position !== undefined) {
// non-indexed buffer geometry
if (Array.isArray(material)) {
for (let i = 0, il = groups.length; i < il; i++) {
const group = groups[i];
const groupMaterial = material[group.materialIndex];
const start = Math.max(group.start, drawRange.start);
const end = Math.min(position.count, Math.min(group.start + group.count, drawRange.start + drawRange.count));
for (let j = start, jl = end; j < jl; j += 3) {
const a = j;
const b = j + 1;
const c = j + 2;
intersection = checkBufferGeometryIntersection(
this,
groupMaterial,
raycaster,
_ray,
position,
morphPosition,
morphTargetsRelative,
uv,
uv2,
a,
b,
c
);
if (intersection) {
intersection.faceIndex = Math.floor(j / 3); // triangle number in non-indexed buffer semantics
intersection.face.materialIndex = group.materialIndex;
intersects.push(intersection);
}
}
}
} else {
const start = Math.max(0, drawRange.start);
const end = Math.min(position.count, drawRange.start + drawRange.count);
for (let i = start, il = end; i < il; i += 3) {
const a = i;
const b = i + 1;
const c = i + 2;
intersection = checkBufferGeometryIntersection(
this,
material,
raycaster,
_ray,
position,
morphPosition,
morphTargetsRelative,
uv,
uv2,
a,
b,
c
);
if (intersection) {
intersection.faceIndex = Math.floor(i / 3); // triangle number in non-indexed buffer semantics
intersects.push(intersection);
}
}
}
}
} else if (geometry.isGeometry) {
console.error('THREE.Mesh.raycast() no longer supports THREE.Geometry. Use THREE.BufferGeometry instead.');
}
}
}
Mesh.prototype.isMesh = true;
function checkIntersection(object, material, raycaster, ray, pA, pB, pC, point) {
let intersect;
if (material.side === BackSide) {
intersect = ray.intersectTriangle(pC, pB, pA, true, point);
} else {
intersect = ray.intersectTriangle(pA, pB, pC, material.side !== DoubleSide, point);
}
if (intersect === null) return null;
_intersectionPointWorld.copy(point);
_intersectionPointWorld.applyMatrix4(object.matrixWorld);
const distance = raycaster.ray.origin.distanceTo(_intersectionPointWorld);
if (distance < raycaster.near || distance > raycaster.far) return null;
return {
distance: distance,
point: _intersectionPointWorld.clone(),
object: object,
};
}
function checkBufferGeometryIntersection(object, material, raycaster, ray, position, morphPosition, morphTargetsRelative, uv, uv2, a, b, c) {
_vA.fromBufferAttribute(position, a);
_vB.fromBufferAttribute(position, b);
_vC.fromBufferAttribute(position, c);
const morphInfluences = object.morphTargetInfluences;
if (morphPosition && morphInfluences) {
_morphA.set(0, 0, 0);
_morphB.set(0, 0, 0);
_morphC.set(0, 0, 0);
for (let i = 0, il = morphPosition.length; i < il; i++) {
const influence = morphInfluences[i];
const morphAttribute = morphPosition[i];
if (influence === 0) continue;
_tempA.fromBufferAttribute(morphAttribute, a);
_tempB.fromBufferAttribute(morphAttribute, b);
_tempC.fromBufferAttribute(morphAttribute, c);
if (morphTargetsRelative) {
_morphA.addScaledVector(_tempA, influence);
_morphB.addScaledVector(_tempB, influence);
_morphC.addScaledVector(_tempC, influence);
} else {
_morphA.addScaledVector(_tempA.sub(_vA), influence);
_morphB.addScaledVector(_tempB.sub(_vB), influence);
_morphC.addScaledVector(_tempC.sub(_vC), influence);
}
}
_vA.add(_morphA);
_vB.add(_morphB);
_vC.add(_morphC);
}
if (object.isSkinnedMesh) {
object.boneTransform(a, _vA);
object.boneTransform(b, _vB);
object.boneTransform(c, _vC);
}
const intersection = checkIntersection(object, material, raycaster, ray, _vA, _vB, _vC, _intersectionPoint);
if (intersection) {
if (uv) {
_uvA.fromBufferAttribute(uv, a);
_uvB.fromBufferAttribute(uv, b);
_uvC.fromBufferAttribute(uv, c);
intersection.uv = Triangle.getUV(_intersectionPoint, _vA, _vB, _vC, _uvA, _uvB, _uvC, new Vector2());
}
if (uv2) {
_uvA.fromBufferAttribute(uv2, a);
_uvB.fromBufferAttribute(uv2, b);
_uvC.fromBufferAttribute(uv2, c);
intersection.uv2 = Triangle.getUV(_intersectionPoint, _vA, _vB, _vC, _uvA, _uvB, _uvC, new Vector2());
}
const face = {
a: a,
b: b,
c: c,
normal: new Vector3(),
materialIndex: 0,
};
Triangle.getNormal(_vA, _vB, _vC, face.normal);
intersection.face = face;
}
return intersection;
}
export { Mesh };
|