-
I'm integrating Jolt Physics into a game engine. When creating a Jolt Physics Mesh Shape using game engine data, a TriangleList is created. This itself isn't an issue. However, when I need to clear this TriangleList, things become complicated. I have to write a helper class to handle this task. Otherwise, I'll have memory leaks and cannot clean up properly. Is there a more efficient way to clear the TriangleList's memory? I'm looking for a solution with either less code or fewer JS-to-WASM function calls, which should improve performance significantly. class JoltMeshData {
public listVec3: Jolt.Vec3[] = [];
public triangleList: Jolt.Triangle[] = [];
public result: Jolt.TriangleList = null!;
public reset(jolt: typeof Jolt) {
this.result = new jolt.TriangleList();
}
public addVec3(jolt: typeof Jolt, x: number, y: number, z: number): Jolt.Vec3 {
const v = new jolt.Vec3(x, y, z);
this.listVec3.push(v);
return v;
}
public addTriangle(jolt: typeof Jolt, v1: Jolt.Vec3, v2: Jolt.Vec3, v3: Jolt.Vec3) {
const triangle = new jolt.Triangle(v1, v2, v3);
this.triangleList.push(triangle);
return triangle;
}
public clear(jolt: typeof Jolt) {
for (let i = 0; i < this.listVec3.length; i++) {
jolt.destroy(this.listVec3[i]);
}
this.listVec3.length = 0;
for (let i = 0; i < this.triangleList.length; i++) {
jolt.destroy(this.triangleList[i]);
}
this.triangleList.length = 0;
jolt.destroy(this.result);
this.result = null!;
}
} let temp = new JoltMeshData();
export const unpackMesh = (jolt: typeof Jolt, mesh: Mesh): Jolt.TriangleList | null => {
if (!mesh) return null;
const positions = mesh.readAttribute(0, gfx.AttributeName.ATTR_POSITION)! as Float32Array;
const indices = mesh.readIndices(0);
if (!positions || !indices) {
console.error('Failed to read mesh data');
return null;
}
temp.reset(jolt);
const triangleList = temp.result;
for (let i = 0; i < indices.length; i += 3) {
const i1 = indices[i] * 3;
const i2 = indices[i + 1] * 3;
const i3 = indices[i + 2] * 3;
const triangle = temp.addTriangle(jolt,
temp.addVec3(jolt, positions[i1], positions[i1 + 1], positions[i1 + 2]),
temp.addVec3(jolt, positions[i2], positions[i2 + 1], positions[i2 + 2]),
temp.addVec3(jolt, positions[i3], positions[i3 + 1], positions[i3 + 2])
);
triangleList.push_back(triangle);
}
return triangleList;
} only in this way can we achieve successful cleanup const begin = jolt.JoltInterface.prototype.sGetFreeMemory();
console.log(`createAndAddMeshBody: ${begin} bytes`);
let triangles = unpackMesh(jolt, mesh);
if (!triangles) {
return null;
}
temp.clear(jolt);
const end = jolt.JoltInterface.prototype.sGetFreeMemory();
console.log(`createAndAddMeshBody: ${begin - end} bytes`); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You don't actually show where you're using The
or even better, you can do something like:
to avoid allocations altogether. |
Beta Was this translation helpful? Give feedback.
You don't actually show where you're using
triangleList
, I'm assuming this is for passing toMeshShapeSettings
in which case it will need to be converted to aTriangleList
object.The
TriangleList
object keeps a copy of the vector, so you can simply do:or even better, you can do something like:
to avoid allocations altogether.