oframe/ogl

updating a geometry with new data

RepComm opened this issue · 1 comments

Hi, I'm attempting to remesh a voxel chunk and am unsure of how to update the geometry.
I have a custom class extending geometry, and can generate a starting mesh:

constructor(gl: OGLRenderingContext, { attributes = {} } = {}) {    
    let mb = Chunk.getMeshBuilder();
    mb.clear();
    mb.cube(0,0,0, 1, 1, 1, {
      back_ZN: true,
      bottom_YN: true,
      front_Z: true,
      left_XN: true,
      right_X: true,
      top_Y: true
    });
    
    let data = mb.build();

    Object.assign(attributes, {
      position: {
        size: 3,
        data: data.vs/*new Float32Array([
          -1, -1, 0,
          3, -1, 0,
          -1, 3, 0
        ])*/
      },
      uv: {
        size: 2,
        data: data.uvs/*new Float32Array([
          0, 0,
          2, 0,
          0, 2
        ])*/
      },
    });

    super(gl, attributes);
  }

image

How do I resubmit verts and uvs to the geometry after calling super() ?

I figured it out!

I duplicated and modified the Geometry class constructors code to produce this:

export interface GeometryAttrs {
  [key: string]: Partial<Attribute>;
}

/*Geometry class extended here, etc etc...*/

updateGeometry(gl: OGLRenderingContext, attributes: GeometryAttrs) {
    this.attributes = attributes;
    // Store one VAO per program attribute locations order
    this.VAOs = {};
    this.drawRange = { start: 0, count: 0 };
    this.instancedCount = 0;
    // Unbind current VAO so that new buffers don't get added to active mesh
    this.gl.renderer.bindVertexArray(null);
    this.gl.renderer.currentGeometry = null;
    // Alias for state store to avoid redundant calls for global state
    this.glState = this.gl.renderer.state;
    // create the buffers
    for (let key in attributes) {
      this.addAttribute(key, attributes[key]);
    }
  }

Essentially just call this with the same thing you would when passing attributes to the constructor, but whenever you feel like updating the mesh.