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

Support normalized attributes #452

Merged
merged 5 commits into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 5 additions & 14 deletions src/core/MeshBVH.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,6 @@ export class MeshBVH {
const geometry = this.geometry;
const indexArr = geometry.index.array;
const posAttr = geometry.attributes.position;
const posArr = posAttr.array;

// support for an interleaved position buffer
const bufferOffset = posAttr.offset || 0;
let stride = 3;
if ( posAttr.isInterleavedBufferAttribute ) {

stride = posAttr.data.stride;

}

let buffer, uint32Array, uint16Array, float32Array;
let byteOffset = 0;
Expand Down Expand Up @@ -230,12 +220,13 @@ export class MeshBVH {
let maxx = - Infinity;
let maxy = - Infinity;
let maxz = - Infinity;

for ( let i = 3 * offset, l = 3 * ( offset + count ); i < l; i ++ ) {

const index = indexArr[ i ] * stride + bufferOffset;
const x = posArr[ index + 0 ];
const y = posArr[ index + 1 ];
const z = posArr[ index + 2 ];
const index = indexArr[ i ];
const x = posAttr.getX( index );
const y = posAttr.getY( index );
const z = posAttr.getZ( index );

if ( x < minx ) minx = x;
if ( x > maxx ) maxx = x;
Expand Down
24 changes: 8 additions & 16 deletions src/core/buildFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,33 +560,25 @@ function getAverage( triangleBounds, offset, count, axis ) {
function computeTriangleBounds( geo, fullBounds ) {

const posAttr = geo.attributes.position;
const posArr = posAttr.array;
const index = geo.index.array;
const triCount = index.length / 3;
const triangleBounds = new Float32Array( triCount * 6 );

// support for an interleaved position buffer
const bufferOffset = posAttr.offset || 0;
let stride = 3;
if ( posAttr.isInterleavedBufferAttribute ) {

stride = posAttr.data.stride;

}
const getter = [ posAttr.getX.bind( posAttr ), posAttr.getY.bind( posAttr ), posAttr.getZ.bind( posAttr ) ];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is probably the strangest line in this PR, and I don't like it very much; it was a way to be able to use the correct getter in the upcoming triangle for loop, since the loop examines the x values for all the vertices in the triangle, then the y, then the z. And I didn't really want to write a three-way switch statement to do that, or really dig too deep into the logic.

There's probably a better way to handle this, even if something as simple as a better name than getter.

Copy link
Owner

Choose a reason for hiding this comment

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

I will propose a new function in three.js that will make this easier and we'll see where that goes.

Copy link
Owner

Choose a reason for hiding this comment

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

For now I've proposed mrdoob/three.js#24515 to support this type of use case more easily. I'm hoping something along these lines in the next three.js release but in the mean time why don't we use an array of strings to index the function names as I'd done here:

https://github.com/mrdoob/three.js/blob/dev/examples/jsm/utils/BufferGeometryUtils.js#L410-L435


for ( let tri = 0; tri < triCount; tri ++ ) {

const tri3 = tri * 3;
const tri6 = tri * 6;
const ai = index[ tri3 + 0 ] * stride + bufferOffset;
const bi = index[ tri3 + 1 ] * stride + bufferOffset;
const ci = index[ tri3 + 2 ] * stride + bufferOffset;

const ai = index[ tri3 + 0 ];
const bi = index[ tri3 + 1 ];
const ci = index[ tri3 + 2 ];

for ( let el = 0; el < 3; el ++ ) {

const a = posArr[ ai + el ];
const b = posArr[ bi + el ];
const c = posArr[ ci + el ];
const a = getter[ el ]( ai );
const b = getter[ el ]( bi );
const c = getter[ el ]( ci );

let min = a;
if ( b < min ) min = b;
Expand Down