Skip to content

Commit

Permalink
very good performance when removing lod
Browse files Browse the repository at this point in the history
is lod even useful
  • Loading branch information
BarthPaleologue committed Nov 7, 2023
1 parent c67ce95 commit dbcc399
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ lowQualityGrassBlade.material = material;

const patchSize = 10;
const patchResolution = 50;
const fieldRadius = 3;
const fieldRadius = 7;

const bladeMeshFromLod = new Array<Mesh>(2);
bladeMeshFromLod[0] = lowQualityGrassBlade;
Expand Down
45 changes: 4 additions & 41 deletions src/ts/instancePatch.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,29 @@
import {Matrix, Quaternion, Vector3} from "@babylonjs/core/Maths/math.vector";
import {Mesh} from "@babylonjs/core/Meshes/mesh";
import {InstancedMesh} from "@babylonjs/core/Meshes/instancedMesh";

function swap(oldInstance: InstancedMesh, newInstance: InstancedMesh) {
newInstance.position.copyFrom(oldInstance.position);
newInstance.rotation.copyFrom(oldInstance.rotation);
newInstance.scaling.copyFrom(oldInstance.scaling);
oldInstance.dispose();
}

export class InstancePatch {
private readonly meshFromLod: Mesh[];
instances: InstancedMesh[];
readonly position: Vector3;
readonly size: number;
readonly resolution: number;
readonly matrixBuffer: Float32Array;
lod: number;

constructor(baseMeshFromLOD: Mesh[], lod: number, patchPosition: Vector3, patchSize: number, patchResolution: number) {
this.meshFromLod = baseMeshFromLOD;
constructor(lod: number, patchPosition: Vector3, patchSize: number, patchResolution: number) {
this.instances = [];
this.position = patchPosition;
this.size = patchSize;
this.resolution = patchResolution;
this.lod = lod;
this.matrixBuffer = new Float32Array(16 * this.resolution * this.resolution);
this.scatter();
this.createMatrixBuffer();
}

setLOD(lod: number) {
public setLOD(lod: number) {
this.lod = lod;
return;
/*if (lod === this.lod) return;
const newInstances = [];
for (const instance of this.instances) {
const bladeType = this.meshFromLod[lod];
const newInstance = bladeType.createInstance(instance.name);
swap(instance, newInstance);
newInstances.push(newInstance);
}
this.instances = newInstances;
this.lod = lod;*/
}

scatter() {
//const instances = [];
//const nbInstances = this.resolution * this.resolution;

const matrices: Matrix[] = [];
private createMatrixBuffer() {
const cellSize = this.size / this.resolution;
let index = 0;
for (let x = 0; x < this.resolution; x++) {
Expand All @@ -65,20 +38,10 @@ export class InstancePatch {
Quaternion.RotationAxis(Vector3.Up(), Math.random() * 2 * Math.PI),
new Vector3(positionX, 0, positionZ)
);
matrices.push(matrix);
matrix.copyToArray(this.matrixBuffer, 16 * index);

index += 1;
}
}

/*const baseMesh = this.meshFromLod[this.lod];
const idx = baseMesh.thinInstanceAdd(matrices);
for(let i = 0; i < nbInstances; i++) {
instances.push(idx + i);
}
return instances;*/
}
}
23 changes: 18 additions & 5 deletions src/ts/instanceScatterer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import {Mesh} from "@babylonjs/core/Meshes/mesh";
import {Vector3} from "@babylonjs/core/Maths/math.vector";
import {InstancePatch} from "./instancePatch";
import { Camera } from "@babylonjs/core/Cameras/camera";
import {InstancedMesh} from "@babylonjs/core/Meshes/instancedMesh";

export class InstanceScatterer {
readonly meshFromLod: Mesh[];
Expand All @@ -20,17 +18,29 @@ export class InstanceScatterer {
this.radius = radius;
this.computeLodLevel = computeLodLevel;

const matricesByLod: Float32Array[][] = [];
for(let lod = 0; lod < this.meshFromLod.length; lod++) {
matricesByLod.push([]);
}
const lods = new Set<number>();

for (let x = -this.radius; x <= this.radius; x++) {
for (let z = -this.radius; z <= this.radius; z++) {
const radiusSquared = x * x + z * z;
if (radiusSquared > this.radius * this.radius) continue;

const patchPosition = new Vector3(x * patchSize, 0, z * patchSize);
const patch = new InstancePatch(this.meshFromLod, 1, patchPosition, patchSize, patchResolution);
const patch = new InstancePatch( 1, patchPosition, patchSize, patchResolution);
patch.setLOD(this.computeLodLevel(patch));

matricesByLod[patch.lod].push(patch.matrixBuffer);
lods.add(patch.lod);

this.map.set(patchPosition, patch);
}
}

this.updateLodMatrices(lods, matricesByLod);
}

update() {
Expand All @@ -56,8 +66,11 @@ export class InstanceScatterer {
matrixBuffersByLod[patch.lod].push(patch.matrixBuffer);
}

for(const lod of changedLODs) {
console.log("Updating LOD " + lod);
this.updateLodMatrices(changedLODs, matrixBuffersByLod);
}

private updateLodMatrices(lods: Set<number>, matrixBuffersByLod: Float32Array[][]) {
for(const lod of lods) {
const mesh = this.meshFromLod[lod];
const matrixBuffers = matrixBuffersByLod[lod];

Expand Down

0 comments on commit dbcc399

Please sign in to comment.