While responding to the page events can work for simple scene interaction, there are some tasks which wont work well for example user creating a typical
first person shooter game in which making the controls is very difficult and contain some tricky math problems most of the people not able clear the problems.
In
Three.JS example folder there are number of different control libraries to utilize which probably cover the main types of controls which are using, some controls are listed below.
Libraries in examples\js\controls
- Device Orientation
- Editor
- Fly
- FirstPerson
- Oculus
- Orbit
- Path
- pointerLock
- Trackball
- Transform
Now look at how to integrate two common control libraries which are the Orbit and Fly.
Orbit
Orbit controls are the great and which allows user to explore an object or model. For example user need to explain about product by rotating and zooming in and out for which user need to use the Orbit controls.
The code below demonstrates a cube which has different colors for each sides. User can rotate the cube by using the left mouse button and camera view point explore the object and mouse wheel is used to zoom in and zoom out the object.
app.js
[c]
var example = (function() {
"use strict";
var scene = new THREE.Scene(),
renderer = window.WebGLRenderingContext ? new THREE.WebGLRenderer() : new THREE.CanvasRenderer(),
light = new THREE.AmbientLight(0xffffff),
camera,
controls,
box;
function initScene() {
renderer.setSize(window.innerWidth, window.innerHeight);
document.getElementById("webgl-container").appendChild(renderer.domElement);
scene.add(light);
camera = new THREE.PerspectiveCamera(
35,
window.innerWidth / window.innerHeight,
1,
1000
);
camera.position.z = 100;
scene.add(camera);
box = new THREE.Mesh(
new THREE.BoxGeometry(
20,
20,
20),
new THREE.MeshBasicMaterial({
vertexColors: THREE.VertexColors
}));
assignColorsToCube(box);
scene.add(box);
controls = new THREE.OrbitControls(camera);
controls.addEventListener('change', render);
render();
}
function assignColorsToCube(cube) {
var colors = [
new THREE.Color("rgb(255,0,0)"),
new THREE.Color("rgb(0,255,0)"),
new THREE.Color("rgb(0,0,255)"),
new THREE.Color("rgb(255,255,0)"),
new THREE.Color("rgb(0,255,255)"),
new THREE.Color("rgb(255,0,255)")
];
for (var i = 0; i < 12; i += 2) {
var color = colors[i / 2];
//each cube face is made up of 2 triangles & we want same color for each
cube.geometry.faces[i].color = color;
cube.geometry.faces[i + 1].color = color;
}
}
function render() {
renderer.render(scene, camera);
}
window.onload = initScene;
return {
scene: scene
}
})();
[/c]
index.html
[c]
<!DOCTYPE html>
<html>
<head>
<title>WebGL with Three.js Fundamentals</title>
</head>
<body>
<div id="webgl-container"></div>
<script src="scripts/three.js"></script>
<script src="scripts/OrbitControls.js"></script>
<script src="scripts/app.js"></script>
</body>
</html>
[/c]
Result
By running the above code in a preferred browser the user will get the output as shown below.
Fly
Implementing Fly controls is very similar to the orbit controls. First user reference to the
FlyControl.js Library then create new control variables to hold the Fly controls and create new
THREE.Clock now initialize the total controls.
A new
THREE.FlyControls instance passing in the camera and user need to set some options like movement and roll speed which is shown in the below demonstrated code.
app.js
[c]
var example = (function() {
"use strict";
var scene = new THREE.Scene(),
renderer = window.WebGLRenderingContext ? new THREE.WebGLRenderer() : new THREE.CanvasRenderer(),
light = new THREE.AmbientLight(0xffffff),
camera,
controls,
clock = new THREE.Clock(),
plane,
box;
function initScene() {
renderer.setSize(window.innerWidth, window.innerHeight);
document.getElementById("webgl-container").appendChild(renderer.domElement);
scene.add(light);
camera = new THREE.PerspectiveCamera(
35,
window.innerWidth / window.innerHeight,
1,
1000
);
camera.position.z = 300;
scene.add(camera);
box = new THREE.Mesh(new THREE.BoxGeometry(20, 20, 20), new THREE.MeshBasicMaterial({
color: 0x0000FF,
}));
//ground
var texture = THREE.ImageUtils.loadTexture('content/grasslight-big.jpg') //texture from three.js examples
var planeMaterial = new THREE.MeshPhongMaterial({
map: texture,
side: THREE.DoubleSide
});
plane = new THREE.Mesh(new THREE.PlaneGeometry(800, 800), planeMaterial);
plane.rotation.x = 90 * (Math.PI / 180);
plane.position.y = -10;
plane.name = "plane";
scene.add(plane);
scene.add(box);
controls = new THREE.FlyControls(camera);
controls.movementSpeed = 100;
controls.domElement = document.getElementById("webgl-container");
controls.rollSpeed = Math.PI / 24;
render();
}
function render() {
var delta = clock.getDelta();
controls.update(delta);
renderer.render(scene, camera);
requestAnimationFrame(render);
}
window.onload = initScene;
return {
scene: scene
}
})();
[/c]
index.html
[c]
<!DOCTYPE html>
<html>
<head>
<title>WebGL with Three.js Fundamentals</title>
</head>
<body>
<div id="webgl-container"></div>
<script src="scripts/three.js"></script>
<script src="scripts/FlyControls.js"></script>
<script src="scripts/app.js"></script>
</body>
</html>
[/c]
Result
By running the above code in a preferred browser, user will get the output as shown below.