瀏覽代碼

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
Don McCurdy 2 年之前
父節點
當前提交
e4f5e0f626
共有 4 個文件被更改,包括 160 次插入52 次删除
  1. 107 16
      src/core/BufferAttribute.js
  2. 53 4
      src/core/InterleavedBufferAttribute.js
  3. 0 10
      src/math/Color.js
  4. 0 22
      src/renderers/webgl/WebGLMorphtargets.js

+ 107 - 16
src/core/BufferAttribute.js

@@ -2,6 +2,7 @@ import { Vector4 } from '../math/Vector4.js';
 import { Vector3 } from '../math/Vector3.js';
 import { Vector3 } from '../math/Vector3.js';
 import { Vector2 } from '../math/Vector2.js';
 import { Vector2 } from '../math/Vector2.js';
 import { Color } from '../math/Color.js';
 import { Color } from '../math/Color.js';
+import { denormalize, normalize } from '../math/MathUtils.js';
 import { StaticDrawUsage } from '../constants.js';
 import { StaticDrawUsage } from '../constants.js';
 
 
 const _vector = /*@__PURE__*/ new Vector3();
 const _vector = /*@__PURE__*/ new Vector3();
@@ -102,9 +103,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;
+
+			}
 
 
 		}
 		}
 
 
@@ -128,8 +139,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;
+
+			}
 
 
 		}
 		}
 
 
@@ -153,9 +173,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;
+
+			}
 
 
 		}
 		}
 
 
@@ -179,10 +209,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;
+
+			}
 
 
 		}
 		}
 
 
@@ -270,6 +311,8 @@ class BufferAttribute {
 
 
 	set( value, offset = 0 ) {
 	set( value, offset = 0 ) {
 
 
+		if ( this.normalized ) value = normalize( value, this.array );
+
 		this.array.set( value, offset );
 		this.array.set( value, offset );
 
 
 		return this;
 		return this;
@@ -278,12 +321,18 @@ class BufferAttribute {
 
 
 	getX( index ) {
 	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 ) {
 	setX( index, x ) {
 
 
+		if ( this.normalized ) x = normalize( x, this.array );
+
 		this.array[ index * this.itemSize ] = x;
 		this.array[ index * this.itemSize ] = x;
 
 
 		return this;
 		return this;
@@ -292,12 +341,18 @@ class BufferAttribute {
 
 
 	getY( index ) {
 	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 ) {
 	setY( index, y ) {
 
 
+		if ( this.normalized ) y = normalize( y, this.array );
+
 		this.array[ index * this.itemSize + 1 ] = y;
 		this.array[ index * this.itemSize + 1 ] = y;
 
 
 		return this;
 		return this;
@@ -306,12 +361,18 @@ class BufferAttribute {
 
 
 	getZ( index ) {
 	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 ) {
 	setZ( index, z ) {
 
 
+		if ( this.normalized ) z = normalize( z, this.array );
+
 		this.array[ index * this.itemSize + 2 ] = z;
 		this.array[ index * this.itemSize + 2 ] = z;
 
 
 		return this;
 		return this;
@@ -320,12 +381,18 @@ class BufferAttribute {
 
 
 	getW( index ) {
 	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 ) {
 	setW( index, w ) {
 
 
+		if ( this.normalized ) w = normalize( w, this.array );
+
 		this.array[ index * this.itemSize + 3 ] = w;
 		this.array[ index * this.itemSize + 3 ] = w;
 
 
 		return this;
 		return this;
@@ -336,6 +403,13 @@ class BufferAttribute {
 
 
 		index *= this.itemSize;
 		index *= this.itemSize;
 
 
+		if ( this.normalized ) {
+
+			x = normalize( x, this.array );
+			y = normalize( y, this.array );
+
+		}
+
 		this.array[ index + 0 ] = x;
 		this.array[ index + 0 ] = x;
 		this.array[ index + 1 ] = y;
 		this.array[ index + 1 ] = y;
 
 
@@ -347,6 +421,14 @@ class BufferAttribute {
 
 
 		index *= this.itemSize;
 		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 + 0 ] = x;
 		this.array[ index + 1 ] = y;
 		this.array[ index + 1 ] = y;
 		this.array[ index + 2 ] = z;
 		this.array[ index + 2 ] = z;
@@ -359,6 +441,15 @@ class BufferAttribute {
 
 
 		index *= this.itemSize;
 		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 + 0 ] = x;
 		this.array[ index + 1 ] = y;
 		this.array[ index + 1 ] = y;
 		this.array[ index + 2 ] = z;
 		this.array[ index + 2 ] = z;

+ 53 - 4
src/core/InterleavedBufferAttribute.js

@@ -1,5 +1,6 @@
 import { Vector3 } from '../math/Vector3.js';
 import { Vector3 } from '../math/Vector3.js';
 import { BufferAttribute } from './BufferAttribute.js';
 import { BufferAttribute } from './BufferAttribute.js';
+import { denormalize, normalize } from '../math/MathUtils.js';
 
 
 const _vector = /*@__PURE__*/ new Vector3();
 const _vector = /*@__PURE__*/ new Vector3();
 
 
@@ -87,6 +88,8 @@ class InterleavedBufferAttribute {
 
 
 	setX( index, x ) {
 	setX( index, x ) {
 
 
+		if ( this.normalized ) x = normalize( x, this.array );
+
 		this.data.array[ index * this.data.stride + this.offset ] = x;
 		this.data.array[ index * this.data.stride + this.offset ] = x;
 
 
 		return this;
 		return this;
@@ -95,6 +98,8 @@ class InterleavedBufferAttribute {
 
 
 	setY( index, y ) {
 	setY( index, y ) {
 
 
+		if ( this.normalized ) y = normalize( y, this.array );
+
 		this.data.array[ index * this.data.stride + this.offset + 1 ] = y;
 		this.data.array[ index * this.data.stride + this.offset + 1 ] = y;
 
 
 		return this;
 		return this;
@@ -103,6 +108,8 @@ class InterleavedBufferAttribute {
 
 
 	setZ( index, z ) {
 	setZ( index, z ) {
 
 
+		if ( this.normalized ) z = normalize( z, this.array );
+
 		this.data.array[ index * this.data.stride + this.offset + 2 ] = z;
 		this.data.array[ index * this.data.stride + this.offset + 2 ] = z;
 
 
 		return this;
 		return this;
@@ -111,6 +118,8 @@ class InterleavedBufferAttribute {
 
 
 	setW( index, w ) {
 	setW( index, w ) {
 
 
+		if ( this.normalized ) w = normalize( w, this.array );
+
 		this.data.array[ index * this.data.stride + this.offset + 3 ] = w;
 		this.data.array[ index * this.data.stride + this.offset + 3 ] = w;
 
 
 		return this;
 		return this;
@@ -119,25 +128,41 @@ class InterleavedBufferAttribute {
 
 
 	getX( index ) {
 	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 ) {
 	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 ) {
 	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 ) {
 	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;
 
 
 	}
 	}
 
 
@@ -145,6 +170,13 @@ class InterleavedBufferAttribute {
 
 
 		index = index * this.data.stride + this.offset;
 		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 + 0 ] = x;
 		this.data.array[ index + 1 ] = y;
 		this.data.array[ index + 1 ] = y;
 
 
@@ -156,6 +188,14 @@ class InterleavedBufferAttribute {
 
 
 		index = index * this.data.stride + this.offset;
 		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 + 0 ] = x;
 		this.data.array[ index + 1 ] = y;
 		this.data.array[ index + 1 ] = y;
 		this.data.array[ index + 2 ] = z;
 		this.data.array[ index + 2 ] = z;
@@ -168,6 +208,15 @@ class InterleavedBufferAttribute {
 
 
 		index = index * this.data.stride + this.offset;
 		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 + 0 ] = x;
 		this.data.array[ index + 1 ] = y;
 		this.data.array[ index + 1 ] = y;
 		this.data.array[ index + 2 ] = z;
 		this.data.array[ index + 2 ] = z;

+ 0 - 10
src/math/Color.js

@@ -580,16 +580,6 @@ class Color {
 		this.g = attribute.getY( index );
 		this.g = attribute.getY( index );
 		this.b = attribute.getZ( index );
 		this.b = attribute.getZ( index );
 
 
-		if ( attribute.normalized === true ) {
-
-			// assuming Uint8Array
-
-			this.r /= 255;
-			this.g /= 255;
-			this.b /= 255;
-
-		}
-
 		return this;
 		return this;
 
 
 	}
 	}

+ 0 - 22
src/renderers/webgl/WebGLMorphtargets.js

@@ -15,22 +15,6 @@ function absNumericalSort( a, b ) {
 
 
 }
 }
 
 
-function denormalize( morph, attribute ) {
-
-	let denominator = 1;
-	const array = attribute.isInterleavedBufferAttribute ? attribute.data.array : attribute.array;
-
-	if ( array instanceof Int8Array ) denominator = 127;
-	else if ( array instanceof Uint8Array ) denominator = 255;
-	else if ( array instanceof Uint16Array ) denominator = 65535;
-	else if ( array instanceof Int16Array ) denominator = 32767;
-	else if ( array instanceof Int32Array ) denominator = 2147483647;
-	else console.error( 'THREE.WebGLMorphtargets: Unsupported morph attribute data type: ', array );
-
-	morph.divideScalar( denominator );
-
-}
-
 function WebGLMorphtargets( gl, capabilities, textures ) {
 function WebGLMorphtargets( gl, capabilities, textures ) {
 
 
 	const influencesList = {};
 	const influencesList = {};
@@ -114,8 +98,6 @@ function WebGLMorphtargets( gl, capabilities, textures ) {
 
 
 							morph.fromBufferAttribute( morphTarget, j );
 							morph.fromBufferAttribute( morphTarget, j );
 
 
-							if ( morphTarget.normalized === true ) denormalize( morph, morphTarget );
-
 							buffer[ offset + stride + 0 ] = morph.x;
 							buffer[ offset + stride + 0 ] = morph.x;
 							buffer[ offset + stride + 1 ] = morph.y;
 							buffer[ offset + stride + 1 ] = morph.y;
 							buffer[ offset + stride + 2 ] = morph.z;
 							buffer[ offset + stride + 2 ] = morph.z;
@@ -127,8 +109,6 @@ function WebGLMorphtargets( gl, capabilities, textures ) {
 
 
 							morph.fromBufferAttribute( morphNormal, j );
 							morph.fromBufferAttribute( morphNormal, j );
 
 
-							if ( morphNormal.normalized === true ) denormalize( morph, morphNormal );
-
 							buffer[ offset + stride + 4 ] = morph.x;
 							buffer[ offset + stride + 4 ] = morph.x;
 							buffer[ offset + stride + 5 ] = morph.y;
 							buffer[ offset + stride + 5 ] = morph.y;
 							buffer[ offset + stride + 6 ] = morph.z;
 							buffer[ offset + stride + 6 ] = morph.z;
@@ -140,8 +120,6 @@ function WebGLMorphtargets( gl, capabilities, textures ) {
 
 
 							morph.fromBufferAttribute( morphColor, j );
 							morph.fromBufferAttribute( morphColor, j );
 
 
-							if ( morphColor.normalized === true ) denormalize( morph, morphColor );
-
 							buffer[ offset + stride + 8 ] = morph.x;
 							buffer[ offset + stride + 8 ] = morph.x;
 							buffer[ offset + stride + 9 ] = morph.y;
 							buffer[ offset + stride + 9 ] = morph.y;
 							buffer[ offset + stride + 10 ] = morph.z;
 							buffer[ offset + stride + 10 ] = morph.z;