FCC Grid (with tensor)

by Chantal Roth

HTML

<div id="container"></div>
<script async src="https://unpkg.com/es-module-shims@1.8.0/dist/es-module-shims.js"></script>

<script type="importmap">
  {
    "imports": {
      "three": "https://unpkg.com/three@0.160.0/build/three.module.js",
      "three/addons/": "https://unpkg.com/three@0.160.0/examples/jsm/"
    }
  }
</script>

CSS

body {
  margin: 0;
  overflow: hidden;
  background-color: #000;
}
#container {
  width: 100%;
  height: 100vh;
}

/* CSS injected by JS for the Metric UI */
#tensor-box {
    position: absolute;
    bottom: 20px;
    left: 20px;
    background: rgba(0, 0, 0, 0.85);
    padding: 15px;
    border-radius: 8px;
    color: white;
    font-family: 'Courier New', monospace;
    border: 1px solid #555;
    pointer-events: none;
    user-select: none;
    box-shadow: 0 0 20px rgba(0,0,0,0.5);
    transition: all 0.2s ease;
}
.tensor-title { text-align: center; font-size: 12px; color: #aaa; margin-bottom: 10px; letter-spacing: 1px;}
.matrix-container {
    display: grid;
    grid-template-columns: repeat(4, 45px); /* Default 4D */
    gap: 6px;
    position: relative;
    padding: 0 12px;
    transition: all 0.2s ease;
}
.matrix-container::before, .matrix-container::after {
    content: ''; position: absolute; top: -5px; bottom: -5px; width: 12px; border: 2px solid #fff;
}
.matrix-container::before { left: 0; border-right: 0; }
.matrix-container::after { right: 0; border-left: 0; }
.cell { text-align: right; font-size: 13px; line-height: 20px; }

JavaScript

import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { GUI } from 'three/addons/libs/lil-gui.module.min.js';

let container, camera, scene, renderer, controls;
let crystalInstances; 
let gridMesh, probeMesh;
let atomOriginalCenters = []; 
let gridOriginalPos;
let clock = new THREE.Clock();

// Matrix DOM elements
let matrixDiv, matrixContainer, matrixTitle;
let matrixCells = [];

// Simulation Parameters
const params = {
    // Crystal Appearance
    color: 0xffe65c, // Deep Gold
    metalness: 0.9,  // Full metal for contrast
    roughness: 0.4,  // Smooth for sharp highlights
    atomRadius: 0.25,
    gridExtent: 2,   // Radius of the crystal grid
    
    // Visual Toggles
    showGrid: false, 
    showMetric: false, // Default OFF
    tensorMode: '4D Spacetime',

    // Probe Position (Where we measure)
    probeX: 0.5,
    probeY: 0.5,
    probeZ: 0.5,

    // Static Deformations (Relativity)
    stretchX: 1.0, stretchY: 1.0, stretchZ: 1.0,
    bendX: 0.0, bendY: 0.0, bendZ: 0.0,
    twist: 0.0, 
    
    // Dynamic Jiggles
    waveEnabled: false, waveSpeed: 2.0, waveFrequency: 2.0, waveAmplitude: 0.1,
    pulseEnabled: false, pulseSpeed: 3.0, pulseFrequency: 1.5, pulseAmplitude: 0.05,
    wobbleEnabled: false, wobbleSpeed: 4.0, wobbleAmplitude: 0.2,
    twistWaveEnabled: false, 
    twistWaveSpeed: 3.0, twistWaveFreq: 0.5, twistWaveAmp: 0.05,
};

init();
animate();

function init() {
    injectCSS();
    createMatrixUI(); 
    
    container = document.getElementById('container');

    // Scene Setup
    scene = new THREE.Scene();
    scene.background = new THREE.Color(0x000000); // Pitch black background

    camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 100);
    camera.position.set(6, 5, 8); 

    renderer = new THREE.WebGLRenderer({ antialias: true });
   ...