-
Notifications
You must be signed in to change notification settings - Fork 707
Expand file tree
/
Copy pathGraphulus-Curve.html
More file actions
334 lines (286 loc) · 10.1 KB
/
Graphulus-Curve.html
File metadata and controls
334 lines (286 loc) · 10.1 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<!doctype html>
<html lang="en">
<head>
<title>Graphulus: Parametric Curves (Three.js)</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link rel=stylesheet href="css/base.css"/>
</head>
<body>
<script src="js/Three.js"></script>
<script src="js/Detector.js"></script>
<script src="js/Stats.js"></script>
<script src="js/TrackballControls.js"></script>
<script src="js/THREEx.KeyboardState.js"></script>
<script src="js/THREEx.FullScreen.js"></script>
<script src="js/THREEx.WindowResize.js"></script>
<script type='text/javascript' src='js/DAT.GUI.min.js'></script>
<!-- http://silentmatt.com/javascript-expression-evaluator/ -->
<script src="js/parser.js"></script>
<!-- jQuery code to display an information button and box when clicked. -->
<script src="js/jquery-1.9.1.js"></script>
<script src="js/jquery-ui.js"></script>
<link rel=stylesheet href="css/jquery-ui.css" />
<link rel=stylesheet href="css/info.css"/>
<script src="js/info.js"></script>
<div id="infoButton"></div>
<div id="infoBox" title="Demo Information">
This three.js demo is part of a collection at
<a href="http://stemkoski.github.io/Three.js/">http://stemkoski.github.io/Three.js/</a>
</div>
<!-- ------------------------------------------------------------ -->
<div id="ThreeJS" style="position: absolute; left:0px; top:0px"></div>
<script>
/*
Three.js "tutorials by example"
Author: Lee Stemkoski
Date: July 2013 (three.js v59dev)
*/
// MAIN
// standard global variables
var container, scene, camera, renderer, controls, stats;
var keyboard = new THREEx.KeyboardState();
var clock = new THREE.Clock();
// custom global variables
var gui, gui_xText, gui_yText, gui_zText,
gui_tMin, gui_tMax,
gui_a, gui_b, gui_c, gui_d,
gui_segments, gui_radiusSegments, gui_tubeRadius;
var autoUpdate = true;
var myKnot;
var tubeMeshWire, tubeMeshSolid;
// extrusion segments -- how many sample points to take along curve.
var segments = 120;
// how many sides the tube cross-section has
var radiusSegments = 6;
var tubeRadius = 0.1;
var tMin = -2.11, tMax = 2.11;
var xFuncText = "t^3 - 3*t";
var yFuncText = "t^4 - 4*t^2";
var zFuncText = "(t^5 - 10*t)/5";
var xFunc = Parser.parse(xFuncText).toJSFunction( ['t'] );
var yFunc = Parser.parse(yFuncText).toJSFunction( ['t'] );
var zFunc = Parser.parse(zFuncText).toJSFunction( ['t'] );
// parameters for the equations
var a = 0.01, b = 0.01, c = 0.01, d = 0.01;
var xMin = xMax = yMin = yMax = zMin = zMax = 0; // for autosizing window
var graphMesh, wireMaterial;
Knot = THREE.Curve.create(
function() {},
function(t)
{
// default: 0 < t < 1
// want: tMin < t < tMax
tRange = tMax - tMin;
t = t * tRange + tMin;
return new THREE.Vector3(xFunc(t), yFunc(t), zFunc(t)).multiplyScalar(1);
}
);
init();
animate();
// FUNCTIONS
function init()
{
// SCENE
scene = new THREE.Scene();
// CAMERA
var SCREEN_WIDTH = window.innerWidth, SCREEN_HEIGHT = window.innerHeight;
var VIEW_ANGLE = 45, ASPECT = SCREEN_WIDTH / SCREEN_HEIGHT, NEAR = 0.1, FAR = 20000;
camera = new THREE.PerspectiveCamera( VIEW_ANGLE, ASPECT, NEAR, FAR);
scene.add(camera);
// RENDERER
if ( Detector.webgl )
renderer = new THREE.WebGLRenderer( {antialias:true} );
else
renderer = new THREE.CanvasRenderer();
renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
container = document.getElementById( 'ThreeJS' );
container.appendChild( renderer.domElement );
// EVENTS
THREEx.WindowResize(renderer, camera);
THREEx.FullScreen.bindKey({ charCode : 'm'.charCodeAt(0) });
// CONTROLS
controls = new THREE.TrackballControls( camera, renderer.domElement );
// STATS
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.bottom = '0px';
stats.domElement.style.zIndex = 100;
container.appendChild( stats.domElement );
// LIGHT
var light = new THREE.PointLight(0xffffff);
light.position.set(0,250,0);
scene.add(light);
// SKYBOX/FOG
scene.fog = new THREE.FogExp2( 0x888888, 0.0025 );
////////////
// CUSTOM //
////////////
scene.add( new THREE.AxisHelper() );
// wireframe for xy-plane
var wireframeMaterial = new THREE.MeshBasicMaterial( { color: 0x000088, wireframe: true, side:THREE.DoubleSide } );
var floorGeometry = new THREE.PlaneGeometry(100,100,10,10);
var floor = new THREE.Mesh(floorGeometry, wireframeMaterial);
floor.position.z = -0.01;
// rotate to lie in x-y plane
// floor.rotation.x = Math.PI / 2;
scene.add(floor);
// "wireframe texture" (from image)
var wireTexture = new THREE.ImageUtils.loadTexture( 'images/square.png' );
wireTexture.wrapS = wireTexture.wrapT = THREE.RepeatWrapping;
wireTexture.repeat.set( 40, 40 );
wireMaterial = new THREE.MeshBasicMaterial( { map: wireTexture, vertexColors: THREE.VertexColors, side:THREE.DoubleSide } );
// Background clear color
// renderer.setClearColorHex( 0x888888, 1 );
///////////////////
// GUI SETUP //
///////////////////
gui = new dat.GUI();
parameters =
{
resetCam: function() { resetCamera(); },
preset1: function() { preset01(); },
graphFunc: function() { createGraph(); },
finalValue: 337
};
// GUI -- equation
gui_xText = gui.add( this, 'xFuncText' ).name('x = f(t) = ');
gui_yText = gui.add( this, 'yFuncText' ).name('y = g(t) = ');
gui_zText = gui.add( this, 'zFuncText' ).name('z = h(t) = ');
gui_tMin = gui.add( this, 'tMin' ).name('t Minimum = ');
gui_tMax = gui.add( this, 'tMax' ).name('t Maximum = ');
gui_segments = gui.add( this, 'segments' ).name('t Segments = ').min(3).max(400).step(1);
gui_radiusSegments = gui.add( this, 'radiusSegments' ).name('r Segments = ').min(3).max(10).step(1);
gui_tubeRadius = gui.add( this, 'tubeRadius' ).name('Tube Radius = ').min(0).max(1);
// GUI -- parameters
var gui_parameters = gui.addFolder('Parameters');
a = b = c = d = 0.01;
autoUpdate = false;
gui_a = gui_parameters.add( this, 'a' ).min(-5).max(5).step(0.01).name('a = ');
gui_a.onChange( function(value) { if (autoUpdate) createGraph(); } );
gui_b = gui_parameters.add( this, 'b' ).min(-5).max(5).step(0.01).name('b = ');
gui_b.onChange( function(value) { if (autoUpdate) createGraph(); } );
gui_c = gui_parameters.add( this, 'c' ).min(-5).max(5).step(0.01).name('c = ');
gui_c.onChange( function(value) { if (autoUpdate) createGraph(); } );
gui_d = gui_parameters.add( this, 'd' ).min(-5).max(5).step(0.01).name('d = ');
gui_d.onChange( function(value) { if (autoUpdate) createGraph(); } );
gui_a.setValue(1);
gui_b.setValue(1);
gui_c.setValue(1);
gui_d.setValue(1);
autoUpdate = true;
// GUI -- preset equations
var gui_preset = gui.addFolder('Preset equations');
gui_preset.add( parameters, 'preset1' ).name('Trefoil');
gui.add( parameters, 'resetCam' ).name("Reset Camera");
gui.add( parameters, 'graphFunc' ).name("Graph Function");
preset01();
}
function createGraph()
{
xFunc = Parser.parse(xFuncText).toJSFunction( ['t'] );
yFunc = Parser.parse(yFuncText).toJSFunction( ['t'] );
zFunc = Parser.parse(zFuncText).toJSFunction( ['t'] );
// knot stuff
myKnot = new Knot;
var closedTube = false; // connect the ends
var debug = false; // show normal vectors
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 = tubeGeometry.boundingBox.min.x;
xMax = tubeGeometry.boundingBox.max.x;
yMin = tubeGeometry.boundingBox.min.y;
yMax = tubeGeometry.boundingBox.max.y;
zMin = tubeGeometry.boundingBox.min.z;
zMax = tubeGeometry.boundingBox.max.z;
if (graphMesh)
{
scene.remove( graphMesh );
// renderer.deallocateObject( graphMesh );
}
wireMaterial.map.repeat.set( segments, radiusSegments );
graphMesh = new THREE.Mesh( tubeGeometry, wireMaterial );
graphMesh.doubleSided = true;
scene.add(graphMesh);
}
function preset01()
{
autoUpdate = false;
gui_xText.setValue("t^3 + a*t");
gui_yText.setValue("t^4 + b*t^2");
gui_zText.setValue("(1/10)*(t^5 + (-10)*t)");
gui_tMin.setValue(-2.1); gui_tMax.setValue(2.1);
gui_a.setValue(-3);
gui_b.setValue(-4);
gui_segments.setValue(120);
createGraph(); resetCamera();
autoUpdate = true;
}
function resetCamera()
{
// CAMERA
var SCREEN_WIDTH = window.innerWidth, SCREEN_HEIGHT = window.innerHeight;
var VIEW_ANGLE = 45, ASPECT = SCREEN_WIDTH / SCREEN_HEIGHT, NEAR = 0.1, FAR = 5000;
camera = new THREE.PerspectiveCamera( VIEW_ANGLE, ASPECT, NEAR, FAR);
camera.position.set( 2*xMax, 0.5*yMax, 4*zMax );
camera.up = new THREE.Vector3( 0, 0, 1 );
camera.lookAt(scene.position);
scene.add(camera);
controls = new THREE.TrackballControls( camera, renderer.domElement );
THREEx.WindowResize(renderer, camera);
}
function animate()
{
requestAnimationFrame( animate );
render();
update();
}
function update()
{
if ( keyboard.pressed("z") )
{
// do something
}
controls.update();
stats.update();
}
function render()
{
renderer.render( scene, camera );
}
</script>
</body>
</html>