forked from cocos2d/cocos2d-html5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvec3.js
199 lines (174 loc) · 6.35 KB
/
vec3.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/**
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2011-2012 cocos2d-x.org
Copyright (c) 2013-2014 Chukong Technologies Inc.
Copyright (c) 2008, Luke Benstead.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
(function(cc) {
/**
* A 3d vector.
* @class
* @param {number} [x]
* @param {number} [y]
* @param {number} [z]
*/
cc.math.Vec3 = cc.kmVec3 = function (x, y, z) {
if(x && y === undefined){
this.x = x.x;
this.y = x.y;
this.z = x.z;
} else {
this.x = x || 0;
this.y = y || 0;
this.z = z || 0;
}
};
cc.math.vec3 = function(x, y, z){
return new cc.math.Vec3(x, y, z);
};
var _p = cc.math.Vec3.prototype;
_p.fill = function (x, y, z) { // =cc.kmVec3Fill
if (x && y === undefined) {
this.x = x.x;
this.y = x.y;
this.z = x.z;
} else {
this.x = x;
this.y = y;
this.z = z;
}
return this;
};
_p.length = function () { //=cc.kmVec3Length
return Math.sqrt(cc.math.square(this.x) + cc.math.square(this.y) + cc.math.square(this.z));
};
_p.lengthSq = function () { //=cc.kmVec3LengthSq
return cc.math.square(this.x) + cc.math.square(this.y) + cc.math.square(this.z)
};
_p.normalize = function () { //= cc.kmVec3Normalize
var l = 1.0 / this.length();
this.x *= l;
this.y *= l;
this.z *= l;
return this;
};
_p.cross = function (vec3) { //= cc.kmVec3Cross
var x = this.x, y = this.y, z = this.z;
this.x = (y * vec3.z) - (z * vec3.y);
this.y = (z * vec3.x) - (x * vec3.z);
this.z = (x * vec3.y) - (y * vec3.x);
return this;
};
_p.dot = function (vec) { //= cc.kmVec3Dot
return ( this.x * vec.x + this.y * vec.y + this.z * vec.z );
};
_p.add = function(vec){ //= cc.kmVec3Add
this.x += vec.x;
this.y += vec.y;
this.z += vec.z;
return this;
};
_p.subtract = function (vec) { // = cc.kmVec3Subtract
this.x -= vec.x;
this.y -= vec.y;
this.z -= vec.z;
return this;
};
_p.transform = function (mat4) { // = cc.kmVec3Transform
var x = this.x, y = this.y, z = this.z, mat = mat4.mat;
this.x = x * mat[0] + y * mat[4] + z * mat[8] + mat[12];
this.y = x * mat[1] + y * mat[5] + z * mat[9] + mat[13];
this.z = x * mat[2] + y * mat[6] + z * mat[10] + mat[14];
return this;
};
_p.transformNormal = function(mat4){
/*
a = (Vx, Vy, Vz, 0)
b = (a×M)T
Out = (bx, by, bz)
*/
//Omits the translation, only scaling + rotating
var x = this.x, y = this.y, z = this.z, mat = mat4.mat;
this.x = x * mat[0] + y * mat[4] + z * mat[8];
this.y = x * mat[1] + y * mat[5] + z * mat[9];
this.z = x * mat[2] + y * mat[6] + z * mat[10];
return this;
};
_p.transformCoord = function(mat4){ // = cc.kmVec3TransformCoord
/*
a = (Vx, Vy, Vz, 1)
b = (a×M)T
Out = 1⁄bw(bx, by, bz)
*/
var v = new cc.math.Vec4(this.x, this.y, this.z, 1.0);
v.transform(mat4);
this.x = v.x / v.w;
this.y = v.y / v.w;
this.z = v.z / v.w;
return this;
};
_p.scale = function(scale){ // = cc.kmVec3Scale
this.x *= scale;
this.y *= scale;
this.z *= scale;
return this;
};
_p.equals = function(vec){ // = cc.kmVec3AreEqual
var EPSILON = cc.math.EPSILON;
return (this.x < (vec.x + EPSILON) && this.x > (vec.x - EPSILON)) &&
(this.y < (vec.y + EPSILON) && this.y > (vec.y - EPSILON)) &&
(this.z < (vec.z + EPSILON) && this.z > (vec.z - EPSILON));
};
_p.inverseTransform = function(mat4){ //= cc.kmVec3InverseTransform
var mat = mat4.mat;
var v1 = new cc.math.Vec3(this.x - mat[12], this.y - mat[13], this.z - mat[14]);
this.x = v1.x * mat[0] + v1.y * mat[1] + v1.z * mat[2];
this.y = v1.x * mat[4] + v1.y * mat[5] + v1.z * mat[6];
this.z = v1.x * mat[8] + v1.y * mat[9] + v1.z * mat[10];
return this;
};
_p.inverseTransformNormal = function(mat4){ // = cc.kmVec3InverseTransformNormal
var x = this.x, y = this.y, z = this.z, mat = mat4.mat;
this.x = x * mat[0] + y * mat[1] + z * mat[2];
this.y = x * mat[4] + y * mat[5] + z * mat[6];
this.z = x * mat[8] + y * mat[9] + z * mat[10];
return this;
};
_p.assignFrom = function(vec){
if(!vec)
return this;
this.x = vec.x;
this.y = vec.y;
this.z = vec.z;
return this;
};
cc.math.Vec3.zero = function(vec){ // = cc.kmVec3Zero
vec.x = vec.y = vec.z = 0.0;
return vec;
};
_p.toTypeArray = function(){ //cc.kmVec3ToTypeArray
var tyArr = new Float32Array(3);
tyArr[0] = this.x;
tyArr[1] = this.y;
tyArr[2] = this.z;
return tyArr;
};
})(cc);