-
Notifications
You must be signed in to change notification settings - Fork 0
/
fourdee.js
203 lines (167 loc) · 5.71 KB
/
fourdee.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
203
/****************
* Object4D API *
****************/
function Object4D()
{
this.position = new THREE.Vector4();
this.rotation = new Euler4D();
this.scale = 1.;
this.geometry = undefined;
this.projection = undefined; // projection is the THREE.Geometry 3D created by projection of the object
this.lineProjection = undefined;
this.position3D = new THREE.Vector3();
this.children3D = [];
this.dirty = true;
this.positionalOnly = false;
}
// int[] faces, int[] materials
Object4D.prototype.setFaceMaterial = function(faces, materials)
{
for(var i = 0; i < materials.length; i++)
this.geometry.faces[faces[i]].materialIndex = materials[i];
}
// int [][] faces
Object4D.prototype.setFaceGroupsMaterial = function(faceGroups)
{
for(var i = 0; i < faceGroups.length; i++)
{
for(var j = 0; j < faceGroups[i].length; j++)
{
this.geometry.faces[faceGroups[i][j]].materialIndex = i;
}
}
}
Object4D.prototype.project = function(defaultProjector, spaceMat, spaceRotationCenter)
{
if(this.projection === undefined)
throw "Pushed Object4D with undefined target 3D geometry";
if(this.projection.vertices.length == 0)
this.initializeProjection();
var objMat = this.buildMatrix5();
for(var vi = 0; vi < this.geometry.vertices4D.length; vi++)
{
var localVertex = this.geometry.vertices4D[vi].clone();
localVertex.sub(this.rotation.center).applyMatrix5(objMat).add(this.rotation.center);
localVertex.sub(spaceRotationCenter).applyMatrix5(spaceMat).add(spaceRotationCenter);
if(this.geometry.projector === null)
this.projection.vertices[vi].copy(defaultProjector.project(localVertex).add(this.position3D));
else
this.projection.vertices[vi].copy(this.geometry.projector.project(localVertex).add(this.position3D));
}
if(this.lineProjection !== undefined)
this.lineProjection.verticesNeedUpdate = true;
this.projection.verticesNeedUpdate = true;
this.projection.elementsNeedUpdate = true;
this.projection.computeFaceNormals();
this.projection.computeVertexNormals();
this.projection.computeFlatVertexNormals();
this.projection.computeBoundingBox();
this.projection.computeBoundingSphere();
}
Object4D.prototype.initializeProjection = function()
{
for(var vi = 0; vi < this.geometry.vertices4D.length; vi++)
this.projection.vertices.push(new THREE.Vector3(0, 0, 0));
this.projection.faces = this.geometry.faces;
}
Object4D.prototype.createLineProjection = function(){
if(this.geometry.edges === undefined)
throw "Can't create line projection of a geometry without egdes";
if(this.projection.vertices.length == 0)
this.initializeProjection();
this.lineProjection = new THREE.Geometry();
for(var i = 0; i < this.geometry.edges.length; i += 2)
{
this.lineProjection.vertices.push(this.projection.vertices[this.geometry.edges[i]]);
this.lineProjection.vertices.push(this.projection.vertices[this.geometry.edges[i+1]]);
}
}
Object4D.prototype.buildMatrix5 = function()
{
var mat = new Matrix5();
mat.scale(this.scale, this.scale, this.scale, this.scale);
// Handle rotations following the order described in this.rotation.order
for(var i = 0; i + 1 < this.rotation.order.length; i += 2)
{
var rotationPlane = this.rotation.order.slice(i, i + 2);
var theta = this.rotation[rotationPlane.toLowerCase()];
if(theta != 0)
mat.rotate(rotationPlane, theta);
}
return mat;
}
Object4D.prototype.add3DChild = function(object3D)
{
this.children3D.push(object3D);
object3D.parent4D = this;
object3D.frustumCulled = false;
return object3D;
}
Object4D.prototype.add3DMeshMaterial = function(material)
{
if(this.projection === undefined)
throw "Can't add Mesh material on an un-projected object";
var mesh3d = new THREE.Mesh(this.projection, material);
if(Array.isArray(material))
this.setFaceGroupsMaterial(this.geometry.faceGroups);
return this.add3DChild(mesh3d);
}
Object4D.prototype.add3DLineSegmentsMaterial = function(material)
{
if(this.projection === undefined)
throw "Can't add lineSegments material on an un-projected object";
if(this.lineProjection === undefined)
this.createLineProjection();
var lines = new THREE.LineSegments(this.lineProjection, material);
return this.add3DChild(lines);
}
Object4D.prototype.get3DBody = function ()
{
return this.children3D[0];
}
/**************
* Proj4D API *
**************/
// Mostly aesthetic ...
function Proj4D() { }
// Override this in your own projection
// Vector4 v
Proj4D.prototype.project = function(v) { }
//
// Linear algebra projection : (x, y, z, w) -> (x, y, z, 0)
//
function OrthoProj()
{
Proj4D.call(this);
}
OrthoProj.prototype = new Proj4D();
OrthoProj.prototype.project = function(v)
{
return new THREE.Vector3(v.x, v.y, v.z);
}
//
// Stereographical projection : uses a 3-sphere to project the 4D point onto the hyperplane w = 0
//
// THREE.Vector4 center, float radius, THREE.Vector4 planeNormal, THREE.Vector4 planePoint
function StereoProj(center, radius)
{
Proj4D.call(this);
this.sphereCenter = center;
this.sphereRadius = radius;
// The pole is the sphere's furthest point from the hyperplane w = 0
this.spherePole = (new THREE.Vector4(0, 0, 0, radius * ((center.w > 0) * 2 - 1))).add(center);
}
StereoProj.prototype = new Proj4D();
StereoProj.prototype.project = function(v)
{
// Projection of the point on the 3-sphere
var sphereProj = v.clone().sub(this.sphereCenter);
sphereProj.multiplyScalar(this.sphereRadius / sphereProj.length()).add(this.sphereCenter);
var u = sphereProj.clone().sub(this.spherePole).normalize();
if(u.w != 0)
{
var t = -this.spherePole.w / u.w;
u = u.multiplyScalar(t).add(this.spherePole);
}
return new THREE.Vector3(u.x, u.y, u.z);
}