3d-game-engine-canvas
Advanced tools
Comparing version 0.1.18 to 0.1.19
{ | ||
"name": "3d-game-engine-canvas", | ||
"version": "0.1.18", | ||
"version": "0.1.19", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -149,2 +149,3 @@ import Camera from "../components/Camera"; | ||
if (!this.transform.parent) throw new Error("No parent"); | ||
this.onDestroy(); | ||
(this.transform.parent instanceof Scene | ||
@@ -154,3 +155,2 @@ ? this.transform.parent | ||
).removeChildren(this); | ||
this.onDestroy(); | ||
} | ||
@@ -157,0 +157,0 @@ |
@@ -140,34 +140,29 @@ import Mesh from "../utilities/Mesh"; | ||
renderMesh(mesh: Mesh, material: Material, camera: Camera) { | ||
if (camera) { | ||
const transformedMesh = mesh.transformToCamera(camera); | ||
const transformedMesh = mesh.transformToCamera(camera); | ||
const res = camera.preClipObject(transformedMesh.boundingSphere); | ||
if (res === -1) return; | ||
const res = camera.preClipObject(transformedMesh.boundingSphere); | ||
if (res === -1) return; | ||
if (!mesh.doubleSided) | ||
transformedMesh.triangles = transformedMesh.triangles.filter( | ||
(t) => t.normal.dotProduct(t.vertices[0].invert()) > 0 | ||
); | ||
if (!mesh.doubleSided) | ||
transformedMesh.triangles = transformedMesh.triangles.filter( | ||
(t) => t.normal.dotProduct(t.vertices[0].invert()) > 0 | ||
); | ||
if (res === 0) { | ||
transformedMesh.triangles = camera.clipObject( | ||
transformedMesh.triangles | ||
); | ||
} | ||
if (res === 0) { | ||
transformedMesh.triangles = camera.clipObject( | ||
transformedMesh.triangles | ||
); | ||
} | ||
const projectedMesh = transformedMesh.project(camera, this); | ||
const projectedMesh = transformedMesh.project(camera, this); | ||
projectedMesh.triangles.forEach((t, i) => { | ||
material.renderTriangle( | ||
t, | ||
transformedMesh.triangles[i], | ||
this, | ||
camera | ||
); | ||
}); | ||
} else { | ||
console.warn("No camera!"); | ||
return; | ||
} | ||
projectedMesh.triangles.forEach((t, i) => { | ||
material.renderTriangle( | ||
t, | ||
transformedMesh.triangles[i], | ||
this, | ||
camera | ||
); | ||
}); | ||
} | ||
} |
@@ -48,2 +48,4 @@ import ClippingPlane from "../utilities/ClippingPlane"; | ||
private _clip: boolean; | ||
clippingPlanes: [ | ||
@@ -62,6 +64,8 @@ ClippingPlane, | ||
near: number = 1, | ||
far: number = 10000 | ||
far: number = 10000, | ||
clip: boolean = true | ||
) { | ||
super(); | ||
this._fov = fov; | ||
this._clip = clip; | ||
this._near = near; | ||
@@ -134,8 +138,10 @@ this._far = far; | ||
let resTriangles: Array<Triangle> | null = triangles; | ||
for (let i = 0; i < this.clippingPlanes.length; i++) { | ||
resTriangles = this.clippingPlanes[i].clipTriangles(resTriangles); | ||
if (resTriangles == null) { | ||
break; | ||
if (this._clip) | ||
for (let i = 0; i < this.clippingPlanes.length; i++) { | ||
resTriangles = | ||
this.clippingPlanes[i].clipTriangles(resTriangles); | ||
if (resTriangles == null) { | ||
break; | ||
} | ||
} | ||
} | ||
return resTriangles; | ||
@@ -142,0 +148,0 @@ } |
@@ -30,2 +30,8 @@ import Component from "../classes/Components/Component"; | ||
async onDestroy() { | ||
this.transform.onSomeGlobalUpdates.removeEventListener( | ||
this.updateTransform.bind(this) | ||
); | ||
} | ||
render(renderer: Renderer, camera: Camera) { | ||
@@ -32,0 +38,0 @@ renderer.renderMesh(this.transformedMesh, this.material, camera); |
@@ -1,5 +0,1 @@ | ||
import Component from "../classes/Components/Component"; | ||
import Renderer from "../classes/Renderer"; | ||
import Material from "../classes/Materials/Material"; | ||
import Mesh from "../utilities/Mesh"; | ||
import Texture from "../utilities/Texture"; | ||
@@ -9,21 +5,8 @@ import BasicMaterial from "../classes/Materials/BasicMaterial"; | ||
import DefaultMeshes from "../tools/DefaultMeshes"; | ||
import Camera from "./Camera"; | ||
import MeshRenderer from "./MeshRenderer"; | ||
export default class SpriteRenderer extends Component { | ||
mesh: Mesh | null; | ||
material: Material; | ||
export default class SpriteRenderer extends MeshRenderer { | ||
constructor(texture: Texture, color: Color) { | ||
super(); | ||
this.mesh = null; | ||
this.material = new BasicMaterial(color, texture, true); | ||
super(DefaultMeshes.plane, new BasicMaterial(color, texture, true)); | ||
} | ||
async start(): Promise<void> { | ||
this.mesh = (await DefaultMeshes.plane).copy(); | ||
} | ||
render(renderer: Renderer, camera: Camera) { | ||
if (this.mesh) renderer.renderMesh(this.mesh, this.material, camera); | ||
} | ||
} |
export default class FileLoader { | ||
static async load(url: string) { | ||
static fileCache: { [key: string]: string } = {}; | ||
static imageCache: { [key: string]: HTMLImageElement } = {}; | ||
static async load(url: string, useCache = true) { | ||
if (useCache) { | ||
const c = this.fileCache[url]; | ||
if (c) return c; | ||
} | ||
const res = await fetch(url); | ||
if (res.status === 0 || res.status === 200) return await res.text(); | ||
else throw Error(`Cannot get file at ${url}`); | ||
if (res.status === 0 || res.status === 200) { | ||
const text = await res.text(); | ||
if (useCache) this.fileCache[url] = text; | ||
return text; | ||
} else throw Error(`Cannot get file at ${url}`); | ||
} | ||
static loadImg(url: string): Promise<HTMLImageElement> { | ||
static loadImg(url: string, useCache = true): Promise<HTMLImageElement> { | ||
if (useCache) { | ||
const c = this.imageCache[url]; | ||
if (c) | ||
return new Promise((res, _rej) => { | ||
res(c); | ||
}); | ||
} | ||
return new Promise((res, _rej) => { | ||
@@ -13,2 +30,3 @@ const img = new Image(); | ||
img.onload = () => { | ||
if (useCache) this.imageCache[url] = img; | ||
res(img); | ||
@@ -15,0 +33,0 @@ }; |
@@ -6,5 +6,9 @@ import Mesh from "../utilities/Mesh"; | ||
export default class ObjLoader { | ||
raw: String; | ||
raw: string; | ||
useCache: boolean; | ||
static cache: { [key: string]: Mesh } = {}; | ||
constructor(text: String) { | ||
constructor(text: string, useCache = true) { | ||
this.useCache = useCache; | ||
this.raw = text; | ||
@@ -20,2 +24,7 @@ if (this.raw.indexOf("\r\n") !== -1) { | ||
parse(hide = false) { | ||
if (this.useCache) { | ||
const c = ObjLoader.cache[this.raw]; | ||
if (c) return c; | ||
} | ||
const lines = this.raw.split("\n"); | ||
@@ -168,4 +177,8 @@ let vertices: Array<Vector3> = []; | ||
} | ||
return new Mesh(triangles); | ||
const res = new Mesh(triangles); | ||
if (this.useCache) { | ||
ObjLoader.cache[this.raw] = res; | ||
} | ||
return res; | ||
} | ||
} |
@@ -214,3 +214,3 @@ import { Sphere } from "./math/Math"; | ||
t.normal, | ||
t.hidden | ||
[t.hidden[0], true, t.hidden[2]] | ||
), | ||
@@ -222,3 +222,3 @@ new Triangle( | ||
t.normal, | ||
t.hidden | ||
[true, t.hidden[1], t.hidden[1]] | ||
), | ||
@@ -258,3 +258,3 @@ ]; | ||
t.normal, | ||
t.hidden | ||
[t.hidden[2], true, t.hidden[0]] | ||
), | ||
@@ -266,3 +266,3 @@ new Triangle( | ||
t.normal, | ||
t.hidden | ||
[true, t.hidden[1], t.hidden[1]] | ||
), | ||
@@ -302,3 +302,3 @@ ]; | ||
t.normal, | ||
t.hidden | ||
[t.hidden[1], true, t.hidden[0]] | ||
), | ||
@@ -310,3 +310,3 @@ new Triangle( | ||
t.normal, | ||
t.hidden | ||
[true, t.hidden[2], t.hidden[2]] | ||
), | ||
@@ -313,0 +313,0 @@ ]; |
@@ -121,2 +121,4 @@ import { clamp } from "./Math"; | ||
blend(v: Color) { | ||
//! Important TODO | ||
if (v.a == 0) return this; //TODO: This is quick fix and it need to be replaced. | ||
this.multiply(1 - this.a / 255).add(v.multiply(v.a / 255)); | ||
@@ -123,0 +125,0 @@ return this; |
@@ -31,1 +31,10 @@ import Vector3 from "./Vector3"; | ||
} | ||
/** | ||
* Return random element from table | ||
* @param array table | ||
* @returns random element from table | ||
*/ | ||
export function getRandomElement(array: Array<any>) { | ||
return array[Math.floor(Math.random() * array.length)]; | ||
} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
6448573
215
5494