Skip to content

Commit 1d29b20

Browse files
committed
add toJSON functions
1 parent 0b05295 commit 1d29b20

21 files changed

+222
-3
lines changed

lib/color3.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
import { EPSILON } from './utils';
22

3+
let _tmp = new Array(3);
4+
35
class _color3 {
46
constructor(r, g, b) {
57
this.r = r;
68
this.g = g;
79
this.b = b;
810
}
11+
12+
toJSON() {
13+
_tmp[0] = this.r;
14+
_tmp[1] = this.g;
15+
_tmp[2] = this.b;
16+
17+
return _tmp;
18+
}
919
}
1020

1121
/**

lib/color4.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
import { EPSILON } from './utils';
22

3+
let _tmp = new Array(4);
4+
35
class _color4 {
46
constructor(r, g, b, a) {
57
this.r = r;
68
this.g = g;
79
this.b = b;
810
this.a = a;
911
}
12+
13+
toJSON() {
14+
_tmp[0] = this.r;
15+
_tmp[1] = this.g;
16+
_tmp[2] = this.b;
17+
_tmp[3] = this.a;
18+
19+
return _tmp;
20+
}
1021
}
1122

1223
/**

lib/mat2.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
import { EPSILON } from './utils';
22

3+
let _tmp = new Array(4);
4+
35
class _mat2 {
46
constructor(m00, m01, m02, m03) {
57
this.m00 = m00;
68
this.m01 = m01;
79
this.m02 = m02;
810
this.m03 = m03;
911
}
12+
13+
toJSON() {
14+
_tmp[0] = this.m00;
15+
_tmp[1] = this.m01;
16+
_tmp[2] = this.m02;
17+
_tmp[3] = this.m03;
18+
19+
return _tmp;
20+
}
1021
}
1122

1223
/**

lib/mat23.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { EPSILON } from './utils';
22

3+
let _tmp = new Array(6);
4+
35
class _mat23 {
46
constructor(m00, m01, m02, m03, m04, m05) {
57
this.m00 = m00;
@@ -9,6 +11,17 @@ class _mat23 {
911
this.m04 = m04;
1012
this.m05 = m05;
1113
}
14+
15+
toJSON() {
16+
_tmp[0] = this.m00;
17+
_tmp[1] = this.m01;
18+
_tmp[2] = this.m02;
19+
_tmp[3] = this.m03;
20+
_tmp[4] = this.m04;
21+
_tmp[5] = this.m05;
22+
23+
return _tmp;
24+
}
1225
}
1326

1427
/**

lib/mat3.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { EPSILON } from './utils';
22
import vec3 from './vec3';
33

4+
let _tmp = new Array(9);
5+
46
class _mat3 {
57
constructor(m00, m01, m02, m03, m04, m05, m06, m07, m08) {
68
this.m00 = m00;
@@ -13,6 +15,20 @@ class _mat3 {
1315
this.m07 = m07;
1416
this.m08 = m08;
1517
}
18+
19+
toJSON() {
20+
_tmp[0] = this.m00;
21+
_tmp[1] = this.m01;
22+
_tmp[2] = this.m02;
23+
_tmp[3] = this.m03;
24+
_tmp[4] = this.m04;
25+
_tmp[5] = this.m05;
26+
_tmp[6] = this.m06;
27+
_tmp[7] = this.m07;
28+
_tmp[8] = this.m08;
29+
30+
return _tmp;
31+
}
1632
}
1733

1834
/**

lib/mat4.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { EPSILON } from './utils';
22

3+
let _tmp = new Array(16);
4+
35
class _mat4 {
46
constructor(
57
m00, m01, m02, m03,
@@ -24,6 +26,27 @@ class _mat4 {
2426
this.m14 = m14;
2527
this.m15 = m15;
2628
}
29+
30+
toJSON() {
31+
_tmp[0] = this.m00;
32+
_tmp[1] = this.m01;
33+
_tmp[2] = this.m02;
34+
_tmp[3] = this.m03;
35+
_tmp[4] = this.m04;
36+
_tmp[5] = this.m05;
37+
_tmp[6] = this.m06;
38+
_tmp[7] = this.m07;
39+
_tmp[8] = this.m08;
40+
_tmp[9] = this.m09;
41+
_tmp[10] = this.m10;
42+
_tmp[11] = this.m11;
43+
_tmp[12] = this.m12;
44+
_tmp[13] = this.m13;
45+
_tmp[14] = this.m14;
46+
_tmp[15] = this.m15;
47+
48+
return _tmp;
49+
}
2750
}
2851

2952
/**

lib/quat.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,24 @@ import vec3 from './vec3';
22
import vec4 from './vec4';
33
import mat3 from './mat3';
44

5+
let _tmp = new Array(4);
6+
57
class _quat {
68
constructor(x, y, z, w) {
79
this.x = x;
810
this.y = y;
911
this.z = z;
1012
this.w = w;
1113
}
14+
15+
toJSON() {
16+
_tmp[0] = this.x;
17+
_tmp[1] = this.y;
18+
_tmp[2] = this.z;
19+
_tmp[3] = this.w;
20+
21+
return _tmp;
22+
}
1223
}
1324

1425
/**
@@ -36,7 +47,9 @@ quat.create = function () {
3647
* @returns {quat} a new quaternion
3748
* @function
3849
*/
39-
quat.new = vec4.new;
50+
quat.new = function (x, y, z, w) {
51+
return new _quat(x, y, z, w);
52+
};
4053

4154
/**
4255
* Creates a new quat initialized with values from an existing quaternion
@@ -45,7 +58,9 @@ quat.new = vec4.new;
4558
* @returns {quat} a new quaternion
4659
* @function
4760
*/
48-
quat.clone = vec4.clone;
61+
quat.clone = function (a) {
62+
return new _quat(a.x, a.y, a.z, a.w);
63+
};
4964

5065
/**
5166
* Copy the values from one quat to another

lib/vec2.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
import { EPSILON, random } from './utils';
22

3+
let _tmp = new Array(2);
4+
35
class _vec2 {
46
constructor(x, y) {
57
this.x = x;
68
this.y = y;
79
}
10+
11+
toJSON() {
12+
_tmp[0] = this.x;
13+
_tmp[1] = this.y;
14+
15+
return _tmp;
16+
}
817
}
918

1019
/**

lib/vec3.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
import { EPSILON, random } from './utils';
22

3+
let _tmp = new Array(3);
4+
35
class _vec3 {
46
constructor(x, y, z) {
57
this.x = x;
68
this.y = y;
79
this.z = z;
810
}
11+
12+
toJSON() {
13+
_tmp[0] = this.x;
14+
_tmp[1] = this.y;
15+
_tmp[2] = this.z;
16+
17+
return _tmp;
18+
}
919
}
1020

1121
/**

lib/vec4.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
import { EPSILON, random } from './utils';
22

3+
let _tmp = new Array(4);
4+
35
class _vec4 {
46
constructor(x, y, z, w) {
57
this.x = x;
68
this.y = y;
79
this.z = z;
810
this.w = w;
911
}
12+
13+
toJSON() {
14+
_tmp[0] = this.x;
15+
_tmp[1] = this.y;
16+
_tmp[2] = this.z;
17+
_tmp[3] = this.w;
18+
19+
return _tmp;
20+
}
1021
}
1122

1223
/**

0 commit comments

Comments
 (0)