-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
429 lines (324 loc) · 12.3 KB
/
index.html
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
<!DOCTYPE html>
<html lang="en"><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>3D function grapher</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
color: black;
font-family: Monospace;
font-size: 13px;
text-align: center;
background-color: #000;
margin: 0px;
overflow: hidden;
}
#info {
padding: 1em;
font-size:1em;
color: black;
}
a {
color: blue;
}
#webglmessage {
font-family: monospace;
font-size: 13px;
text-align: center;
background: rgb(0, 0, 50);
color: #fff;
padding: 1em;
width: 475px;
margin: 5em auto 0;
display: none;
}
#label{
width: 100%;
pointer-events: all;
z-index:999;
position:absolute;
bottom:0%;
background-color: white;
border-top: 2px solid #eee;
}
#inputbox{
font-size:2em;
color: black;
margin: auto;
padding: 0.25em;
text-align: middle;
}
#inputbox input{
width:calc(40% - 1em);
font-size:inherit;
margin:0.25em; padding:0.25em;background-color:inherit;border:none; border-bottom: 3px solid gray;
}
#functionTextRHS{
width:25%;
text-align: left;
}
#functionTextLHS{
text-align: right;
}
#presets{
font-family: Sans-Serif;
position:absolute;
max-width:20%;
min-width:10em;
top:0%;
left:0%;
bottom:20%;
z-index: 5555;
display: flex;
flex-direction: column;
color: black;
overflow:auto;
}
.preset{
font-size:1.25em;
color: black;
border: 1px solid #eee;
background-color: white;
margin: 0.25em;
padding: 0.2em;
border-radius: 8px;
}
.preset:hover{
background-color: #e0e0e0;
}
.preset:active{
background-color: #77f;
color: white;
}
#latexrenderingcontainer{
width: 100%;
pointer-events: none;
z-index:1000;
position:absolute;
bottom:0%;
text-align: center;
color: black;
font-size:2em;
background-color: white;
border: #eee;
}
#latexrendering{
margin:0.25em; padding:0.25em;
}
</style>
<script src="3DFunctionPlotter_files/lib/three.js"></script>
<script src="3DFunctionPlotter_files/lib/jquery.min.js"></script>
<script src="mathquill.js"></script>
<link type="text/css" href="mathquill.css" rel="stylesheet" />
<script src="3DFunctionPlotter_files/lib/OrbitControls.js"></script>
<script src="3DFunctionPlotter_files/lib/DetectWebGL.js"></script>
<script src="3DFunctionPlotter_files/lib/stats.js"></script>
<script src="3DFunctionPlotter_files/lib/dat.js"></script>
<script src="3DFunctionPlotter_files/MarchingCubes.js"></script>
<script src="3DFunctionPlotter_files/implicitFunctionTextBoxInput.js"></script>
<script src="3DFunctionPlotter_files/materialsandgui.js"></script>
<script>
if ( WEBGL.isWebGLAvailable() === false ) {
document.body.appendChild( WEBGL.getWebGLErrorMessage() );
}
var MARGIN = 0;
var SCREEN_WIDTH = window.innerWidth;
var SCREEN_HEIGHT = window.innerHeight - 2 * MARGIN;
var container;
var camera, scene, renderer;
var mesh, texture, geometry, materials, material, current_material;
var light, pointLight, ambientLight;
var effect, resolution, numBlobs;
var effectController;
var controls;
var time = 0;
var clock = new THREE.Clock();
var paused = false;
function init() {
container = document.getElementById( 'container' );
// CAMERA
camera = new THREE.PerspectiveCamera( 45, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 10000 );
camera.position.set( -500, 500, 1500 );
// SCENE
scene = new THREE.Scene();
// LIGHTS
light = new THREE.DirectionalLight( 0xffffff );
light.position.set( 0.5, 0.5, 1 );
scene.add( light );
light2 = new THREE.DirectionalLight( 0xffffff );
light2.position.set( -0.5, -0.5, -1 );
scene.add( light2 );
pointLight = new THREE.PointLight( 0xff3300 );
pointLight.position.set( 0, 0, 20 );
scene.add( pointLight );
pointLight2 = new THREE.PointLight( 0xff3300 );
pointLight2.position.set( 0, -20, -20 );
scene.add( pointLight2 );
pointLight3 = new THREE.PointLight( 0xff3300 );
pointLight3.position.set( 0, 20, -20 );
scene.add( pointLight3 );
ambientLight = new THREE.AmbientLight( 0x858585 );
scene.add( ambientLight );
// MATERIALS
materials = generateMaterials();
current_material = "textured";
// MARCHING CUBES
resolution = 28;
numBlobs = 10;
effect = new THREE.MarchingCubes( resolution, materials[ current_material ].m, true, true );
effect.position.set( 0, 0, 0 );
effect.scale.set( 700, 700, 700 );
effect.enableUvs = true;
effect.enableColors = false;
scene.add( effect );
// RENDERER
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
renderer.domElement.style.position = "absolute";
renderer.domElement.style.top = MARGIN + "px";
renderer.domElement.style.left = "0px";
container.appendChild( renderer.domElement );
renderer.gammaInput = true;
renderer.gammaOutput = true;
renderer.setClearColor( 0xffffff, 1 );
// CONTROLS
controls = new THREE.OrbitControls( camera, renderer.domElement );
controls.enableDamping = true; // an animation loop is required when either damping or auto-rotation are enabled
controls.dampingFactor = 0.80;
controls.autoRotate=true;
controls.enableKeys = false;
// GUI
setupGui();
// STATS
//stats = new Stats();
//container.appendChild( stats.dom );
// EVENTS
window.addEventListener( 'resize', onWindowResize, false );
}
//
function onWindowResize( event ) {
SCREEN_WIDTH = window.innerWidth;
SCREEN_HEIGHT = window.innerHeight - 2 * MARGIN;
camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
camera.updateProjectionMatrix();
renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
}
function animate() {
requestAnimationFrame( animate );
render();
//stats.update();
}
var last3Deltas = [0,0,0];
function render() {
var delta = clock.getDelta();
if(!paused)time += delta * effectController.speed * 0.5;
//if fps is choppy (below 30fps) decrease resolution
last3Deltas.shift();
last3Deltas.push(delta);
if(last3Deltas.reduce((accum, currentVal)=>accum + currentVal/10) > 1/30 && effectController.resolution > 30){
effectController.resolution-=5;
gui.__folders['Simulation'].__controllers[1].domElement.childNodes[0].children[0].value -= 5;
}
// marching cubes
if ( effectController.resolution !== resolution ) {
resolution = effectController.resolution;
effect.init( Math.floor( resolution ) );
}
//march the cubes
effect.reset(); //clear buffer
let func = (x,y,z, time) => -(Math.sin(x*20) + Math.sin(y*20) + Math.sin(z*20));
//func = (x,y,z, time) => ( 0.000001 - (x-0.5) * (x-0.5) - (y-0.5)*(y-0.5) - (z-0.5)*(z-0.5) ) + 0.125*(Math.sin(time*3) + 1.1);
//why does the first function not work but the second does? For that matter, I thought marching cubes rendered f()=0 - why are negative values also showing up?
effect.addFunction(providedFunc, time);
//
// materials
effect.material.color.setHSL( effectController.hue, effectController.saturation, effectController.lightness );
//effect.material.shininess = effectController.shininess;
// lights
light.position.set( effectController.lx, effectController.ly, effectController.lz );
light.position.normalize();
pointLight.color.setHSL( effectController.lhue, effectController.lsaturation, effectController.llightness );
controls.update(delta);
// render
//renderer.clearColor();
renderer.render( scene, camera );
}
function queueTextUpdate(){
window.setTimeout(updateText,1);
}
function setup(){
queueTextUpdate();
//provided functions
for(var name of ['sin','cos','atan','tan','sqrt','exp','abs']){
window[name] = Math[name];
}
init();
animate();
//hide fancy latex display when moused over
document.getElementById("inputbox").addEventListener("mouseover",function(){
document.getElementById("latexrenderingcontainer").style.display = "none";
});
document.getElementById("inputbox").addEventListener("mouseout",function(){
document.getElementById("latexrenderingcontainer").style.display = "";
});
updateFancyLatexDisplay();
setupPresetButtons();
}
window.onload = function(){
window.setTimeout(setup,1);
}
function setupPresetButtons(){
var elems = document.getElementsByClassName("preset");
for(var i=0;i<elems.length;i++){
elems[i].onclick = function(self){
let eqn = self.target.dataset["eqn"].split("=");
document.getElementById("functionTextLHS").value=eqn[0];
document.getElementById("functionTextRHS").value=eqn[1];
queueTextUpdate();
};
}
}
let MQ = MathQuill.getInterface(2);
function updateFancyLatexDisplay(){
var lhsText = document.getElementById("functionTextLHS").value;
var rhsText = document.getElementById("functionTextRHS").value;
var totalEquation = lhsText + "=" + rhsText;
totalEquation = totalEquation.replace(/[*]/g,""); //a^b -> a**b, to use JS's exponent
for(var name of ['sin','cos','atan','tan','sqrt','exp','abs']){
totalEquation = totalEquation.replace(new RegExp(name,'g'),"\\text{"+name+'}');
}
totalEquation = totalEquation.replace(/time/g,'t');
var elem = document.getElementById('latexrendering');
elem.innerHTML = totalEquation;
MQ.StaticMath(elem);
}
</script>
</head>
<body>
<div id="container"><canvas width="1360" height="827" style="width: 1360px; height: 827px; position: absolute; top: 0px; left: 0px; color: black;"></canvas></div>
</div>
<div id="label">
<div id="inputbox">
<div id="errormessage"></div>
<input type="text" class="mathinput" value="x+y+z+sin(x)" id="functionTextLHS" onkeyup="queueTextUpdate()"></input>=<input type="text" class="mathinput" id="functionTextRHS" value="0+sin(z*time)" onkeyup="queueTextUpdate()"></input>
</div>
</div>
<div id="latexrenderingcontainer"><span id="latexrendering"></span></div>
<div id='presets'>
<h1> Presets </h1>
<div class='preset' data-eqn="(abs(sin(time)))*(x^2+y^2+3*z*y) + (1-abs(sin(time)))*(x^2+y^2)=5">Cylinder homotopy</div>
<div class='preset' data-eqn="x^2+((z+y)^2)/2=5">Diagonal cylinder</div>
<div class='preset' data-eqn="x^2+y^2+z^2=5">Sphere</div>
<div class='preset' data-eqn="(x^2+y^2+z^2+5^2-1^2)^2 - 4*5^2*(x^2+y^2)=0">Torus</div>
<div class='preset' data-eqn="(x^2+y^2+z^2+5^2-1^2)^2 - 4*5^2*(x^2+y^2) + 100*((x-6)^2+5*(y-1)^2+z^2-5)/1.3^((x-5)^2+5*(y-1)^2+z^2)=0">Torus With A Bump</div>
<div class='preset' data-eqn="z=cos(x)+cos(y)">Egg Carton?</div>
<div class='preset'data-eqn="x*y^2 + y*x^2 + z*x^3=5">Two Disconnected Planes</div>
<div class='preset' data-eqn="x*(y^2+z^2) + y*(x^2+z^2) + z*(y^2+x^2)=5">Smooth Cubic Curve</div>
<div class='preset' data-eqn="x^2+y^2+z^2-x*y*z=5+5*sin(time)">Disconnected Tetrahedron (Kummer Surface)</div>
<div class='preset' data-eqn="(x^2+y^2)*(z^2+y^2)=5+4*sin(time)">Infinitely Long Ninja Star</div>
<div class='preset' data-eqn="(x^2+y^2+z^2-50)+(y^2)/(x^2+y^2+z^2-50)+(z^2)/(x^2+y^2+z^2-50)=0">Not A Manifold: Fresnel Wave Surface</div>
<div id="info">3D Function Grapher - Adapted from <a href="https://threejs.org/examples/webgl_marchingcubes.html" target="_blank" rel="noopener">threejs.org marching cubes</a>
</div>
</body></html>