awv3
Version:
⚡ AWV3 embedded CAD
145 lines (104 loc) • 4.47 kB
JavaScript
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import * as THREE from 'three';
import { createCaption } from 'awv3/three/helpers';
var LinearDimension =
/*#__PURE__*/
function (_THREE$Object3D) {
_inheritsLoose(LinearDimension, _THREE$Object3D);
function LinearDimension(start, end, offset, label) {
var _this2, _this3, _this$caption$positio;
var _this;
_this = _THREE$Object3D.call(this) || this;
_this.rotateOnWorldAxis(new THREE.Vector3(0, 1, 0), Math.PI / 2);
_this.lineGeometry = new THREE.CylinderGeometry(0.5, 0.5, 1);
_this.lineMaterial = new THREE.MeshBasicMaterial({
color: '#00aa00'
});
_this.arrowGeometry = new THREE.CylinderGeometry(0.5, 2.5, 7);
_this.arrowGeometry.translate(0, -3.5, 0);
_this.arrowMaterial = new THREE.MeshBasicMaterial({
color: '#ff0000'
});
_this.lines = [];
for (var i = 0; i < 4; ++i) {
_this.lines.push(new THREE.Mesh(_this.lineGeometry, _this.lineMaterial));
}
(_this2 = _this).add.apply(_this2, _this.lines);
_this.arrows = [];
for (var _i = 0; _i < 2; ++_i) {
_this.arrows.push(new THREE.Mesh(_this.arrowGeometry, _this.arrowMaterial));
}
(_this3 = _this).add.apply(_this3, _this.arrows);
var startVec = new (Function.prototype.bind.apply(THREE.Vector3, [null].concat(start)))();
var endVec = new (Function.prototype.bind.apply(THREE.Vector3, [null].concat(end)))();
var offVec = new (Function.prototype.bind.apply(THREE.Vector3, [null].concat(offset)))();
var dimVec = startVec.clone().add(endVec).divideScalar(2);
if (offVec.x !== 0) {
dimVec.x = offVec.x;
} else if (offVec.y !== 0) {
dimVec.y = offVec.y;
}
var directionX = offVec.x === 0;
var dirVec = new THREE.Vector3(directionX ? 1 : 0, directionX ? 0 : 1, 0);
var _map = [startVec, endVec].map(function (x) {
return x.clone().sub(dimVec).projectOnVector(dirVec).add(dimVec);
}),
startFootVec = _map[0],
endFootVec = _map[1];
if (endFootVec.clone().sub(startFootVec).dot(dirVec) < 0) dirVec.negate();
_this.updateLine(0, startVec, startFootVec, false);
_this.updateLine(1, startFootVec, dimVec, true);
_this.updateLine(2, dimVec, endFootVec, true);
_this.updateLine(3, endFootVec, endVec, false);
_this.updateArrow(0, startFootVec, dirVec.clone().multiplyScalar(-1));
_this.updateArrow(1, endFootVec, dirVec.clone().multiplyScalar(1));
_this.caption = createCaption(label, 'black', 1, 'Arial', 85);
(_this$caption$positio = _this.caption.position).set.apply(_this$caption$positio, dimVec.toArray());
_this.add(_this.caption);
return _this;
}
var _proto = LinearDimension.prototype;
_proto.updateLine = function updateLine(i, start, end, shorten) {
var line = this.lines[i];
line.position.lerpVectors(start, end, 0.5);
line.quaternion.setFromUnitVectors(new THREE.Vector3(0, 1, 0), end.clone().sub(start).normalize());
line.scale.y = start.distanceTo(end);
};
_proto.updateArrow = function updateArrow(i, end, dir) {
var arrow = this.arrows[i];
arrow.position.copy(end);
arrow.quaternion.setFromUnitVectors(new THREE.Vector3(0, 1, 0), dir.clone().normalize());
};
_proto.updateScale = function updateScale(scale) {
for (var _iterator = this.lines || [], _isArray = Array.isArray(_iterator), _i2 = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
var _ref;
if (_isArray) {
if (_i2 >= _iterator.length) break;
_ref = _iterator[_i2++];
} else {
_i2 = _iterator.next();
if (_i2.done) break;
_ref = _i2.value;
}
var _line = _ref;
_line.scale.x = _line.scale.z = scale;
}
for (var _iterator2 = this.arrows || [], _isArray2 = Array.isArray(_iterator2), _i3 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
var _ref2;
if (_isArray2) {
if (_i3 >= _iterator2.length) break;
_ref2 = _iterator2[_i3++];
} else {
_i3 = _iterator2.next();
if (_i3.done) break;
_ref2 = _i3.value;
}
var _arrow = _ref2;
_arrow.scale.set(scale, scale, scale);
}
var captionScale = scale * 75;
this.caption.scale.set(captionScale, captionScale, captionScale);
};
return LinearDimension;
}(THREE.Object3D);
export { LinearDimension as default };