
// Create points geometry
const zValues = Array.from({length: actuators.length}, () => (Math.random()-0.5)*0.1); //Array(97).fill(0.2);
//zValues[15] = +0.5;
//zValues[16] = -0.5;

const points = [];
for (let i = 0; i < zValues.length; i++) {
    points.push([actuators[i][0], actuators[i][1], zValues[i]]); // Push x, y, z for each vertex
}

console.log(points)

// Initialize Three.js scene
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

// White directional light at 0.65 intensity shining from the top.
scene.add(new THREE.HemisphereLight(0x606060, 0x404040));
let directionalLight = new THREE.DirectionalLight(0xffffff, 0.65);
scene.add(directionalLight);

// Create geometry and material for points
const pointGeometry = new THREE.SphereGeometry(0.02);
const pointMaterial = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
points.forEach(point => {
    const sphere = new THREE.Mesh(pointGeometry, pointMaterial);
    sphere.position.set(point[0], point[1], point[2]);
    scene.add(sphere);
});

// Triangulate points using Delaunator
const delaunay = new Delaunator(...points);
const triangles = delaunay.triangles;

// Create geometry for the surface
const surfaceGeometry = new THREE.BufferGeometry();
const vertices = new Float32Array(triangles.length * 3);

for (let i = 0; i < triangles.length; i++) {
    const index = triangles[i] * 3;
    vertices[i * 3] = points[index][0];
    vertices[i * 3 + 1] = points[index][1];
    vertices[i * 3 + 2] = points[index][2];
}

surfaceGeometry.setAttribute('position', new THREE.BufferAttribute(vertices, 3));

// Material for the surface
const surfaceMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000, opacity: 0.5, transparent: true });

// Create surface mesh
const surfaceMesh = new THREE.Mesh(surfaceGeometry, surfaceMaterial);
scene.add(surfaceMesh);

// Create wireframe material
const wireframeMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000, wireframe: true });

// Create wireframe mesh
const wireframeMesh = new THREE.Mesh(surfaceGeometry, wireframeMaterial);
scene.add(wireframeMesh);

// Position the camera
camera.position.set(3, 0, 3);
camera.lookAt(0, 0, 0);

// Render loop
function animate() {
    requestAnimationFrame(animate);
    renderer.render(scene, camera);
    }
animate();

