@react-vertex/orbit-camera
Version:
Orbit Camera and Controls for React Vertex
206 lines (166 loc) • 5.6 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.OrbitCamera = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _glMatrix = require("gl-matrix");
var _lodash = _interopRequireDefault(require("lodash.throttle"));
var OrbitCamera = /*#__PURE__*/function () {
function OrbitCamera(fov, aspect) {
var near = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
var far = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 1000;
(0, _classCallCheck2["default"])(this, OrbitCamera);
this.projection = void 0;
this.view = void 0;
this.matrix = void 0;
this.position = _glMatrix.vec3.create();
this.up = _glMatrix.vec3.create();
this.right = _glMatrix.vec3.create();
this.normal = _glMatrix.vec3.create();
this.userRotate = true;
this.userRotateX = true;
this.userRotateY = true;
this.userDolly = true;
this.rotX = 0;
this.rotY = 0;
this.steps = 0;
this.listeners = [];
this.matrix = _glMatrix.mat4.create();
this.view = _glMatrix.mat4.create();
_glMatrix.mat4.invert(this.view, this.matrix);
var radians = fov * Math.PI / 180.0;
this.projection = _glMatrix.mat4.create();
_glMatrix.mat4.perspective(this.projection, radians, aspect, near, far);
}
(0, _createClass2["default"])(OrbitCamera, [{
key: "setProjection",
value: function setProjection(fov, aspect) {
var near = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
var far = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 1000;
var radians = fov * Math.PI / 180.0;
_glMatrix.mat4.perspective(this.projection, radians, aspect, near, far);
}
}, {
key: "dolly",
value: function dolly(delta) {
if (this.userDolly) {
var next = _glMatrix.vec3.create();
var step = delta - this.steps;
next[0] = this.position[0];
next[1] = this.position[1];
next[2] = this.position[2] - step;
this.steps = delta;
this.setPosition(next);
}
}
}, {
key: "setPosition",
value: function setPosition(position) {
this.position[0] = position[0] || 0;
this.position[1] = position[1] || 0;
this.position[2] = position[2] || 0;
this.update();
}
}, {
key: "upRightNormal",
value: function upRightNormal() {
var up = _glMatrix.vec4.create();
_glMatrix.vec4.set(up, 0, 1, 0, 0);
_glMatrix.vec4.transformMat4(up, up, this.matrix);
_glMatrix.vec3.copy(this.up, up);
var right = _glMatrix.vec4.create();
_glMatrix.vec4.set(right, 1, 0, 0, 0);
_glMatrix.vec4.transformMat4(right, right, this.matrix);
_glMatrix.vec3.copy(this.right, right);
var normal = _glMatrix.vec4.create();
_glMatrix.vec4.set(normal, 0, 0, 1, 0);
_glMatrix.vec4.transformMat4(normal, normal, this.matrix);
_glMatrix.vec3.copy(this.normal, normal);
}
}, {
key: "setRotationX",
value: function setRotationX(rotX) {
this.rotX = rotX;
if (this.rotX > 360 || this.rotX < -360) {
this.rotX = this.rotX % 360;
}
this.update();
}
}, {
key: "incRotationX",
value: function incRotationX(rotX) {
if (this.userRotate && this.userRotateX) {
this.rotX += rotX;
if (this.rotX > 360 || this.rotX < -360) {
this.rotX = this.rotX % 360;
}
this.update();
}
}
}, {
key: "setRotationY",
value: function setRotationY(rotY) {
this.rotY = rotY;
if (this.rotY > 360 || this.rotY < -360) {
this.rotY = this.rotY % 360;
}
this.update();
}
}, {
key: "incRotationY",
value: function incRotationY(rotY) {
if (this.userRotate && this.userRotateY) {
this.rotY += rotY;
if (this.rotY > 360 || this.rotY < -360) {
this.rotY = this.rotY % 360;
}
this.update();
}
}
}, {
key: "addListener",
value: function addListener(func) {
var wait = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 16;
var listener = (0, _lodash["default"])(func, wait);
this.listeners.push({
listener: listener,
id: func
});
}
}, {
key: "removeListener",
value: function removeListener(func) {
var index = this.listeners.findIndex(function (d) {
return d.id === func;
});
if (index !== -1) {
this.listeners.splice(index, 1);
}
}
}, {
key: "exportView",
value: function exportView() {
return _glMatrix.mat4.clone(this.view);
}
}, {
key: "update",
value: function update() {
var _this = this;
_glMatrix.mat4.identity(this.matrix);
_glMatrix.mat4.rotateX(this.matrix, this.matrix, this.rotX * Math.PI / 180);
_glMatrix.mat4.rotateY(this.matrix, this.matrix, this.rotY * Math.PI / 180);
_glMatrix.mat4.translate(this.matrix, this.matrix, this.position);
_glMatrix.mat4.invert(this.view, this.matrix);
this.upRightNormal();
this.listeners.forEach(function (item) {
item.listener(_this);
});
}
}]);
return OrbitCamera;
}();
exports.OrbitCamera = OrbitCamera;
//# sourceMappingURL=OrbitCamera.js.map