From 70c7452ee963e3c5c1ff2d55396e5eb7d35e53f0 Mon Sep 17 00:00:00 2001 From: Don McCurdy Date: Wed, 17 Aug 2022 20:37:40 -0300 Subject: [PATCH] BufferAttribute: Support (de)normalization in accessors. (#22874) * 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 --- src/core/BufferAttribute.js | 148 +++++++++++++++++++++++++++++++++++- 1 file changed, 147 insertions(+), 1 deletion(-) diff --git a/src/core/BufferAttribute.js b/src/core/BufferAttribute.js index 688842c1186d09..0fb0260578cb4d 100644 --- a/src/core/BufferAttribute.js +++ b/src/core/BufferAttribute.js @@ -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'; @@ -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 ) { @@ -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;