Skip to content

Commit de3a463

Browse files
committed
Add attributes as a texture array
1 parent 9ab67f8 commit de3a463

File tree

3 files changed

+170
-0
lines changed

3 files changed

+170
-0
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,38 @@ updateFrom( camera : PerspectiveCamera | PhysicalCamera ) : void
658658

659659
Copies all fields from the passed PhysicalCamera if available otherwise the defaults are used.
660660

661+
## GeometryAttributesTextureArray
662+
663+
### .setNormalAttribute
664+
665+
```js
666+
setNormalAttribute( attr : BufferAttribute ) : void
667+
```
668+
669+
### .setTangentAttribute
670+
671+
```js
672+
setTangentAttribute( attr : BufferAttribute ) : void
673+
```
674+
675+
### .setUvAttribute
676+
677+
```js
678+
setUvAttribute( attr : BufferAttribute ) : void
679+
```
680+
681+
### .setColorAttribute
682+
683+
```js
684+
setColorAttribute( attr : BufferAttribute ) : void
685+
```
686+
687+
### .updateFrom
688+
689+
```js
690+
updateFrom( normal : BufferAttribute, tangent : BufferAttribute, uv : BufferAttribute, color : BufferAttribute ) : void
691+
```
692+
661693
## MaterialsTexture
662694

663695
_extends DataTexture_
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { FloatAttributeTextureArray } from './FloatAttributeTextureArray.js';
2+
3+
export class GeometryAttributesTextureArray extends FloatAttributeTextureArray {
4+
5+
setNormalAttribute( attr ) {
6+
7+
this.updateAttribute( 0, attr );
8+
9+
}
10+
11+
setTangentAttribute( attr ) {
12+
13+
this.updateAttribute( 1, attr );
14+
15+
}
16+
17+
setUvAttribute( attr ) {
18+
19+
this.updateAttribute( 2, attr );
20+
21+
}
22+
23+
setColorAttribute( attr ) {
24+
25+
this.updateAttribute( 3, attr );
26+
27+
}
28+
29+
updateFrom( normal, tangent, uv, color ) {
30+
31+
this.setAttributes( [ normal, tangent, uv, color ] );
32+
33+
}
34+
35+
}

0 commit comments

Comments
 (0)