-
-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathMeshBVH.serialize.test.js
164 lines (110 loc) · 4.97 KB
/
MeshBVH.serialize.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
import {
BufferGeometry,
SphereGeometry,
BufferAttribute,
} from 'three';
import {
MeshBVH,
} from '../src/index.js';
describe( 'Serialization', () => {
let geometry;
beforeEach( () => {
geometry = new SphereGeometry( 1, 10, 10 );
} );
it( 'should serialize then deserialize to the same structure.', () => {
const bvh = new MeshBVH( geometry );
const serialized = MeshBVH.serialize( bvh );
const deserializedBVH = MeshBVH.deserialize( serialized, geometry );
// use a custom object since anonymous functions cause the
// test function to fail
const testObj = { ...bvh };
delete testObj.resolveTriangleIndex;
expect( deserializedBVH ).toMatchObject( testObj );
} );
it( 'should serialize then deserialize to the same structure with indirect = true.', () => {
const bvh = new MeshBVH( geometry, { indirect: true } );
const serialized = MeshBVH.serialize( bvh );
const deserializedBVH = MeshBVH.deserialize( serialized, geometry );
// use a custom object since anonymous functions cause the
// test function to fail
const testObj = { ...bvh };
delete testObj.resolveTriangleIndex;
expect( deserializedBVH ).toMatchObject( testObj );
expect( bvh.resolveTriangleIndex( 0 ) ).toEqual( deserializedBVH.resolveTriangleIndex( 0 ) );
} );
it( 'should create a new index if one does not exist when deserializing.', () => {
const bvh = new MeshBVH( geometry );
const serialized = MeshBVH.serialize( bvh );
geometry.setIndex( null );
MeshBVH.deserialize( serialized, geometry );
expect( geometry.index ).toBeTruthy();
} );
it( 'should create an index buffer with Uint16Array if the geometry is small enough.', () => {
const geometry = new BufferGeometry();
geometry.setAttribute( 'position', new BufferAttribute( new Float32Array( 60000 * 3 ), 3, false ) );
const bvh = new MeshBVH( geometry );
expect( geometry.index.array instanceof Uint16Array ).toBe( true );
expect( bvh ).toBeTruthy();
} );
it( 'should create an index buffer with Uint32Array if the geometry is large enough.', () => {
const geometry = new BufferGeometry();
geometry.setAttribute( 'position', new BufferAttribute( new Float32Array( 70000 * 3 ), 3, false ) );
const bvh = new MeshBVH( geometry );
expect( geometry.index.array instanceof Uint32Array ).toBe( true );
expect( bvh ).toBeTruthy();
} );
describe( 'cloneBuffers', () => {
it( 'should clone the index buffer from the target geometry when true.', () => {
const bvh = new MeshBVH( geometry );
const serialized = MeshBVH.serialize( bvh, { cloneBuffers: true, indirect: true } );
expect( geometry.index.array ).not.toBe( serialized.index );
expect( bvh._roots ).not.toBe( serialized.roots );
expect( bvh._roots[ 0 ] ).not.toBe( serialized.roots[ 0 ] );
expect( bvh._roots ).toEqual( serialized.roots );
expect( bvh._indirectBuffer ).toBe( serialized.indirectBuffer );
} );
it( 'should clone the index buffer from the target geometry when false.', () => {
const bvh = new MeshBVH( geometry );
const serialized = MeshBVH.serialize( bvh, { cloneBuffers: false, indirect: true } );
expect( geometry.index.array ).toBe( serialized.index );
expect( bvh._roots ).toBe( serialized.roots );
expect( bvh._roots[ 0 ] ).toBe( serialized.roots[ 0 ] );
expect( bvh._roots ).toEqual( serialized.roots );
expect( bvh._indirectBuffer ).toBe( serialized.indirectBuffer );
} );
} );
describe( 'setIndex', () => {
it( 'should not copy the index buffer onto the target geometry if setIndex is false.', () => {
const cloned = geometry.clone();
const bvh = new MeshBVH( geometry );
const serialized = MeshBVH.serialize( bvh, { cloneBuffers: true } );
expect( cloned.index.array ).not.toBe( serialized.index );
expect( cloned.index.array ).not.toEqual( serialized.index );
MeshBVH.deserialize( serialized, cloned, { setIndex: false } );
expect( cloned.index.array ).not.toBe( serialized.index );
expect( cloned.index.array ).not.toEqual( serialized.index );
} );
it( 'should copy the index buffer onto the target geometry if setIndex is true.', () => {
const cloned = geometry.clone();
const bvh = new MeshBVH( geometry );
const serialized = MeshBVH.serialize( bvh, { cloneBuffers: true } );
expect( cloned.index.array ).not.toBe( serialized.index );
expect( cloned.index.array ).not.toEqual( serialized.index );
MeshBVH.deserialize( serialized, cloned, { setIndex: true } );
expect( cloned.index.array ).not.toBe( serialized.index );
expect( cloned.index.array ).toEqual( serialized.index );
} );
} );
describe( 'indirect', () => {
it( 'should correctly deserialize the bvh.', () => {
const cloned = geometry.clone();
const bvh = new MeshBVH( geometry, { indirect: true } );
const serialized = MeshBVH.serialize( bvh );
const deserialized = MeshBVH.deserialize( serialized, cloned );
expect( deserialized.indirect ).toBe( true );
expect( () => {
deserialized.resolveTriangleIndex( 0 );
} ).not.toThrow();
} );
} );
} );