Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
AIFanatic committed Sep 1, 2024
1 parent 56db977 commit 4cac624
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 17 deletions.
98 changes: 92 additions & 6 deletions dist/trident.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@ var EventSystem = class {
}
}
};
var EventSystemLocal = class {
static events = {};
static on(event, callback, id) {
const eventId = id ? `${event}-${id}` : event;
if (!this.events[eventId]) this.events[eventId] = [];
this.events[eventId].push(callback);
console.log(`Registered ${eventId}`);
}
static emit(event, id, ...args) {
const eventId = `${event}-${id}`;
if (!this.events[eventId]) {
return;
}
console.log(`Listeners for ${this.events[eventId].length}`);
for (let i = 0; i < this.events[eventId].length; i++) {
this.events[eventId][i](...args);
}
}
};

// src/utils/Utils.ts
var Utils = class {
Expand Down Expand Up @@ -61,6 +80,8 @@ var Component = class _Component {
}
LateUpdate() {
}
Destroy() {
}
};

// src/math/Matrix4.ts
Expand Down Expand Up @@ -797,11 +818,12 @@ var Transform = class extends Component {
}
onChanged() {
EventSystem.emit("CallUpdate", this, true);
EventSystem.emit("TransformUpdated", this);
}
UpdateMatrices() {
this._localToWorldMatrix.compose(this.position, this.rotation, this.scale);
this._worldToLocalMatrix.copy(this._localToWorldMatrix).invert();
EventSystem.emit("TransformUpdated", this);
EventSystemLocal.emit("TransformUpdated", this.gameObject.id, this);
}
Update() {
this.UpdateMatrices();
Expand Down Expand Up @@ -867,6 +889,7 @@ var Camera = class _Camera extends Component {

// src/GameObject.ts
var GameObject = class {
id = Utils.UUID();
name = "GameObject";
scene;
transform;
Expand Down Expand Up @@ -909,6 +932,14 @@ var GameObject = class {
}
}
}
Destroy() {
for (const component of this.componentsArray) {
component.Destroy();
}
this.componentsArray = [];
this.componentsMapped.clear();
this.scene.RemoveGameObject(this);
}
};

// src/renderer/RenderGraph.ts
Expand Down Expand Up @@ -7928,9 +7959,15 @@ var Meshletizer = class _Meshletizer {
// src/components/MeshletMesh.ts
var meshletsCache = /* @__PURE__ */ new Map();
var MeshletMesh = class extends Component {
geometry;
materialsMapped = /* @__PURE__ */ new Map();
enableShadows = true;
meshlets = [];
Start() {
EventSystemLocal.on("TransformUpdated", (transform) => {
EventSystem.emit("MeshletUpdated", this);
}, this.gameObject.id);
}
AddMaterial(material) {
if (!this.materialsMapped.has(material.constructor.name)) this.materialsMapped.set(material.constructor.name, []);
this.materialsMapped.get(material.constructor.name)?.push(material);
Expand All @@ -7939,9 +7976,12 @@ var MeshletMesh = class extends Component {
return this.materialsMapped.get(type.name) || [];
}
async SetGeometry(geometry) {
const cached = meshletsCache.get(geometry);
this.geometry = geometry;
let cached = meshletsCache.get(geometry);
if (cached) {
this.meshlets.push(...cached);
cached.instanceCount++;
meshletsCache.set(geometry, cached);
this.meshlets.push(...cached.meshlets);
return;
}
const pa = geometry.attributes.get("position");
Expand All @@ -7958,7 +7998,18 @@ var MeshletMesh = class extends Component {
await Meshoptimizer.load();
const allMeshlets = await Meshletizer.Build(interleavedVertices, indices);
this.meshlets = allMeshlets;
meshletsCache.set(geometry, this.meshlets);
meshletsCache.set(geometry, { meshlets: this.meshlets, instanceCount: 0 });
}
Destroy() {
EventSystem.emit("MeshletDeleted", this);
const cached = meshletsCache.get(this.geometry);
if (!cached) throw Error("Geometry should be in meshletsCache");
cached.instanceCount--;
meshletsCache.set(this.geometry, cached);
if (cached.instanceCount <= 0) {
meshletsCache.delete(this.geometry);
}
EventSystem.emit("RemovedComponent", this, this.gameObject.scene);
}
};

Expand Down Expand Up @@ -8101,6 +8152,20 @@ var PrepareSceneData = class extends RenderPass {
this.meshletInfoBuffer = new BufferMemoryAllocator(meshMatrixBufferSize);
this.vertexBuffer = new BufferMemoryAllocator(meshMatrixBufferSize);
this.objectInfoBufferV2 = new BufferMemoryAllocator(meshMatrixBufferSize);
EventSystem.on("MeshletUpdated", (mesh) => {
console.log("Meshlet updated");
if (this.meshMatrixInfoBuffer.has(mesh.id)) {
this.meshMatrixInfoBuffer.set(mesh.id, mesh.transform.localToWorldMatrix.elements);
}
});
EventSystem.on("MeshletDeleted", (mesh) => {
console.log("Meshlet deleted");
if (this.meshMatrixInfoBuffer.has(mesh.id)) this.meshMatrixInfoBuffer.delete(mesh.id);
if (this.meshMaterialInfo.has(mesh.id)) this.meshMaterialInfo.delete(mesh.id);
for (const meshlet of mesh.meshlets) {
this.objectInfoBufferV2.delete(`${mesh.id}-${meshlet.id}`);
}
});
}
getVertexInfo(meshlet) {
return meshlet.vertices_gpu;
Expand Down Expand Up @@ -8219,7 +8284,10 @@ var PrepareSceneData = class extends RenderPass {
const meshCache = /* @__PURE__ */ new Map();
for (const mesh of sceneMeshlets) {
if (!this.meshMaterialInfo.has(mesh.id)) this.meshMaterialInfo.set(mesh.id, this.getMeshMaterialInfo(mesh));
if (!this.meshMatrixInfoBuffer.has(mesh.id)) this.meshMatrixInfoBuffer.set(mesh.id, mesh.transform.localToWorldMatrix.elements);
if (!this.meshMatrixInfoBuffer.has(mesh.id)) {
console.log(mesh.id, mesh.transform.localToWorldMatrix.elements);
this.meshMatrixInfoBuffer.set(mesh.id, mesh.transform.localToWorldMatrix.elements);
}
let meshIndex = meshCache.get(mesh.id);
if (meshIndex === void 0) {
meshIndex = meshCache.size;
Expand Down Expand Up @@ -8604,6 +8672,16 @@ var Scene = class {
componentsArray.push(component);
this.componentsByType.set(component.name, componentsArray);
});
EventSystem.on("RemovedComponent", (component, scene) => {
let componentsArray = this.componentsByType.get(component.name);
if (componentsArray) {
const index = componentsArray.indexOf(component);
if (index !== -1) {
componentsArray.splice(index, 1);
this.componentsByType.set(component.name, componentsArray);
}
}
});
}
AddGameObject(gameObject) {
this.gameObjects.push(gameObject);
Expand All @@ -8614,6 +8692,10 @@ var Scene = class {
GetComponents(type) {
return this.componentsByType.get(type.name) || [];
}
RemoveGameObject(gameObject) {
const index = this.gameObjects.indexOf(gameObject);
if (index !== -1) this.gameObjects.splice(index, 1);
}
Start() {
if (this.hasStarted) return;
for (const gameObject of this.gameObjects) gameObject.Start();
Expand Down Expand Up @@ -8903,8 +8985,12 @@ async function Application() {
}
}
setTimeout(() => {
console.log("Updtin");
console.log("Updating");
lastMesh.transform.position.x += 2;
setTimeout(() => {
console.log("Destroying");
lastMesh.transform.gameObject.Destroy();
}, 5e3);
}, 5e3);
scene.Start();
}
Expand Down
31 changes: 29 additions & 2 deletions src/Events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ import { Scene } from "./Scene";
import { Camera } from "./components/Camera";
import { Component } from "./components/Component";
import { Light } from "./components/Light";
import { Mesh } from "./components/Mesh";
import { MeshletMesh } from "./components/MeshletMesh";
import { Transform } from "./components/Transform";

export interface Events {
AddedComponent: (component: Component, scene: Scene) => void;
RemovedComponent: (component: Component, scene: Scene) => void;
CallUpdate: (component: Component, flag: boolean) => void;
TransformUpdated: (component: Transform) => void;
LightUpdated: (light: Light) => void;
MeshUpdated: (mesh: Mesh) => void;
MeshletUpdated: (meshlet: MeshletMesh) => void;
MeshletDeleted: (meshlet: MeshletMesh) => void;
MainCameraUpdated: (camera: Camera) => void;
};

Expand All @@ -33,4 +35,29 @@ export class EventSystem {
callbacks[i](...args);
}
}
}



export class EventSystemLocal {
private static events: EventMap = {};

public static on<K extends keyof Events>(event: K, callback: Events[K], id?: string) {
const eventId = id ? `${event}-${id}` : event;
if (!this.events[eventId]) this.events[eventId] = [];
this.events[eventId].push(callback);
console.log(`Registered ${eventId}`); // Debugging line to confirm registration
}

public static emit<K extends keyof Events>(event: K, id: string, ...args: Parameters<Events[K]>) {
const eventId = `${event}-${id}`;
if (!this.events[eventId]) {
// console.log(`No listeners for ${eventId}`); // Debugging line to check if listeners are being called
return;
}
console.log(`Listeners for ${this.events[eventId].length}`); // Debugging line to check if listeners are being called
for (let i = 0; i < this.events[eventId].length; i++) {
this.events[eventId][i](...args);
}
}
}
11 changes: 11 additions & 0 deletions src/GameObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { Component } from "./components/Component";
import { Scene } from "./Scene";
import { Transform } from "./components/Transform";
import { Camera } from "./components/Camera";
import { Utils } from "./utils/Utils";

export class GameObject {
public id = Utils.UUID();
public name: string = "GameObject";
public scene: Scene;

Expand Down Expand Up @@ -57,4 +59,13 @@ export class GameObject {
}
}
}

public Destroy() {
for (const component of this.componentsArray) {
component.Destroy();
}
this.componentsArray = [];
this.componentsMapped.clear();
this.scene.RemoveGameObject(this);
}
}
15 changes: 15 additions & 0 deletions src/Scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,26 @@ export class Scene {
// }

});

EventSystem.on("RemovedComponent", (component, scene) => {
let componentsArray = this.componentsByType.get(component.name);
if (componentsArray) {
const index = componentsArray.indexOf(component);
if (index !== -1) {
componentsArray.splice(index, 1);
this.componentsByType.set(component.name, componentsArray);
}
}
});
}

public AddGameObject(gameObject: GameObject) { this.gameObjects.push(gameObject) }
public GetGameObjects(): GameObject[] { return this.gameObjects }
public GetComponents<T extends Component>(type: new(...args: any[]) => T): T[] { return this.componentsByType.get(type.name) as T[] || [] }
public RemoveGameObject(gameObject: GameObject) {
const index = this.gameObjects.indexOf(gameObject);
if (index !== -1) this.gameObjects.splice(index, 1);
}

public Start() {
if (this.hasStarted) return;
Expand Down
1 change: 1 addition & 0 deletions src/components/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ export class Component {
public Start() {}
public Update() {}
public LateUpdate() {}
public Destroy() {}
}
2 changes: 1 addition & 1 deletion src/components/Light.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EventSystem } from "../Events";
import { EventSystem, EventSystemLocal } from "../Events";
import { Color } from "../math/Color";
import { Vector3 } from "../math/Vector3";
import { Renderer } from "../renderer/Renderer";
Expand Down
33 changes: 29 additions & 4 deletions src/components/MeshletMesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@ import { Meshlet } from "../plugins/meshlets/Meshlet";
import { Geometry, InterleavedVertexAttribute } from "../Geometry";
import { Meshletizer } from "../plugins/meshlets/Meshletizer";
import { Meshoptimizer } from "../plugins/meshlets/Meshoptimizer";
import { EventSystem, EventSystemLocal } from "../Events";

const meshletsCache: Map<Geometry, Meshlet[]> = new Map();
const meshletsCache: Map<Geometry, {meshlets: Meshlet[], instanceCount: number}> = new Map();

export class MeshletMesh extends Component {
private geometry: Geometry;
private materialsMapped: Map<string, Material[]> = new Map();

public enableShadows: boolean = true;

public meshlets: Meshlet[] = [];

public Start(): void {
EventSystemLocal.on("TransformUpdated", transform => {
EventSystem.emit("MeshletUpdated", this);
}, this.gameObject.id)
}

public AddMaterial(material: Material) {
if (!this.materialsMapped.has(material.constructor.name)) this.materialsMapped.set(material.constructor.name, []);
this.materialsMapped.get(material.constructor.name)?.push(material);
Expand All @@ -24,9 +32,12 @@ export class MeshletMesh extends Component {
}

public async SetGeometry(geometry: Geometry) {
const cached = meshletsCache.get(geometry);
this.geometry = geometry;
let cached = meshletsCache.get(geometry);
if (cached) {
this.meshlets.push(...cached);
cached.instanceCount++;
meshletsCache.set(geometry, cached);
this.meshlets.push(...cached.meshlets);
return;
}

Expand All @@ -48,6 +59,20 @@ export class MeshletMesh extends Component {
const allMeshlets = await Meshletizer.Build(interleavedVertices, indices);

this.meshlets = allMeshlets;
meshletsCache.set(geometry, this.meshlets);
meshletsCache.set(geometry, {meshlets: this.meshlets, instanceCount: 0});
}

public Destroy() {
EventSystem.emit("MeshletDeleted", this);
const cached = meshletsCache.get(this.geometry);
if (!cached) throw Error("Geometry should be in meshletsCache");

cached.instanceCount--;
meshletsCache.set(this.geometry, cached);
if (cached.instanceCount <= 0) {
meshletsCache.delete(this.geometry);
}

EventSystem.emit("RemovedComponent", this, this.gameObject.scene);
}
}
5 changes: 3 additions & 2 deletions src/components/Transform.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EventSystem } from "../Events";
import { EventSystem, EventSystemLocal } from "../Events";
import { Matrix4 } from "../math/Matrix4";
import { ObservableQuaternion, Quaternion } from "../math/Quaternion";
import { ObservableVector3, Vector3 } from "../math/Vector3";
Expand Down Expand Up @@ -37,12 +37,13 @@ export class Transform extends Component {

private onChanged() {
EventSystem.emit("CallUpdate", this, true);
EventSystem.emit("TransformUpdated", this);
}

private UpdateMatrices() {
this._localToWorldMatrix.compose(this.position, this.rotation, this.scale);
this._worldToLocalMatrix.copy(this._localToWorldMatrix).invert();
EventSystem.emit("TransformUpdated", this);
EventSystemLocal.emit("TransformUpdated", this.gameObject.id, this);
}

public Update() {
Expand Down
Loading

0 comments on commit 4cac624

Please sign in to comment.