forked from strin/curvy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make-graph.js
160 lines (139 loc) · 5.5 KB
/
make-graph.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
var graphMeshes, graphMesh, wireMaterial;
var xMin = 0.0, xMax = 1.0;
var yMin = 0.0, yMax = 1.0;
var zMin = 0.0, zMax = 1.0; // for autosizing window
function createGraph() {
xFunc = Parser.parse(xFuncText).toJSFunction(['t', 'i']);
yFunc = Parser.parse(yFuncText).toJSFunction(['t', 'i']);
zFunc = Parser.parse(zFuncText).toJSFunction(['t', 'i']);
yawFunc = Parser.parse(yawFuncText).toJSFunction(['t', 'i']);
var dronBezigers = makeBeziers();
updateGraph(dronBeziers, tMax);
}
function updateGraph(dronBeziers, tCur)
{
while (graphMeshes && graphMeshes.length > 0) {
graphMesh = graphMeshes.shift();
scene.remove(graphMesh);
}
xMin = 0.0, xMax = 1.0;
yMin = 0.0, yMax = 1.0;
zMin = 0.0, zMax = 1.0;
graphMeshes = [];
//return;
for (var id = 0; id < numDrones; id++) {
var beziers = dronBeziers[id];
var ib = beziers.length;
while(--ib >= 0) {
var bz = beziers[ib];
if (bz.mesh) {
if (bz.t0 > tCur) {
scene.remove(bz.mesh);
continue;
}
if (bz.t1 <= tCur) {
scene.add(bz.mesh);
graphMeshes.push(bz.mesh);
continue;
}
scene.remove(bz.mesh);
//bz.mesh = null;
}
Knot = THREE.Curve.create(
function () { },
function (t) {
// default: 0 < t < 1
// want: tMin < t < tMax
if(tCur >= bz.t1)
bz.iter0to1(t);
else
bz.iter(t * (tCur - bz.t0) + bz.t0);
return new THREE.Vector3(bz.x, bz.y, bz.z).multiplyScalar(1);
}
);
// knot stuff
myKnot = new Knot;
var closedTube = false; // connect the ends
var debug = false; // show normal vectors
//var segments = Math.ceil((tMax - tMin) * Math.pow(10, timeDivs));
var segments = 8;//timeDivs;
var tubeGeometry = new THREE.TubeGeometry(myKnot, segments, tubeRadius, radiusSegments, closedTube, debug);
///////////////////////////////////////////////
// calculate vertex colors based on T values //
///////////////////////////////////////////////
var color, point, face, numberOfSides, vertexIndex;
// faces are indexed using characters
var faceIndices = ['a', 'b', 'c', 'd'];
// first, assign colors to vertices as desired
for (var s = 0; s <= segments; s++) {
for (var r = 0; r < radiusSegments; r++) {
vertexIndex = r + s * radiusSegments;
color = new THREE.Color(0xffffff);
// according to length along curve, repeat once
color.setHSL((1 * s / segments) % 1, 1, 0.5);
// according to radius segment -- ribbons of color
// color.setHSV( (1 * r / radiusSegments) % 1, 1, 1 );
tubeGeometry.colors[vertexIndex] = color; // use this array for convenience
}
}
// copy the colors as necessary to the face's vertexColors array.
for (var i = 0; i < tubeGeometry.faces.length; i++) {
face = tubeGeometry.faces[i];
numberOfSides = (face instanceof THREE.Face3) ? 3 : 4;
for (var j = 0; j < numberOfSides; j++) {
vertexIndex = face[faceIndices[j]];
face.vertexColors[j] = tubeGeometry.colors[vertexIndex];
}
}
///////////////////////
// end vertex colors //
///////////////////////
// for auto-sizing window
tubeGeometry.computeBoundingBox();
xMin = Math.min(xMin, tubeGeometry.boundingBox.min.x);
xMax = Math.max(xMax, tubeGeometry.boundingBox.max.x);
yMin = Math.min(yMin, tubeGeometry.boundingBox.min.y);
yMax = Math.max(yMax, tubeGeometry.boundingBox.max.y);
zMin = Math.min(zMin, tubeGeometry.boundingBox.min.z);
zMax = Math.max(zMax, tubeGeometry.boundingBox.max.z);
wireMaterial.map.repeat.set(segments, radiusSegments);
graphMesh = new THREE.Mesh(tubeGeometry, wireMaterial);
graphMesh.doubleSided = true;
bz.mesh = tCur >= bz.t1 ? graphMesh : null;
scene.add(graphMesh);
graphMeshes.push(graphMesh);
}
}
}
var dest_tMax = tMax, dest_tMin = tMin;
var tInterval = 0.01; // s.
var tCurrent = tMax;
var playTimerId = null;
function playGraph() {
if (playTimerId) {
clearTimeout(playTimerId);
tMin = dest_tMin;
tMax = dest_tMax;
updateGraph(dronBeziers, tMax);
}
dest_tMax = tMax, dest_tMin = tMin;
tInterval = 0.01; // s.
tCurrent = tMin;
playTimerId = null;
//console.log(playSpeed);
function playGraphOne() {
updateGraph(dronBeziers, tCurrent);
tCurrent += tInterval * playSpeed;
//console.log("tCurrent", tCurrent, dest_tMax);
if (tCurrent <= dest_tMax) {
playTimerId = window.setTimeout(
playGraphOne,
tInterval * 1000.
);
}
}
playTimerId = window.setTimeout(
playGraphOne,
tInterval * 1000.
);
}