webgl/public/gl/core/Entity.js

55 lines
1.5 KiB
JavaScript
Raw Normal View History

2023-01-19 03:43:02 +00:00
import { Vec3 } from '../math/Vec3.js'
import { Mat4 } from '../math/Mat4.js'
export class Entity {
static #id = 0
constructor(mesh) {
this.mesh = mesh
this.position = new Vec3()
this.rotation = new Vec3()
this.scale = new Vec3(1, 1, 1)
this.id = Entity.#id
Entity.#id++
}
tran() {
2023-01-19 04:44:45 +00:00
this.rotation.x %= 360;
this.rotation.y %= 360;
this.rotation.z %= 360;
2023-01-19 18:39:40 +00:00
const tran = new Mat4().identity()
const d = tran.data
const c3 = Math.cos(this.rotation.z * (Math.PI / 180))
const s3 = Math.sin(this.rotation.z * (Math.PI / 180))
const c2 = Math.cos(this.rotation.x * (Math.PI / 180))
const s2 = Math.sin(this.rotation.x * (Math.PI / 180))
const c1 = Math.cos(this.rotation.y * (Math.PI / 180))
const s1 = Math.sin(this.rotation.y * (Math.PI / 180))
d[0] = this.scale.x * (c1 * c3 + s1 * s2 * s3);
d[1] = this.scale.x * (c2 * s3)
d[2] = this.scale.x * (c1 * s2 * s3 - c3 * s1)
d[3] = 0
d[4] = this.scale.y * (c3 * s1 * s2 - c1 * s3)
d[5] = this.scale.y * (c2 * c3)
d[6] = this.scale.y * (c1 * c3 * s2 + s1 * s3)
d[7] = 0
d[8] = this.scale.z * (c2 * s1)
d[9] = this.scale.z * (-s2)
d[10] = this.scale.z * (c1 * c2)
d[11] = 0
d[12] = this.position.x
d[13] = this.position.y
d[14] = this.position.z
d[15] = 1
return tran
2023-01-19 03:43:02 +00:00
}
}