99 lines
No EOL
2.6 KiB
JavaScript
99 lines
No EOL
2.6 KiB
JavaScript
import * as GL from '/gl/gl.js'
|
|
import { gen_cube } from './cube.js'
|
|
|
|
async function main() {
|
|
|
|
var Renderer = new GL.Renderer()
|
|
var Scene = new GL.Scene()
|
|
var Camera = new GL.Camera()
|
|
|
|
var Shader = new GL.Shader(
|
|
await GL.readFileAsync("shader/simple.vert"),
|
|
await GL.readFileAsync("shader/simple.frag")
|
|
)
|
|
|
|
var Material = new GL.Material(Shader)
|
|
|
|
var cube_data = gen_cube()
|
|
var Mesh = new GL.Mesh(cube_data[2])
|
|
.store(cube_data[0], 3)
|
|
.store(cube_data[1], 3)
|
|
.finish()
|
|
|
|
var Cube = new GL.Entity(Mesh)
|
|
Cube.position.z = -4;
|
|
|
|
Scene.add(Material, Cube)
|
|
|
|
const a = new GL.Vec3(1,1,1)
|
|
|
|
GL.Loop(() => {
|
|
Renderer.draw(Scene, Camera)
|
|
Cube.rotation.add(a)
|
|
updateInputs(Camera)
|
|
});
|
|
}
|
|
|
|
const clamp = (num, min, max) => Math.min(Math.max(num, min), max);
|
|
|
|
const LEFT = 37;
|
|
const RIGHT = 39;
|
|
const UP = 38;
|
|
const DOWN = 40;
|
|
const W = 87;
|
|
const A = 65;
|
|
const S = 83;
|
|
const D = 68;
|
|
const Q = 81;
|
|
const E = 69;
|
|
const lookSpeed = 50;
|
|
const moveSpeed = 10;
|
|
function updateInputs(Camera) {
|
|
var rotate = new GL.Vec3();
|
|
if (keys[RIGHT]) rotate.y += 1;
|
|
if (keys[LEFT]) rotate.y -= 1;
|
|
if (keys[UP]) rotate.x -= 1;
|
|
if (keys[DOWN]) rotate.x += 1;
|
|
if(rotate.dot(rotate) > Number.EPSILON) {
|
|
var normal = rotate.normalize()
|
|
console.log(lookSpeed * GL.DT * normal.x, lookSpeed * GL.DT * normal.y)
|
|
Camera.rotation.x += lookSpeed * GL.DT * normal.x;
|
|
Camera.rotation.y += lookSpeed * GL.DT * normal.y;
|
|
Camera.rotation.z += lookSpeed * GL.DT * normal.z;
|
|
}
|
|
Camera.rotation.x = clamp(Camera.rotation.x, -90, 90)
|
|
Camera.rotation.y = Camera.rotation.y % 360
|
|
|
|
const yaw = Camera.rotation.y * (Math.PI/180);
|
|
var forward = new GL.Vec3(Math.sin(yaw), 0, Math.cos(yaw));
|
|
var right = new GL.Vec3(forward.z, 0, -forward.x);
|
|
var up = new GL.Vec3(0, 1, 0);
|
|
|
|
var move = new GL.Vec3();
|
|
if (keys[W]) move.sub(forward)
|
|
if (keys[S]) move.add(forward)
|
|
if (keys[A]) move.sub(right)
|
|
if (keys[D]) move.add(right)
|
|
if (keys[Q]) move.sub(up)
|
|
if (keys[E]) move.add(up)
|
|
|
|
if(move.dot(move) > Number.EPSILON) {
|
|
var normal = move.normalize()
|
|
Camera.position.x += moveSpeed * GL.DT * normal.x;
|
|
Camera.position.y += moveSpeed * GL.DT * normal.y;
|
|
Camera.position.z += moveSpeed * GL.DT * normal.z;
|
|
}
|
|
}
|
|
|
|
var keys = {}
|
|
function setupInputs() {
|
|
document.onkeydown = function(e) {
|
|
keys[e.keyCode] = true;
|
|
};
|
|
document.onkeyup = function(e) {
|
|
keys[e.keyCode] = false;
|
|
}
|
|
}
|
|
|
|
setupInputs()
|
|
main() |