Skip to content

Commit

Permalink
improve resizeBuffers
Browse files Browse the repository at this point in the history
  • Loading branch information
agargaro committed Nov 26, 2024
1 parent 07edb0a commit f6b6bea
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
2 changes: 1 addition & 1 deletion examples/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ scene.add(instancedMesh);
main.createView({ scene, camera, enabled: false });

setInterval(() => {
instancedMesh.resize(++count);
instancedMesh.resizeBuffers(++count);
instancedMesh.instancesCount++;

instancedMesh.instances[count - 1].position.randomDirection().multiplyScalar(((Math.random() * 0.99 + 0.01) * worldSize) / 2);
Expand Down
34 changes: 24 additions & 10 deletions src/core/feature/Resize.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { DataTexture, FloatType, RedFormat } from 'three';
import { resizeSquareTextureArray_mat4, resizeSquareTextureArray_vec4 } from '../../utils/ResizeSquareTextureArray.js';
import { InstancedMesh2 } from '../InstancedMesh2.js';

declare module '../InstancedMesh2.js' {
interface InstancedMesh2 {
resize(count: number): void;
// addInstances(count: number): void;
interface InstancedMesh2<TCustomData = {}> {
resizeBuffers(count: number): this;
// addInstances(count: number, onInstanceCreation?: UpdateEntityCallback<Entity<TCustomData>>): this;
// removeInstances(count: number): void;
}
}

InstancedMesh2.prototype.resize = function (count: number): void {
InstancedMesh2.prototype.resizeBuffers = function (count: number): InstancedMesh2 {
const oldCount = this._maxCount;
this._maxCount = count;

if (this.instanceIndex) {
Expand All @@ -18,10 +20,9 @@ InstancedMesh2.prototype.resize = function (count: number): void {
this._indexArray = this.instanceIndex.array = indexArray;
}

const oldVisibilityCount = this.visibilityArray.length;
this.visibilityArray.length = count;
if (count > oldVisibilityCount) {
this.visibilityArray.fill(true, oldVisibilityCount);
if (count > oldCount) {
this.visibilityArray.fill(true, oldCount);
}

this.matricesTexture.dispose();
Expand All @@ -34,10 +35,23 @@ InstancedMesh2.prototype.resize = function (count: number): void {
this._colorArray = this.colorsTexture.image.data as unknown as Float32Array;
}

if (this.morphTexture) { // test it
const oldArray = this.morphTexture.image.data;
const size = oldArray.length / oldCount;
this.morphTexture.dispose();
this.morphTexture = new DataTexture(new Float32Array(size * count), size, count, RedFormat, FloatType);
this.morphTexture.image.data.set(oldArray);
}

// TODO custom uniform texture

if (this.instances) {
const currentCount = this.instances.length;
this.createInstances(null, currentCount, count - currentCount);
if (this.instances) { // capire meglio se le vogliamo già creare
this.createInstances(null, oldCount, count - oldCount);
}

return this;
};

// InstancedMesh2.prototype.addInstances = function (count: number, onInstanceCreation?: UpdateEntityCallback<Entity<any>>): InstancedMesh2 {
// return this;
// };

0 comments on commit f6b6bea

Please sign in to comment.