Today, I'll teach you how to create an introductory scene with Three.js which is probably the most useful library for you to work with, as it already has many useful functions pre-written, which would otherwise require a lot ofΒ effort.
Fundamentals
Three.js is a 3D library that tries to make it as easy as possible to get 3D content on a webpage. Three.js is often confused with WebGL since more often than not, but not always, three.js uses WebGL to draw 3D.
WebGL is a very low-level system that only draws points, lines, and triangles. To do anything useful with WebGL generally requires quite a bit of code and that is where three.js comes in. It handles stuff like scenes, lights, shadows, materials, textures, 3d math, all things that you'd have to write yourself if you were to use WebGL directly.
Getting started
Go ahead and initialise our new project using the CodePen playground or setup your own project on Visual Studio Code with the following file structure under your src folder.
ThreeJS Scene Template
|- /src
|- index.html
|- style.css
|- Avatar.png
|- spaceTexture.png
|- moonTexture.png
|- normalMap.png
|- script.js
Part 1: HTML
The HTML is really basic. Edit your index.html
and replace it with the following code.
<canvas id="bg"></canvas>
Part 2: CSS
Next step is to add the following styles and complete our style.css
file. Just make sure the div that will wrap the canvas fits the document, and apply any size you want to your plane div element.
@import url('https://fonts.googleapis.com/css2?family=Rubik:wght@700&display=swap');
html,
body {
height: 100vh;
}
body {
background-color: #000;
color: #fff;
display: flex;
align-items: center;
font-family: 'Rubik', sans-serif;
margin: 0;
overflow: hidden;
}
div {
position: absolute;
left: 0;
top: 0;
padding: 1em;
}
canvas {
position: fixed;
top: 0;
left: 0;
}
Part 3: JavaScript
Now we can implement our JavaScript logic to our ThreeJS setup like so.
import * as THREE from 'https://cdn.jsdelivr.net/npm/three@0.126/build/three.module.js';
import { OrbitControls } from 'https://cdn.jsdelivr.net/npm/three@0.126/examples/jsm/controls/OrbitControls.js';
//creating the scene
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({
canvas: document.querySelector('#bg'),
});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableKeys = false;
controls.maxDistance = 5000.0;
controls.enableDamping = true;
controls.dampingFactor = 0.16;
controls.target.set(0, 0, 0);
// If you use THREE.OrbitControls and you want to change the camera target or position you need to use the update method.
camera.position.set(0, 5, 0);
controls.update();
document.body.appendChild(renderer.domElement);
THREE.ImageUtils.crossOrigin = '';
const texture = THREE.ImageUtils.loadTexture('avatar.png');
texture.anisotropy = renderer.getMaxAnisotropy();
//Stars
function addStar() {
const geometry = new THREE.SphereGeometry(0.25, 24, 24);
const material = new THREE.MeshStandardMaterial({ color: 0xffffff });
const star = new THREE.Mesh(geometry, material);
const [x, y, z] = Array(3)
.fill()
.map(() => THREE.MathUtils.randFloatSpread(300));
star.position.set(x, y, z);
scene.add(star);
}
Array(200).fill().forEach(addStar);
// Lights
const pointLight = new THREE.PointLight(0xffffff, 2);
pointLight.position.set(5, 5, 5);
const ambientLight = new THREE.AmbientLight(0xffffff, 1);
scene.add(pointLight, ambientLight);
// Background
const spaceTexture = new THREE.TextureLoader().load('https://media.istockphoto.com/photos/aerosol-clouds-space-haze-or-cosmic-rays-pink-pastel-blue-space-sky-picture-id1306540124?b=1&k=20&m=1306540124&s=170667a&w=0&h=Cpdu6iLmqYM7BFjoq0xvTnR6ws12ba6v7yxAFhCenyI=');
scene.background = spaceTexture;
// Moon
const moonTexture = new THREE.TextureLoader().load('data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAoHCBYWFRgWFhYYGRgaGhoaGhwcGh8eHBkcHBwaGhocGhgcIS4lHB4rHxoaJjgmKy8xNTU1HCQ7QDs0Py40NTEBDAwMDw8PEA8PET8dGB0xMTE0NDE0PzQxPzExMTExMTExNDQxMTExMTExMTExMTExMTExMTExMTExMTExMTExMf/AABEIAJ8BPgMBIgACEQEDEQH/xAAaAAACAwEBAAAAAAAAAAAAAAADBAECBQAG/8QAMRAAAQMCBAQFBAIDAQEAAAAAAQACEQMhBDFBURJhcfCBkaGxwQUTItEy4RRC8VJy/8QAFgEBAQEAAAAAAAAAAAAAAAAA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BDgcciRbQEHznLlCmjSe0iQ2Dmdf1K0+OBEDyuuDpzv1QCpkBwOV5sB+oTzgxwBDHTvp4BLiJmJ5dlFp1S35E28kCD6JmIi+py6KRTAF8/D2T1eHGQAOV1T7bYyQZ773gSchHwka9Jzv9oj2yy3Wx/jiZhGbRadI6IrBZQvm7LLLrfVGqCNPQBaD8I0LjRjKwQN22UObZFDCrfbdugWDOSkBG+0VH2SgFwqCSimiVPByQAgrgj/bO3qu+2dvVBQN5ldAV/tFR9ooKBWlSWFcGlBCgFX4Co4CgiV3Eu4CpDEEWV2kbKA1cZQTxBWaAqBingQEEbKRCGZUcR2CAsKHQhSdl0HZBLoVZVuFdDt0A+EK0KftndWDCgqAFxYFP2zup4SgC5ijzRoKgtKAUKRAROAqppFBVzJURG6JwFd9o9lAMKQFb7ZVg0oIPRU4UWCu4EH//2Q==');
const normalTexture = new THREE.TextureLoader().load('https://www.filterforge.com/filters/9158-normal.jpg');
const moon = new THREE.Mesh(
new THREE.SphereGeometry(3, 32, 32),
new THREE.MeshStandardMaterial({
map: moonTexture,
normalMap: normalTexture,
})
);
scene.add(moon);
moon.position.z = 60;
moon.position.setX(-10);
//Torus
const ringGeometry = new THREE.TorusGeometry(10,3,16,100);
const material = new THREE.MeshBasicMaterial({color: 'red'});
const torus = new THREE.Mesh(ringGeometry,material);
scene.add(torus);
//cube
const cubeGeometry = new THREE.BoxGeometry(3, 3, 3);
let materials = [];
for(let counter = 0; counter < 6; counter++) {
materials.push(new THREE.MeshBasicMaterial({color: 'white', map: texture}));
}
var cube = new THREE.Mesh(cubeGeometry, materials);
scene.add(cube);
//loop
var animate = function () {
requestAnimationFrame( animate );
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
torus.rotation.x += 0.01;
torus.rotation.y += 0.005;
torus.rotation.z += 0.01;
moon.rotation.x += 0.005;
renderer.render(scene, camera);
};
animate();
Cool! Now, if you save it, you should see this in the browser:
Recap
If you followed along then you should have completed the project and finished off your basic scene setup with ThreeJS.
Now if you made it this far, then I am linking the code to my Sandbox for you to fork or clone and then the job's done.
Top comments (0)