Skip to content

Commit

Permalink
BufferAttribute: Support (de)normalization in accessors. (mrdoob#22874)
Browse files Browse the repository at this point in the history
* BufferAttribute: Support (de)normalization in accessors.

Clean up WebGLMorphtargets.js

BufferAttribute: Add normalization support for u32, i32, and UintClampedArray

* Revert changes to MathUtils

* Clean up Color.js
  • Loading branch information
donmccurdy authored and snagy committed Sep 13, 2022
1 parent 6f15682 commit 6e5f09a
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 36 deletions.
123 changes: 107 additions & 16 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 { denormalize, normalize } from '../math/MathUtils.js';
import { StaticDrawUsage } from '../constants.js';

const _vector = /*@__PURE__*/ new Vector3();
Expand Down Expand Up @@ -100,9 +101,19 @@ class BufferAttribute {

}

array[ offset ++ ] = color.r;
array[ offset ++ ] = color.g;
array[ offset ++ ] = color.b;
if ( this.normalized ) {

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

} else {

array[ offset ++ ] = color.r;
array[ offset ++ ] = color.g;
array[ offset ++ ] = color.b;

}

}

Expand All @@ -126,8 +137,17 @@ class BufferAttribute {

}

array[ offset ++ ] = vector.x;
array[ offset ++ ] = vector.y;
if ( this.normalized ) {

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

} else {

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

}

}

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

}

array[ offset ++ ] = vector.x;
array[ offset ++ ] = vector.y;
array[ offset ++ ] = vector.z;
if ( this.normalized ) {

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

} else {

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

}

}

Expand All @@ -177,10 +207,21 @@ class BufferAttribute {

}

array[ offset ++ ] = vector.x;
array[ offset ++ ] = vector.y;
array[ offset ++ ] = vector.z;
array[ offset ++ ] = vector.w;
if ( this.normalized ) {

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

} else {

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

}

}

Expand Down Expand Up @@ -268,6 +309,8 @@ class BufferAttribute {

set( value, offset = 0 ) {

if ( this.normalized ) value = normalize( value, this.array );

this.array.set( value, offset );

return this;
Expand All @@ -276,12 +319,18 @@ class BufferAttribute {

getX( index ) {

return this.array[ index * this.itemSize ];
let x = this.array[ index * this.itemSize ];

if ( this.normalized ) x = denormalize( x, this.array );

return x;

}

setX( index, x ) {

if ( this.normalized ) x = normalize( x, this.array );

this.array[ index * this.itemSize ] = x;

return this;
Expand All @@ -290,12 +339,18 @@ class BufferAttribute {

getY( index ) {

return this.array[ index * this.itemSize + 1 ];
let y = this.array[ index * this.itemSize + 1 ];

if ( this.normalized ) y = denormalize( y, this.array );

return y;

}

setY( index, y ) {

if ( this.normalized ) y = normalize( y, this.array );

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

return this;
Expand All @@ -304,12 +359,18 @@ class BufferAttribute {

getZ( index ) {

return this.array[ index * this.itemSize + 2 ];
let z = this.array[ index * this.itemSize + 2 ];

if ( this.normalized ) z = denormalize( z, this.array );

return z;

}

setZ( index, z ) {

if ( this.normalized ) z = normalize( z, this.array );

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

return this;
Expand All @@ -318,12 +379,18 @@ class BufferAttribute {

getW( index ) {

return this.array[ index * this.itemSize + 3 ];
let w = this.array[ index * this.itemSize + 3 ];

if ( this.normalized ) w = denormalize( w, this.array );

return w;

}

setW( index, w ) {

if ( this.normalized ) w = normalize( w, this.array );

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

return this;
Expand All @@ -334,6 +401,13 @@ class BufferAttribute {

index *= this.itemSize;

if ( this.normalized ) {

x = normalize( x, this.array );
y = normalize( y, this.array );

}

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

Expand All @@ -345,6 +419,14 @@ class BufferAttribute {

index *= this.itemSize;

if ( this.normalized ) {

x = normalize( x, this.array );
y = normalize( y, this.array );
z = normalize( z, this.array );

}

this.array[ index + 0 ] = x;
this.array[ index + 1 ] = y;
this.array[ index + 2 ] = z;
Expand All @@ -357,6 +439,15 @@ class BufferAttribute {

index *= this.itemSize;

if ( this.normalized ) {

x = normalize( x, this.array );
y = normalize( y, this.array );
z = normalize( z, this.array );
w = normalize( w, this.array );

}

this.array[ index + 0 ] = x;
this.array[ index + 1 ] = y;
this.array[ index + 2 ] = z;
Expand Down
57 changes: 53 additions & 4 deletions src/core/InterleavedBufferAttribute.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Vector3 } from '../math/Vector3.js';
import { BufferAttribute } from './BufferAttribute.js';
import { denormalize, normalize } from '../math/MathUtils.js';

const _vector = /*@__PURE__*/ new Vector3();

Expand Down Expand Up @@ -85,6 +86,8 @@ class InterleavedBufferAttribute {

setX( index, x ) {

if ( this.normalized ) x = normalize( x, this.array );

this.data.array[ index * this.data.stride + this.offset ] = x;

return this;
Expand All @@ -93,6 +96,8 @@ class InterleavedBufferAttribute {

setY( index, y ) {

if ( this.normalized ) y = normalize( y, this.array );

this.data.array[ index * this.data.stride + this.offset + 1 ] = y;

return this;
Expand All @@ -101,6 +106,8 @@ class InterleavedBufferAttribute {

setZ( index, z ) {

if ( this.normalized ) z = normalize( z, this.array );

this.data.array[ index * this.data.stride + this.offset + 2 ] = z;

return this;
Expand All @@ -109,6 +116,8 @@ class InterleavedBufferAttribute {

setW( index, w ) {

if ( this.normalized ) w = normalize( w, this.array );

this.data.array[ index * this.data.stride + this.offset + 3 ] = w;

return this;
Expand All @@ -117,32 +126,55 @@ class InterleavedBufferAttribute {

getX( index ) {

return this.data.array[ index * this.data.stride + this.offset ];
let x = this.data.array[ index * this.data.stride + this.offset ];

if ( this.normalized ) x = denormalize( x, this.array );

return x;

}

getY( index ) {

return this.data.array[ index * this.data.stride + this.offset + 1 ];
let y = this.data.array[ index * this.data.stride + this.offset + 1 ];

if ( this.normalized ) y = denormalize( y, this.array );

return y;

}

getZ( index ) {

return this.data.array[ index * this.data.stride + this.offset + 2 ];
let z = this.data.array[ index * this.data.stride + this.offset + 2 ];

if ( this.normalized ) z = denormalize( z, this.array );

return z;

}

getW( index ) {

return this.data.array[ index * this.data.stride + this.offset + 3 ];
let w = this.data.array[ index * this.data.stride + this.offset + 3 ];

if ( this.normalized ) w = denormalize( w, this.array );

return w;

}

setXY( index, x, y ) {

index = index * this.data.stride + this.offset;

if ( this.normalized ) {

x = normalize( x, this.array );
y = normalize( y, this.array );

}

this.data.array[ index + 0 ] = x;
this.data.array[ index + 1 ] = y;

Expand All @@ -154,6 +186,14 @@ class InterleavedBufferAttribute {

index = index * this.data.stride + this.offset;

if ( this.normalized ) {

x = normalize( x, this.array );
y = normalize( y, this.array );
z = normalize( z, this.array );

}

this.data.array[ index + 0 ] = x;
this.data.array[ index + 1 ] = y;
this.data.array[ index + 2 ] = z;
Expand All @@ -166,6 +206,15 @@ class InterleavedBufferAttribute {

index = index * this.data.stride + this.offset;

if ( this.normalized ) {

x = normalize( x, this.array );
y = normalize( y, this.array );
z = normalize( z, this.array );
w = normalize( w, this.array );

}

this.data.array[ index + 0 ] = x;
this.data.array[ index + 1 ] = y;
this.data.array[ index + 2 ] = z;
Expand Down
10 changes: 0 additions & 10 deletions src/math/Color.js
Original file line number Diff line number Diff line change
Expand Up @@ -574,16 +574,6 @@ class Color {
this.g = attribute.getY( index );
this.b = attribute.getZ( index );

if ( attribute.normalized === true ) {

// assuming Uint8Array

this.r /= 255;
this.g /= 255;
this.b /= 255;

}

return this;

}
Expand Down
Loading

0 comments on commit 6e5f09a

Please sign in to comment.