Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve performance #2

Merged
merged 4 commits into from
Oct 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,32 @@ import { $autoRandom } from "./bind";
import { createTemplateCell } from "./lib/cell";
import { setRLE } from "./lib/setRLE";

const WORLD_SIZE = 32 * 2;
const stackHeight = 1;

export class App {
private prevGrid: BitGrid | null = null;
private prevPrevGrid: BitGrid | null = null;
private bitWorld = BitWorld.make({ width: WORLD_SIZE, height: WORLD_SIZE });
private worldSize = 32 * 2;
private bitWorld: BitWorld;
private generation = 0;
public historySize = 16;
private templateMesh: BABYLON.Mesh;
private cellMaterial: BABYLON.StandardMaterial;
private cellMeshes: BABYLON.InstancedMesh[][] = [];

/** ここに入っていれば isVisible=false */
private meshPool: BABYLON.InstancedMesh[] = [];

constructor(
private engine: BABYLON.Engine,
private scene: BABYLON.Scene,
private camera: BABYLON.ArcRotateCamera,
private pointLight: BABYLON.PointLight
) {
this.bitWorld = BitWorld.make({
width: this.worldSize,
height: this.worldSize,
});
this.bitWorld.random();
this.initCamera();
const { templateCell, cellMaterial } = createTemplateCell(scene);
Expand All @@ -38,6 +45,7 @@ export class App {
}

setSize(size: number) {
this.worldSize = size;
this.generation = 0;
this.camera.target.y = 0;
this.pointLight.position.y = 0;
Expand Down Expand Up @@ -67,7 +75,10 @@ export class App {
this.bitWorld.forEach((x, y, alive) => {
if (alive === 1) {
// セルのインスタンスを作成
const instance = this.templateMesh.createInstance(`cell_${x}_${y}`);
const instance =
this.meshPool.pop() ??
this.templateMesh.createInstance(`cell_${crypto.randomUUID()}`);
instance.isVisible = true;
instance.position = new BABYLON.Vector3(
x,
this.generation * stackHeight,
Expand All @@ -89,7 +100,15 @@ export class App {
this.cellMeshes.length - this.historySize
);
old.forEach((a) => {
a.forEach((c) => c.dispose());
a.forEach((c) => {
// プールに空きがあれば追加
if (this.meshPool.length < this.worldSize * this.worldSize) {
c.isVisible = false;
this.meshPool.push(c);
} else {
c.dispose();
}
});
});
this.cellMeshes = this.cellMeshes.slice(
this.cellMeshes.length - this.historySize
Expand All @@ -103,6 +122,10 @@ export class App {
}

clearCell() {
this.meshPool.forEach((c) => {
c.dispose();
});
this.meshPool = [];
this.cellMeshes.forEach((a) => {
a.forEach((c) => c.dispose());
});
Expand Down