Creating a 3D heart in Three.js that you can rotate and zoom involves several steps. Below is an example of how you can achieve this. The heart shape can be created using THREE.Shape and extruded into 3D using THREE.ExtrudeGeometry. You can then add controls for rotation and zoom using OrbitControls.
Here’s the complete code:
Full Example Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3D Heart in Three.js</title>
<style>
body {
margin: 0;
overflow: hidden;
}
canvas {
display: block;
}
</style>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r152/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.152.0/examples/js/controls/OrbitControls.js"></script>
<script>
// Scene, Camera, Renderer
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);
// Create a heart shape
const x = 0, y = 0;
const heartShape = new THREE.Shape();
heartShape.moveTo(x + 0, y + 0.5);
heartShape.bezierCurveTo(x + 0, y + 0.5, x - 0.5, y + 0, x + 0, y - 0.5);
heartShape.bezierCurveTo(x + 0.5, y + 0, x + 0, y + 0.5, x + 0, y + 0.5);
// Extrude the shape into 3D
const extrudeSettings = {
depth: 0.3,
bevelEnabled: true,
bevelThickness: 0.05,
bevelSize: 0.05,
bevelSegments: 10
};
const geometry = new THREE.ExtrudeGeometry(heartShape, extrudeSettings);
const material = new THREE.MeshPhongMaterial({ color: 0xff0000, shininess: 100 });
const heart = new THREE.Mesh(geometry, material);
scene.add(heart);
// Add lighting
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
scene.add(ambientLight);
const pointLight = new THREE.PointLight(0xffffff, 1);
pointLight.position.set(5, 5, 5);
scene.add(pointLight);
// Position the camera
camera.position.z = 2;
// Add OrbitControls for rotation and zoom
const controls = new THREE.OrbitControls(camera, renderer.domElement);
// Handle window resize
window.addEventListener('resize', () => {
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
});
// Animation loop
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
Explanation of the Code
-
Heart Shape:
- The heart shape is created using
THREE.Shape and bezierCurveTo to define the curves of the heart.
-
Extrusion:
- The 2D heart shape is extruded into 3D using
THREE.ExtrudeGeometry. The extrudeSettings object defines the depth and beveling of the extrusion.
-
Lighting:
- Ambient and point lights are added to illuminate the heart and make it look more realistic.
-
Camera and Controls:
- A
PerspectiveCamera is used for a 3D perspective view.
OrbitControls allows the user to rotate, zoom, and pan around the heart.
-
Responsive Design:
- The
resize event listener ensures the canvas resizes properly when the browser window is resized.
-
Animation Loop:
- The
animate function continuously renders the scene, allowing for smooth interaction.
How to Use
- Copy the code into an HTML file.
- Open the file in a browser.
- Use your mouse to rotate, zoom, and pan around the 3D heart.
Enjoy your interactive 3D heart! ❤️