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 20, 2022
1 parent aa17433 commit 70c7452
Showing 1 changed file with 147 additions and 1 deletion.
148 changes: 147 additions & 1 deletion src/core/BufferAttribute.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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';

Expand Down Expand Up @@ -85,6 +86,150 @@ class BufferAttribute {

}

copyColorsArray( colors ) {

const array = this.array;
let offset = 0;

for ( let i = 0, l = colors.length; i < l; i ++ ) {

let color = colors[ i ];

if ( color === undefined ) {

console.warn( 'THREE.BufferAttribute.copyColorsArray(): color is undefined', i );
color = new Color();

}

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;

}

}

return this;

}

copyVector2sArray( vectors ) {

const array = this.array;
let offset = 0;

for ( let i = 0, l = vectors.length; i < l; i ++ ) {

let vector = vectors[ i ];

if ( vector === undefined ) {

console.warn( 'THREE.BufferAttribute.copyVector2sArray(): vector is undefined', i );
vector = new Vector2();

}

if ( this.normalized ) {

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

} else {

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

}

}

return this;

}

copyVector3sArray( vectors ) {

const array = this.array;
let offset = 0;

for ( let i = 0, l = vectors.length; i < l; i ++ ) {

let vector = vectors[ i ];

if ( vector === undefined ) {

console.warn( 'THREE.BufferAttribute.copyVector3sArray(): vector is undefined', i );
vector = new Vector3();

}

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;

}

}

return this;

}

copyVector4sArray( vectors ) {

const array = this.array;
let offset = 0;

for ( let i = 0, l = vectors.length; i < l; i ++ ) {

let vector = vectors[ i ];

if ( vector === undefined ) {

console.warn( 'THREE.BufferAttribute.copyVector4sArray(): vector is undefined', i );
vector = new Vector4();

}

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;

}

}

return this;

}

applyMatrix3( m ) {

if ( this.itemSize === 2 ) {
Expand Down Expand Up @@ -165,7 +310,8 @@ class BufferAttribute {

set( value, offset = 0 ) {

// Matching BufferAttribute constructor, do not normalize the array.
if ( this.normalized ) value = normalize( value, this.array );

this.array.set( value, offset );

return this;
Expand Down

0 comments on commit 70c7452

Please sign in to comment.