awv3
Version:
⚡ AWV3 embedded CAD
121 lines (94 loc) • 4.33 kB
JavaScript
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import * as THREE from 'three';
var Raycaster =
/*#__PURE__*/
function (_THREE$Raycaster) {
_inheritsLoose(Raycaster, _THREE$Raycaster);
function Raycaster(interaction, options) {
var _this;
if (options === void 0) {
options = {
approach: Raycaster.Approach.FirstMatch
};
}
_this = _THREE$Raycaster.call(this) || this;
_this.interaction = interaction;
_this.view = interaction.view;
_this.linePrecision = 1.5;
return _this;
}
var _proto = Raycaster.prototype;
_proto.isActuallyVisible = function isActuallyVisible(obj) {
while (obj) {
if (obj.visible === false) return false;
if (obj.material && obj.material.visible === false) return false;
obj = obj.parent;
}
return true;
};
_proto.castObjects = function castObjects(objects, intersects, filter) {
if (intersects === void 0) {
intersects = [];
}
if (filter === void 0) {
filter = undefined;
}
for (var i = 0, l = objects.length, object; i < l; i++) {
object = objects[i];
if ( // No filter, filter is empty, or object is part of filter
//(!filter || filter.length === 0 || filter.indexOf(object) >= 0) &&
// must be interactive
object.interactive && // must have interaction
object.interaction && // must be enabled
object.interaction.enabled && // muste be activ (have interaction related listeners)
object.interaction._active && // must be visible
this.isActuallyVisible(object)) {
// ... then we intersect
this.intersect(object, object.interaction.recursive, intersects, object.interaction.pierce, object.interaction.types, object);
}
} // Sort items after priority. The receiver has it, and each object can define one as well on the prototype.
// If no priority has been given or if it yields 0, the distance will be used to determine the hits importance
intersects.sort(function (a, b) {
return (b.receiver.interaction.priority || 0) + b.object.interactionPriority - ((a.receiver.interaction.priority || 0) + a.object.interactionPriority) || a.distance - b.distance;
});
return intersects;
};
_proto.intersect = function intersect(object, recursive, intersects, pierce, types, parent) {
var op = true; // Inspect types
if (!!types) {
op = object.type === 'Object3D' || object.type === 'Group' || object.type === 'Scene' || pierce.indexOf(object.type) > -1 || types.indexOf(object.type) > -1;
} // false op stops operation right here, undefined op at least proceeds with childs
if (op == false) return;
var count = intersects.length; // true op allows raycast
if (op == true) object.raycast(this, intersects, parent.interaction.approach);
if (intersects.length != count) {
for (var i = count, intersect, l = intersects.length; i < l; i++) {
intersect = intersects[i];
intersect.receiver = parent; // If the parent/receiver is not recursive data.object should point back to it
if (!parent.interaction.recursive) {
intersect.receiver.object = parent;
}
}
} // If the root is not recursive there's no point in iterating further
if (!parent.interaction.recursive) return;
for (var _i = 0, _l = object.children.length, child, isMultiMaterial; _i < _l; _i++) {
child = object.children[_i];
isMultiMaterial = Array.isArray(child.material); // To proceed, a child:
if ( // is visible
child.visible && // is interactive
child.interactive && ( // doesn't have a material
!child.material || // OR has a single material and it is visible
!isMultiMaterial && child.material.visible === true || // OR has many materials and the first is visible
// TODO: is this really what we want? seems kind of random
isMultiMaterial && child.material[0].visible === true)) {
this.intersect(child, true, intersects, pierce, types, parent);
}
}
};
return Raycaster;
}(THREE.Raycaster); // TODO: Deprecate approaches
// Do we really need to fork each objects raycast if it perhaps only brings a small benefit anyway?
export { Raycaster as default };
Raycaster.Approach = {
Default: 'Default'
};