Head light

head light

HTML

<div id="info">Head Light</div>
<script src="http://cdnjs.cloudflare.com/ajax/libs/three.js/r70/three.min.js"></script>
<script src="https://dl.dropboxusercontent.com/u/3587259/Code/Threejs/OrbitControls.js">
    
</script>
<script src="http://jyunming-chen.github.io/tutsplus/js/KeyboardState.js"></script>

CSS

#info {
    position: absolute;
    top: 0px;
    width: 100%;
    padding: 10px;
    text-align: center;
    color: #ffff00
}

JavaScript

var camera, scene, renderer, geometry, material, mesh, light, controls;
var keyboard = new KeyboardState();

// where the light is fixed in EyeCoordinate
var headPos = new THREE.Vector3(0,300,200);

init();
animate();

function init() {
    scene = new THREE.Scene();

    camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 1000);
    camera.position.z = 500;
    scene.add(camera);

    geometry = new THREE.BoxGeometry(100, 20, 20);
    material = new THREE.MeshLambertMaterial();

    mesh = new THREE.Mesh(geometry, material);
    scene.add(mesh);

    light = new THREE.PointLight(0xffffff);
    light.position.copy (headPos);
    scene.add(light);

    var gridXZ = new THREE.GridHelper(100, 10);
    gridXZ.setColors(new THREE.Color(0xff0000), new THREE.Color(0xffffff));
    scene.add(gridXZ);

    renderer = new THREE.WebGLRenderer();
    renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.setClearColor(0x888888);

    controls = new THREE.OrbitControls(camera, renderer.domElement);

    document.body.appendChild(renderer.domElement);
}

function animate() {
    controls.update();
    keyboard.update();

    // apply transform of camera to get (light's) world coordinate
    light.position.copy (headPos.clone().applyMatrix4(camera.matrix));
    
    requestAnimationFrame(animate);
    render();
}

function render() {
    renderer.render(scene, camera);
}

// important to add this 
// in jsfiddle!
window.focus();