11import { DataArrayTexture , FloatType , RGBAFormat } from 'three' ;
22import { FloatVertexAttributeTexture } from 'three-mesh-bvh' ;
33
4- function copyArrayToArray ( fromArray , fromStride , toArray , offset ) {
4+ function copyArrayToArray ( fromArray , fromStride , toArray , toStride , offset ) {
5+
6+ if ( fromStride > toStride ) {
7+
8+ throw new Error ( ) ;
9+
10+ }
511
612 const count = fromArray . length / fromStride ;
713 for ( let i = 0 ; i < count ; i ++ ) {
814
915 const i4 = 4 * i ;
1016 const is = fromStride * i ;
11- toArray [ offset + i4 + 0 ] = fromArray [ is + 0 ] ;
12- toArray [ offset + i4 + 1 ] = fromStride >= 2 ? fromArray [ is + 1 ] : 0 ;
13- toArray [ offset + i4 + 2 ] = fromStride >= 3 ? fromArray [ is + 2 ] : 0 ;
14- toArray [ offset + i4 + 3 ] = fromStride >= 4 ? fromArray [ is + 3 ] : 0 ;
17+ for ( let j = 0 ; j < toStride ; j ++ ) {
18+
19+ toArray [ offset + i4 + j ] = fromStride >= j + 1 ? fromArray [ is + j ] : 0 ;
20+
21+ }
1522
1623 }
1724
@@ -29,13 +36,23 @@ export class FloatAttributeTextureArray extends DataArrayTexture {
2936
3037 }
3138
32- updateTexture ( index , attr ) {
39+ updateAttribute ( index , attr ) {
3340
34- // update all textures
41+ // update the texture
3542 const tex = this . _textures [ index ] ;
3643 tex . updateFrom ( attr ) ;
3744
38- const { width, height, data } = this . image ;
45+ // ensure compatibility
46+ const baseImage = tex . image ;
47+ const image = this . image ;
48+ if ( baseImage . width !== image . width || baseImage . height !== image . height ) {
49+
50+ throw new Error ( 'FloatAttributeTextureArray: Attribute must be the same dimensions when updating single layer.' ) ;
51+
52+ }
53+
54+ // update the image
55+ const { width, height, data } = image ;
3956 const length = width * height * 4 ;
4057 const offset = length * index ;
4158 let itemSize = attr . itemSize ;
@@ -45,7 +62,8 @@ export class FloatAttributeTextureArray extends DataArrayTexture {
4562
4663 }
4764
48- copyArrayToArray ( tex . image . data , itemSize , data , offset ) ;
65+ // copy the data
66+ copyArrayToArray ( tex . image . data , itemSize , data , 4 , offset ) ;
4967
5068 this . dispose ( ) ;
5169 this . needsUpdate = true ;
@@ -90,23 +108,21 @@ export class FloatAttributeTextureArray extends DataArrayTexture {
90108 }
91109
92110 // determine if we need to create a new array
93- const rootTexture = textures [ 0 ] ;
94- let { data , width , depth , height } = this . image ;
95- if ( rootTexture . image . width !== width || rootTexture . image . height !== height || depth !== attrsLength ) {
111+ const baseTexture = textures [ 0 ] ;
112+ const baseImage = baseTexture . image ;
113+ const image = this . image ;
96114
97- width = rootTexture . image . width ;
98- height = rootTexture . image . height ;
99- depth = attrsLength ;
100- data = new Float32Array ( width * height * depth * 4 ) ;
115+ if ( baseImage . width !== image . width || baseImage . height !== image . height || baseImage . depth !== attrsLength ) {
101116
102- this . image . width = width ;
103- this . image . height = height ;
104- this . image . depth = depth ;
105- this . image . data = data ;
117+ image . width = baseImage . width ;
118+ image . height = baseImage . height ;
119+ image . depth = attrsLength ;
120+ image . data = new Float32Array ( image . width * image . height * image . depth * 4 ) ;
106121
107122 }
108123
109124 // copy the other texture data into the data array texture
125+ const { data, width, height } = image ;
110126 for ( let i = 0 , l = attrsLength ; i < l ; i ++ ) {
111127
112128 const tex = textures [ i ] ;
@@ -120,7 +136,7 @@ export class FloatAttributeTextureArray extends DataArrayTexture {
120136
121137 }
122138
123- copyArrayToArray ( tex . image . data , itemSize , data , offset ) ;
139+ copyArrayToArray ( tex . image . data , itemSize , data , 4 , offset ) ;
124140
125141 }
126142
0 commit comments