2023-07-22 01:11:18 +00:00
|
|
|
import * as THREE from 'three';
|
|
|
|
|
2023-07-24 23:05:06 +00:00
|
|
|
import * as POLYTOPES from './polytopes.js';
|
2023-07-23 01:07:24 +00:00
|
|
|
|
2023-07-24 23:05:06 +00:00
|
|
|
import { FourDShape } from './fourDShape.js';
|
2023-07-24 02:34:48 +00:00
|
|
|
|
2023-07-25 05:43:26 +00:00
|
|
|
import { GUI } from 'lil-gui';
|
2023-07-22 03:24:42 +00:00
|
|
|
|
2023-07-22 06:59:48 +00:00
|
|
|
|
2023-07-28 07:40:39 +00:00
|
|
|
const DEFAULT_SHAPE = '120-cell';
|
2023-07-29 01:52:43 +00:00
|
|
|
const DEFAULT_COLOR = 0x90ebff;
|
|
|
|
const DEFAULT_BG = 0xdddddd;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-07-23 08:16:02 +00:00
|
|
|
|
2023-07-23 05:57:34 +00:00
|
|
|
// hacky stuff for 4d rotations
|
2023-07-23 01:07:24 +00:00
|
|
|
|
2023-07-23 05:57:34 +00:00
|
|
|
// see https://math.stackexchange.com/questions/1402362/can-rotations-in-4d-be-given-an-explicit-matrix-form#1402376
|
2023-07-23 01:07:24 +00:00
|
|
|
|
|
|
|
|
2023-07-23 05:57:34 +00:00
|
|
|
|
|
|
|
function rotZW(theta) {
|
|
|
|
const ctheta = Math.cos(theta);
|
|
|
|
const stheta = Math.sin(theta);
|
|
|
|
return new THREE.Matrix4(
|
|
|
|
ctheta, -stheta, 0, 0,
|
|
|
|
stheta, ctheta, 0, 0,
|
|
|
|
0, 0, 1, 0,
|
|
|
|
0, 0, 0, 1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function rotYW(theta) {
|
|
|
|
const ctheta = Math.cos(theta);
|
|
|
|
const stheta = Math.sin(theta);
|
|
|
|
return new THREE.Matrix4(
|
|
|
|
ctheta, 0, -stheta, 0,
|
|
|
|
0, 1, 0, 0,
|
|
|
|
stheta, 0, ctheta, 0,
|
|
|
|
0, 0, 0, 1,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function rotYZ(theta) {
|
|
|
|
const ctheta = Math.cos(theta);
|
|
|
|
const stheta = Math.sin(theta);
|
|
|
|
return new THREE.Matrix4(
|
|
|
|
ctheta, 0, 0, -stheta,
|
|
|
|
0, 1, 0, 0,
|
|
|
|
0, 0, 1, 0,
|
|
|
|
stheta, 0, 0, ctheta,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function rotXW(theta) {
|
|
|
|
const ctheta = Math.cos(theta);
|
|
|
|
const stheta = Math.sin(theta);
|
|
|
|
return new THREE.Matrix4(
|
|
|
|
1, 0, 0, 0,
|
|
|
|
0, ctheta, -stheta, 0,
|
|
|
|
0, stheta, ctheta, 0,
|
|
|
|
0, 0, 0, 1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function rotXZ(theta) {
|
|
|
|
const ctheta = Math.cos(theta);
|
|
|
|
const stheta = Math.sin(theta);
|
|
|
|
return new THREE.Matrix4(
|
|
|
|
1, 0, 0, 0,
|
|
|
|
0, ctheta, 0, -stheta,
|
|
|
|
0, 0, 1, 0,
|
|
|
|
0, stheta, 0, ctheta,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function rotXY(theta) {
|
|
|
|
const ctheta = Math.cos(theta);
|
|
|
|
const stheta = Math.sin(theta);
|
|
|
|
return new THREE.Matrix4(
|
|
|
|
1, 0, 0, 0,
|
|
|
|
0, 1, 0, 0,
|
|
|
|
0, 0, ctheta, -stheta,
|
|
|
|
0, 0, stheta, ctheta,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-07-22 06:59:48 +00:00
|
|
|
|
2023-07-28 07:39:57 +00:00
|
|
|
function fallbackCopyTextToClipboard(text) {
|
|
|
|
var textArea = document.createElement("textarea");
|
|
|
|
textArea.value = text;
|
|
|
|
|
|
|
|
// Avoid scrolling to bottom
|
|
|
|
textArea.style.top = "0";
|
|
|
|
textArea.style.left = "0";
|
|
|
|
textArea.style.position = "fixed";
|
|
|
|
|
|
|
|
document.body.appendChild(textArea);
|
|
|
|
textArea.focus();
|
|
|
|
textArea.select();
|
|
|
|
|
|
|
|
try {
|
|
|
|
var successful = document.execCommand('copy');
|
|
|
|
var msg = successful ? 'successful' : 'unsuccessful';
|
|
|
|
console.log('Fallback: Copying text command was ' + msg);
|
|
|
|
} catch (err) {
|
|
|
|
console.error('Fallback: Oops, unable to copy', err);
|
|
|
|
}
|
|
|
|
|
|
|
|
document.body.removeChild(textArea);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function copyTextToClipboard(text) {
|
|
|
|
if (!navigator.clipboard) {
|
|
|
|
fallbackCopyTextToClipboard(text);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
navigator.clipboard.writeText(text).then(function() {
|
|
|
|
console.log('Async: Copying to clipboard was successful!');
|
|
|
|
}, function(err) {
|
|
|
|
console.error('Async: Could not copy text: ', err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-22 06:59:48 +00:00
|
|
|
|
2023-07-22 03:24:42 +00:00
|
|
|
|
|
|
|
|
2023-07-22 06:59:48 +00:00
|
|
|
|
2023-07-22 01:11:18 +00:00
|
|
|
const scene = new THREE.Scene();
|
|
|
|
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
|
|
|
|
|
2023-07-22 05:14:35 +00:00
|
|
|
const light = new THREE.PointLight(0xffffff, 2);
|
|
|
|
light.position.set(10, 10, 10);
|
|
|
|
scene.add(light);
|
|
|
|
|
2023-07-27 22:40:27 +00:00
|
|
|
const light2 = new THREE.PointLight(0xffffff, 2);
|
|
|
|
light2.position.set(-10, 5, 10);
|
|
|
|
scene.add(light);
|
|
|
|
|
2023-07-22 05:14:35 +00:00
|
|
|
|
2023-07-24 02:34:48 +00:00
|
|
|
const amblight = new THREE.AmbientLight(0xffffff, 0.5);
|
2023-07-22 05:14:35 +00:00
|
|
|
scene.add(amblight);
|
|
|
|
|
2023-07-29 01:52:43 +00:00
|
|
|
scene.background = new THREE.Color(DEFAULT_BG);
|
2023-07-22 05:14:35 +00:00
|
|
|
|
2023-07-24 23:05:06 +00:00
|
|
|
|
2023-07-23 08:16:02 +00:00
|
|
|
const renderer = new THREE.WebGLRenderer({antialias: true});
|
2023-07-22 01:11:18 +00:00
|
|
|
renderer.setSize( window.innerWidth, window.innerHeight );
|
|
|
|
document.body.appendChild( renderer.domElement );
|
|
|
|
|
2023-07-24 23:55:33 +00:00
|
|
|
|
2023-07-29 01:52:43 +00:00
|
|
|
const material = new THREE.MeshStandardMaterial(
|
|
|
|
{ color: DEFAULT_COLOR }
|
|
|
|
);
|
2023-07-22 03:40:01 +00:00
|
|
|
|
2023-07-29 01:52:43 +00:00
|
|
|
const node_ms = [ material ];
|
2023-07-24 02:00:34 +00:00
|
|
|
|
2023-07-29 01:52:43 +00:00
|
|
|
const link_ms = [ material ];
|
2023-07-22 06:59:48 +00:00
|
|
|
|
2023-07-25 05:43:26 +00:00
|
|
|
const STRUCTURES = {
|
|
|
|
'5-cell': POLYTOPES.cell5(),
|
|
|
|
'16-cell': POLYTOPES.cell16(),
|
|
|
|
'tesseract': POLYTOPES.tesseract(),
|
|
|
|
'24-cell': POLYTOPES.cell24(),
|
2023-07-27 21:18:27 +00:00
|
|
|
'120-cell': POLYTOPES.cell120(),
|
|
|
|
'600-cell': POLYTOPES.cell600()
|
2023-07-25 05:43:26 +00:00
|
|
|
};
|
|
|
|
|
2023-07-25 06:50:37 +00:00
|
|
|
|
2023-07-25 05:43:26 +00:00
|
|
|
let shape = false;
|
|
|
|
|
|
|
|
function createShape(name) {
|
|
|
|
if( shape ) {
|
|
|
|
scene.remove(shape);
|
|
|
|
}
|
|
|
|
shape = new FourDShape(node_ms, link_ms, STRUCTURES[name]);
|
|
|
|
scene.add(shape);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-07-24 23:55:33 +00:00
|
|
|
|
2023-07-29 00:40:15 +00:00
|
|
|
function floatParam(params, param) {
|
|
|
|
const value = params.get(param);
|
2023-07-28 07:39:57 +00:00
|
|
|
if( value ) {
|
|
|
|
const fl = parseFloat(value);
|
|
|
|
if( fl !== NaN ) {
|
|
|
|
return fl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2023-07-22 03:24:42 +00:00
|
|
|
|
2023-07-29 01:52:43 +00:00
|
|
|
function stringtoHex(cstr) {
|
|
|
|
return parseInt('0x' + cstr.substr(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
function hexToString(hex) {
|
|
|
|
return '#' + hex.toString(16);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function colorParam(params, param, dft) {
|
|
|
|
const value = params.get(param);
|
|
|
|
if( value ) {
|
|
|
|
const hex = stringtoHex(value);
|
|
|
|
if( hex !== NaN ) {
|
|
|
|
return hex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dft;
|
|
|
|
}
|
|
|
|
|
2023-07-22 03:24:42 +00:00
|
|
|
|
2023-07-24 03:57:51 +00:00
|
|
|
camera.position.z = 4;
|
2023-07-24 02:34:48 +00:00
|
|
|
|
2023-07-25 06:50:37 +00:00
|
|
|
const dragK = 0.005;
|
2023-07-28 01:04:08 +00:00
|
|
|
const damping = 0.99;
|
2023-07-24 06:28:23 +00:00
|
|
|
|
2023-07-23 05:57:34 +00:00
|
|
|
let theta = 0;
|
2023-07-24 03:57:51 +00:00
|
|
|
let psi = 0;
|
2023-07-28 00:47:33 +00:00
|
|
|
let theta0 = 0;
|
|
|
|
let psi0 = 0;
|
|
|
|
let dragx0 = 0;
|
|
|
|
let dragy0 = 0;
|
2023-07-28 01:04:08 +00:00
|
|
|
let dtheta = 0;
|
|
|
|
let dpsi = 0;
|
|
|
|
let dragging = false;
|
2023-07-24 03:57:51 +00:00
|
|
|
|
2023-07-28 01:04:08 +00:00
|
|
|
|
|
|
|
renderer.domElement.addEventListener("pointerdown", (event) => {
|
2023-07-24 03:57:51 +00:00
|
|
|
if( event.buttons === 1 ) {
|
2023-07-28 00:47:33 +00:00
|
|
|
theta0 = theta;
|
|
|
|
psi0 = psi;
|
|
|
|
dragx0 = event.clientX;
|
|
|
|
dragy0 = event.clientY;
|
2023-07-28 01:04:08 +00:00
|
|
|
dragging = true;
|
2023-07-24 03:57:51 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-07-28 01:04:08 +00:00
|
|
|
renderer.domElement.addEventListener("pointermove", (event) => {
|
2023-07-24 03:57:51 +00:00
|
|
|
if( event.buttons === 1 ) {
|
2023-07-28 01:04:08 +00:00
|
|
|
const theta1 = theta0 + (event.clientX - dragx0) * dragK;
|
|
|
|
const psi1 = psi0 + (event.clientY - dragy0) * dragK;
|
|
|
|
dtheta = theta1 - theta;
|
|
|
|
dpsi = psi1 - psi;
|
|
|
|
theta = theta1;
|
|
|
|
psi = psi1;
|
2023-07-24 03:57:51 +00:00
|
|
|
}
|
|
|
|
})
|
2023-07-22 01:11:18 +00:00
|
|
|
|
2023-07-28 01:04:08 +00:00
|
|
|
renderer.domElement.addEventListener("pointerup", (event) => {
|
|
|
|
dragging = false;
|
|
|
|
})
|
2023-07-28 00:47:33 +00:00
|
|
|
|
|
|
|
|
2023-07-25 05:43:26 +00:00
|
|
|
// set up GUI
|
|
|
|
|
|
|
|
const gui = new GUI();
|
|
|
|
|
2023-07-28 07:39:57 +00:00
|
|
|
const linkUrl = new URL(window.location.toLocaleString());
|
|
|
|
|
2023-07-29 00:40:15 +00:00
|
|
|
const link_params = {};
|
|
|
|
|
|
|
|
const urlParams = linkUrl.searchParams;
|
|
|
|
for( const param of [ "shape", "xRotate", "yRotate" ]) {
|
|
|
|
const value = urlParams.get(param);
|
|
|
|
if( value ) {
|
|
|
|
link_params[param] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
link_params['hyperplane'] = floatParam(urlParams, 'hyperplane');
|
2023-07-29 01:52:43 +00:00
|
|
|
link_params['thickness'] = floatParam(urlParams, 'thickness');
|
|
|
|
link_params['color'] = colorParam(urlParams, 'color', DEFAULT_COLOR);
|
|
|
|
link_params['background'] = colorParam(urlParams, 'background', DEFAULT_BG);
|
2023-07-29 00:40:15 +00:00
|
|
|
|
|
|
|
dpsi = floatParam(urlParams, 'dpsi');
|
|
|
|
dtheta = floatParam(urlParams, 'dtheta');
|
|
|
|
|
2023-07-28 07:39:57 +00:00
|
|
|
|
2023-07-25 05:43:26 +00:00
|
|
|
const gui_params = {
|
2023-07-29 00:40:15 +00:00
|
|
|
shape: link_params['shape'] || DEFAULT_SHAPE,
|
2023-07-29 01:52:43 +00:00
|
|
|
thickness: link_params['thickness'] || 1,
|
|
|
|
color: link_params['color'] || DEFAULT_COLOR,
|
|
|
|
background: link_params['background'] || DEFAULT_BG,
|
2023-07-29 00:40:15 +00:00
|
|
|
hyperplane: link_params['hyperplane'] || 2,
|
|
|
|
xRotate: link_params['xRotate'] || 'YW',
|
|
|
|
yRotate: link_params['yRotate'] || 'XZ',
|
2023-07-28 07:39:57 +00:00
|
|
|
damping: false,
|
2023-07-29 00:40:15 +00:00
|
|
|
"copy link": function () {
|
|
|
|
const url = new URL(linkUrl.origin + linkUrl.pathname);
|
|
|
|
url.searchParams.append("shape", gui_params.shape);
|
2023-07-29 01:52:43 +00:00
|
|
|
url.searchParams.append("thickness", gui_params.thickness.toString());
|
|
|
|
url.searchParams.append("color", hexToString(gui_params.color));
|
|
|
|
url.searchParams.append("background", hexToString(gui_params.background));
|
2023-07-29 00:40:15 +00:00
|
|
|
url.searchParams.append("hyperplane", gui_params.hyperplane.toString());
|
|
|
|
url.searchParams.append("xRotate", gui_params.xRotate);
|
|
|
|
url.searchParams.append("yRotate", gui_params.yRotate);
|
|
|
|
url.searchParams.append("dtheta", dtheta.toString());
|
|
|
|
url.searchParams.append("dpsi", dpsi.toString());
|
|
|
|
copyTextToClipboard(url);
|
2023-07-28 07:39:57 +00:00
|
|
|
}
|
2023-07-25 05:43:26 +00:00
|
|
|
};
|
|
|
|
|
2023-07-28 07:39:57 +00:00
|
|
|
|
|
|
|
|
2023-07-25 05:43:26 +00:00
|
|
|
gui.add(gui_params, 'shape',
|
2023-07-27 21:18:27 +00:00
|
|
|
[ '5-cell', '16-cell', 'tesseract', '24-cell', '120-cell', '600-cell' ]
|
2023-07-25 05:43:26 +00:00
|
|
|
).onChange(createShape)
|
|
|
|
|
2023-07-25 06:50:37 +00:00
|
|
|
gui.add(gui_params, 'hyperplane', 1.5, 4);
|
2023-07-29 01:52:43 +00:00
|
|
|
gui.add(gui_params, 'thickness', 0.01, 4);
|
|
|
|
gui.addColor(gui_params, 'color').onChange((c) => {
|
|
|
|
console.log(`Setting material colour to ${c}`);
|
|
|
|
material.color = new THREE.Color(c);
|
|
|
|
});
|
|
|
|
gui.addColor(gui_params, 'background').onChange((c) => {
|
|
|
|
console.log(`Setting background colour to ${c}`);
|
|
|
|
scene.background = new THREE.Color(c);
|
|
|
|
});
|
2023-07-25 05:43:26 +00:00
|
|
|
gui.add(gui_params, 'xRotate', [ 'YW', 'YZ', 'ZW' ]);
|
|
|
|
gui.add(gui_params, 'yRotate', [ 'XZ', 'XY', 'XW' ]);
|
2023-07-28 01:04:08 +00:00
|
|
|
gui.add(gui_params, 'damping');
|
2023-07-29 00:40:15 +00:00
|
|
|
gui.add(gui_params, 'copy link');
|
2023-07-25 05:43:26 +00:00
|
|
|
|
|
|
|
const ROTFN = {
|
|
|
|
XY: rotXY,
|
|
|
|
XZ: rotXZ,
|
|
|
|
XW: rotXW,
|
|
|
|
YZ: rotYZ,
|
|
|
|
YW: rotYW,
|
|
|
|
ZW: rotZW,
|
|
|
|
};
|
|
|
|
|
2023-07-22 01:11:18 +00:00
|
|
|
|
2023-07-22 03:24:42 +00:00
|
|
|
|
2023-07-28 07:39:57 +00:00
|
|
|
createShape(gui_params["shape"]);
|
|
|
|
|
|
|
|
|
2023-07-23 05:57:34 +00:00
|
|
|
const rotation = new THREE.Matrix4();
|
2023-07-22 06:59:48 +00:00
|
|
|
|
2023-07-22 01:11:18 +00:00
|
|
|
function animate() {
|
|
|
|
requestAnimationFrame( animate );
|
|
|
|
|
2023-07-28 01:04:08 +00:00
|
|
|
if( ! dragging ) {
|
|
|
|
theta += dtheta;
|
|
|
|
psi += dpsi;
|
|
|
|
if( gui_params.damping ) {
|
|
|
|
dtheta = dtheta * damping;
|
|
|
|
dpsi = dpsi * damping;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-25 05:43:26 +00:00
|
|
|
const rotations = [
|
|
|
|
ROTFN[gui_params.xRotate](theta),
|
|
|
|
ROTFN[gui_params.yRotate](psi)
|
|
|
|
];
|
|
|
|
shape.hyperplane = gui_params.hyperplane;
|
2023-07-29 01:52:43 +00:00
|
|
|
shape.geom_scale = gui_params.thickness;
|
2023-07-23 08:16:02 +00:00
|
|
|
shape.render3(rotations);
|
|
|
|
|
2023-07-22 01:11:18 +00:00
|
|
|
renderer.render( scene, camera );
|
|
|
|
}
|
2023-07-28 07:40:39 +00:00
|
|
|
animate();
|