more rendering code, not much changes, beginning refactor of the rendering API
This commit is contained in:
@@ -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);
|
||||||
}
|
}
|
||||||
@@ -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;
|
||||||
+17
-18
@@ -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,7 +89,7 @@ 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;
|
||||||
@@ -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(
|
||||||
|
|||||||
+28
-23
@@ -8,8 +8,7 @@ class Vector2 {
|
|||||||
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;
|
||||||
}
|
}
|
||||||
@@ -20,8 +19,7 @@ class 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -31,8 +29,7 @@ class 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -42,8 +39,7 @@ class 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -53,8 +49,7 @@ class 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -86,8 +81,7 @@ class Vector2 {
|
|||||||
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;
|
||||||
}
|
}
|
||||||
@@ -100,8 +94,7 @@ class Vector2 {
|
|||||||
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;
|
||||||
}
|
}
|
||||||
@@ -114,8 +107,7 @@ class Vector2 {
|
|||||||
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;
|
||||||
}
|
}
|
||||||
@@ -128,8 +120,7 @@ class Vector2 {
|
|||||||
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;
|
||||||
}
|
}
|
||||||
@@ -182,14 +173,21 @@ class Vector2 {
|
|||||||
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 };
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if this.xy below threshold, if so - set to 0 (each param individually)
|
||||||
|
* @param {number} Deadzone treshold e.g. 1-4e (0.{4 - 0's}1)
|
||||||
|
* @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 {
|
deadZone(threshold: number = 1e-4): Vector2 {
|
||||||
return new Vector2(
|
return new Vector2(
|
||||||
Math.abs(this.x) < threshold ? 0 : this.x,
|
Math.abs(this.x) < threshold ? 0 : this.x,
|
||||||
@@ -197,6 +195,14 @@ class Vector2 {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if *this*xy below threshold, if so - set to 0 (each param individually)
|
||||||
|
* @param {number} Deadzone treshold e.g. 1-4e (0.{4 - 0's}1)
|
||||||
|
* @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 {
|
deadZoneEq(threshold: number = 1e-4): this {
|
||||||
if (Math.abs(this.x) < threshold) this.x = 0;
|
if (Math.abs(this.x) < threshold) this.x = 0;
|
||||||
if (Math.abs(this.y) < threshold) this.y = 0;
|
if (Math.abs(this.y) < threshold) this.y = 0;
|
||||||
@@ -241,8 +247,7 @@ class Vector2 {
|
|||||||
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;
|
const cos = dot / mag;
|
||||||
return Math.acos(Math.min(1, Math.max(-1, cos)));
|
return Math.acos(Math.min(1, Math.max(-1, cos)));
|
||||||
|
|||||||
@@ -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>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user