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

PLYLoader: Allow custom attributes. #25001

Merged
merged 1 commit into from
Nov 24, 2022

Conversation

alankalb
Copy link
Contributor

@alankalb alankalb commented Nov 21, 2022

Related discussion: https://discourse.threejs.org/t/dracoloader-how-to-read-custom-attributes/14785

Description

When creating a .ply file, it is possible to add custom element properties that do not map to the default buffer attributes for position, normal, uv and color that are included in the PLYLoader. This adds the ability to map custom element properties to a new buffer attribute.

A new property, customPropertyMapping was added to PLYLoader and can be set via the method setCustomPropertyNameMapping. The name of the new buffer attribute is added as the object key and the element properties are added as the object value as a list. For example, the following will map the element properties custom_property_a and custom_property_b to the attribute customAttribute:

loader.setCustomPropertyMapping( {
  customAttribute: ['custom_property_a', 'custom_property_b'],
} );

The function handleElement within the parse method will find the value for each custom property in an element and push it to its corresponding customAttribute within the temporary buffer created in the parseASCII or parseBinary functions. postProcess will then set a new Float32BufferAttribute attribute on the geometry. The number of element properties in the properties list is used to set the item size of the attribute. From the example above the resulting geometry will have a customAttribute buffer attribute with an item size of 2.

This was tested with a .ply file with the following element properties:

0: {type: 'double', name: 'x'}
1: {type: 'double', name: 'y'}
2: {type: 'double', name: 'z'}
3: {type: 'double', name: 'signed_distance'}

The signed_distance property was mapped to the attribute signedDistance using:

loader.setCustomPropertyMapping( {
  signedDistance: ['signed_distance'],
} );

Parsing the file resulted in a geometry with the following attributes:

{
   position: {
      array: [...],
      itemSize: 3,
      ...
   },
   signedDistance: {
     array: [...],
     itemSize: 1,
     ...
  }
}

@Mugen87
Copy link
Collaborator

Mugen87 commented Nov 22, 2022

Is there any popular PLY exporter that supports custom attributes? Do you create the PLY file with a tool or custom code? Do you mind sharing such a PLY file in this issue?

Adding support for custom attribute names was important since PLY does not clearly specify the names of common attributes. However, adding support for uncommon, custom attributes is a bit more special.

@alankalb
Copy link
Contributor Author

@Mugen87 happly is a common exporter that allows for custom element properties. From happly:

Although the .ply format is commonly used to store 3D mesh and point cloud data, the format itself technically has nothing to do with meshes or point clouds; it simply specifies a collection elements, and data (called properties) associated with those elements. For instance in a mesh, the elements are vertices and faces; vertices then have properties like "position" and "color", while faces have a property which is a list of vertex indices. hapPLY exposes a general API for reading and writing elements and properties, as well as special-purpose helpers for the common conventions surrounding mesh data.

I am using happly at Lumafield to generate .ply files with a signed_distance property. This property represents a deviation between points on two meshes after a comparison. We have a custom vert shader that uses the signed distance values to derive vertex color values using a linear color map. Below is an example of the final render:

image

@Mugen87
Copy link
Collaborator

Mugen87 commented Nov 23, 2022

Thanks for sharing the additional information! I agree that it makes sense in context of PLY to allow custom attributes. It's definitely better than supporting properties like signed_distance explicitly since the final property set might vary from app to app.

@Mugen87 Mugen87 changed the title PLYLoader allow custom attributes PLYLoader: Allow custom attributes. Nov 23, 2022
@Mugen87 Mugen87 added this to the r147 milestone Nov 24, 2022
@Mugen87 Mugen87 merged commit f67d871 into mrdoob:dev Nov 24, 2022
@0b5vr 0b5vr mentioned this pull request Dec 7, 2022
12 tasks
0b5vr added a commit to three-types/three-ts-types that referenced this pull request Dec 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants