summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/rectangles/models/vec3.js
blob: b3825a914759d35a65b811c3d3f8d90c984cd10b (plain)
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
function vec3(a,b,c){
	this.a = a
	this.b = b
	this.c = c
}
vec3.prototype.add = function(v){
	this.a += v.a
	this.b += v.b
	this.c += v.c
	return this
}
vec3.prototype.sub = function(v){
	this.a -= v.a
	this.b -= v.b
	this.c -= v.c
	return this
}
vec3.prototype.clone = function(){
  return new vec3(this.a, this.b, this.c)
}

// input: mat4 projection matrix
vec3.prototype.apply_projection = function (m) {
	var x = this.a, y = this.b, z = this.c;

	var e = m.elements;
	var d = 1 / ( e[3] * x + e[7] * y + e[11] * z + e[15] ); // perspective divide

	this.x = ( e[0] * x + e[4] * y + e[8]  * z + e[12] ) * d;
	this.y = ( e[1] * x + e[5] * y + e[9]  * z + e[13] ) * d;
	this.z = ( e[2] * x + e[6] * y + e[10] * z + e[14] ) * d;

	return this;
}

vec3.prototype.serialize = function(){
  return [ round(this.a), round(this.b), round(this.c) ]
}
vec3.prototype.deserialize = function(data){
  this.a = data[0]
  this.b = data[1]
  this.c = data[2] || data[0]
  return this
}
vec3.prototype.clone = function(){
  return new vec3(this.a, this.b, this.c)
}
vec3.prototype.assign = function(v){
  this.a = v.a
  this.b = v.b
  this.c = v.c
  return this
}
vec3.prototype.mul = function(n) {
  this.a *= n
  this.b *= n
  this.c *= n
  return this
}