Make folder
mkdir flyingspaceship
cd flyingspaceship
Generate boilerplate with yeoman
yo threejs-boilerplate
Serves Static Files
make server
bower install threex.spaceships
package.require.js
at project rootFind the require line and add
require([ 'bower_components/threex.spaceships/package.require.js'
, 'bower_components/threex.spaceships/examples/vendor/three.js/examples/js/loaders/OBJMTLLoader.js'
, 'bower_components/threex.spaceships/examples/vendor/three.js/examples/js/loaders/OBJLoader.js'
, 'bower_components/threex.spaceships/examples/vendor/three.js/examples/js/loaders/MTLLoader.js'
], function(){
// ...
});
put that after the scene init
var spaceship = null;
THREEx.SpaceShips.loadSpaceFighter03(function(object3d){
scene.add(object3d)
spaceship = object3d
})
var keyboard = new THREEx.KeyboardState();
if( keyboard.pressed("shift+H") ){
console.log('you are pressing shift and H')
}
Install it with bower
bower install threex.keyboardstate
Include it with require.js
require([ 'bower_components/threex.keyboardstate/package.require.js'
], function(){
// ...
});
Done... not too hard like last time
The Algo
Pitfalls
// create keyboard instance
var keyboard = new THREEx.KeyboardState();
// add function in rendering loop
onRenderFcts.push(function(delta, now){
// only if the spaceship is loaded
if( spaceship === null ) return;
// set the speed
var speed = 1;
// only if spaceships is loaded
if( keyboard.pressed('down') ){
spaceship.position.y -= speed * delta;
}else if( keyboard.pressed('up') ){
spaceship.position.y += speed * delta;
}
})
Earth, cloud, atmosphere, stars, moon, shadow
based on planetpixelemporium
bower install threex.planets
Now it is in bower_components/threex.planets/
Rather Easy...
Find require.js call in the header and put that
require([ 'bower_components/threex.planets/package.require.js'
], function(){
// ...
});
Done... not too hard
var moonMesh = THREEx.Planets.createMoon()
scene.add(moonMesh)
spaceship = object3d
spaceship.rotateY(Math.PI/2)
spaceship.position.x = -1
The Result link
Initial position
function resetMoon(){
moonMesh.position.x = 5
moonMesh.position.x += 5 * (Math.random()-0.5)
moonMesh.position.y = 2 * (Math.random()-0.5)
}
resetMoon()
Animate the moon and handle limits
onRenderFcts.push(function(delta, now){
// move the moon to the left
moonMesh.position.x += -1 * delta;
// make it warp
if( moonMesh.position.x < -3 ) resetMoon()
})
Part of threex.planets module
var starSphere = THREEx.Planets.createStarfield()
scene.add(starSphere)
onRenderFcts.push(function(delta, now){
// only if the spaceship is loaded
if( spaceship === null ) return
// compute distance between spaceship and the moon
var distance = moonMesh.position.distanceTo(spaceship.position)
if( distance < 0.3 ){
resetMoon()
}
})
bower install webaudiox
now files are in bower_components/webaudiox
as usual
require([ 'bower_components/webaudiox/build/webaudiox.js'
], function(){
// ...
});
var context = new AudioContext()
var lineOut = new WebAudiox.LineOut(context)
lineOut.volume = 0.2
var soundBuffer;
// load the sound
var soundUrl = 'sounds/102720__sarge4267__explosion.wav'
WebAudiox.loadBuffer(context, soundUrl, function(buffer){
soundBuffer = buffer
})
// setup a play function
function playExplosionSound(){
if( !soundBuffer ) return
var source = context.createBufferSource()
source.buffer = soundBuffer
source.connect(lineOut.destination)
source.start(0)
return source
}
var distance = moonMesh.position.distanceTo(spaceship.position)
if( distance < 0.3 ){
resetMoon()
playExplosionSound()
}