@mlightcad/three-viewcube
Version:
A highly customizable standalone view cube addon for three.js
124 lines • 4.81 kB
JavaScript
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 __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
import * as THREE from 'three';
import { createTextSprite } from './viewCubeData';
import { FixedPosGizmo, ObjectPosition } from './fixedPosGizmo';
/**
* Default axes option values
*/
export var DEFAULT_AXES_OPTIONS = {
pos: ObjectPosition.LEFT_BOTTOM,
size: 100,
hasZAxis: true
};
/**
* An axis gizmo to visualize the axes in a simple way.
* The X axis is red, the Y axis is green, and the Z axis is blue by default
*/
var AxesGizmo = /** @class */ (function (_super) {
__extends(AxesGizmo, _super);
function AxesGizmo(camera, renderer, options) {
var _this = this;
var mergedOptions = __assign(__assign({}, DEFAULT_AXES_OPTIONS), options);
_this = _super.call(this, camera, renderer, mergedOptions.size, options.pos) || this;
_this.hasZAxis = mergedOptions.hasZAxis;
var vertices = [0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0];
var colors = [1, 0, 0, 1, 0.6, 0, 0, 1, 0, 0.6, 1, 0];
if (_this.hasZAxis) {
vertices.push(0, 0, 0, 0, 0, 2);
colors.push(0, 0, 1, 0, 0.6, 1);
}
var geometry = new THREE.BufferGeometry();
geometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3));
geometry.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3));
var material = new THREE.LineBasicMaterial({
vertexColors: true,
toneMapped: false
});
_this.axes = new THREE.LineSegments(geometry, material);
_this.axes.position.set(-1, -1, -1);
_this.add(_this.axes);
_this.xText = createTextSprite('X');
_this.xText.position.set(1.5, -1, -1);
_this.add(_this.xText);
_this.yText = createTextSprite('Y');
_this.yText.position.set(-1, 1.5, -1);
_this.add(_this.yText);
if (_this.hasZAxis) {
_this.zText = createTextSprite('Z');
_this.zText.position.set(-1, -1, 1.5);
_this.add(_this.zText);
}
return _this;
}
/**
* Set color of x-axis and y-axis
* @param xAxisColor color of x-axis
* @param yAxisColor color of y-axis
*/
AxesGizmo.prototype.setLineColors = function (xAxisColor, yAxisColor) {
var color = new THREE.Color();
var array = this.axes.geometry.attributes.color.array;
color.set(xAxisColor);
color.toArray(array, 0);
color.toArray(array, 3);
color.set(yAxisColor);
color.toArray(array, 6);
color.toArray(array, 9);
this.axes.geometry.attributes.color.needsUpdate = true;
return this;
};
/**
* Set text color
* @param color text color
*/
AxesGizmo.prototype.setTextColor = function (color) {
this.xText.material.color = color;
this.yText.material.color = color;
};
/**
* Free the GPU-related resources allocated by this instance. Call this method whenever this instance
* is no longer used in your app.
*/
AxesGizmo.prototype.dispose = function () {
var _a, _b;
this.axes.geometry.dispose();
var material = this.axes.material;
material.dispose();
this.xText.geometry.dispose();
this.xText.material.dispose();
this.yText.geometry.dispose();
this.yText.material.dispose();
if (this.hasZAxis) {
(_a = this.zText) === null || _a === void 0 ? void 0 : _a.geometry.dispose();
(_b = this.zText) === null || _b === void 0 ? void 0 : _b.material.dispose();
}
};
return AxesGizmo;
}(FixedPosGizmo));
export { AxesGizmo };
//# sourceMappingURL=axesGizmo.js.map