-
-
Notifications
You must be signed in to change notification settings - Fork 35.2k
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
Add BufferGeometry index optimization and memory functions #14116
Add BufferGeometry index optimization and memory functions #14116
Conversation
|
It does not! Thanks for catching that. I'll rework it so each group is optimized independently. I suppose one of the complexities is that groups can have overlapping draw indices, right? If that's the case I'll probably just print a warning and not optimize.
I didn't realize that was there! I think Thanks @takahirox! |
I think so.
Perhaps it sounds reasonable. Good start. |
To summarize, you provide a method that will convert non-indexed "triangle soup" to indexed This is akin to Does this PR work for interleaved BufferAttrbutes? |
That's right, but in this case it's using every attribute in the hash to make sure they're all the same before merging the vertices.
I hadn't seen that function, but yeah! A precision value isn't required here -- it's optional and defaults to 3. Maybe it makes more sense for the function to be called Would it make sense to expose precision as an option for
It doesn't. I hadn't encountered those before -- I can take a look at what it would take to support that. Is it okay if the process of merging vertices de-interleaves the attributes? Thanks! |
Of course. That is required for
No, a precision value is required. It has a default value of 3.
That was my thinking, too.
Probably not.
It would make sense. There has been no demand for that, though. |
Ha, fair enough.
I'll think about that a little more, then. It also just occurred to me that a BufferAttribute can be shared across multiple pieces of geometry in the scene, so modifying them in place probably isn't okay (as I am here), is that right? It seems like the function should be creating and setting new BufferAttributes, right? |
Where I think this PR may be useful is in merging (sharing) vertices. I do not think reducing the footprint is that big of a deal. There will be limits to the use cases for with your approach will work. You will have to determine that yourself. :) In the end, it remains to be seen if this PR is "useful enough". |
…-geometry-indices
…s, make it work with interleavedAttributes
That's good to know! I was kind of wondering if that was going to happen. Is there a roadmap or anything documenting some of the future plans for the library? I don't see anything in the wiki or docs.
I recognize that my use cases aren't necessarily common ones. I do feel that minimizing the memory footprint before exporting a model to a file is more generally useful, especially when the intent is to load that back to the web. I'd included the function in BufferGeometry because it seemed like a natural fit along with "toNonIndexed". If the consensus is that this belongs somewhere else then I'll gladly move it! I'm just here to contribute work I'm doing otherwise to a broader community who can hopefully benefit. On that topic, is there any documentation on vision or what does or doesn't belong in the core library? Or is it more community driven? And finally, here are some of the changes and updates I've made: Changes
|
I have a coupld of late API change requests.
I recommend changing name |
Merging master
@Usnul Sorry I missed these comments. I can see the argument for using I was also looking back at the memory use function and realized that it probably won't work with interleaved attributes, so I'll fix that, as well. Thanks! |
I've update the PR, again! If this all looks good I can add documentation for the function to the BufferGeometry docs page! Changes
|
This PR was helpful to me for reducing the size of files when converting an old Geometry example ( https://gist.github.com/donmccurdy/bf17af7acfcdd5ebc57da1200e7b1e46 |
…-geometry-indices
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestions:
- Move
.mergeVertices
to BufferGeometryUtils- I can't think of a compelling reason it needs to be in the BufferGeometry class, so inclined to leave it out.
- Consider leaving
InterleavedBufferAttribute
clone and copy methods for another PR- Are they needed in this one? I don't see where, if so. We've discussed these methods before (InterleavedBufferAttribute has no clone method #15395, GLTFLoader: Support morph targets w/ interleaved attributes #12800 (comment)) and it was thorny, so separating them would simplify this review.
- (Optional) Add a parameter to
mergeVertices
so it works with points and lines- The fact that this requires an extra parameter seems like an argument in favor of creating MeshGeometry / PointGeometry / ... classes.
- (Optional) Basic unit tests
Done. The function now returns a new
Are you referring to these lines? if ( oldAttribute.isInterleavedBufferAttribute ) {
attribute = new THREE.BufferAttribute( buffer, oldAttribute.itemSize, oldAttribute.itemSize );
} else {
attribute = geometry.getAttribute( name ).clone();
attribute.setArray( buffer );
} You mean I should get rid of the special case for the interleaved attributes? I can add a warning log, instead. The other two I'll leave for other PRs, I think. I'll take a look at adding unit tests but the point geometry etc seems more complicated. You can see the new function working in the two examples in my original repo: https://gkjohnson.github.io/threejs-sandbox/mergeVertices/ |
This could serve as a unit test: Could you modify the 1 - Edit the processGeometry function to this: function processGeometry( bufGeometry ) {
// Obtain a indexed BufferGeometry with merged vertices
var indexedBufferGeom = THREE.BufferGeometryUtils.mergeVertices( bufGeometry, 0.0001 );
// Create index arrays mapping the indexed vertices to bufGeometry vertices
mapIndices( bufGeometry, indexedBufferGeom );
} 2 - Delete the function Oh, and you will have to include the |
You've added |
Ha, I completely forgot I did that (it's been awhile) -- I've removed it, now.
That's not a bad idea. I'll look into doing that in a bit. Thanks! |
I've gotten the function processGeometry( bufGeometry ) {
// Ony consider the position values when merging the vertices
var posOnlyBufGeometry = new THREE.BufferGeometry();
posOnlyBufGeometry.addAttribute( 'position', bufGeometry.getAttribute( 'position' ) );
posOnlyBufGeometry.setIndex( bufGeometry.getIndex() );
// Merge the vertices so the triangle soup is converted to indexed triangles
var indexedBufferGeom = THREE.BufferGeometryUtils.mergeVertices( posOnlyBufGeometry );
// Create index arrays mapping the indexed vertices to bufGeometry vertices
mapIndices( bufGeometry, indexedBufferGeom );
} |
examples/webgl_physics_volume.html
Outdated
var indexedBufferGeom = createIndexedBufferGeometryFromGeometry( geometry ); | ||
|
||
// Create index arrays mapping the indexed vertices to bufGeometry vertices | ||
// Create index arrays mapping the indexed vertices to bufGeometry vertices |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: i think this got converted from tabs to spaces
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep. Line 199 too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks I'll get these when I get home today
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done! Let me know if there's anything else!
Thanks! |
Can you update the docs please? |
I added
optimizeTriangleIndices
andgetMemoryUse
functions to the BufferGeometry object.Behavior
BufferGeometry.mergeVertices(tolerance = 1e-4)
Updates (or creates) the vertex attributes and index arrays with optimized, deduped versions representing the same mesh. The
tolerance
argument determines the decimal precision used when comparing and deduping vertex attributes.BufferGeometryUtils.estimateMemoryUse(geometry)
Returns the memory allocated for the geometry (vertex attribute and index arrays) as bytes. Useful for seeing how much memory a model is currently taking up.
BufferGeometry.interleaveAttributes(attrArray)
Converts the provided normal attributes into a set of interleaved attributes that share a buffer.
Other
copy
andclone
functions to InterleavedAttributeUses
Example
https://gkjohnson.github.io/threejs-sandbox/mergeVertices/
Thanks!
Garrett