fourdjs/main.js

189 lines
3.6 KiB
JavaScript
Raw Normal View History

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-24 23:05:06 +00:00
import { FourDShape } from './fourDShape.js';
2023-07-24 02:34:48 +00:00
2023-07-24 02:00:34 +00:00
const NODE_OPACITY = 1.0;
const LINK_OPACITY = 0.7;
2023-07-22 03:24:42 +00:00
2023-07-22 06:59:48 +00:00
2023-07-23 05:57:34 +00:00
// hacky stuff for 4d rotations
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 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-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 );
const light = new THREE.PointLight(0xffffff, 2);
light.position.set(10, 10, 10);
scene.add(light);
2023-07-24 02:34:48 +00:00
const amblight = new THREE.AmbientLight(0xffffff, 0.5);
scene.add(amblight);
scene.background = new THREE.Color(0xdddddd);
2023-07-24 23:05:06 +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:05:06 +00:00
const struct = POLYTOPES.cell24();
2023-07-24 02:34:48 +00:00
// TODO = automate going from struct labels to colours, this now only
// works with the 24-cell
2023-07-24 02:34:48 +00:00
const node_ms = {};
2023-07-24 02:34:48 +00:00
node_ms["RED"] = new THREE.MeshStandardMaterial( { color: 0x990044 } );
node_ms["GREEN"] = new THREE.MeshStandardMaterial( { color: 0xffffee } );
node_ms["BLUE"] = new THREE.MeshStandardMaterial( { color: 0x101010 } );
for( const label in node_ms ) {
const node_m = node_ms[label];
node_m.roughness = 0.2;
2023-07-22 03:40:01 +00:00
if( NODE_OPACITY < 1.0 ) {
node_m.transparent = true;
node_m.opacity = NODE_OPACITY;
}
2023-07-24 02:00:34 +00:00
}
const link_m = new THREE.MeshStandardMaterial(
{ color: 0xb0b0b0 } );
2023-07-22 03:40:01 +00:00
2023-07-22 03:24:42 +00:00
link_m.metalness = 0.4;
link_m.roughness = 0.0;
2023-07-24 02:00:34 +00:00
if( LINK_OPACITY < 1.0 ) {
link_m.transparent = true;
link_m.opacity = LINK_OPACITY;
}
2023-07-22 06:59:48 +00:00
2023-07-22 03:24:42 +00:00
const shape = new FourDShape(node_ms, link_m, struct);
2023-07-22 03:40:01 +00:00
2023-07-22 04:52:42 +00:00
scene.add(shape);
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
const dragK = 0.01;
2023-07-23 05:57:34 +00:00
let theta = 0;
2023-07-24 03:57:51 +00:00
let psi = 0;
let startX = 0;
let startY = 0;
let startX0 = 0;
let startY0 = 0;
2023-07-24 03:57:51 +00:00
renderer.domElement.addEventListener("mousedown", (event) => {
if( event.buttons === 1 ) {
startX = event.clientX;
startY = event.clientY;
startX0 = theta / dragK;
startY0 = theta / dragK;
2023-07-24 03:57:51 +00:00
}
})
renderer.domElement.addEventListener("mousemove", (event) => {
if( event.buttons === 1 ) {
theta = (event.clientX - startX + startX0) * dragK;
psi = (event.clientY - startY + startY0) * dragK;
2023-07-24 03:57:51 +00:00
}
})
2023-07-22 01:11:18 +00:00
2023-07-22 03:24:42 +00:00
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 );
const rotations = [rotYZ(theta), rotXZ(psi)];
shape.render3(rotations);
2023-07-22 01:11:18 +00:00
renderer.render( scene, camera );
}
animate();