-
-
Notifications
You must be signed in to change notification settings - Fork 395
/
threejs_template.html
429 lines (322 loc) · 15.2 KB
/
threejs_template.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>
<head>
<title></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 { margin: 0px; overflow: hidden; }
#menu-container { position: absolute; bottom: 30px; right: 40px; }
#menu-content { position: absolute; bottom: 0px; right: 0px;
display: none; background-color: #F5F5F5; border-bottom: 1px solid black;
border-right: 1px solid black; border-left: 1px solid black; }
#menu-content div { border-top: 1px solid black; padding: 10px; white-space: nowrap; }
</style>
</head>
<body>
SAGE_SCRIPTS
<script>
var scene = new THREE.Scene();
var renderer = new THREE.WebGLRenderer( { antialias: true, preserveDrawingBuffer: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setClearColor( 0xffffff, 1 );
document.body.appendChild( renderer.domElement );
var options = SAGE_OPTIONS;
// When animations are supported by the viewer, the value 'false'
// will be replaced with an option set in Python by the user
var animate = false; // options.animate;
var b = SAGE_BOUNDS; // bounds
if ( b[0].x === b[1].x ) {
b[0].x -= 1;
b[1].x += 1;
}
if ( b[0].y === b[1].y ) {
b[0].y -= 1;
b[1].y += 1;
}
if ( b[0].z === b[1].z ) {
b[0].z -= 1;
b[1].z += 1;
}
var rRange = Math.sqrt( Math.pow( b[1].x - b[0].x, 2 )
+ Math.pow( b[1].y - b[0].y, 2 ) );
var xRange = b[1].x - b[0].x;
var yRange = b[1].y - b[0].y;
var zRange = b[1].z - b[0].z;
var ar = options.aspect_ratio;
var a = [ ar[0], ar[1], ar[2] ]; // aspect multipliers
var autoAspect = 2.5;
if ( zRange > autoAspect * rRange && a[2] === 1 ) a[2] = autoAspect * rRange / zRange;
// Distance from (xMid,yMid,zMid) to any corner of the bounding box, after applying aspect_ratio.
var midToCorner = Math.sqrt( a[0]*a[0]*xRange*xRange + a[1]*a[1]*yRange*yRange + a[2]*a[2]*zRange*zRange ) / 2;
var xMid = ( b[0].x + b[1].x ) / 2;
var yMid = ( b[0].y + b[1].y ) / 2;
var zMid = ( b[0].z + b[1].z ) / 2;
var box = new THREE.Geometry();
box.vertices.push( new THREE.Vector3( a[0]*b[0].x, a[1]*b[0].y, a[2]*b[0].z ) );
box.vertices.push( new THREE.Vector3( a[0]*b[1].x, a[1]*b[1].y, a[2]*b[1].z ) );
var boxMesh = new THREE.Line( box );
if ( options.frame ) scene.add( new THREE.BoxHelper( boxMesh, 'black' ) );
if ( options.axes_labels ) {
var d = options.decimals; // decimals
var offsetRatio = 0.1;
var al = options.axes_labels;
var offset = offsetRatio * a[1]*( b[1].y - b[0].y );
var xm = xMid.toFixed(d);
if ( /^-0.?0*$/.test(xm) ) xm = xm.substr(1);
addLabel( al[0] + '=' + xm, a[0]*xMid, a[1]*b[1].y+offset, a[2]*b[0].z );
addLabel( ( b[0].x ).toFixed(d), a[0]*b[0].x, a[1]*b[1].y+offset, a[2]*b[0].z );
addLabel( ( b[1].x ).toFixed(d), a[0]*b[1].x, a[1]*b[1].y+offset, a[2]*b[0].z );
var offset = offsetRatio * a[0]*( b[1].x - b[0].x );
var ym = yMid.toFixed(d);
if ( /^-0.?0*$/.test(ym) ) ym = ym.substr(1);
addLabel( al[1] + '=' + ym, a[0]*b[1].x+offset, a[1]*yMid, a[2]*b[0].z );
addLabel( ( b[0].y ).toFixed(d), a[0]*b[1].x+offset, a[1]*b[0].y, a[2]*b[0].z );
addLabel( ( b[1].y ).toFixed(d), a[0]*b[1].x+offset, a[1]*b[1].y, a[2]*b[0].z );
var offset = offsetRatio * a[1]*( b[1].y - b[0].y );
var zm = zMid.toFixed(d);
if ( /^-0.?0*$/.test(zm) ) zm = zm.substr(1);
addLabel( al[2] + '=' + zm, a[0]*b[1].x, a[1]*b[0].y-offset, a[2]*zMid );
addLabel( ( b[0].z ).toFixed(d), a[0]*b[1].x, a[1]*b[0].y-offset, a[2]*b[0].z );
addLabel( ( b[1].z ).toFixed(d), a[0]*b[1].x, a[1]*b[0].y-offset, a[2]*b[1].z );
}
function addLabel( text, x, y, z ) {
var fontsize = 14;
var canvas = document.createElement( 'canvas' );
var pixelRatio = Math.round( window.devicePixelRatio );
canvas.width = 128 * pixelRatio;
canvas.height = 32 * pixelRatio; // powers of two
canvas.style.width = '128px';
canvas.style.height = '32px';
var context = canvas.getContext( '2d' );
context.scale( pixelRatio, pixelRatio );
context.fillStyle = 'black';
context.font = fontsize + 'px monospace';
context.textAlign = 'center';
context.textBaseline = 'middle';
context.fillText( text, canvas.width/2/pixelRatio, canvas.height/2/pixelRatio );
var texture = new THREE.Texture( canvas );
texture.needsUpdate = true;
var sprite = new THREE.Sprite( new THREE.SpriteMaterial( { map: texture } ) );
sprite.position.set( x, y, z );
// Set the initial scale based on plot size to accomodate orthographic projection.
// For other projections, the scale will get reset each frame based on camera distance.
var scale = midToCorner/2;
sprite.scale.set( scale, scale*.25 ); // ratio of canvas width to height
scene.add( sprite );
}
if ( options.axes ) scene.add( new THREE.AxesHelper( Math.min( a[0]*b[1].x, a[1]*b[1].y, a[2]*b[1].z ) ) );
var camera = createCamera();
camera.up.set( 0, 0, 1 );
camera.position.set( a[0]*(xMid+xRange), a[1]*(yMid+yRange), a[2]*(zMid+zRange) );
function createCamera() {
var aspect = window.innerWidth / window.innerHeight;
if ( options.projection === 'orthographic' ) {
var camera = new THREE.OrthographicCamera( -1, 1, 1, -1, -1000, 1000 );
updateCameraAspect( camera, aspect );
return camera;
}
return new THREE.PerspectiveCamera( 45, aspect, 0.1, 1000 );
}
function updateCameraAspect( camera, aspect ) {
if ( camera.isPerspectiveCamera ) {
camera.aspect = aspect;
} else if ( camera.isOrthographicCamera ) {
// Fit the camera frustum to the bounding box's diagonal so that the entire plot fits
// within at the default zoom level and camera position.
if ( aspect > 1 ) { // Wide window
camera.top = midToCorner;
camera.right = midToCorner * aspect;
} else { // Tall or square window
camera.top = midToCorner / aspect;
camera.right = midToCorner;
}
camera.bottom = -camera.top;
camera.left = -camera.right;
}
camera.updateProjectionMatrix();
}
var lights = SAGE_LIGHTS;
for ( var i=0 ; i < lights.length ; i++ ) {
var light = new THREE.DirectionalLight( lights[i].color, 1 );
light.position.set( a[0]*lights[i].x, a[1]*lights[i].y, a[2]*lights[i].z );
if ( lights[i].parent === 'camera' ) {
light.target.position.set( a[0]*xMid, a[1]*yMid, a[2]*zMid );
scene.add( light.target );
camera.add( light );
} else scene.add( light );
}
scene.add( camera );
var ambient = SAGE_AMBIENT;
scene.add( new THREE.AmbientLight( ambient.color, 1 ) );
var controls = new THREE.OrbitControls( camera, renderer.domElement );
controls.target.set( a[0]*xMid, a[1]*yMid, a[2]*zMid );
controls.addEventListener( 'change', function() { if ( !animate ) render(); } );
window.addEventListener( 'resize', function() {
renderer.setSize( window.innerWidth, window.innerHeight );
updateCameraAspect( camera, window.innerWidth / window.innerHeight );
if ( !animate ) render();
} );
var texts = SAGE_TEXTS;
for ( var i=0 ; i < texts.length ; i++ )
addLabel( texts[i].text, a[0]*texts[i].x, a[1]*texts[i].y, a[2]*texts[i].z );
var points = SAGE_POINTS;
for ( var i=0 ; i < points.length ; i++ ) addPoint( points[i] );
function addPoint( json ) {
var geometry = new THREE.Geometry();
var v = json.point;
geometry.vertices.push( new THREE.Vector3( a[0]*v[0], a[1]*v[1], a[2]*v[2] ) );
var canvas = document.createElement( 'canvas' );
canvas.width = 128;
canvas.height = 128;
var context = canvas.getContext( '2d' );
context.arc( 64, 64, 64, 0, 2 * Math.PI );
context.fillStyle = json.color;
context.fill();
var texture = new THREE.Texture( canvas );
texture.needsUpdate = true;
var transparent = json.opacity < 1 ? true : false;
var size = camera.isOrthographicCamera ? json.size : json.size/100;
var material = new THREE.PointsMaterial( { size: size, map: texture,
transparent: transparent, opacity: json.opacity,
alphaTest: .1 } );
var c = new THREE.Vector3();
geometry.computeBoundingBox();
geometry.boundingBox.getCenter( c );
geometry.translate( -c.x, -c.y, -c.z );
var mesh = new THREE.Points( geometry, material );
mesh.position.set( c.x, c.y, c.z );
scene.add( mesh );
}
var lines = SAGE_LINES;
for ( var i=0 ; i < lines.length ; i++ ) addLine( lines[i] );
function addLine( json ) {
var geometry = new THREE.Geometry();
for ( var i=0 ; i < json.points.length ; i++ ) {
var v = json.points[i];
geometry.vertices.push( new THREE.Vector3( a[0]*v[0], a[1]*v[1], a[2]*v[2] ) );
}
var transparent = json.opacity < 1 ? true : false;
var material = new THREE.LineBasicMaterial( { color: json.color, linewidth: json.linewidth,
transparent: transparent, opacity: json.opacity } );
var c = new THREE.Vector3();
geometry.computeBoundingBox();
geometry.boundingBox.getCenter( c );
geometry.translate( -c.x, -c.y, -c.z );
var mesh = new THREE.Line( geometry, material );
mesh.position.set( c.x, c.y, c.z );
scene.add( mesh );
}
var surfaces = SAGE_SURFACES;
for ( var i=0 ; i < surfaces.length ; i++ ) addSurface( surfaces[i] );
function addSurface( json ) {
var useFaceColors = 'faceColors' in json ? true : false;
var geometry = new THREE.Geometry();
for ( var i=0 ; i < json.vertices.length ; i++ ) {
var v = json.vertices[i];
geometry.vertices.push( new THREE.Vector3( a[0]*v.x, a[1]*v.y, a[2]*v.z ) );
}
for ( var i=0 ; i < json.faces.length ; i++ ) {
var f = json.faces[i];
for ( var j=0 ; j < f.length - 2 ; j++ ) {
var face = new THREE.Face3( f[0], f[j+1], f[j+2] );
if ( useFaceColors ) face.color.set( json.faceColors[i] );
geometry.faces.push( face );
}
}
geometry.computeVertexNormals();
var side = json.singleSide ? THREE.FrontSide : THREE.DoubleSide;
var transparent = json.opacity < 1 ? true : false;
var material = new THREE.MeshPhongMaterial( { side: side,
color: useFaceColors ? 'white' : json.color,
vertexColors: useFaceColors ? THREE.FaceColors : THREE.NoColors,
transparent: transparent, opacity: json.opacity,
shininess: 20, flatShading: json.useFlatShading } );
var c = new THREE.Vector3();
geometry.computeBoundingBox();
geometry.boundingBox.getCenter( c );
geometry.translate( -c.x, -c.y, -c.z );
var mesh = new THREE.Mesh( geometry, material );
mesh.position.set( c.x, c.y, c.z );
if ( transparent && json.renderOrder ) mesh.renderOrder = json.renderOrder
scene.add( mesh );
if ( json.showMeshGrid ) {
var geometry = new THREE.Geometry();
for ( var i=0 ; i < json.faces.length ; i++ ) {
var f = json.faces[i];
for ( var j=0 ; j < f.length ; j++ ) {
var k = j === f.length-1 ? 0 : j+1;
var v1 = json.vertices[f[j]];
var v2 = json.vertices[f[k]];
// vertices in opposite directions on neighboring faces
var nudge = f[j] < f[k] ? .0005*zRange : -.0005*zRange;
geometry.vertices.push( new THREE.Vector3( a[0]*v1.x, a[1]*v1.y, a[2]*(v1.z+nudge) ) );
geometry.vertices.push( new THREE.Vector3( a[0]*v2.x, a[1]*v2.y, a[2]*(v2.z+nudge) ) );
}
}
var material = new THREE.LineBasicMaterial( { color: 'black', linewidth: 1 } );
var c = new THREE.Vector3();
geometry.computeBoundingBox();
geometry.boundingBox.getCenter( c );
geometry.translate( -c.x, -c.y, -c.z );
var mesh = new THREE.LineSegments( geometry, material );
mesh.position.set( c.x, c.y, c.z );
scene.add( mesh );
}
}
var scratch = new THREE.Vector3();
function render() {
if ( animate ) requestAnimationFrame( render );
renderer.render( scene, camera );
// Resize text based on distance from camera.
// Not neccessary for orthographic due to the nature of the projection (preserves sizes).
if ( !camera.isOrthographicCamera ) {
for ( var i=0 ; i < scene.children.length ; i++ ) {
if ( scene.children[i].type === 'Sprite' ) {
var sprite = scene.children[i];
var adjust = scratch.addVectors( sprite.position, scene.position )
.sub( camera.position ).length() / 5;
sprite.scale.set( adjust, .25*adjust ); // ratio of canvas width to height
}
}
}
}
render();
controls.update();
if ( !animate ) render();
// menu functions
function toggleMenu() {
var m = document.getElementById( 'menu-content' );
if ( m.style.display === 'block' ) m.style.display = 'none'
else m.style.display = 'block';
}
function saveAsPNG() {
var a = document.body.appendChild( document.createElement( 'a' ) );
a.href = renderer.domElement.toDataURL( 'image/png' );
a.download = 'screenshot';
a.click();
}
function saveAsHTML() {
toggleMenu(); // otherwise visible in output
event.stopPropagation();
var blob = new Blob( [ '<!DOCTYPE html>\n' + document.documentElement.outerHTML ] );
var a = document.body.appendChild( document.createElement( 'a' ) );
a.href = window.URL.createObjectURL( blob );
a.download = 'graphic.html';
a.click();
}
function getViewpoint() {
var info = '<pre>' + JSON.stringify( camera, null, '\t' ) + '</pre>';
window.open().document.write( info );
}
</script>
<div id="menu-container" onclick="toggleMenu()">ⓘ
<div id="menu-content">
<div onclick="saveAsPNG()">Save as PNG</div>
<div onclick="saveAsHTML()">Save as HTML</div>
<div onclick="getViewpoint()">Camera Info</div>
<div>Close Menu</div>
</div></div>
</body>
</html>