|
| 1 | +import { DataArrayTexture } from 'three'; |
| 2 | +import { FloatVertexAttributeTexture } from 'three-mesh-bvh'; |
| 3 | + |
| 4 | +export class FloatAttributeTextureArray extends DataArrayTexture { |
| 5 | + |
| 6 | + constructor() { |
| 7 | + |
| 8 | + super(); |
| 9 | + this._textures = []; |
| 10 | + |
| 11 | + } |
| 12 | + |
| 13 | + updateTexture( index, attr ) { |
| 14 | + |
| 15 | + // update all textures |
| 16 | + const tex = this._textures[ index ]; |
| 17 | + tex.updateFrom( attr ); |
| 18 | + |
| 19 | + const { width, height, data } = this.image; |
| 20 | + const length = width * height * 4; |
| 21 | + const offset = length * index; |
| 22 | + const floatView = new Float32Array( data.buffer, offset * 4, length ); |
| 23 | + floatView.set( tex.image.data ); |
| 24 | + |
| 25 | + this.dispose(); |
| 26 | + this.needsUpdate = true; |
| 27 | + |
| 28 | + } |
| 29 | + |
| 30 | + setAttributes( attrs ) { |
| 31 | + |
| 32 | + // ensure the attribute count |
| 33 | + const itemCount = attrs[ 0 ].count; |
| 34 | + const attrsLength = attrs.length; |
| 35 | + for ( let i = 0, l = attrsLength; i < l; i ++ ) { |
| 36 | + |
| 37 | + if ( attrs[ i ].count !== itemCount ) { |
| 38 | + |
| 39 | + throw new Error( 'FloatAttributeTextureArray: All attributes must have the same item count.' ); |
| 40 | + |
| 41 | + } |
| 42 | + |
| 43 | + } |
| 44 | + |
| 45 | + // initialize all textures |
| 46 | + const textures = this._textures; |
| 47 | + while ( textures.length < attrsLength ) { |
| 48 | + |
| 49 | + const tex = new FloatVertexAttributeTexture(); |
| 50 | + tex.overrideItemSize = 4; |
| 51 | + textures.push( tex ); |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | + while ( textures.length > attrsLength ) { |
| 56 | + |
| 57 | + textures.pop(); |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + // update all textures |
| 62 | + for ( let i = 0, l = attrsLength; i < l; i ++ ) { |
| 63 | + |
| 64 | + textures[ i ].updateFrom( attrs[ i ] ); |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + // determine if we need to create a new array |
| 69 | + const rootTexture = textures[ 0 ]; |
| 70 | + let { data, width, depth, height } = this.image; |
| 71 | + if ( rootTexture.image.width !== width || rootTexture.image.height !== height || depth !== attrsLength ) { |
| 72 | + |
| 73 | + width = rootTexture.image.width; |
| 74 | + height = rootTexture.image.height; |
| 75 | + depth = attrsLength; |
| 76 | + data = new Float32Array( width * height * depth * 4 ); |
| 77 | + |
| 78 | + this.image.width = width; |
| 79 | + this.image.height = height; |
| 80 | + this.image.depth = depth; |
| 81 | + this.image.data = data; |
| 82 | + |
| 83 | + } |
| 84 | + |
| 85 | + // copy the other texture data into the data array texture |
| 86 | + for ( let i = 0, l = attrsLength; i < l; i ++ ) { |
| 87 | + |
| 88 | + const tex = textures[ i ]; |
| 89 | + const length = width * height * 4; |
| 90 | + const offset = length * i; |
| 91 | + const floatView = new Float32Array( data.buffer, offset * 4, length ); |
| 92 | + floatView.set( tex.image.data ); |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + // reset the texture |
| 97 | + this.dispose(); |
| 98 | + this.needsUpdate = true; |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | +} |
0 commit comments