awv3
Version:
⚡ AWV3 embedded CAD
331 lines (258 loc) • 10.6 kB
JavaScript
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import * as THREE from 'three';
import Object3 from '../../../three/object3';
import View from '../../../core/view';
var BaseGraphics =
/*#__PURE__*/
function (_THREE$Object3D) {
_inheritsLoose(BaseGraphics, _THREE$Object3D);
function BaseGraphics(plugin, nLines, nArcs, nArrows) {
var _this2;
var _this;
if (nLines === void 0) {
nLines = 0;
}
if (nArcs === void 0) {
nArcs = 0;
}
if (nArrows === void 0) {
nArrows = 0;
}
_this = _THREE$Object3D.call(this) || this;
Object.defineProperty(_assertThisInitialized(_this), "onRender", {
configurable: true,
enumerable: true,
writable: true,
value: function value() {
var position = new THREE.Vector3();
_this.handles[0].getWorldPosition(position);
var scale = _this.view.calculateScaleFactor(position, 1.0);
for (var _iterator = _this.lines || [], _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
var _ref;
if (_isArray) {
if (_i >= _iterator.length) break;
_ref = _iterator[_i++];
} else {
_i = _iterator.next();
if (_i.done) break;
_ref = _i.value;
}
var _line = _ref;
_line.scale.x = _line.scale.z = scale;
}
for (var _iterator2 = _this.arcs || [], _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
var _ref2;
if (_isArray2) {
if (_i2 >= _iterator2.length) break;
_ref2 = _iterator2[_i2++];
} else {
_i2 = _iterator2.next();
if (_i2.done) break;
_ref2 = _i2.value;
}
var _arc = _ref2;
for (var _iterator4 = _arc, _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : _iterator4[Symbol.iterator]();;) {
var _ref4;
if (_isArray4) {
if (_i4 >= _iterator4.length) break;
_ref4 = _iterator4[_i4++];
} else {
_i4 = _iterator4.next();
if (_i4.done) break;
_ref4 = _i4.value;
}
var _arcSegment = _ref4;
_arcSegment.scale.x = _arcSegment.scale.z = scale;
}
}
for (var _iterator3 = _this.arrows || [], _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
var _ref3;
if (_isArray3) {
if (_i3 >= _iterator3.length) break;
_ref3 = _iterator3[_i3++];
} else {
_i3 = _iterator3.next();
if (_i3.done) break;
_ref3 = _i3.value;
}
var _arrow = _ref3;
_arrow.scale.set(7 * scale, 15 * scale, 7 * scale);
}
var pos2 = _this.view.getPoint2(position.clone());
if (!_this.dimPos2.equals(pos2)) {
_this.dimPos2.copy(pos2);
_this.textNode.style.transform = "translate3d(" + (pos2.x + 15) + "px, calc(-50% + " + (pos2.y - 15) + "px), 0)";
}
if (_this.textNode.innerHTML !== _this.handles[0].text) _this.textNode.innerHTML = _this.handles[0].text;
}
});
var colors = plugin.session.globals.colors;
_this.lineGeometry = new THREE.CylinderBufferGeometry(1, 1, 1);
_this.lineMaterial = new THREE.MeshBasicMaterial({
color: colors.primary
});
_this.arcSegmentGeometry = new THREE.CylinderBufferGeometry(1, 1, 1, 4, 1);
_this.arcSegmentMaterial = new THREE.MeshBasicMaterial({
color: colors.primary
});
_this.arrowGeometry = new THREE.ConeBufferGeometry(1, 1);
_this.arrowGeometry.translate(0, -0.5, 0);
_this.arrowMaterial = new THREE.MeshBasicMaterial({
color: colors.primary
});
_this.arcSegments = 32;
_this.plugin = plugin;
_this.handles = plugin.handlePrototypes.map(function (handlePrototype) {
return new handlePrototype(plugin);
});
(_this2 = _this).add.apply(_this2, _this.handles);
if (nLines) {
var _this3;
_this.lines = [];
for (var i = 0; i < nLines; ++i) {
_this.lines.push(new THREE.Mesh(_this.lineGeometry, _this.lineMaterial));
}
(_this3 = _this).add.apply(_this3, _this.lines);
}
if (nArcs) {
var _this4;
_this.arcs = [];
for (var _i5 = 0; _i5 < nArcs; ++_i5) {
_this.arcs.push([]);
for (var j = 0; j < _this.arcSegments; ++j) {
_this.arcs[_i5].push(new THREE.Mesh(_this.arcSegmentGeometry, _this.arcSegmentMaterial));
}
}
(_this4 = _this).add.apply(_this4, _this.arcs);
}
if (nArrows) {
var _this5;
_this.arrows = [];
for (var _i6 = 0; _i6 < nArrows; ++_i6) {
_this.arrows.push(new THREE.Mesh(_this.arrowGeometry, _this.arrowMaterial));
}
(_this5 = _this).add.apply(_this5, _this.arrows);
}
_this.dimPos2 = new THREE.Vector2();
_this.text = '';
_this.textNode = document.createElement('div');
_this.textNode.setAttribute('style', "position: absolute;\n display: inline-block;\n top: 0; left: 0; padding: 1px 2px;\n color: " + colors.primary + ";\n font-size: 9px;\n pointer-events: none;");
_this.userData.meta = {};
for (var _iterator5 = _this.handles, _isArray5 = Array.isArray(_iterator5), _i7 = 0, _iterator5 = _isArray5 ? _iterator5 : _iterator5[Symbol.iterator]();;) {
var _ref5;
if (_isArray5) {
if (_i7 >= _iterator5.length) break;
_ref5 = _iterator5[_i7++];
} else {
_i7 = _iterator5.next();
if (_i7.done) break;
_ref5 = _i7.value;
}
var _hdl = _ref5;
_hdl.type = 'DimensionHandle';
}
_this.handles[0].material.meta = {
material: {
color: _this.handles[0].material.color.clone(),
opacity: _this.handles[0].material.opacity
}
};
_this.destroyed = false;
_this.viewFound().then(function (view) {
if (_this.destroyed) return;
view.dom.appendChild(_this.textNode);
_this.createInteraction({
priority: 1000
}).on(Object3.Events.Lifecycle.Rendered, _this.onRender, {
sync: false
});
});
return _this;
}
var _proto = BaseGraphics.prototype;
_proto.destroy = function destroy() {
this.destroyed = true;
if (this.textNode.parentElement) this.textNode.parentElement.removeChild(this.textNode);
_THREE$Object3D.prototype.destroy.call(this);
};
_proto.updateLine = function updateLine(i, start, end) {
var line = Number.isInteger(i) ? this.lines[i] : i; // line.geometry describes a cylinder with axis from [0, -0.5, 0] to [0, 0.5, 0] and raidus 1
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.updateArc = function updateArc(i, center, radius, startAngle, endAngle) {
var _this6 = this;
var arc = this.arcs[i];
for (var j = 0; j < this.arcSegments; ++j) {
var arcSegment = arc[j];
var _map = [j, j + 1].map(function (k) {
return THREE.Math.lerp(startAngle, endAngle, k / _this6.arcSegments);
}),
sStartAngle = _map[0],
sEndAngle = _map[1];
var _map2 = [sStartAngle, sEndAngle].map(function (angle) {
return new THREE.Vector3(Math.cos(angle), Math.sin(angle), 0).multiplyScalar(radius).add(center);
}),
start = _map2[0],
end = _map2[1];
this.updateLine(arcSegment, start, end);
}
};
_proto.updateArrow = function updateArrow(i, end, dir) {
var arrow = this.arrows[i]; // arrow.geometry describes a cone with axis from [0, -1, 0] (r=1) to [0, 0, 0] (r=0)
arrow.position.copy(end);
arrow.quaternion.setFromUnitVectors(new THREE.Vector3(0, 1, 0), dir.clone().normalize());
};
_proto.updateCoordinateSystem = function updateCoordinateSystem(csys, instanceCsys) {
csys = csys.map(function (row) {
return new THREE.Vector3().fromArray(row);
});
instanceCsys = instanceCsys.map(function (row) {
return new THREE.Vector3().fromArray(row);
});
this.matrix.makeBasis(instanceCsys[1], instanceCsys[2], instanceCsys[3]).setPosition(instanceCsys[0]).multiply(new THREE.Matrix4().makeBasis(csys[1], csys[2], csys[3]).setPosition(csys[0])).decompose(this.position, this.quaternion, this.scale);
this.matrixWorldNeedsUpdate = true;
this.updateMatrixWorld();
};
_proto.updateFromState = function updateFromState(info) {
this.textNode.style.display = info.visible ? 'inline-block' : 'none';
this.visible = info.visible;
this.lastState = info;
this.userData.meta.id = info.dimension.id;
this.updateCoordinateSystem(info.dimension.coordinateSystem, info.instanceCsys);
this.handles.forEach(function (handle) {
return handle.updateFromState(info);
});
if (!info.virtual) {
this.handles[0].material.meta.id = info.dimension.id;
this.handles[0].material.meta.box = this.handles[0].updateBounds().bounds.box;
} else {
//drop meta if this is preview dimension
//(when creating new angular dimension on two lines)
for (var _iterator6 = this.handles, _isArray6 = Array.isArray(_iterator6), _i8 = 0, _iterator6 = _isArray6 ? _iterator6 : _iterator6[Symbol.iterator]();;) {
var _ref6;
if (_isArray6) {
if (_i8 >= _iterator6.length) break;
_ref6 = _iterator6[_i8++];
} else {
_i8 = _iterator6.next();
if (_i8.done) break;
_ref6 = _i8.value;
}
var _handle = _ref6;
_handle.material.meta = undefined;
}
}
var colors = this.plugin.session.globals.colors;
this.lineMaterial.color = new THREE.Color(colors.primary);
this.arcSegmentMaterial.color = new THREE.Color(colors.primary);
this.arrowMaterial.color = new THREE.Color(colors.primary);
this.textNode.style.color = colors.primary;
this.onRender();
this.view.invalidate();
};
return BaseGraphics;
}(THREE.Object3D);
export { BaseGraphics as default };