A simple Three.js project using Vite to create a 3D scene with a red cube, interactive camera controls, and smooth animations. Three.js Basic Scene with OrbitControls This project demonstrates a basic Three.js setup, including a 3D scene, a red cube, a perspective camera, and interactive camera controls using OrbitControls. It also includes a continuous rendering loop for smooth animations.
- 3D Scene: A red cube in a Three.js scene.
- Interactive Camera: Use
OrbitControlsto rotate, zoom, and pan. - Auto-Rotation: The camera automatically rotates around the scene.
- Smooth Rendering: A continuous loop ensures smooth animations.
- Node.js installed.
-
Clone or download this repository.
-
Navigate to the project folder in your terminal.
-
Install dependencies:
npm install
To start the development server, run the following command:
npm run devThis will start the server and open your default browser to your localhost. The site will reload automatically as you make changes to your code.
-
A THREE.Scene is created to hold all 3D objects.
-
A red cube is added to the scene using THREE.BoxGeometry and THREE.MeshBasicMaterial.
-
A THREE.PerspectiveCamera is initialized with:
-
Field of View (FOV): 45
-
Aspect Ratio: window.innerWidth / window.innerHeight
-
Near and Far Clipping Planes: 0.1 and 200
-
The camera is positioned at z = 5 to view the cube.
- A THREE.WebGLRenderer is linked to a
<canvas>element in the HTML. - The renderer size is set to match the window dimensions.
-
OrbitControls is used to enable interactive camera movement.
-
Features:
-
Damping: Smoothens camera movements.
-
Auto-Rotation: Automatically rotates the camera around the scene.
-
-
A continuous rendering loop is implemented using requestAnimationFrame.
-
The loop updates the controls and re-renders the scene in each frame.
-
HTML and CSS The
<canvas>element is used as the rendering target for Three.js. -
Basic CSS ensures the canvas fills the entire window without scrollbars.
<canvas class="threejs"></canvas>body {
margin: 0;
padding: 0;
overflow: hidden;
}
-
Three.js: A JavaScript 3D library.
-
OrbitControls: A Three.js addon for interactive camera controls.
This project is open-source and available under the MIT License.