Skip to content
Snippets Groups Projects
sharedbuffergeometry.js 3.31 KiB
export default class SharedBufferGeometry {
  constructor(material, primitiveMode, maxBufferSize) {
    this.material = material;
    this.primitiveMode = primitiveMode;

    console.log("maxBufferSize", maxBufferSize);
    this.maxBufferSize = maxBufferSize;
    this.geometries = [];
    this.current = null;
    this.drawing = new THREE.Object3D();
    this.addBuffer();
  }

  restartPrimitive() {
    if (this.idx.position >= this.current.attributes.position.count) {
      console.error("maxBufferSize limit exceeded");
    } else if (this.idx.position !== 0) {
      let prev = (this.idx.position - 1) * 3;
      const position = this.current.attributes.position.array;
      this.addVertex(position[prev++], position[prev++], position[prev++]);

      this.idx.color++;
      this.idx.normal++;
    }
  }

  remove(prevIdx, idx) {
    // Loop through all the attributes: position, color, normal,...
    if (this.idx.position > idx.position) {
      for (const key in this.idx) {
        const componentSize = 3;
        let pos = prevIdx[key] * componentSize;
        const start = (idx[key] + 1) * componentSize;
        const end = this.idx[key] * componentSize;
        for (let i = start; i < end; i++) {
          this.current.attributes[key].array[pos++] = this.current.attributes[key].array[i];
        }
        const diff = idx[key] - prevIdx[key] + 1;
        this.idx[key] -= diff;
      }
    } else {
      for (const key in this.idx) {
        const diff = idx[key] - prevIdx[key];
        this.idx[key] -= diff;
      }
    }

    this.update();
  }

  undo(prevIdx) {
    this.idx = prevIdx;
    this.update();
  }

  addBuffer() {
    const geometry = new THREE.BufferGeometry();

    const vertices = new Float32Array(this.maxBufferSize * 3);
    const normals = new Float32Array(this.maxBufferSize * 3);
    const colors = new Float32Array(this.maxBufferSize * 3);

    const mesh = new THREE.Mesh(geometry, this.material);

    mesh.drawMode = this.primitiveMode;

    mesh.frustumCulled = false;
    mesh.vertices = vertices;

    const object3D = new THREE.Object3D();