global timing source

This commit is contained in:
41666 2023-10-01 12:00:31 -04:00
parent 0043f3ae51
commit 8a42f36ddc
2 changed files with 13 additions and 8 deletions

View file

@ -4,6 +4,7 @@ export class App {
fov: 45, fov: 45,
} }
) { ) {
this._now = 0;
this.registry = { this.registry = {
onStart: [], onStart: [],
onUpdate: [], onUpdate: [],
@ -77,16 +78,22 @@ export class App {
} }
start() { start() {
this.registry.onStart.forEach((fn) => fn(this.gl, this)); this.registry.onStart.forEach((fn) => fn(this));
} }
update() { update() {
this.registry.onBeforeUpdate.forEach((fn) => fn(this.gl, this)); this.registry.onBeforeUpdate.forEach((fn) => fn(this));
this.registry.onUpdate.forEach((fn) => fn(this.gl, this)); this.registry.onUpdate.forEach((fn) => fn(this));
} }
loop() { loop(now) {
this._now = now;
this.update(); this.update();
requestAnimationFrame(() => this.loop());
requestAnimationFrame((newNow) => this.loop(newNow));
}
now() {
return this._now;
} }
} }

View file

@ -3,7 +3,6 @@ export class Shader {
this.gl = app.gl; this.gl = app.gl;
this.app = app; this.app = app;
this.program = this.gl.createProgram(); this.program = this.gl.createProgram();
this.startTime = Date.now();
} }
attach(type, source) { attach(type, source) {
@ -49,8 +48,7 @@ export class Shader {
} }
updateTime() { updateTime() {
const now = Date.now(); const time = this.app.now();
const time = now - this.startTime;
const sinTime = Math.sin(time); const sinTime = Math.sin(time);
const cosTime = Math.cos(time); const cosTime = Math.cos(time);
this.gl.uniform1f(this.location("uTime"), time); this.gl.uniform1f(this.location("uTime"), time);