-
-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathVertexAttributeTexture.test.js
215 lines (144 loc) · 5.07 KB
/
VertexAttributeTexture.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import {
FloatVertexAttributeTexture,
UIntVertexAttributeTexture,
IntVertexAttributeTexture,
} from '../src/index.js';
import {
BufferAttribute,
RedFormat,
RGFormat,
RGBAFormat,
RGIntegerFormat,
RGBAIntegerFormat,
FloatType,
UnsignedShortType,
IntType,
UnsignedByteType,
ByteType,
} from 'three';
describe( 'FloatVertexAttributeTexture', () => {
it( 'should be able to take a normalized uint8 array.', () => {
const arr = new Uint8Array( 4 );
arr.fill( 255 );
const ba = new BufferAttribute( arr, 1, true );
const tex = new FloatVertexAttributeTexture();
tex.updateFrom( ba );
expect( tex.source.data.data ).toEqual( new Uint8Array( [ 255, 255, 255, 255 ] ) );
} );
it( 'should be able to take a normalized int8 array.', () => {
const arr = new Int8Array( 4 );
arr.fill( - 127 );
const ba = new BufferAttribute( arr, 1, true );
const tex = new FloatVertexAttributeTexture();
tex.updateFrom( ba );
expect( tex.source.data.data ).toEqual( new Int8Array( [ - 127, - 127, - 127, - 127 ] ) );
} );
} );
describe( 'VertexAttributeTexture', () => {
describe( 'overrideItemSize', () => {
it( 'should reset the itemSize if it is set.', () => {
const ba = new BufferAttribute( new Uint8Array( 8 ), 1, false );
const tex = new UIntVertexAttributeTexture();
tex.overrideItemSize = 4;
tex.updateFrom( ba );
expect( tex.type ).toBe( UnsignedByteType );
expect( tex.format ).toBe( RGBAIntegerFormat );
expect( tex.internalFormat ).toBe( 'RGBA8UI' );
expect( ba.itemSize ).toBe( 1 );
expect( ba.count ).toBe( 8 );
expect( tex.image.width ).toBe( 2 );
} );
it( 'should throw an error if it does not divide evenly into buffer length.', () => {
const ba = new BufferAttribute( new Uint8Array( 8 ), 1, false );
const tex = new UIntVertexAttributeTexture();
tex.overrideItemSize = 3;
let caught = false;
try {
tex.updateFrom( ba );
} catch {
caught = true;
}
expect( caught ).toBe( true );
} );
} );
it( 'should automatically use RGBAFormat when passing in an attribute with a stride of 3.', () => {
{
const ba = new BufferAttribute( new Float32Array( 6 ), 3, false );
const tex = new FloatVertexAttributeTexture();
tex.updateFrom( ba );
expect( tex.image.data ).toHaveLength( 16 );
expect( tex.image.data ).toEqual( new Float32Array( [ 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 ] ) );
expect( tex.format ).toBe( RGBAFormat );
expect( tex.internalFormat ).toBe( 'RGBA32F' );
}
{
const ba = new BufferAttribute( new Uint8Array( 6 ), 3, false );
const tex = new UIntVertexAttributeTexture();
tex.updateFrom( ba );
expect( tex.image.data ).toHaveLength( 16 );
expect( tex.image.data ).toEqual( new Uint8Array( [ 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 ] ) );
expect( tex.format ).toBe( RGBAIntegerFormat );
expect( tex.internalFormat ).toBe( 'RGBA8UI' );
}
} );
it( 'should create a large enough texture to store all data.', () => {
{
const ba = new BufferAttribute( new Float32Array( 6 ), 2, false );
const tex = new FloatVertexAttributeTexture();
tex.updateFrom( ba );
expect( tex.image.data ).toHaveLength( 8 );
expect( tex.image.width ).toBe( 2 );
expect( tex.image.height ).toBe( 2 );
}
{
const ba = new BufferAttribute( new Uint8Array( 20 ), 2, false );
const tex = new UIntVertexAttributeTexture();
tex.updateFrom( ba );
expect( tex.image.data ).toHaveLength( 32 );
expect( tex.image.width ).toBe( 4 );
expect( tex.image.height ).toBe( 4 );
}
} );
it( 'should choose correct type, format, and internal format based on attribute parameters.', () => {
{
const ba = new BufferAttribute( new Float32Array( 6 ), 2, false );
const tex = new FloatVertexAttributeTexture();
tex.updateFrom( ba );
expect( tex.type ).toBe( FloatType );
expect( tex.format ).toBe( RGFormat );
expect( tex.internalFormat ).toBe( 'RG32F' );
}
{
const ba = new BufferAttribute( new Uint8Array( 6 ), 1, true );
const tex = new FloatVertexAttributeTexture();
tex.updateFrom( ba );
expect( tex.type ).toBe( UnsignedByteType );
expect( tex.format ).toBe( RedFormat );
expect( tex.internalFormat ).toBe( 'R8' );
}
{
const ba = new BufferAttribute( new Int8Array( 6 ), 4, true );
const tex = new FloatVertexAttributeTexture();
tex.updateFrom( ba );
expect( tex.type ).toBe( ByteType );
expect( tex.format ).toBe( RGBAFormat );
expect( tex.internalFormat ).toBe( 'RGBA8_SNORM' );
}
{
const ba = new BufferAttribute( new Uint16Array( 6 ), 2, false );
const tex = new UIntVertexAttributeTexture();
tex.updateFrom( ba );
expect( tex.type ).toBe( UnsignedShortType );
expect( tex.format ).toBe( RGIntegerFormat );
expect( tex.internalFormat ).toBe( 'RG16UI' );
}
{
const ba = new BufferAttribute( new Int32Array( 6 ), 4, false );
const tex = new IntVertexAttributeTexture();
tex.updateFrom( ba );
expect( tex.type ).toBe( IntType );
expect( tex.format ).toBe( RGBAIntegerFormat );
expect( tex.internalFormat ).toBe( 'RGBA32I' );
}
} );
} );