Seeking assistance! How can I customize the colors of a 3D cube in the Three.js
library rather than using the default settings?
This is what I have managed to create so far -
HTML
<div id="container"></div>
JavaScript
// revolutions per second
var angularSpeed = 0.2;
var lastTime = 0;
// this function runs on each animation frame
function animate() {
// update
var time = (new Date()).getTime();
var timeDiff = time - lastTime;
var angleChange = angularSpeed * timeDiff * 2 * Math.PI / 1000;
cube.y += angleChange;
lastTime = time;
// render
renderer.render(scene, camera);
// request new frame
requestAnimationFrame(function () {
animate();
});
}
// create renderer
var container = document.getElementById("container");
var renderer = new THREE.WebGLRenderer();
renderer.setSize(container.offsetWidth, container.offsetHeight);
container.appendChild(renderer.domElement);
// set up camera
var camera = new THREE.PerspectiveCamera(45,
window.innerWidth / window.innerHeight, 1, 1000);
camera.position.z = 700;
// establish scene
var scene = new THREE.Scene();
// define cube dimensions and color
var cube = new THREE.Mesh(new THREE.CubeGeometry(400, 100, 200),
new THREE.MeshBasicMaterial({
wireframe: true,
color: '#ff0000'
}));
cube.rotation.x = Math.PI * 0.1;
cube.rotation.y = Math.PI * 0.3;
scene.add(cube);
// initiate animation
animate();
Here is a Fiddle showcasing it.