import { initializeContext, Vec3, Mat4 } from "./common.js"; import { Graphics, fullscreenCanvas, Texture } from "./graphics.js"; import * as drawing from "./draw.js"; import * as wasm from "./wasm.js"; const vertexShader = `#version 300 es in vec3 a_position; in vec2 a_tex_position; in vec4 a_color; out vec2 v_tex_position; out vec4 v_color; uniform mat4 u_matrix; uniform bool u_isTex; void main() { vec4 transformed = u_matrix * vec4(a_position.xyz, 1.0); gl_Position = transformed; if (u_isTex) { v_tex_position = a_tex_position; } else { v_color = a_color; } } `; const fragmentShader = `#version 300 es precision highp float; in vec2 v_tex_position; in vec4 v_color; out vec4 outColor; uniform bool u_isTex; uniform sampler2D u_texture; void main() { if (u_isTex) { outColor = texture(u_texture, v_tex_position); } else { outColor = v_color; } } `; function draw(gfx, angle, tex) { gfx.clear(0, 0, 0, 0); let right = gfx.ctx.canvas.width; let left = 0; let top = gfx.ctx.canvas.height; let bottom = 0; let near = -100; let far = 100; let mo = Mat4.orthographic(left, right, bottom, top, near, far); let mi = Mat4.isometric(); let m = mi.multNew(mo); //m = m.multNew(Mat4.rotation_x(angle)); //m = m.multNew(Mat4.rotation_y(angle)); //m = m.multNew(Mat4.rotation_z(angle)); gfx.ctx.uniformMatrix4fv(gfx.getUniform("u_matrix"), false, m.splat()); let exts = new Vec3(50, 50, 20); for (let i = 0; i < 10; ++i) { for (let j = 0; j < 10; ++j) { //drawing.drawIsometricCube(gfx, new Vec3(exts.x * i, 1000 - exts.y * j, 0), exts, [Math.sin(angle * i + j), Math.cos(angle * i + j), -Math.sin(angle * i + j), 1]); if ((i + j) % 2) drawing.drawIsometricCube(gfx, new Vec3(exts.x * i, 1000 - exts.y * j, 0), exts, [1, 1, 1, 1]); else drawing.drawIsometricCube(gfx, new Vec3(exts.x * i, 1000 - exts.y * j, 0), exts, [0, 0, 0, 1]); } } } (async () => { const canvasId = "game"; const ctx = initializeContext(canvasId); if (ctx === null) return; const gfx = new Graphics(ctx, vertexShader, fragmentShader); fullscreenCanvas(gfx, canvasId); const a_position = gfx.createAttribute("a_position"); a_position.format(3, gfx.ctx.FLOAT, false, 0, 0); const a_color = gfx.createAttribute("a_color"); a_color.format(4, gfx.ctx.FLOAT, false, 0, 0); const a_tex_position = gfx.createAttribute("a_tex_position"); a_tex_position.format(2, gfx.ctx.FLOAT, false, 0, 0); gfx.createUniform("u_matrix"); gfx.createUniform("u_isTex"); let city = await Texture.load(ctx, "../../assets/genetica/rt/City Night.jpg"); let angle = 0; let prevTimestamp = 0; const frame = (timestamp) => { const deltaTime = (timestamp - prevTimestamp) / 1000; prevTimestamp = timestamp; fullscreenCanvas(gfx, "game"); draw(gfx, angle, city); angle += Math.PI * deltaTime * 0.5; window.requestAnimationFrame(frame); }; window.requestAnimationFrame((timestamp) => { prevTimestamp = timestamp; window.requestAnimationFrame(frame); }); let wasmgl = new wasm.WASMGL(await wasm.loadWasmModule("./src/wasm/module.wasm")); })();