-
-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathutils.js
95 lines (60 loc) · 1.66 KB
/
utils.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
import { getBVHExtremes } from '../src';
import { Vector3, Quaternion, Euler } from 'three';
// https://stackoverflow.com/questions/3062746/special-simple-random-number-generator
let _seed = null;
export function setSeed( seed ) {
_seed = seed;
}
export function random() {
if ( _seed === null ) throw new Error();
const a = 1103515245;
const c = 12345;
const m = 2e31;
_seed = ( a * _seed + c ) % m;
return _seed / m;
}
// Returns the max tree depth of the BVH
export function getMaxDepth( bvh ) {
return getBVHExtremes( bvh )[ 0 ].depth.max;
}
export function setRandomVector( vector, length ) {
vector
.set(
Math.random() - 0.5,
Math.random() - 0.5,
Math.random() - 0.5
)
.normalize()
.multiplyScalar( length );
return vector;
}
export function getRandomOrientation( matrix, range ) {
const pos = new Vector3();
const quat = new Quaternion();
const sca = new Vector3( 1, 1, 1 );
setRandomVector( pos, range );
quat.setFromEuler( new Euler( Math.random() * 180, Math.random() * 180, Math.random() * 180 ) );
matrix.compose( pos, quat, sca );
return matrix;
}
export function runOptionsMatrix( options, cb ) {
traverse( Object.keys( options ) );
function traverse( remainingKeys, state = {} ) {
if ( remainingKeys.length === 0 ) {
cb( { ...state } );
return;
}
let values;
const key = remainingKeys.pop();
if ( Array.isArray( options[ key ] ) ) {
values = options[ key ];
} else {
values = [ options[ key ] ];
}
for ( let i = 0, l = values.length; i < l; i ++ ) {
const value = values[ i ];
const newState = { ...state, [ key ]: value };
traverse( [ ...remainingKeys ], newState );
}
}
}