forked from jeromeetienne/threex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTHREEx.GeometryUtils.js
202 lines (163 loc) · 5.24 KB
/
THREEx.GeometryUtils.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
200
201
202
// This THREEx helper provide various basic functions for ```THREE.Geometry```.
// It is able to scale, translate, center a geometry. Other functions may be
// added soon.
// The API is chained for convenience.
//
// ## Scale
// To make the geometry twice larger in ```y```
// ```
// var scale = new THREE.Vector3(1,2,1);
// THREEx.GeometryUtils.scale(geometry, scale);
// ```
// ## Translate
// To make the geometry move 100 further in ```x```
// ```
// var translation = new THREE.Vector3(100,0,0);
// THREEx.GeometryUtils.translate(geometry, translation);
// ```
// ## Center
// To center the geometry on its middle point
// ```
// THREEx.GeometryUtils.center(geometry);
// ```
// ## middlePoint
// To compute the middle point of a geometry
// ```
// THREEx.GeometryUtils.middlePoint(geometry);
// ```
// # Code
//
/** @namespace */
var THREEx = THREEx || {};
THREEx.GeometryUtils = THREEx.GeometryUtils || {};
// TODO
// - chained API
// - possibility a matrix to reduce computation ?
//////////////////////////////////////////////////////////////////////////////////
// //
//////////////////////////////////////////////////////////////////////////////////
/**
* Change the scale of a geometry
*
* @params {THREE.Geometry} geometry the geometry to compute on
* @params {THREE.Vector3} scale the middlepoint of the geometry
*/
THREEx.GeometryUtils.scale = function(geometry, scale)
{
// change all geometry.vertices
for(var i = 0; i < geometry.vertices.length; i++) {
var vertex = geometry.vertices[i];
vertex.position.multiplySelf(scale);
}
// mark the vertices as dirty
geometry.__dirtyVertices = true;
// return this, to get chained API
return this;
}
THREEx.GeometryUtils.size = function(geometry)
{
// compute bounding box - TODO is that needed ?
geometry.computeBoundingBox();
// compute middle
var size= new THREE.Vector3()
size.x = geometry.boundingBox.max.x - geometry.boundingBox.min.x;
size.y = geometry.boundingBox.max.y - geometry.boundingBox.min.y;
size.z = geometry.boundingBox.max.z - geometry.boundingBox.min.z;
// return the just computed middle
return size;
}
THREEx.GeometryUtils.normalize = function(geometry)
{
var size = THREEx.GeometryUtils.size(geometry);
var scale;
if( size.x >= size.y && size.x >= size.z ){
scale = 1/size.x;
}else if( size.y >= size.x && size.y >= size.z ){
scale = 1/size.y;
}else{
scale = 1/size.z;
}
return THREEx.GeometryUtils.scale(geometry, new THREE.Vector3(scale, scale, scale));
}
//////////////////////////////////////////////////////////////////////////////////
// //
//////////////////////////////////////////////////////////////////////////////////
/**
* Compute the "middlePoint" aka the point at the middle of the boundingBox
*
* @params {THREE.Geometry} the geometry to compute on
* @returns {THREE.Vector3} the middlepoint of the geometry
*/
THREEx.GeometryUtils.middlePoint = function(geometry)
{
// compute bounding box
geometry.computeBoundingBox();
// compute middle
var middle = new THREE.Vector3()
middle.x = ( geometry.boundingBox.max.x + geometry.boundingBox.min.x ) / 2;
middle.y = ( geometry.boundingBox.max.y + geometry.boundingBox.min.y ) / 2;
middle.z = ( geometry.boundingBox.max.z + geometry.boundingBox.min.z ) / 2;
// return the just computed middle
return middle;
}
//////////////////////////////////////////////////////////////////////////////////
// //
//////////////////////////////////////////////////////////////////////////////////
THREEx.GeometryUtils.translate = function(geometry, delta)
{
// change all geometry.vertices
for(var i = 0; i < geometry.vertices.length; i++) {
var vertex = geometry.vertices[i];
vertex.position.addSelf(delta);
}
// mark the vertices as dirty
geometry.__dirtyVertices = true;
// return this, to get chained API
return this;
}
THREEx.GeometryUtils.rotate = function(geometry, angles, order)
{
order = order || 'XYZ';
// compute bounding box - TODO is that needed ?
geometry.computeBoundingBox();
var matrix = new THREE.Matrix4();
matrix.setRotationFromEuler(angles, order);
geometry.applyMatrix( matrix );
// mark the vertices as dirty
geometry.__dirtyVertices = true;
// return this, to get chained API
return this;
}
/**
* Center the geometry on its middlepoint
*/
THREEx.GeometryUtils.center = function(geometry, noX, noY, noZ)
{
// compute delta
var delta = this.middlePoint(geometry).negate();
if( noX ) delta.x = 0;
if( noY ) delta.y = 0;
if( noZ ) delta.z = 0;
return this.translate(geometry, delta)
}
//////////////////////////////////////////////////////////////////////////////////
// //
//////////////////////////////////////////////////////////////////////////////////
/**
* Initial version of attachement
* - geometry2 is the one which is moved
* - TODO make something more flexible... especially on the attachement config
*/
THREEx.GeometryUtils.attachRightLeft = function(geometry1, geometry2, delta)
{
if( delta === undefined ) delta = 0;
// compute bounding box
geometry1.computeBoundingBox();
geometry2.computeBoundingBox();
var maxX1 = geometry1.boundingBox.x[ 1 ]
var minX2 = geometry2.boundingBox.x[ 0 ];
var vector = new THREE.Vector3();
vector.x = maxX1+ (-minX2) + delta;
this.translate(geometry2, vector);
return this;
}