1
1
import { vec2 , vec3 , glMatrix } from 'gl-matrix' ;
2
2
3
+ export function isGlMatrixType ( prop , length ) {
4
+ if ( prop === null || prop === undefined ) {
5
+ return false ;
6
+ }
7
+
8
+ const usingTypedArray = glMatrix . ARRAY_TYPE === Float32Array ;
9
+ const isArray = usingTypedArray ? typeof prop === 'object' && prop . byteLength !== undefined : Array . isArray ( prop ) ;
10
+
11
+ return isArray && prop . length === length ;
12
+ }
13
+
14
+ export const isVec2 = prop => isGlMatrixType ( prop , 2 ) ;
15
+ export const isVec3 = prop => isGlMatrixType ( prop , 3 ) ;
16
+ export const isMat2d = prop => isGlMatrixType ( prop , 6 ) ;
17
+ export const isMat4 = prop => isGlMatrixType ( prop , 16 ) ;
18
+
19
+ export function isVec2Shape ( prop ) {
20
+ if ( prop === null || prop === undefined ) {
21
+ return false ;
22
+ }
23
+
24
+ const allowedKeys = [ 'x' , 'y' ] ;
25
+
26
+ return ! ! Object . keys ( prop ) . filter ( key => allowedKeys . includes ( key ) ) . length ;
27
+ }
28
+
29
+ export function isVec3Shape ( prop ) {
30
+ if ( prop === null || prop === undefined ) {
31
+ return false ;
32
+ }
33
+
34
+ const allowedKeys = [ 'x' , 'y' , 'z' ] ;
35
+
36
+ return ! ! Object . keys ( prop ) . filter ( key => allowedKeys . includes ( key ) ) . length ;
37
+ }
38
+
3
39
export function setVec2FromProp ( v , prop , defaultValue = 0 ) {
4
40
let x = defaultValue ;
5
41
let y = defaultValue ;
6
42
7
- if ( ! prop ) {
43
+ if ( prop === undefined ) {
8
44
return vec2 . set ( v , x , y ) ;
9
45
}
10
46
11
47
if ( typeof prop === 'number' ) {
12
48
x = prop ;
13
49
y = prop ;
14
- } else {
15
- const usingTypedArray = glMatrix . ARRAY_TYPE === Float32Array ;
16
- const isArray = usingTypedArray ? prop . byteLength !== undefined : Array . isArray ( prop ) ;
17
-
18
- if ( isArray ) {
19
- [ x , y ] = prop ;
20
- } else if ( typeof prop === 'object' ) {
21
- x = prop . x !== undefined ? prop . x : defaultValue ;
22
- y = prop . y !== undefined ? prop . y : defaultValue ;
50
+ } else if ( typeof prop === 'object' ) {
51
+ if ( isVec2 ( prop ) ) {
52
+ return vec2 . copy ( v , prop ) ;
23
53
}
54
+
55
+ x = prop . x !== undefined ? prop . x : defaultValue ;
56
+ y = prop . y !== undefined ? prop . y : defaultValue ;
24
57
}
25
58
26
59
return vec2 . set ( v , x , y ) ;
@@ -31,25 +64,22 @@ export function setVec3FromProp(v, prop, defaultX = 0, defaultY = 0, defaultZ =
31
64
let y = defaultY ;
32
65
let z = defaultZ ;
33
66
34
- if ( ! prop ) {
67
+ if ( prop === undefined ) {
35
68
return vec3 . set ( v , x , y , z ) ;
36
69
}
37
70
38
71
if ( typeof prop === 'number' ) {
39
72
x = prop ;
40
73
y = prop ;
41
74
z = prop ;
42
- } else {
43
- const usingTypedArray = glMatrix . ARRAY_TYPE === Float32Array ;
44
- const isArray = usingTypedArray ? prop . byteLength !== undefined : Array . isArray ( prop ) ;
45
-
46
- if ( isArray ) {
47
- [ x , y , z ] = prop ;
48
- } else if ( typeof prop === 'object' ) {
49
- x = prop . x !== undefined ? prop . x : defaultX ;
50
- y = prop . y !== undefined ? prop . y : defaultY ;
51
- z = prop . z !== undefined ? prop . z : defaultZ ;
75
+ } else if ( typeof prop === 'object' ) {
76
+ if ( isVec3 ( prop ) ) {
77
+ return vec3 . copy ( v , prop ) ;
52
78
}
79
+
80
+ x = prop . x !== undefined ? prop . x : defaultX ;
81
+ y = prop . y !== undefined ? prop . y : defaultY ;
82
+ z = prop . z !== undefined ? prop . z : defaultZ ;
53
83
}
54
84
55
85
return vec3 . set ( v , x , y , z ) ;
0 commit comments