html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Squishy Cube</title>
<style>
body {
margin: 0;
overflow: hidden;
background: #000;
}
canvas {
display: block;
}
#info {
position: absolute;
top: 10px;
width: 100%;
text-align: center;
color: white;
font-family: Arial, sans-serif;
pointer-events: none;
}
</style>
</head>
<body>
<div id="info">Click and drag to squish the cube</div>
<script>
// Import Three.js from CDN
document.write('<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"><\/script>');
// Wait for Three.js to load
window.addEventListener('load', function() {
// Initialize variables
let scene, camera, renderer;
let cube, cubeMesh;
const originalVertices = [];
let raycaster, mouse;
let isDragging = false;
let selectedVertex = null;
let selectedVertices = [];
let maxPull = 2;
// Set up the scene
function init() {
// Create scene
scene = new THREE.Scene();
// Create camera
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 5;
// Create renderer
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x000000);
document.body.appendChild(renderer.domElement);
// Create cube geometry with subdivisions for better squishing
const geometry = new THREE.BoxGeometry(2, 2, 2, 8, 8, 8);
// Store original vertex positions
for (let i = 0; i < geometry.attributes.position.array.length; i += 3) {
originalVertices.push({
x: geometry.attributes.position.array[i],
y: geometry.attributes.position.array[i + 1],
z: geometry.attributes.position.array[i + 2]
});
}
// Create jelly-like material
const material = new THREE.MeshPhongMaterial({
color: 0x00aaff,
specular: 0x00ffff,
shininess: 30,
transparent: true,
opacity: 0.8
});
// Create mesh
cubeMesh = new THREE.Mesh(geometry, material);
scene.add(cubeMesh);
// Add lights
const ambientLight = new THREE.AmbientLight(0x404040);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
directionalLight.position.set(1, 1, 1);
scene.add(directionalLight);
// Initialize raycaster and mouse
raycaster = new THREE.Raycaster();
mouse = new THREE.Vector2();
// Add event listeners
window.addEventListener('resize', onWindowResize);
window.addEventListener('mousedown', onMouseDown);
window.addEventListener('mousemove', onMouseMove);
window.addEventListener('mouseup', onMouseUp);
window.addEventListener('touchstart', onTouchStart, { passive: false });
window.addEventListener('touchmove', onTouchMove, { passive: false });
window.addEventListener('touchend', onTouchEnd);
// Start animation loop
animate();
}
// Handle window resize
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
// Mouse/Touch event handlers
function onMouseDown(event) {
event.preventDefault();
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
checkIntersection();
isDragging = true;
}
function onMouseMove(event) {
if (!isDragging) return;
event.preventDefault();
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
pullVertices();
}
function onMouseUp(event) {
isDragging = false;
selectedVertices = [];
relaxVertices();
}
function onTouchStart(event) {
event.preventDefault();
const touch = event.touches[0];
mouse.x = (touch.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(touch.clientY / window.innerHeight) * 2 + 1;
checkIntersection();
isDragging = true;
}
function onTouchMove(event) {
if (!isDragging) return;
event.preventDefault();
const touch = event.touches[0];
mouse.x = (touch.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(touch.clientY / window.innerHeight) * 2 + 1;
pullVertices();
}
function onTouchEnd(event) {
isDragging = false;
selectedVertices = [];
relaxVertices();
}
// Find vertices to pull
function checkIntersection() {
raycaster.setFromCamera(mouse, camera);
const intersects = raycaster.intersectObject(cubeMesh);
if (intersects.length > 0) {
const faceIndex = intersects[0].faceIndex;
const geometry = cubeMesh.geometry;
const position = geometry.attributes.position;
const impactPoint = intersects[0].point;
// Find vertices near impact point
selectedVertices = [];
for (let i = 0; i < position.count; i++) {
const vertex = new THREE.Vector3().fromBufferAttribute(position, i);
vertex.applyMatrix4(cubeMesh.matrixWorld);
// Calculate distance from impact point
const distance = impactPoint.distanceTo(vertex);
// If within radius of influence, add to selected vertices
if (distance < 1.0) {
selectedVertices.push({
index: i,
distance: distance,
direction: new THREE.Vector3().subVectors(vertex, impactPoint).normalize()
});
}
}
}
}
// Pull vertices based on mouse position
function pullVertices() {
if (selectedVertices.length === 0) return;
raycaster.setFromCamera(mouse, camera);
const direction = raycaster.ray.direction.clone();
const geometry = cubeMesh.geometry;
const position = geometry.attributes.position;
for (const vertex of selectedVertices) {
const i = vertex.index;
const influenceFactor = 1 - Math.min(vertex.distance, 1.0);
// Calculate pull vector
const pullVector = direction.clone().multiplyScalar(influenceFactor * maxPull);
// Apply pull to vertex
position.array[i * 3] = originalVertices[i].x + pullVector.x * influenceFactor;
position.array[i * 3 + 1] = originalVertices[i].y + pullVector.y * influenceFactor;
position.array[i * 3 + 2] = originalVertices[i].z + pullVector.z * influenceFactor;
}
position.needsUpdate = true;
}
// Return vertices to original position
function relaxVertices() {
const geometry = cubeMesh.geometry;
const position = geometry.attributes.position;
// Animate vertices back to original positions
const relaxInterval = setInterval(() => {
let allRelaxed = true;
for (let i = 0; i < position.count; i++) {
const currentX = position.array[i * 3];
const currentY = position.array[i * 3 + 1];
const currentZ = position.array[i * 3 + 2];
const targetX = originalVertices[i].x;
const targetY = originalVertices[i].y;
const targetZ = originalVertices[i].z;
// Calculate distance to original position
const dx = targetX - currentX;
const dy = targetY - currentY;
const dz = targetZ - currentZ;
// Check if vertex is close enough to original position
if (Math.abs(dx) > 0.01 || Math.abs(dy) > 0.01 || Math.abs(dz) > 0.01) {
allRelaxed = false;
// Move vertex toward original position
position.array[i * 3] += dx * 0.1;
position.array[i * 3 + 1] += dy * 0.1;
position.array[i * 3 + 2] += dz * 0.1;
}
}
position.needsUpdate = true;
// Stop animation when all vertices are back to original positions
if (allRelaxed) {
clearInterval(relaxInterval);
}
}, 16);
}
// Animation loop
function animate() {
requestAnimationFrame(animate);
// Slowly rotate the cube
cubeMesh.rotation.x += 0.003;
cubeMesh.rotation.y += 0.005;
renderer.render(scene, camera);
}
// Start the application
init();
});
</script>
</body>
</html>