-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
|
@@ -22,7 +23,10 @@ class BufferAttribute { | |
this.array = array; | ||
this.itemSize = itemSize; | ||
this.count = array !== undefined ? array.length / itemSize : 0; | ||
this.normalized = normalized === true; | ||
|
||
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 }; | ||
|
@@ -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; | ||
|
@@ -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 ); | ||
|
||
} | ||
|
||
|
@@ -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 ); | ||
|
||
} | ||
|
||
|
@@ -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 ); | ||
|
||
} | ||
|
||
|
@@ -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; | ||
|
@@ -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 ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is better to fix There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, it still works. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed. Inlining |
||
_vector.y = this.getY( i ); | ||
_vector.z = this.getZ( i ); | ||
|
||
_vector.applyMatrix4( m ); | ||
|
||
|
@@ -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 ); | ||
|
||
|
@@ -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 ); | ||
|
||
|
@@ -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; | ||
|
||
|
@@ -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; | ||
|
||
|
@@ -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; | ||
|
||
|
@@ -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; | ||
|
||
|
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.
I think this is not needed because the original line will perform the same steps in the setter?