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

BufferAttribute: Support (de)normalization in accessors (v2) #24087

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 60 additions & 35 deletions src/core/BufferAttribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Vector4 } from '../math/Vector4.js';
import { Vector3 } from '../math/Vector3.js';
import { Vector2 } from '../math/Vector2.js';
import { Color } from '../math/Color.js';
import { createNormalizeTransform, createDenormalizeTransform } from '../math/MathUtils.js';
import { StaticDrawUsage } from '../constants.js';

const _vector = /*@__PURE__*/ new Vector3();
Expand All @@ -22,7 +23,10 @@ class BufferAttribute {
this.array = array;
this.itemSize = itemSize;
this.count = array !== undefined ? array.length / itemSize : 0;
this.normalized = normalized === true;

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is not needed because the original line will perform the same steps in the setter?

this._normalized = normalized === true;
this._normalize = normalized ? createNormalizeTransform( array ) : ( value ) => value;
this._denormalize = normalized ? createDenormalizeTransform( array ) : ( value ) => value;

this.usage = StaticDrawUsage;
this.updateRange = { offset: 0, count: - 1 };
Expand All @@ -39,6 +43,22 @@ class BufferAttribute {

}

get normalized() {

return this._normalized;

}

set normalized( normalized ) {

const array = this.array;

this._normalized = normalized;
this._normalize = normalized ? createNormalizeTransform( array ) : ( value ) => value;
this._denormalize = normalized ? createDenormalizeTransform( array ) : ( value ) => value;

}

setUsage( value ) {

this.usage = value;
Expand Down Expand Up @@ -100,9 +120,9 @@ class BufferAttribute {

}

array[ offset ++ ] = color.r;
array[ offset ++ ] = color.g;
array[ offset ++ ] = color.b;
array[ offset ++ ] = this._normalize( color.r, array );
array[ offset ++ ] = this._normalize( color.g, array );
array[ offset ++ ] = this._normalize( color.b, array );

}

Expand All @@ -126,8 +146,8 @@ class BufferAttribute {

}

array[ offset ++ ] = vector.x;
array[ offset ++ ] = vector.y;
array[ offset ++ ] = this._normalize( vector.x, array );
array[ offset ++ ] = this._normalize( vector.y, array );

}

Expand All @@ -151,9 +171,9 @@ class BufferAttribute {

}

array[ offset ++ ] = vector.x;
array[ offset ++ ] = vector.y;
array[ offset ++ ] = vector.z;
array[ offset ++ ] = this._normalize( vector.x, array );
array[ offset ++ ] = this._normalize( vector.y, array );
array[ offset ++ ] = this._normalize( vector.z, array );

}

Expand All @@ -177,11 +197,10 @@ class BufferAttribute {

}

array[ offset ++ ] = vector.x;
array[ offset ++ ] = vector.y;
array[ offset ++ ] = vector.z;
array[ offset ++ ] = vector.w;

array[ offset ++ ] = this._normalize( vector.x, array );
array[ offset ++ ] = this._normalize( vector.y, array );
array[ offset ++ ] = this._normalize( vector.z, array );
array[ offset ++ ] = this._normalize( vector.w, array );
}

return this;
Expand Down Expand Up @@ -222,7 +241,9 @@ class BufferAttribute {

for ( let i = 0, l = this.count; i < l; i ++ ) {

_vector.fromBufferAttribute( this, i );
_vector.x = this.getX( i );
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it is better to fix Vector.fromBufferAttribute...

Copy link
Contributor

Choose a reason for hiding this comment

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

No, it still works.
Then I don't think this line (and lines similar to this) should be changed?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Indeed. Inlining fromBufferAttribute() only makes the code more verbose.

_vector.y = this.getY( i );
_vector.z = this.getZ( i );

_vector.applyMatrix4( m );

Expand All @@ -238,7 +259,9 @@ class BufferAttribute {

for ( let i = 0, l = this.count; i < l; i ++ ) {

_vector.fromBufferAttribute( this, i );
_vector.x = this.getX( i );
_vector.y = this.getY( i );
_vector.z = this.getZ( i );

_vector.applyNormalMatrix( m );

Expand All @@ -254,7 +277,9 @@ class BufferAttribute {

for ( let i = 0, l = this.count; i < l; i ++ ) {

_vector.fromBufferAttribute( this, i );
_vector.x = this.getX( i );
_vector.y = this.getY( i );
_vector.z = this.getZ( i );

_vector.transformDirection( m );

Expand All @@ -268,63 +293,63 @@ class BufferAttribute {

set( value, offset = 0 ) {

this.array.set( value, offset );
this.array.set( this._normalize( value ), offset );

return this;

}

getX( index ) {

return this.array[ index * this.itemSize ];
return this._denormalize( this.array[ index * this.itemSize ] );

}

setX( index, x ) {

this.array[ index * this.itemSize ] = x;
this.array[ index * this.itemSize ] = this._normalize( x );

return this;

}

getY( index ) {

return this.array[ index * this.itemSize + 1 ];
return this._denormalize( this.array[ index * this.itemSize + 1 ] );

}

setY( index, y ) {

this.array[ index * this.itemSize + 1 ] = y;
this.array[ index * this.itemSize + 1 ] = this._normalize( y );

return this;

}

getZ( index ) {

return this.array[ index * this.itemSize + 2 ];
return this._denormalize( this.array[ index * this.itemSize + 2 ] );

}

setZ( index, z ) {

this.array[ index * this.itemSize + 2 ] = z;
this.array[ index * this.itemSize + 2 ] = this._normalize( z );

return this;

}

getW( index ) {

return this.array[ index * this.itemSize + 3 ];
return this._denormalize( this.array[ index * this.itemSize + 3 ] );

}

setW( index, w ) {

this.array[ index * this.itemSize + 3 ] = w;
this.array[ index * this.itemSize + 3 ] = this._normalize( w );

return this;

Expand All @@ -334,8 +359,8 @@ class BufferAttribute {

index *= this.itemSize;

this.array[ index + 0 ] = x;
this.array[ index + 1 ] = y;
this.array[ index + 0 ] = this._normalize( x );
this.array[ index + 1 ] = this._normalize( y );

return this;

Expand All @@ -345,9 +370,9 @@ class BufferAttribute {

index *= this.itemSize;

this.array[ index + 0 ] = x;
this.array[ index + 1 ] = y;
this.array[ index + 2 ] = z;
this.array[ index + 0 ] = this._normalize( x );
this.array[ index + 1 ] = this._normalize( y );
this.array[ index + 2 ] = this._normalize( z );

return this;

Expand All @@ -357,10 +382,10 @@ class BufferAttribute {

index *= this.itemSize;

this.array[ index + 0 ] = x;
this.array[ index + 1 ] = y;
this.array[ index + 2 ] = z;
this.array[ index + 3 ] = w;
this.array[ index + 0 ] = this._normalize( x );
this.array[ index + 1 ] = this._normalize( y );
this.array[ index + 2 ] = this._normalize( z );
this.array[ index + 3 ] = this._normalize( w );

return this;

Expand Down
Loading