more rendering code, not much changes, beginning refactor of the rendering API

This commit is contained in:
2026-04-02 06:31:30 +02:00
parent 92418e7e4e
commit 60313f5373
7 changed files with 317 additions and 263 deletions
+19 -2
View File
@@ -4,6 +4,7 @@ class Renderer {
private gl: WebGL2RenderingContext; private gl: WebGL2RenderingContext;
private posBuffer!: WebGLBuffer; private posBuffer!: WebGLBuffer;
private program!: WebGLProgram; private program!: WebGLProgram;
private drawMode: DrawMode = DrawMode.TEX;
constructor(canvas: HTMLCanvasElement) { constructor(canvas: HTMLCanvasElement) {
let context = canvas.getContext('webgl2'); let context = canvas.getContext('webgl2');
@@ -45,7 +46,6 @@ class Renderer {
this.gl.viewport(0, 0, this.gl.canvas.width, this.gl.canvas.height); this.gl.viewport(0, 0, this.gl.canvas.width, this.gl.canvas.height);
let resUniLoc = this.gl.getUniformLocation(program, 'u_resolution'); let resUniLoc = this.gl.getUniformLocation(program, 'u_resolution');
this.gl.uniform2f(resUniLoc, this.gl.canvas.width, this.gl.canvas.height);
this.gl.clearColor(0, 0, 0, 0); this.gl.clearColor(0, 0, 0, 0);
this.gl.clear(this.gl.COLOR_BUFFER_BIT); this.gl.clear(this.gl.COLOR_BUFFER_BIT);
@@ -56,6 +56,7 @@ class Renderer {
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, pos); this.gl.bindBuffer(this.gl.ARRAY_BUFFER, pos);
this.gl.vertexAttribPointer(0, 2, this.gl.FLOAT, false, 0, 0); this.gl.vertexAttribPointer(0, 2, this.gl.FLOAT, false, 0, 0);
this.gl.uniform2f(resUniLoc, this.gl.canvas.width, this.gl.canvas.height);
} catch (error) { } catch (error) {
console.log(error); console.log(error);
this.gl.VERTEX_SHADER; this.gl.VERTEX_SHADER;
@@ -91,7 +92,17 @@ class Renderer {
this.gl.viewport(0, 0, width, height); this.gl.viewport(0, 0, width, height);
} }
/**
* Set the draw mode to one of the enum values
* @param {DrawMode} mode
* @returns {void}
*/
public set SetDrawMode(mode: DrawMode) {}
public Draw(pos: Vector2, size: number): void { public Draw(pos: Vector2, size: number): void {
//this.gl.clearColor(0, 0, 0, 0);
// this.gl.clear(this.gl.COLOR_BUFFER_BIT);
let x1 = pos.x - size; let x1 = pos.x - size;
let x2 = pos.x + size; let x2 = pos.x + size;
let y1 = pos.y + size; let y1 = pos.y + size;
@@ -101,7 +112,7 @@ class Renderer {
this.gl.bufferData( this.gl.bufferData(
this.gl.ARRAY_BUFFER, this.gl.ARRAY_BUFFER,
new Float32Array([x1, y1, x2, y1, x2, y2, x1, y1, x1, y2, x2, y2]), new Float32Array([x1, y1, x2, y1, x2, y2, x1, y1, x1, y2, x2, y2]),
this.gl.STATIC_DRAW this.gl.DYNAMIC_DRAW
); );
var primitiveType = this.gl.TRIANGLES; var primitiveType = this.gl.TRIANGLES;
@@ -111,4 +122,10 @@ class Renderer {
} }
} }
enum DrawMode {
TEX = 0,
WIRE = 1,
VERT = 2
}
export default Renderer; export default Renderer;
@@ -11,5 +11,5 @@ void main() {
gl_Position = vec4(clipSpace * vec2(1, -1), 0, 1); gl_Position = vec4(clipSpace * vec2(1, -1), 0, 1);
//gl_Position = vec4(a_position, 0, 1); //gl_Position = vec4(a_position, 0, 1);
v_color = vec4(a_position.xyx * 0.5 + 0.5, 1.0); v_color = vec4(a_position.xy, 0.0, 1.0);
} }
+37
View File
@@ -0,0 +1,37 @@
namespace WebGEngine {
class Engine {
private _GL: WebGL2RenderingContext;
private _DrawMode: EngineDrawMode = EngineDrawMode.TEXTURE;
constructor(canvas: HTMLCanvasElement) {
let context = canvas.getContext('webgl2');
if (!context) throw new Error(`Failed to get webgl2 context from ${canvas}`);
this._GL = context;
this.init();
}
private init(): void {}
}
enum EngineDrawMode {
TEXTURE = 0,
WIREFRAME = 1,
VERTEX = 2
}
interface ShaderSrc {
type: number,
}
interface EngineConfig {
canvas: HTMLCanvasElement,
vertexShader: string,
fragmentShader: string,
additionalShaders?:
}
}
export default WebGEngine;
+18 -19
View File
@@ -2,10 +2,9 @@ import Renderer from '$lib/shimeji/renderer/renderer';
import Vector2 from '../vector'; import Vector2 from '../vector';
export class Shimeji { export class Shimeji {
private element: HTMLElement;
private canvas: HTMLCanvasElement; private canvas: HTMLCanvasElement;
private renderer: Renderer; private renderer: Renderer;
private rect: DOMRect; private size: number;
private pos: Vector2 = Vector2.zero; private pos: Vector2 = Vector2.zero;
private vel: Vector2 = Vector2.zero; private vel: Vector2 = Vector2.zero;
@@ -29,11 +28,10 @@ export class Shimeji {
private bounds: { minX: number; minY: number; maxX: number; maxY: number }; private bounds: { minX: number; minY: number; maxX: number; maxY: number };
constructor(element: HTMLElement, canvas: HTMLCanvasElement, renderer: Renderer) { constructor(size: number = 40, canvas: HTMLCanvasElement, renderer: Renderer) {
this.element = element; this.size = size;
this.canvas = canvas; this.canvas = canvas;
this.renderer = renderer; this.renderer = renderer;
this.rect = element.getBoundingClientRect();
this.bounds = { this.bounds = {
minX: 0, minX: 0,
@@ -43,13 +41,10 @@ export class Shimeji {
}; };
this.pos.set(window.innerWidth / 2, window.innerHeight / 2); this.pos.set(window.innerWidth / 2, window.innerHeight / 2);
this.vel.set(Math.floor(Math.random() * 5000) - 2500, Math.floor(Math.random() * 5000) - 2500);
this.updateBounds(); this.updateBounds();
window.addEventListener('resize', () => this.updateBounds()); window.addEventListener('resize', () => this.updateBounds());
console.log(this.element.getBoundingClientRect());
this.setupDragEvents();
} }
private updateBounds(): void { private updateBounds(): void {
@@ -94,8 +89,8 @@ export class Shimeji {
} }
} }
private setupDragEvents(): void { /* private setupDragEvents(): void {
this.element.addEventListener('mousedown', (e) => { this.element.addEventListener('mousedown', (e) => {
e.preventDefault(); e.preventDefault();
this.isBeingDragged = true; this.isBeingDragged = true;
this.element.style.cursor = 'grabbing'; this.element.style.cursor = 'grabbing';
@@ -129,9 +124,10 @@ export class Shimeji {
this.element.addEventListener('dragstart', (e) => { this.element.addEventListener('dragstart', (e) => {
e.preventDefault(); e.preventDefault();
}); });
} } */
private updatePhysics(dt: number): void { private updatePhysics(dt: number): void {
console.log(this.vel.x, this.vel.y);
if (this.isBeingDragged) { if (this.isBeingDragged) {
const direction: Vector2 = this.mousePos.sub(this.pivot).sub(this.pos); const direction: Vector2 = this.mousePos.sub(this.pivot).sub(this.pos);
@@ -148,17 +144,21 @@ export class Shimeji {
this.vel.y += this.GRAVITY; this.vel.y += this.GRAVITY;
this.vel.mulEq(this.DAMPING); this.vel.mulEq(this.DAMPING);
if (this.pos.y + this.rect.width >= this.bounds.maxY) { if (this.pos.y + this.size >= this.bounds.maxY) {
this.vel.x *= this.FRICTION; this.vel.x *= this.FRICTION;
} }
this.vel.deadZoneEq(0.00001);
//if (Math.abs(this.vel.x) <= 0.00001) this.vel.x = 0;
//if (Math.abs(this.vel.y) <= 0.00001) this.vel.y = 0;
const outVel = this.vel.mul(dt); const outVel = this.vel.mul(dt);
this.pos.addEq(outVel); this.pos.addEq(outVel);
} }
private applyBoundaries(): void { private applyBoundaries(): void {
const leftBound = this.rect.width / 2; const leftBound = this.size;
const rightBound = this.bounds.maxX - this.rect.width / 2; const rightBound = this.bounds.maxX - this.size;
if (this.pos.x < leftBound) { if (this.pos.x < leftBound) {
this.pos.x = leftBound; this.pos.x = leftBound;
@@ -168,8 +168,8 @@ export class Shimeji {
this.vel.x = -this.RESTITUTION * this.vel.x; this.vel.x = -this.RESTITUTION * this.vel.x;
} }
const topBound = this.rect.height / 2; const topBound = this.size;
const bottomBound = this.bounds.maxY - this.rect.height / 2; const bottomBound = this.bounds.maxY - this.size;
if (this.pos.y < topBound) { if (this.pos.y < topBound) {
this.pos.y = topBound; this.pos.y = topBound;
@@ -192,8 +192,7 @@ export class Shimeji {
} }
private render(): void { private render(): void {
this.element.style.transform = `translate(${this.pos.x}px, ${this.pos.y}px)`; this.renderer.Draw(this.pos, this.size);
this.renderer.Draw(this.pos, this.rect.width / 2);
/* if(this.isBeingDragged) { /* if(this.isBeingDragged) {
this.drawRay( this.drawRay(
+238 -233
View File
@@ -1,276 +1,281 @@
class Vector2 { class Vector2 {
public x: number; public x: number;
public y: number; public y: number;
constructor(x?: number, y?: number); constructor(x?: number, y?: number);
constructor(v?: Vector2); constructor(v?: Vector2);
constructor(x: Vector2 | number = 0, y: number = 0) { constructor(x: Vector2 | number = 0, y: number = 0) {
if (typeof x === 'object' && x !== null) { if (typeof x === 'object' && x !== null) {
this.x = x.x; this.x = x.x;
this.y = x.y; this.y = x.y;
} } else {
else { this.x = x;
this.x = x; this.y = y;
this.y = y; }
} }
}
add(s: number): Vector2; add(s: number): Vector2;
add(v: Vector2): Vector2; add(v: Vector2): Vector2;
add(arg: number | Vector2): Vector2 { add(arg: number | Vector2): Vector2 {
if (typeof arg === 'number') { if (typeof arg === 'number') {
return new Vector2(this.x + arg, this.y + arg); return new Vector2(this.x + arg, this.y + arg);
} } else {
else { return new Vector2(this.x + arg.x, this.y + arg.y);
return new Vector2(this.x + arg.x, this.y + arg.y); }
} }
}
sub(s: number): Vector2; sub(s: number): Vector2;
sub(v: Vector2): Vector2; sub(v: Vector2): Vector2;
sub(arg: number | Vector2): Vector2 { sub(arg: number | Vector2): Vector2 {
if (typeof arg === 'number') { if (typeof arg === 'number') {
return new Vector2(this.x - arg, this.y - arg); return new Vector2(this.x - arg, this.y - arg);
} } else {
else { return new Vector2(this.x - arg.x, this.y - arg.y);
return new Vector2(this.x - arg.x, this.y - arg.y); }
} }
}
mul(s: number): Vector2; mul(s: number): Vector2;
mul(v: Vector2): Vector2; mul(v: Vector2): Vector2;
mul(arg: number | Vector2): Vector2 { mul(arg: number | Vector2): Vector2 {
if (typeof arg === 'number') { if (typeof arg === 'number') {
return new Vector2(this.x * arg, this.y * arg); return new Vector2(this.x * arg, this.y * arg);
} } else {
else { return new Vector2(this.x * arg.x, this.y * arg.y);
return new Vector2(this.x * arg.x, this.y * arg.y); }
} }
}
div(s: number): Vector2; div(s: number): Vector2;
div(v: Vector2): Vector2; div(v: Vector2): Vector2;
div(arg: number | Vector2): Vector2 { div(arg: number | Vector2): Vector2 {
if (typeof arg === 'number') { if (typeof arg === 'number') {
return new Vector2(this.x / arg, this.y / arg); return new Vector2(this.x / arg, this.y / arg);
} } else {
else { return new Vector2(this.x / arg.x, this.y / arg.y);
return new Vector2(this.x / arg.x, this.y / arg.y); }
} }
}
equals(v: Vector2, epsilon: number = 1e-10): boolean { equals(v: Vector2, epsilon: number = 1e-10): boolean {
return Math.abs(this.x - v.x) < epsilon && Math.abs(this.y - v.y) < epsilon; return Math.abs(this.x - v.x) < epsilon && Math.abs(this.y - v.y) < epsilon;
} }
get sqrLength(): number { get sqrLength(): number {
return this.x * this.x + this.y * this.y; return this.x * this.x + this.y * this.y;
} }
get length(): number { get length(): number {
return Math.sqrt(this.sqrLength); return Math.sqrt(this.sqrLength);
} }
get normalized(): Vector2 { get normalized(): Vector2 {
const len = this.length; const len = this.length;
return len === 0 ? new Vector2(0, 0) : new Vector2(this.x / len, this.y / len); return len === 0 ? new Vector2(0, 0) : new Vector2(this.x / len, this.y / len);
} }
get angle(): number { get angle(): number {
return Math.atan2(this.y, this.x); return Math.atan2(this.y, this.x);
} }
addEq(s: number): this; addEq(s: number): this;
addEq(v: Vector2): this; addEq(v: Vector2): this;
addEq(arg: number | Vector2): this { addEq(arg: number | Vector2): this {
if (typeof arg === 'number') { if (typeof arg === 'number') {
this.x += arg; this.x += arg;
this.y += arg; this.y += arg;
} } else {
else { this.x += arg.x;
this.x += arg.x; this.y += arg.y;
this.y += arg.y; }
} return this;
return this; }
}
subEq(s: number): this; subEq(s: number): this;
subEq(v: Vector2): this; subEq(v: Vector2): this;
subEq(arg: number | Vector2): this { subEq(arg: number | Vector2): this {
if (typeof arg === 'number') { if (typeof arg === 'number') {
this.x -= arg; this.x -= arg;
this.y -= arg; this.y -= arg;
} } else {
else { this.x -= arg.x;
this.x -= arg.x; this.y -= arg.y;
this.y -= arg.y; }
} return this;
return this; }
}
mulEq(s: number): this; mulEq(s: number): this;
mulEq(v: Vector2): this; mulEq(v: Vector2): this;
mulEq(arg: number | Vector2): this { mulEq(arg: number | Vector2): this {
if (typeof arg === 'number') { if (typeof arg === 'number') {
this.x *= arg; this.x *= arg;
this.y *= arg; this.y *= arg;
} } else {
else { this.x *= arg.x;
this.x *= arg.x; this.y *= arg.y;
this.y *= arg.y; }
} return this;
return this; }
}
divEq(s: number): this; divEq(s: number): this;
divEq(v: Vector2): this; divEq(v: Vector2): this;
divEq(arg: number | Vector2): this { divEq(arg: number | Vector2): this {
if (typeof arg === 'number') { if (typeof arg === 'number') {
this.x /= arg; this.x /= arg;
this.y /= arg; this.y /= arg;
} } else {
else { this.x /= arg.x;
this.x /= arg.x; this.y /= arg.y;
this.y /= arg.y; }
} return this;
return this; }
}
cross(v: Vector2): number { cross(v: Vector2): number {
return this.x * v.y - this.y * v.x; return this.x * v.y - this.y * v.x;
} }
dot(v: Vector2): number { dot(v: Vector2): number {
return this.x * v.x + this.y * v.y; return this.x * v.x + this.y * v.y;
} }
set(x: number, y: number): this { set(x: number, y: number): this {
this.x = x; this.x = x;
this.y = y; this.y = y;
return this; return this;
} }
normalize(): this { normalize(): this {
const len = this.length; const len = this.length;
if (len !== 0) { if (len !== 0) {
this.x /= len; this.x /= len;
this.y /= len; this.y /= len;
} }
return this; return this;
} }
get perpendicular(): Vector2 { get perpendicular(): Vector2 {
return new Vector2(-this.y, this.x); return new Vector2(-this.y, this.x);
} }
clone(): Vector2 { clone(): Vector2 {
return new Vector2(this.x, this.y); return new Vector2(this.x, this.y);
} }
toString(): string { toString(): string {
return `Vector2(${this.x}, ${this.y})`; return `Vector2(${this.x}, ${this.y})`;
} }
toArray(): [number, number] { toArray(): [number, number] {
return [this.x, this.y]; return [this.x, this.y];
} }
[Symbol.iterator](): Iterator<number> { [Symbol.iterator](): Iterator<number> {
let index = 0; let index = 0;
const components = [this.x, this.y]; const components = [this.x, this.y];
return { return {
next: (): IteratorResult<number> => { next: (): IteratorResult<number> => {
if (index < components.length) { if (index < components.length) {
return { value: components[index++], done: false }; return { value: components[index++], done: false };
} } else {
else { return { value: undefined as any, done: true };
return { value: undefined as any, done: true }; }
} }
}, };
}; }
}
deadZone(threshold: number = 1e-4): Vector2 { /**
return new Vector2( * Checks if this.xy below threshold, if so - set to 0 (each param individually)
Math.abs(this.x) < threshold ? 0 : this.x, * @param {number} Deadzone treshold e.g. 1-4e (0.{4 - 0's}1)
Math.abs(this.y) < threshold ? 0 : this.y * @returns {this} new Vector2 with values of this or 0
); * @example
} * // sets this.x or this.y to 0 if less then 0.0000001
* vecFoo.deadZone(1e-6);
*/
deadZone(threshold: number = 1e-4): Vector2 {
return new Vector2(
Math.abs(this.x) < threshold ? 0 : this.x,
Math.abs(this.y) < threshold ? 0 : this.y
);
}
deadZoneEq(threshold: number = 1e-4): this { /**
if (Math.abs(this.x) < threshold) this.x = 0; * Checks if *this*xy below threshold, if so - set to 0 (each param individually)
if (Math.abs(this.y) < threshold) this.y = 0; * @param {number} Deadzone treshold e.g. 1-4e (0.{4 - 0's}1)
return this; * @returns {Vector2} this - i.e. the vector that calls the method.
} * @example
* // sets this.x or this.y to 0 if less then 0.0000001
* vecFoo.deadZone(1e-6);
*/
deadZoneEq(threshold: number = 1e-4): this {
if (Math.abs(this.x) < threshold) this.x = 0;
if (Math.abs(this.y) < threshold) this.y = 0;
return this;
}
static fromAngle(angle: number, magnitude: number = 1): Vector2 { static fromAngle(angle: number, magnitude: number = 1): Vector2 {
return new Vector2(Math.cos(angle) * magnitude, Math.sin(angle) * magnitude); return new Vector2(Math.cos(angle) * magnitude, Math.sin(angle) * magnitude);
} }
static dot(a: Vector2, b: Vector2): number { static dot(a: Vector2, b: Vector2): number {
return a.x * b.x + a.y * b.y; return a.x * b.x + a.y * b.y;
} }
static cross(a: Vector2, b: Vector2): number { static cross(a: Vector2, b: Vector2): number {
return a.x * b.y - a.y * b.x; return a.x * b.y - a.y * b.x;
} }
static distance(a: Vector2, b: Vector2): number { static distance(a: Vector2, b: Vector2): number {
const dx = a.x - b.x; const dx = a.x - b.x;
const dy = a.y - b.y; const dy = a.y - b.y;
return Math.sqrt(dx * dx + dy * dy); return Math.sqrt(dx * dx + dy * dy);
} }
static distanceSqr(a: Vector2, b: Vector2): number { static distanceSqr(a: Vector2, b: Vector2): number {
const dx = a.x - b.x; const dx = a.x - b.x;
const dy = a.y - b.y; const dy = a.y - b.y;
return dx * dx + dy * dy; return dx * dx + dy * dy;
} }
static lerp(a: Vector2, b: Vector2, t: number): Vector2 { static lerp(a: Vector2, b: Vector2, t: number): Vector2 {
return new Vector2(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t); return new Vector2(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t);
} }
static clamp(v: Vector2, min: Vector2, max: Vector2): Vector2 { static clamp(v: Vector2, min: Vector2, max: Vector2): Vector2 {
return new Vector2( return new Vector2(
Math.min(Math.max(v.x, min.x), max.x), Math.min(Math.max(v.x, min.x), max.x),
Math.min(Math.max(v.y, min.y), max.y) Math.min(Math.max(v.y, min.y), max.y)
); );
} }
static angleBetween(a: Vector2, b: Vector2): number { static angleBetween(a: Vector2, b: Vector2): number {
const dot = Vector2.dot(a, b); const dot = Vector2.dot(a, b);
const mag = a.length * b.length; const mag = a.length * b.length;
if (mag === 0) if (mag === 0) return 0;
return 0;
const cos = dot / mag;
return Math.acos(Math.min(1, Math.max(-1, cos)));
}
static get zero(): Vector2 { const cos = dot / mag;
return new Vector2(0, 0); return Math.acos(Math.min(1, Math.max(-1, cos)));
} }
static get one(): Vector2 { static get zero(): Vector2 {
return new Vector2(1, 1); return new Vector2(0, 0);
} }
static get up(): Vector2 { static get one(): Vector2 {
return new Vector2(0, 1); return new Vector2(1, 1);
} }
static get down(): Vector2 { static get up(): Vector2 {
return new Vector2(0, -1); return new Vector2(0, 1);
} }
static get left(): Vector2 { static get down(): Vector2 {
return new Vector2(-1, 0); return new Vector2(0, -1);
} }
static get right(): Vector2 { static get left(): Vector2 {
return new Vector2(1, 0); return new Vector2(-1, 0);
} }
static get right(): Vector2 {
return new Vector2(1, 0);
}
} }
export default Vector2; export default Vector2;
+4 -8
View File
@@ -1,7 +1,6 @@
<script lang="ts"> <script lang="ts">
import Renderer from '$lib/shimeji/renderer/renderer'; import Renderer from '$lib/shimeji/renderer/renderer';
import { Shimeji } from '$lib/stores/shimeji'; import { Shimeji } from '$lib/stores/shimeji';
import Vector2 from '$lib/vector';
import { onMount } from 'svelte'; import { onMount } from 'svelte';
const drag: number = 3; const drag: number = 3;
@@ -28,16 +27,13 @@
canvas.width = window.innerWidth; canvas.width = window.innerWidth;
canvas.height = window.innerHeight; canvas.height = window.innerHeight;
const vert = await fetchShader('src/lib/shimeji/renderer/shaders/vertex_shader.vert'); //const vert = await fetchShader('src/lib/shimeji/renderer/shaders/vertex_shader.vert');
const frag = await fetchShader('src/lib/shimeji/renderer/shaders/fragment_shader.frag'); //const frag = await fetchShader('src/lib/shimeji/renderer/shaders/fragment_shader.frag');
console.log(vert, '\n', frag);
renderer = new Renderer(canvas); renderer = new Renderer(canvas);
await renderer.init(); await renderer.init();
renderer.Draw(new Vector2(500, 500), 50);
/* shimeji = new Shimeji(sprite, canvas, renderer); shimeji = new Shimeji(40, canvas, renderer);
shimeji.start(); shimeji.start();
shown = true; shown = true;
@@ -52,7 +48,7 @@
if (e.key === 'space') { if (e.key === 'space') {
shimeji.moveTo(genRandomPos(200, window.innerWidth - 200)); shimeji.moveTo(genRandomPos(200, window.innerWidth - 200));
} }
}); */ });
}); });
</script> </script>