I have done the source code for my lesson, which I shall publish later on my website:
<script src="three.min.js"></script>
<script>
var renderer, scene, camera, geometry, material, mesh, light, axisRotation;
function createScene() {
renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.set( 3, 3, 3 );
camera.lookAt( scene.position );
scene.add( camera );
geometry = new THREE.CubeGeometry( 1, 1, 1 );
material = new THREE.MeshLambertMaterial( { color: 0xff0000 } );
mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );
light = new THREE.PointLight( 0xffff00 );
light.position.set( 10, 10, 10 );
scene.add( light );
}
function rotateCube( axis ) {
switch ( axis ) {
case 'x':
mesh.rotation.x += 0.02;
break;
case 'y':
mesh.rotation.y += 0.02;
break;
case 'z':
mesh.rotation.z += 0.02;
break;
}
}
function toggleObjectWireframe() {
material.wireframe = !material.wireframe;
}
function renderScene() {
renderer.render( scene, camera );
}
function animateScene() {
requestAnimationFrame( animateScene );
renderScene();
rotateCube( axisRotation );
}
function getChar( event ) {
var inputChar = null;
if ( event.which === null ) {
if ( event.keyCode < 32 ) inputChar = null;
else inputChar = String.fromCharCode( event.keyCode );
}
if ( event.which != 0 && event.charCode != 0 ) {
if ( event.which < 32 ) inputChar = null;
else inputChar = String.fromCharCode( event.which );
}
if ( ~['x', 'y', 'z'].indexOf( inputChar ) ) {
axisRotation = inputChar;
}
else if ( inputChar == 'w' ) toggleObjectWireframe();
else inputChar = null;
}
function initWebGLApp() {
axisRotation = 'x';
createScene();
animateScene();
}
function setCssStyle() {
document.body.style.width = "100%";
document.body.style.height = "100%";
document.body.style.margin = 0;
document.body.style.padding = 0;
document.querySelector('canvas').style.display = 'block';
}
window.onload = function() {
initWebGLApp();
setCssStyle();
document.onkeypress = getChar;
}
</script>
You can see the working results on JSFiddle.