fix: proper update with locking
This commit is contained in:
parent
b63b7d2b3f
commit
77c7b9cade
1 changed files with 22 additions and 21 deletions
7
main.mjs
7
main.mjs
|
@ -10,6 +10,7 @@ let mouse_pos = [0, 0];
|
|||
|
||||
// Update the Camera position
|
||||
function updateCamera(gl, program) {
|
||||
if (document.pointerLockElement) {
|
||||
if (keymap.get("w")) {
|
||||
camera_pos[2] += 0.1;
|
||||
}
|
||||
|
@ -28,6 +29,7 @@ function updateCamera(gl, program) {
|
|||
if (keymap.get("q")) {
|
||||
camera_pos[1] += -0.1;
|
||||
}
|
||||
}
|
||||
|
||||
// Position
|
||||
const pos = gl.getUniformLocation(program, "camera_pos");
|
||||
|
@ -51,15 +53,15 @@ function draw(gl, program) {
|
|||
TIME += 1 / 60;
|
||||
gl.uniform1f(loc, TIME);
|
||||
|
||||
if (document.pointerLockElement) {
|
||||
updateCamera(gl, program);
|
||||
}
|
||||
|
||||
// Draw the frame recursively on next frame
|
||||
gl.drawArrays(gl.TRIANGLES, 0, 6);
|
||||
requestAnimationFrame(() => draw(gl, program));
|
||||
}
|
||||
|
||||
//** MAIN PROGRAM **//
|
||||
|
||||
// Initialize the shader
|
||||
const [gl, program, canvas] = await initShader(CANVAS_WIDTH, CANVAS_HEIGHT);
|
||||
|
||||
|
@ -85,5 +87,4 @@ canvas.addEventListener("click", async () => {
|
|||
});
|
||||
|
||||
// Update loop
|
||||
updateCamera(gl, program);
|
||||
draw(gl, program);
|
||||
|
|
Loading…
Reference in a new issue