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

ModelMesh support custom vertex element and vertex buffer binding #1588

Merged
merged 76 commits into from
Jun 25, 2023

Conversation

GuoLei1990
Copy link
Member

@GuoLei1990 GuoLei1990 commented Jun 4, 2023

Please check if the PR fulfills these requirements

  • The commit message follows our guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

Featutres:

  • ModelMesh can get advanced data even if the data is set via Buffer and vice versa, It was a hack to implement advanced data reading before
engine.resourceManager.load<GLTFResource>({
      url: "https://gw.alipayobjects.com/os/OasisHub/267000040/9994/%25E5%25BD%2592%25E6%25A1%25A3.gltf",
      params: {
        keepMeshData: true,
      },
    })
    .then((glTF) => {
      // Get advanced data
      const mesh = glTF.meshes![0][0];
      const positions = mesh.getPositions();
      const normals = mesh.getNormals();
      const uvs = mesh.getUVs();

      ......
    });
  • Buffer support store cpu data when it is readable

Additional supplement:
This is a function implementation for reading Mesh low-level data, considering the most complex vertex structure (interleaved and normalized), maybe the physical MeshCollider needs it!

/**
 * Get typed array of position data, consider buffer interleaved and normalized position data.
 * @param mesh - Mesh
 * @returns Typed array of position data
 */
function getPositionTypedArray(mesh: ModelMesh): TypedArray | null {
  const vertexElements = mesh.vertexElements;
  const bufferBindings = mesh.vertexBufferBindings;
  for (let i = 0, n = vertexElements.length; i < n; i++) {
    const vertexElement = vertexElements[i];
    if (vertexElement.semantic === "POSITION") {
      const formatMetaInfo = vertexElement._formatMetaInfo;
      const bufferBinding = bufferBindings[vertexElement.bindingIndex];

      if (!bufferBinding) {
        return null;
      }

      const stride = bufferBinding.stride;
      const formatType = formatMetaInfo.type;
      const verticesData = mesh._getVertexTypedArray(
        bufferBinding.buffer.data.buffer,
        formatType
      );
      const BYTES_PER_ELEMENT = verticesData.BYTES_PER_ELEMENT;

      if (stride === BYTES_PER_ELEMENT * 3) {
        if (formatMetaInfo.normalized) {
          const normalizedVerticesData = verticesData.slice();
          const scaleFactor = formatMetaInfo.normalizedScaleFactor;

          for (let i = 0, n = verticesData.length; i < n; i++) {
            normalizedVerticesData[i] *= scaleFactor;
          }
          return normalizedVerticesData;
        } else {
          // return origin data directly
          return verticesData;
        }
      } else {
        const vertexCount = mesh.vertexCount;
        const positionBuffer = new ArrayBuffer(BYTES_PER_ELEMENT * 3);
        const positionData = mesh._getVertexTypedArray(
          positionBuffer,
          formatType
        );
        const elementOffset = vertexElement.offset;
        const scaleFactor = formatMetaInfo.normalizedScaleFactor;

        for (let i = 0; i < vertexCount; i++) {
          const pOffset = i * 3;
          const vOffset = (i * stride + elementOffset) / BYTES_PER_ELEMENT;
          if (formatMetaInfo.normalized) {
            positionData[pOffset] = verticesData[vOffset];
            positionData[pOffset + 1] = verticesData[vOffset + 1];
            positionData[pOffset + 2] = verticesData[vOffset + 2];
          } else {
            positionData[pOffset] = verticesData[vOffset] * scaleFactor;
            positionData[pOffset + 1] = verticesData[vOffset + 1] * scaleFactor;
            positionData[pOffset + 2] = verticesData[vOffset + 2] * scaleFactor;
          }
        }
        return positionData;
      }
    }
  }
  return null;
}

@GuoLei1990 GuoLei1990 added the enhancement New feature or request label Jun 4, 2023
@GuoLei1990 GuoLei1990 added this to the 1.1 milestone Jun 4, 2023
@GuoLei1990 GuoLei1990 self-assigned this Jun 4, 2023
@GuoLei1990 GuoLei1990 marked this pull request as draft June 4, 2023 11:55

// Test reset vertex elements if internal buffer is destroyed
modelMesh.setVertexElements([
new VertexElement("POSITION", 0, VertexElementFormat.Vector3, 0),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a enum refer to "POSITION", make it easy to use

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

* Buffer must be readable.
* If the data you get is modified, must call `setData()` to update buffer to GPU.
*/
get data(): Uint8Array {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will better to be writable, If user modify data in GPU and read back

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

User use setData() to write data, data will automatically sync


const vertexBufferInfos = this._vertexBufferInfos;
const vertexBufferBindings = this._vertexBufferBindings;
for (let i = 0, n = vertexBufferBindings.length; i < n; i++) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused buffers are not destroyed.

    // Repro code
    modelMesh.setVertexBufferBinding(posBuffer, 12, 0);
    modelMesh.setVertexBufferBinding(norBuffer, 12, 2);
    modelMesh.setVertexBufferBinding(uvBuffer, 8, 3);
    modelMesh.setNormals(null);
    modelMesh.uploadData(false);
    console.log(“buffers”, modelMesh._vertexBufferBindings);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VertexBufferBindings shouldn't clear( user-custom-set and even interleaved buffer), but vertexElements not remove normal vertexElement, this is a bug and has been fixed!

Copy link
Collaborator

@cptbtptpbcptdtptp cptbtptpbcptdtptp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

@GuoLei1990 GuoLei1990 merged commit 1d15f56 into galacean:dev/1.1 Jun 25, 2023
5 checks passed
@GuoLei1990 GuoLei1990 changed the title Model Mesh supports two-way data synchronization Model Mesh supports custom vertex element and vertex buffer binding Jun 25, 2023
@GuoLei1990 GuoLei1990 changed the title Model Mesh supports custom vertex element and vertex buffer binding Model Mesh support custom vertex element and vertex buffer binding Jun 25, 2023
@GuoLei1990 GuoLei1990 changed the title Model Mesh support custom vertex element and vertex buffer binding ModelMesh support custom vertex element and vertex buffer binding Jun 25, 2023
GuoLei1990 added a commit to Sway007/engine that referenced this pull request Jul 16, 2023
* dev/1.1:
  Support ktx2 loader (galacean#1625)
  feat: add texture plugin to support ktx loader (galacean#1640)
  Fix blend shape bug due to buffer index errors (galacean#1632)
  Model Mesh supports two-way data synchronization (galacean#1588)
@GuoLei1990 GuoLei1990 deleted the pr/ModeMesh-read-data branch September 18, 2023 14:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request mesh resource Resource-related functions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants