pipes/main.js

136 lines
3.8 KiB
JavaScript
Raw Normal View History

2025-10-30 07:24:39 +00:00
// TODO: corner mesh
// TODO: cylinder mesh
// TODO: end point mesh
2025-10-29 08:32:35 +00:00
2025-10-29 09:44:08 +00:00
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
2025-10-30 08:14:09 +00:00
import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js';
2025-10-29 09:44:08 +00:00
2025-10-29 08:32:35 +00:00
// Initialise 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);
// Set up lights
const ambientLight = new THREE.AmbientLight(0x404040);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
directionalLight.position.set(1, 3, 2);
scene.add(directionalLight);
scene.add(directionalLight.target);
// Position the camera
camera.position.z = 5;
2025-10-29 09:44:08 +00:00
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.update();
2025-10-29 08:32:35 +00:00
2025-10-29 20:30:18 +00:00
// Make materials
const redMaterial = new THREE.MeshLambertMaterial({ color: 0xff0000 });
const greenMaterial = new THREE.MeshLambertMaterial({ color: 0x00ff00 });
const blueMaterial = new THREE.MeshLambertMaterial({ color: 0x0000ff });
2025-10-30 07:24:39 +00:00
// Make cubes
2025-10-29 20:30:18 +00:00
function makeCube(xCoord, material) {
2025-10-29 08:32:35 +00:00
const geometry = new THREE.BoxGeometry();
const cube = new THREE.Mesh(geometry, material);
cube.position.set(xCoord, 0, 0);
scene.add(cube);
return cube;
}
2025-10-30 08:14:09 +00:00
function makeCylinder(material) {
const radius = .25;
const height = 1;
const radialSegments = 8;
const geometry = new THREE.CylinderGeometry(radius, radius, height, radialSegments);
const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
return mesh;
}
2025-10-30 07:24:39 +00:00
class CornerCurve extends THREE.Curve {
constructor(scale) {
super();
this.scale = scale;
}
getPoint(t) {
2025-10-30 08:14:09 +00:00
const tx = Math.sin(.5 * Math.PI * t) * this.scale - .5;
const ty = Math.cos(.5 * Math.PI * t) * this.scale - .5;
2025-10-30 07:24:39 +00:00
const tz = 0;
return new THREE.Vector3(tx, ty, tz);
}
2025-10-29 08:32:35 +00:00
}
2025-10-30 07:24:39 +00:00
function makeCorner(material) {
2025-10-30 08:14:09 +00:00
const path = new CornerCurve(.5);
2025-10-30 07:24:39 +00:00
const tubularSegments = 20;
const radius = .25;
const radialSegments = 8;
const closed = false;
const geometry = new THREE.TubeGeometry(path, tubularSegments, radius, radialSegments, closed);
2025-10-30 08:14:09 +00:00
2025-10-30 07:24:39 +00:00
const mesh = new THREE.Mesh(geometry, material);
2025-10-30 08:14:09 +00:00
scene.add(mesh);
return mesh;
}
function makeCap(material) {
const radius = .25;
const height = .5;
const radialSegments = 8;
const cylinder = new THREE.CylinderGeometry(radius, radius, height, radialSegments);
// Shift cylinder vertices down
const positionAttribute = cylinder.getAttribute('position');
const vertex = new THREE.Vector3();
for (let i = 0; i < positionAttribute.count; i++) {
vertex.fromBufferAttribute(positionAttribute, i);
positionAttribute.setXYZ(i, vertex.x, vertex.y - .25, vertex.z);
}
const sphereRadius = .375;
const widthSegments = 12;
const heightSegments = 8;
const sphere = new THREE.SphereGeometry(sphereRadius, widthSegments, heightSegments);
var capGeometry = BufferGeometryUtils.mergeGeometries([cylinder, sphere], false);
2025-10-30 07:24:39 +00:00
2025-10-30 08:14:09 +00:00
const mesh = new THREE.Mesh(capGeometry, material);
2025-10-30 07:24:39 +00:00
scene.add(mesh);
return mesh;
}
let meshes = [];
2025-10-30 08:14:09 +00:00
const cylinder = makeCylinder(redMaterial);
cylinder.position.x = -1;
meshes.push(cylinder);
const corner = makeCorner(greenMaterial);
meshes.push(corner);
const cap = makeCap(blueMaterial);
cap.position.x = 1;
meshes.push(cap);
2025-10-30 07:24:39 +00:00
2025-10-29 08:32:35 +00:00
// Animation loop
function animate() {
requestAnimationFrame(animate);
2025-10-30 07:24:39 +00:00
for (let i = 0; i < meshes.length; i++) {
2025-10-30 08:14:09 +00:00
const mesh = meshes[i];
mesh.rotation.x += 0.01;
mesh.rotation.y += 0.01;
2025-10-29 08:32:35 +00:00
}
2025-10-29 09:44:08 +00:00
controls.update();
2025-10-29 08:32:35 +00:00
renderer.render(scene, camera);
}
animate();