| 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 |
1×
1×
1×
1×
379×
379×
1×
882×
882×
882×
1×
1×
1×
1×
1×
1×
1×
| import Vector3 from './Vector3';
import glMatrix from '../dep/glmatrix';
var vec3 = glMatrix.vec3;
var mat4 = glMatrix.mat4;
var vec4 = glMatrix.vec4;
/**
* @constructor
* @alias clay.Plane
* @param {clay.Vector3} [normal]
* @param {number} [distance]
*/
var Plane = function(normal, distance) {
/**
* Normal of the plane
* @type {clay.Vector3}
*/
this.normal = normal || new Vector3(0, 1, 0);
/**
* Constant of the plane equation, used as distance to the origin
* @type {number}
*/
this.distance = distance || 0;
};
Plane.prototype = {
constructor: Plane,
/**
* Distance from a given point to the plane
* @param {clay.Vector3} point
* @return {number}
*/
distanceToPoint: function(point) {
return vec3.dot(point.array, this.normal.array) - this.distance;
},
/**
* Calculate the projection point on the plane
* @param {clay.Vector3} point
* @param {clay.Vector3} out
* @return {clay.Vector3}
*/
projectPoint: function(point, out) {
if (!out) {
out = new Vector3();
}
var d = this.distanceToPoint(point);
vec3.scaleAndAdd(out.array, point.array, this.normal.array, -d);
out._dirty = true;
return out;
},
/**
* Normalize the plane's normal and calculate the distance
*/
normalize: function() {
var invLen = 1 / vec3.len(this.normal.array);
vec3.scale(this.normal.array, invLen);
this.distance *= invLen;
},
/**
* If the plane intersect a frustum
* @param {clay.Frustum} Frustum
* @return {boolean}
*/
intersectFrustum: function(frustum) {
// Check if all coords of frustum is on plane all under plane
var coords = frustum.vertices;
var normal = this.normal.array;
var onPlane = vec3.dot(coords[0].array, normal) > this.distance;
for (var i = 1; i < 8; i++) {
if ((vec3.dot(coords[i].array, normal) > this.distance) != onPlane) {
return true;
}
}
},
/**
* Calculate the intersection point between plane and a given line
* @function
* @param {clay.Vector3} start start point of line
* @param {clay.Vector3} end end point of line
* @param {clay.Vector3} [out]
* @return {clay.Vector3}
*/
intersectLine: (function() {
var rd = vec3.create();
return function(start, end, out) {
var d0 = this.distanceToPoint(start);
var d1 = this.distanceToPoint(end);
if ((d0 > 0 && d1 > 0) || (d0 < 0 && d1 < 0)) {
return null;
}
// Ray intersection
var pn = this.normal.array;
var d = this.distance;
var ro = start.array;
// direction
vec3.sub(rd, end.array, start.array);
vec3.normalize(rd, rd);
var divider = vec3.dot(pn, rd);
// ray is parallel to the plane
if (divider === 0) {
return null;
}
if (!out) {
out = new Vector3();
}
var t = (vec3.dot(pn, ro) - d) / divider;
vec3.scaleAndAdd(out.array, ro, rd, -t);
out._dirty = true;
return out;
};
})(),
/**
* Apply an affine transform matrix to plane
* @function
* @return {clay.Matrix4}
*/
applyTransform: (function() {
var inverseTranspose = mat4.create();
var normalv4 = vec4.create();
var pointv4 = vec4.create();
pointv4[3] = 1;
return function(m4) {
m4 = m4.array;
// Transform point on plane
vec3.scale(pointv4, this.normal.array, this.distance);
vec4.transformMat4(pointv4, pointv4, m4);
this.distance = vec3.dot(pointv4, this.normal.array);
// Transform plane normal
mat4.invert(inverseTranspose, m4);
mat4.transpose(inverseTranspose, inverseTranspose);
normalv4[3] = 0;
vec3.copy(normalv4, this.normal.array);
vec4.transformMat4(normalv4, normalv4, inverseTranspose);
vec3.copy(this.normal.array, normalv4);
};
})(),
/**
* Copy from another plane
* @param {clay.Vector3} plane
*/
copy: function(plane) {
vec3.copy(this.normal.array, plane.normal.array);
this.normal._dirty = true;
this.distance = plane.distance;
},
/**
* Clone a new plane
* @return {clay.Plane}
*/
clone: function() {
var plane = new Plane();
plane.copy(this);
return plane;
}
};
export default Plane;
|