@@ -3384,17 +3384,16 @@ function computeMikkTSpaceTangents( geometry, MikkTSpace, negateSign = true ) {
3384
3384
3385
3385
if ( attribute . normalized || attribute . isInterleavedBufferAttribute ) {
3386
3386
3387
- const srcArray = attribute . isInterleavedBufferAttribute ? attribute . data . array : attribute . array ;
3388
3387
const dstArray = new Float32Array ( attribute . getCount ( ) * attribute . itemSize ) ;
3389
3388
3390
3389
for ( let i = 0 , j = 0 ; i < attribute . getCount ( ) ; i ++ ) {
3391
3390
3392
- dstArray [ j ++ ] = MathUtils . denormalize ( attribute . getX ( i ) , srcArray ) ;
3393
- dstArray [ j ++ ] = MathUtils . denormalize ( attribute . getY ( i ) , srcArray ) ;
3391
+ dstArray [ j ++ ] = attribute . getX ( i ) ;
3392
+ dstArray [ j ++ ] = attribute . getY ( i ) ;
3394
3393
3395
3394
if ( attribute . itemSize > 2 ) {
3396
3395
3397
- dstArray [ j ++ ] = MathUtils . denormalize ( attribute . getZ ( i ) , srcArray ) ;
3396
+ dstArray [ j ++ ] = attribute . getZ ( i ) ;
3398
3397
3399
3398
}
3400
3399
@@ -3733,7 +3732,7 @@ function interleaveAttributes( attributes ) {
3733
3732
let arrayLength = 0 ;
3734
3733
let stride = 0 ;
3735
3734
3736
- // calculate the the length and type of the interleavedBuffer
3735
+ // calculate the length and type of the interleavedBuffer
3737
3736
for ( let i = 0 , l = attributes . length ; i < l ; ++ i ) {
3738
3737
3739
3738
const attribute = attributes [ i ] ;
@@ -3903,7 +3902,7 @@ function estimateBytesUsed( geometry ) {
3903
3902
/**
3904
3903
* @param {BufferGeometry } geometry
3905
3904
* @param {number } tolerance
3906
- * @return {BufferGeometry> }
3905
+ * @return {BufferGeometry }
3907
3906
*/
3908
3907
function mergeVertices ( geometry , tolerance = 1e-4 ) {
3909
3908
@@ -3921,22 +3920,33 @@ function mergeVertices( geometry, tolerance = 1e-4 ) {
3921
3920
3922
3921
// attributes and new attribute arrays
3923
3922
const attributeNames = Object . keys ( geometry . attributes ) ;
3924
- const attrArrays = { } ;
3925
- const morphAttrsArrays = { } ;
3923
+ const tmpAttributes = { } ;
3924
+ const tmpMorphAttributes = { } ;
3926
3925
const newIndices = [ ] ;
3927
3926
const getters = [ 'getX' , 'getY' , 'getZ' , 'getW' ] ;
3927
+ const setters = [ 'setX' , 'setY' , 'setZ' , 'setW' ] ;
3928
3928
3929
- // initialize the arrays
3929
+ // Initialize the arrays, allocating space conservatively. Extra
3930
+ // space will be trimmed in the last step.
3930
3931
for ( let i = 0 , l = attributeNames . length ; i < l ; i ++ ) {
3931
3932
3932
3933
const name = attributeNames [ i ] ;
3934
+ const attr = geometry . attributes [ name ] ;
3933
3935
3934
- attrArrays [ name ] = [ ] ;
3936
+ tmpAttributes [ name ] = new BufferAttribute (
3937
+ new attr . array . constructor ( attr . count * attr . itemSize ) ,
3938
+ attr . itemSize ,
3939
+ attr . normalized
3940
+ ) ;
3935
3941
3936
3942
const morphAttr = geometry . morphAttributes [ name ] ;
3937
3943
if ( morphAttr ) {
3938
3944
3939
- morphAttrsArrays [ name ] = new Array ( morphAttr . length ) . fill ( ) . map ( ( ) => [ ] ) ;
3945
+ tmpMorphAttributes [ name ] = new BufferAttribute (
3946
+ new morphAttr . array . constructor ( morphAttr . count * morphAttr . itemSize ) ,
3947
+ morphAttr . itemSize ,
3948
+ morphAttr . normalized
3949
+ ) ;
3940
3950
3941
3951
}
3942
3952
@@ -3974,26 +3984,27 @@ function mergeVertices( geometry, tolerance = 1e-4 ) {
3974
3984
3975
3985
} else {
3976
3986
3977
- // copy data to the new index in the attribute arrays
3987
+ // copy data to the new index in the temporary attributes
3978
3988
for ( let j = 0 , l = attributeNames . length ; j < l ; j ++ ) {
3979
3989
3980
3990
const name = attributeNames [ j ] ;
3981
3991
const attribute = geometry . getAttribute ( name ) ;
3982
3992
const morphAttr = geometry . morphAttributes [ name ] ;
3983
3993
const itemSize = attribute . itemSize ;
3984
- const newarray = attrArrays [ name ] ;
3985
- const newMorphArrays = morphAttrsArrays [ name ] ;
3994
+ const newarray = tmpAttributes [ name ] ;
3995
+ const newMorphArrays = tmpMorphAttributes [ name ] ;
3986
3996
3987
3997
for ( let k = 0 ; k < itemSize ; k ++ ) {
3988
3998
3989
3999
const getterFunc = getters [ k ] ;
3990
- newarray . push ( attribute [ getterFunc ] ( index ) ) ;
4000
+ const setterFunc = setters [ k ] ;
4001
+ newarray [ setterFunc ] ( nextIndex , attribute [ getterFunc ] ( index ) ) ;
3991
4002
3992
4003
if ( morphAttr ) {
3993
4004
3994
4005
for ( let m = 0 , ml = morphAttr . length ; m < ml ; m ++ ) {
3995
4006
3996
- newMorphArrays [ m ] . push ( morphAttr [ m ] [ getterFunc ] ( index ) ) ;
4007
+ newMorphArrays [ m ] [ setterFunc ] ( nextIndex , morphAttr [ m ] [ getterFunc ] ( index ) ) ;
3997
4008
3998
4009
}
3999
4010
@@ -4011,31 +4022,29 @@ function mergeVertices( geometry, tolerance = 1e-4 ) {
4011
4022
4012
4023
}
4013
4024
4014
- // Generate typed arrays from new attribute arrays and update
4015
- // the attributeBuffers
4025
+ // generate result BufferGeometry
4016
4026
const result = geometry . clone ( ) ;
4017
- for ( let i = 0 , l = attributeNames . length ; i < l ; i ++ ) {
4018
-
4019
- const name = attributeNames [ i ] ;
4020
- const oldAttribute = geometry . getAttribute ( name ) ;
4021
-
4022
- const buffer = new oldAttribute . array . constructor ( attrArrays [ name ] ) ;
4023
- const attribute = new BufferAttribute ( buffer , oldAttribute . itemSize , oldAttribute . normalized ) ;
4027
+ for ( const name in geometry . attributes ) {
4024
4028
4025
- result . setAttribute ( name , attribute ) ;
4029
+ const tmpAttribute = tmpAttributes [ name ] ;
4026
4030
4027
- // Update the attribute arrays
4028
- if ( name in morphAttrsArrays ) {
4031
+ result . setAttribute ( name , new BufferAttribute (
4032
+ tmpAttribute . array . slice ( 0 , nextIndex * tmpAttribute . itemSize ) ,
4033
+ tmpAttribute . itemSize ,
4034
+ tmpAttribute . normalized ,
4035
+ ) ) ;
4029
4036
4030
- for ( let j = 0 ; j < morphAttrsArrays [ name ] . length ; j ++ ) {
4037
+ if ( ! ( name in tmpMorphAttributes ) ) continue ;
4031
4038
4032
- const oldMorphAttribute = geometry . morphAttributes [ name ] [ j ] ;
4039
+ for ( let j = 0 ; j < tmpMorphAttributes [ name ] . length ; j ++ ) {
4033
4040
4034
- const buffer = new oldMorphAttribute . array . constructor ( morphAttrsArrays [ name ] [ j ] ) ;
4035
- const morphAttribute = new BufferAttribute ( buffer , oldMorphAttribute . itemSize , oldMorphAttribute . normalized ) ;
4036
- result . morphAttributes [ name ] [ j ] = morphAttribute ;
4041
+ const tmpMorphAttribute = tmpMorphAttributes [ name ] [ j ] ;
4037
4042
4038
- }
4043
+ result . morphAttributes [ name ] [ j ] = new BufferAttribute (
4044
+ tmpMorphAttribute . array . slice ( 0 , nextIndex * tmpMorphAttribute . itemSize ) ,
4045
+ tmpMorphAttribute . itemSize ,
4046
+ tmpMorphAttribute . normalized ,
4047
+ ) ;
4039
4048
4040
4049
}
4041
4050
@@ -4052,7 +4061,7 @@ function mergeVertices( geometry, tolerance = 1e-4 ) {
4052
4061
/**
4053
4062
* @param {BufferGeometry } geometry
4054
4063
* @param {number } drawMode
4055
- * @return {BufferGeometry> }
4064
+ * @return {BufferGeometry }
4056
4065
*/
4057
4066
function toTrianglesDrawMode ( geometry , drawMode ) {
4058
4067
0 commit comments