UNPKG

ts-game-engine

Version:

Simple WebGL game/render engine written in TypeScript

92 lines (91 loc) 4.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const Component_1 = require("./Component"); const gl_matrix_1 = require("gl-matrix"); class Transform extends Component_1.Component { constructor() { super(); this.position = gl_matrix_1.vec3.create(); this.rotation = gl_matrix_1.quat.create(); this.scale = gl_matrix_1.vec3.fromValues(1, 1, 1); this.modelMatrix = gl_matrix_1.mat4.create(); this.normalMatrix = gl_matrix_1.mat3.create(); this.right = gl_matrix_1.vec3.fromValues(1, 0, 0); this.up = gl_matrix_1.vec3.fromValues(0, 1, 0); this.forward = gl_matrix_1.vec3.fromValues(0, 0, -1); this.isTransformDirty = true; } get Position() { return this.position; } set Position(position) { if (gl_matrix_1.vec3.equals(position, this.position)) return; this.position = position; this.isTransformDirty = true; } get Rotation() { return this.rotation; } set Rotation(rotation) { if (gl_matrix_1.quat.equals(rotation, this.rotation)) return; this.rotation = rotation; this.isTransformDirty = true; } get Scale() { return this.scale; } set Scale(scale) { if (gl_matrix_1.vec3.equals(scale, this.scale)) return; this.scale = scale; this.isTransformDirty = true; } get ModelMatrix() { return this.modelMatrix; } get NormalMatrix() { return this.normalMatrix; } get Right() { return this.right; } get Up() { return this.up; } get Forward() { return this.forward; } Update(deltaTime) { if (this.isTransformDirty) this.UpdateTransform(); } Move(direction) { if (direction[0] === 0 && direction[1] === 0 && direction[2] === 0) return; gl_matrix_1.vec3.add(this.position, this.position, direction); this.isTransformDirty = true; } Rotate(x, y, z, worldSpace) { if (x === 0 && y === 0 && z === 0) return; // quat.rotateX(this.rotation, this.rotation, glMatrix.toRadian(x)); // quat.rotateY(this.rotation, this.rotation, glMatrix.toRadian(y)); // quat.rotateZ(this.rotation, this.rotation, glMatrix.toRadian(z)); let q = gl_matrix_1.quat.create(); gl_matrix_1.quat.fromEuler(q, x, y, z); if (worldSpace) { let tempQ = gl_matrix_1.quat.create(); let invertRotation = gl_matrix_1.quat.create(); gl_matrix_1.quat.invert(invertRotation, this.rotation); gl_matrix_1.quat.multiply(this.rotation, this.rotation, gl_matrix_1.quat.multiply(tempQ, invertRotation, gl_matrix_1.quat.multiply(tempQ, q, this.rotation))); } else { gl_matrix_1.quat.multiply(this.rotation, this.rotation, q); } this.isTransformDirty = true; } SetEulerAngles(x, y, z) { const newRotation = gl_matrix_1.quat.create(); gl_matrix_1.quat.fromEuler(newRotation, x, y, z); this.Rotation = newRotation; } UpdateTransform() { var _a, _b; gl_matrix_1.mat4.fromRotationTranslationScale(this.modelMatrix, this.rotation, this.position, this.scale); if (this.scale[0] === this.scale[1] && this.scale[0] === this.scale[2]) { gl_matrix_1.mat3.fromMat4(this.normalMatrix, this.modelMatrix); } else { let tempMatrix = gl_matrix_1.mat4.create(); gl_matrix_1.mat4.invert(tempMatrix, this.modelMatrix); gl_matrix_1.mat4.transpose(tempMatrix, tempMatrix); gl_matrix_1.mat3.fromMat4(this.normalMatrix, tempMatrix); } this.right[0] = this.modelMatrix[0]; this.right[1] = this.modelMatrix[1]; this.right[2] = this.modelMatrix[2]; this.up[0] = this.modelMatrix[4]; this.up[1] = this.modelMatrix[5]; this.up[2] = this.modelMatrix[6]; this.forward[0] = -this.modelMatrix[8]; this.forward[1] = -this.modelMatrix[9]; this.forward[2] = -this.modelMatrix[10]; (_b = (_a = this).OnTransformChange) === null || _b === void 0 ? void 0 : _b.call(_a); this.isTransformDirty = false; } } exports.Transform = Transform;