11import { vec2 , vec3 , glMatrix } from 'gl-matrix' ;
22
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+
339export function setVec2FromProp ( v , prop , defaultValue = 0 ) {
440 let x = defaultValue ;
541 let y = defaultValue ;
642
7- if ( ! prop ) {
43+ if ( prop === undefined ) {
844 return vec2 . set ( v , x , y ) ;
945 }
1046
1147 if ( typeof prop === 'number' ) {
1248 x = prop ;
1349 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 ) ;
2353 }
54+
55+ x = prop . x !== undefined ? prop . x : defaultValue ;
56+ y = prop . y !== undefined ? prop . y : defaultValue ;
2457 }
2558
2659 return vec2 . set ( v , x , y ) ;
@@ -31,25 +64,22 @@ export function setVec3FromProp(v, prop, defaultX = 0, defaultY = 0, defaultZ =
3164 let y = defaultY ;
3265 let z = defaultZ ;
3366
34- if ( ! prop ) {
67+ if ( prop === undefined ) {
3568 return vec3 . set ( v , x , y , z ) ;
3669 }
3770
3871 if ( typeof prop === 'number' ) {
3972 x = prop ;
4073 y = prop ;
4174 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 ) ;
5278 }
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 ;
5383 }
5484
5585 return vec3 . set ( v , x , y , z ) ;
0 commit comments