46 lines
No EOL
1.3 KiB
JavaScript
46 lines
No EOL
1.3 KiB
JavaScript
import { Vec3 } from '../math/Vec3.js'
|
|
import { Mat4 } from '../math/Mat4.js'
|
|
|
|
export class Camera {
|
|
|
|
constructor() {
|
|
this.position = new Vec3()
|
|
this.rotation = new Vec3()
|
|
}
|
|
|
|
view() {
|
|
const view = new Mat4()
|
|
const d = view.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))
|
|
|
|
const u = new Vec3((c1 * c3 + s1 * s2 * s3), (c2 * s3), (c1 * s2 * s3 - c3 * s1));
|
|
const v = new Vec3((c3 * s1 * s2 - c1 * s3), (c2 * c3), (c1 * c3 * s2 + s1 * s3));
|
|
const w = new Vec3((c2 * s1), (-s2), (c1 * c2));
|
|
|
|
d[0] = u.x;
|
|
d[1] = v.x;
|
|
d[2] = w.x;
|
|
d[3] = 1
|
|
d[4] = u.y;
|
|
d[5] = v.y;
|
|
d[6] = w.y;
|
|
d[7] = 1
|
|
d[8] = u.z;
|
|
d[9] = v.z;
|
|
d[10] = w.z;
|
|
d[11] = 1
|
|
d[12] = -u.dot(this.position)
|
|
d[13] = -v.dot(this.position)
|
|
d[14] = -w.dot(this.position)
|
|
d[15] = 1
|
|
|
|
return view
|
|
}
|
|
|
|
} |