mirror of
https://github.com/wahyd4/three.js.git
synced 2026-08-09 04:56:01 +10:00
207 lines
4.9 KiB
HTML
207 lines
4.9 KiB
HTML
<!DOCTYPE HTML>
|
|
<html lang="en">
|
|
<head>
|
|
<title>three.js misc - sound</title>
|
|
<meta charset="utf-8">
|
|
<style type="text/css">
|
|
body {
|
|
background-color: #000000;
|
|
margin: 0px;
|
|
overflow: hidden;
|
|
font-family:Monospace;
|
|
font-size:13px;
|
|
text-align:center;
|
|
font-weight: bold;
|
|
text-align:center;
|
|
}
|
|
|
|
a {
|
|
color:#0078ff;
|
|
}
|
|
|
|
#info {
|
|
color:#fff;
|
|
position: absolute;
|
|
top: 0px; width: 100%;
|
|
padding: 5px;
|
|
z-index:100;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
|
|
<div id="info">
|
|
<a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - webgl 3d sounds example -
|
|
music by <a href="http://www.newgrounds.com/audio/listen/358232" target="_blank">larrylarrybb</a> and
|
|
<a href="http://www.newgrounds.com/audio/listen/376737" target="_blank">skullbeatz</a> <br/><br/>
|
|
|
|
navigate with WASD / arrows / mouse
|
|
</div>
|
|
|
|
|
|
<div id="container"></div>
|
|
|
|
<script type="text/javascript" src="../build/Three.js"></script>
|
|
|
|
<script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
|
|
<script type="text/javascript" src="js/Detector.js"></script>
|
|
|
|
<script type="text/javascript">
|
|
|
|
if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
|
|
|
|
var container;
|
|
var camera, scene, renderer;
|
|
var light, pointLight;
|
|
|
|
var mesh;
|
|
var material_sphere1, material_sphere2;
|
|
|
|
var sound1, sound2;
|
|
|
|
var Sound = function ( sources, radius, volume ) {
|
|
|
|
var audio = document.createElement( 'audio' );
|
|
|
|
for ( var i = 0; i < sources.length; i ++ ) {
|
|
|
|
var source = document.createElement( 'source' );
|
|
source.src = sources[ i ];
|
|
|
|
audio.appendChild( source );
|
|
|
|
}
|
|
|
|
this.position = new THREE.Vector3();
|
|
|
|
this.play = function () {
|
|
|
|
audio.play();
|
|
|
|
}
|
|
|
|
this.update = function ( camera ) {
|
|
|
|
var distance = this.position.distanceTo( camera.position );
|
|
|
|
if ( distance <= radius ) {
|
|
|
|
audio.volume = volume * ( 1 - distance / radius );
|
|
|
|
} else {
|
|
|
|
audio.volume = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
init();
|
|
animate();
|
|
|
|
function init() {
|
|
|
|
container = document.getElementById( 'container' );
|
|
|
|
scene = new THREE.Scene();
|
|
scene.fog = new THREE.FogExp2( 0x000000, 0.0035 );
|
|
|
|
camera = new THREE.FirstPersonCamera( {
|
|
fov: 50, aspect: window.innerWidth / window.innerHeight, near: 1, far: 10000,
|
|
movementSpeed: 70, lookSpeed: 0.05, noFly: true, lookVertical: false
|
|
} );
|
|
|
|
camera.position.set( 0, 25, 0 );
|
|
|
|
light = new THREE.DirectionalLight( 0xffffff );
|
|
light.position.set( 0, 0.5, 1 );
|
|
light.position.normalize();
|
|
scene.add( light );
|
|
|
|
var sphere = new THREE.SphereGeometry( 20, 32, 16 );
|
|
|
|
material_sphere1 = new THREE.MeshLambertMaterial( { color: 0xffaa00, shading: THREE.FlatShading } );
|
|
material_sphere2 = new THREE.MeshLambertMaterial( { color: 0xff2200, shading: THREE.FlatShading } );
|
|
|
|
var cube = new THREE.CubeGeometry( 5, 40, 5 );
|
|
var material_cube = new THREE.MeshLambertMaterial( { color: 0xffff00, shading: THREE.FlatShading } );
|
|
material_cube.color.setHSV( 0.1, 0.7, 1 );
|
|
|
|
// sound spheres
|
|
|
|
var s = 1;
|
|
|
|
var mesh1 = new THREE.Mesh( sphere, material_sphere1 );
|
|
mesh1.position.set( -250, 30, 0 );
|
|
mesh1.scale.set( s, s, s );
|
|
scene.add( mesh1 );
|
|
|
|
sound1 = new Sound( [ 'sounds/358232_j_s_song.mp3', 'sounds/358232_j_s_song.ogg' ], 275, 1 );
|
|
sound1.position.copy( mesh1.position );
|
|
sound1.play();
|
|
|
|
//
|
|
|
|
var mesh2 = new THREE.Mesh( sphere, material_sphere2 );
|
|
mesh2.position.set( 250, 30, 0 );
|
|
mesh2.scale.set( s, s, s );
|
|
scene.add( mesh2 );
|
|
|
|
sound2 = new Sound( [ 'sounds/376737_Skullbeatz___Bad_Cat_Maste.mp3', 'sounds/376737_Skullbeatz___Bad_Cat_Maste.ogg' ], 275, 1 );
|
|
sound2.position.copy( mesh2.position );
|
|
sound2.play();
|
|
|
|
|
|
// ground
|
|
|
|
var material_wireframe = new THREE.MeshLambertMaterial( { color: 0xffaa00, wireframe: true, wireframeLinewidth: 1 } );
|
|
material_wireframe.color.setHSV( 0.1, 0.2, 0.25 );
|
|
|
|
|
|
var plane = new THREE.PlaneGeometry( 1000, 1000, 100, 100 );
|
|
mesh = new THREE.Mesh( plane, material_wireframe );
|
|
mesh.position.y = 0.1;
|
|
mesh.rotation.x = -1.57;
|
|
scene.add( mesh );
|
|
|
|
|
|
renderer = new THREE.WebGLRenderer( { clearColor: 0x000000, clearAlpha: 1, antialias: false } );
|
|
renderer.setSize( window.innerWidth, window.innerHeight );
|
|
|
|
container.innerHTML = "";
|
|
container.appendChild( renderer.domElement );
|
|
|
|
renderer.autoClear = false;
|
|
|
|
}
|
|
|
|
function animate() {
|
|
|
|
requestAnimationFrame( animate );
|
|
render();
|
|
|
|
}
|
|
|
|
|
|
function render() {
|
|
|
|
var time = new Date().getTime() * 0.005;
|
|
|
|
material_sphere1.color.setHSV( 0.0, 0.3 + 0.7 * ( 1 + Math.cos(time) ) / 2, 1 );
|
|
material_sphere2.color.setHSV( 0.1, 0.3 + 0.7 * ( 1 + Math.sin(time) ) / 2, 1 );
|
|
|
|
renderer.clear();
|
|
renderer.render( scene, camera );
|
|
|
|
sound1.update( camera );
|
|
sound2.update( camera );
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
</body>
|
|
</html>
|