UNPKG

awv3

Version:
163 lines (137 loc) 6.42 kB
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose"; import * as THREE from 'three'; import { createCaption } from '../../three/helpers'; //Axis params: var xAxisColor = 0xc23369; var yAxisColor = 0x28d79f; var zAxisColor = 0x28b4d7; var highlightPointsColor = 0x800080; var segmentsNum = 10; //Scale multipliers (scales only on create) var offsetMultiplier = 12; var axisMultiplier = 500; var Csys = /*#__PURE__*/ function (_THREE$Object3D) { _inheritsLoose(Csys, _THREE$Object3D); function Csys(plugin, frstPoint, scndPoint) { var _this; _this = _THREE$Object3D.call(this) || this; var distance = frstPoint.distanceTo(scndPoint); var offset = distance / offsetMultiplier; var signedMaxLen = scndPoint.x - frstPoint.x; var tmp = scndPoint.y - frstPoint.y; signedMaxLen = Math.abs(signedMaxLen) < Math.abs(tmp) ? tmp : signedMaxLen; tmp = scndPoint.z - frstPoint.z; signedMaxLen = Math.abs(signedMaxLen) < Math.abs(tmp) ? tmp : signedMaxLen; signedMaxLen += 2 * offset; var segLen = Math.abs(signedMaxLen) / segmentsNum; //TODO: use scale? _this.cylinderRadius = distance / axisMultiplier; var dividerRadius = 2 * _this.cylinderRadius; var dividerLength = 2 * _this.cylinderRadius; var highlightSphereRadius = 4 * _this.cylinderRadius; var arrowLength = 8 * _this.cylinderRadius; var boundPointsRadius = 3 * _this.cylinderRadius; var startPoint = new THREE.Vector3(frstPoint.x, frstPoint.y, frstPoint.z).addScalar(-offset); var dividerGeom = new THREE.CylinderGeometry(dividerRadius, dividerRadius, dividerLength); var arrowGeom = new THREE.CylinderGeometry(0, _this.cylinderRadius * 3, arrowLength); var pointMarkGeom = new THREE.SphereGeometry(highlightSphereRadius, 10, 10); var axisInfo = [{ label: 'X', direction: new THREE.Vector3(1, 0, 0), axisLen: scndPoint.x - frstPoint.x + 2 * offset, color: xAxisColor, highlightPoints: [{ position: frstPoint, value: frstPoint.x }, { position: scndPoint, value: scndPoint.x }] }, { label: 'Y', direction: new THREE.Vector3(0, 1, 0), axisLen: scndPoint.y - frstPoint.y + 2 * offset, color: yAxisColor, highlightPoints: [{ position: frstPoint, value: frstPoint.y }, { position: scndPoint, value: scndPoint.y }] }, { label: 'Z', direction: new THREE.Vector3(0, 0, 1), axisLen: scndPoint.z - frstPoint.z + 2 * offset, color: zAxisColor, highlightPoints: [{ position: frstPoint, value: frstPoint.z }, { position: scndPoint, value: scndPoint.z }] }]; //First and second points. /* const sphereGeometry = new THREE.SphereGeometry(boundPointsRadius, 10, 10) let shpereMaterial = new THREE.MeshBasicMaterial({ color: new THREE.Color(0xff0000), }) let objBoundPoint = new THREE.Mesh(sphereGeometry, shpereMaterial.clone()) objBoundPoint.position.set(frstPoint.x, frstPoint.y, frstPoint.z) this.add(objBoundPoint) shpereMaterial.color = new THREE.Color(0x0000ff) objBoundPoint = new THREE.Mesh(sphereGeometry, shpereMaterial) objBoundPoint.position.set(scndPoint.x, scndPoint.y, scndPoint.z) this.add(objBoundPoint)*/ //Axes. axisInfo.forEach(function (curInfo) { //Draw cylinder. var axisGeom = new THREE.CylinderGeometry(_this.cylinderRadius, _this.cylinderRadius, curInfo.axisLen); var material = new THREE.MeshBasicMaterial({ color: new THREE.Color(curInfo.color) }); var xAxis = new THREE.Mesh(axisGeom, material.clone()); var curDirection = curInfo.direction; var axisPosition = curDirection.clone().multiplyScalar(curInfo.axisLen / 2); axisPosition.add(startPoint); xAxis.position.set(axisPosition.x, axisPosition.y, axisPosition.z); xAxis.quaternion.setFromUnitVectors(new THREE.Vector3(0, 1, 0), new THREE.Vector3(curDirection.x, curDirection.y, curDirection.z)); _this.add(xAxis); //TODO: use merged geometry? //Draw arrow with axis name. var arrow = new THREE.Mesh(arrowGeom, material.clone()); var arrowPosition = curDirection.clone().multiplyScalar(curInfo.axisLen + arrowLength / 2); arrowPosition.add(startPoint); arrow.position.set(arrowPosition.x, arrowPosition.y, arrowPosition.z); arrow.quaternion.setFromUnitVectors(new THREE.Vector3(0, 1, 0), new THREE.Vector3(curDirection.x, curDirection.y, curDirection.z)); _this.add(arrow); var caption = createCaption(curInfo.label, 'lightgrey', 2 * _this.cylinderRadius, 'monospace'); caption.position.set(arrowPosition.x, arrowPosition.y, arrowPosition.z); _this.add(caption); //Draw dividers. var divider = new THREE.Mesh(dividerGeom, material.clone()); divider.quaternion.setFromUnitVectors(new THREE.Vector3(0, 1, 0), new THREE.Vector3(curDirection.x, curDirection.y, curDirection.z)); var dividerPosition = curDirection.clone().multiplyScalar(segLen); dividerPosition.add(startPoint); var dividerOffset = segLen; for (var i = 0; i < segmentsNum && dividerOffset <= curInfo.axisLen; ++i) { divider.position.set(dividerPosition.x, dividerPosition.y, dividerPosition.z); _this.add(divider); divider = divider.clone(); dividerPosition.add(curDirection.clone().multiplyScalar(segLen)); dividerOffset += segLen; } //Draw highlighted points. curInfo.highlightPoints.forEach(function (curPoint) { material.color = new THREE.Color(highlightPointsColor); var pointMark = new THREE.Mesh(pointMarkGeom, material.clone()); var pointMarkPosition = curPoint.position.clone().multiply(curDirection); pointMarkPosition.add(startPoint).sub(startPoint.clone().multiply(curDirection)); pointMark.position.set(pointMarkPosition.x, pointMarkPosition.y, pointMarkPosition.z); _this.add(pointMark); var caption = createCaption(curPoint.value.toFixed(2), 'lightgrey', 2 * _this.cylinderRadius, 'monospace'); caption.position.set(pointMarkPosition.x, pointMarkPosition.y, pointMarkPosition.z); _this.add(caption); }); }); return _this; } return Csys; }(THREE.Object3D); export { Csys as default };