UNPKG

@mlightcad/three-viewcube

Version:

A highly customizable standalone view cube addon for three.js

135 lines 5.8 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); import * as THREE from 'three'; /** * Enum to define postion of the gizmo. */ export var ObjectPosition; (function (ObjectPosition) { ObjectPosition[ObjectPosition["LEFT_BOTTOM"] = 0] = "LEFT_BOTTOM"; ObjectPosition[ObjectPosition["LEFT_TOP"] = 1] = "LEFT_TOP"; ObjectPosition[ObjectPosition["RIGHT_TOP"] = 2] = "RIGHT_TOP"; ObjectPosition[ObjectPosition["RIGHT_BOTTOM"] = 4] = "RIGHT_BOTTOM"; })(ObjectPosition || (ObjectPosition = {})); /** * A customizable gizmo with fixed postion in viewport */ var FixedPosGizmo = /** @class */ (function (_super) { __extends(FixedPosGizmo, _super); /** * Construct one instance of this gizmo * @param camera Camera used in your canvas * @param renderer Renderer used in your canvas * @param dimension Size of area ocupied by this gizmo. Because width and height of this area is same, * it is single value. The real size of the objet will be calculated automatically considering rotation. * @param pos Position of the gizmo */ function FixedPosGizmo(camera, renderer, dimension, pos) { if (dimension === void 0) { dimension = 150; } if (pos === void 0) { pos = ObjectPosition.RIGHT_TOP; } var _this = _super.call(this) || this; _this.camera = camera; _this.renderer = renderer; _this.gizmoCamera = new THREE.OrthographicCamera(-2, 2, 2, -2, 0, 4); _this.gizmoCamera.position.set(0, 0, 2); _this.gizmoDim = dimension; _this.gizmoPos = pos; _this.initialize(); return _this; } /** * Function called by constructor to initialize this gizmo. The children class can override this function * to add its own initialization logic. */ FixedPosGizmo.prototype.initialize = function () { }; /** * Update and rerender this gizmo */ FixedPosGizmo.prototype.update = function () { this.updateOrientation(); // Store autoClear flag value var autoClear = this.renderer.autoClear; this.renderer.autoClear = false; this.renderer.clearDepth(); var viewport = new THREE.Vector4(); this.renderer.getViewport(viewport); var pos = this.calculateViewportPos(); this.renderer.setViewport(pos.x, pos.y, this.gizmoDim, this.gizmoDim); this.renderer.render(this, this.gizmoCamera); this.renderer.setViewport(viewport.x, viewport.y, viewport.z, viewport.w); // Restore autoClear flag vlaue this.renderer.autoClear = autoClear; }; /** * Free the GPU-related resources allocated by this instance. Call this method whenever this instance * is no longer used in your app. */ FixedPosGizmo.prototype.dispose = function () { }; FixedPosGizmo.prototype.updateOrientation = function () { this.quaternion.copy(this.camera.quaternion).invert(); this.updateMatrixWorld(); }; FixedPosGizmo.prototype.calculatePosInViewport = function (offsetX, offsetY, bbox) { var x = ((offsetX - bbox.min.x) / this.gizmoDim) * 2 - 1; var y = -((offsetY - bbox.min.y) / this.gizmoDim) * 2 + 1; return { x: x, y: y }; }; FixedPosGizmo.prototype.calculateViewportPos = function () { var domElement = this.renderer.domElement; var canvasWidth = domElement.offsetWidth; var canvasHeight = domElement.offsetHeight; var pos = this.gizmoPos; var length = this.gizmoDim; var x = canvasWidth - length; var y = canvasHeight - length; switch (pos) { case ObjectPosition.LEFT_BOTTOM: x = 0; y = 0; break; case ObjectPosition.LEFT_TOP: x = 0; break; case ObjectPosition.RIGHT_BOTTOM: y = 0; break; } return { x: x, y: y }; }; FixedPosGizmo.prototype.calculateViewportBbox = function () { var domElement = this.renderer.domElement; var canvasWidth = domElement.offsetWidth; var canvasHeight = domElement.offsetHeight; var pos = this.gizmoPos; var length = this.gizmoDim; var bbox = new THREE.Box2(new THREE.Vector2(canvasWidth - length, 0), new THREE.Vector2(canvasWidth, length)); switch (pos) { case ObjectPosition.LEFT_BOTTOM: bbox.set(new THREE.Vector2(0, canvasHeight - length), new THREE.Vector2(length, canvasHeight)); break; case ObjectPosition.LEFT_TOP: bbox.set(new THREE.Vector2(0, 0), new THREE.Vector2(length, length)); break; case ObjectPosition.RIGHT_BOTTOM: bbox.set(new THREE.Vector2(canvasWidth - length, canvasHeight - length), new THREE.Vector2(canvasWidth, canvasHeight)); break; } return bbox; }; return FixedPosGizmo; }(THREE.Object3D)); export { FixedPosGizmo }; //# sourceMappingURL=fixedPosGizmo.js.map