webgl_loader_sea3d_sound.html 10.5 KB
Newer Older
J
Jean Carlo Deconto 已提交
1 2 3
<!DOCTYPE html>
<html lang="en">
	<head>
S
sunag 已提交
4
		<title>three.js webgl - sea3d / sound</title>
J
Jean Carlo Deconto 已提交
5 6
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
M
Mr.doob 已提交
7
		<link type="text/css" rel="stylesheet" href="main.css">
J
Jean Carlo Deconto 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
		<style>
			#blocker {
				position: absolute;
				width: 100%;
				height: 100%;
				background-color: rgba(0,0,0,0.5);
			}

			#instructions {

				width: 100%;
				height: 100%;

				display: -webkit-box;
				display: -moz-box;
				display: box;

				-webkit-box-orient: horizontal;
				-moz-box-orient: horizontal;
				box-orient: horizontal;

				-webkit-box-pack: center;
				-moz-box-pack: center;
				box-pack: center;

				-webkit-box-align: center;
				-moz-box-align: center;
				box-align: center;

				color: #ffffff;
				text-align: center;

				cursor: pointer;

			}
		</style>
	</head>
	<body>
		<div id="info">
M
Mr.doob 已提交
47
			<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - Exported by <a href="https://github.com/sunag/sea3d" target="_blank" rel="noopener">SEA3D Exporter</a> and edited by <a href="https://github.com/sunag/sea3d"  target="_blank" rel="noopener">SEA3D Studio</a>
J
Jean Carlo Deconto 已提交
48 49 50 51 52 53 54 55 56 57 58
		</div>

		<div id="blocker">

			<div id="instructions">
				<span style="font-size:40px">Click to play</span>
				<br />
				(W, A, S, D = Move, MOUSE = Look around)
			</div>

		</div>
M
Mr.doob 已提交
59

S
sunag 已提交
60 61
		<script type="module">

M
Mr.doob 已提交
62
			import * as THREE from '../build/three.module.js';
S
sunag 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76

			import { PointerLockControls } from './jsm/controls/PointerLockControls.js';

			import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
			import { RenderPass } from './jsm/postprocessing/RenderPass.js';
			import { ShaderPass } from './jsm/postprocessing/ShaderPass.js';
			import { CopyShader } from './jsm/shaders/CopyShader.js';
			import { ColorCorrectionShader } from './jsm/shaders/ColorCorrectionShader.js';
			import { VignetteShader } from './jsm/shaders/VignetteShader.js';

			import { SEA3D } from './jsm/loaders/sea3d/SEA3DLoader.js';
			import './jsm/loaders/sea3d/SEA3DLZMA.js'; // sea3d lzma extension

			import Stats from './jsm/libs/stats.module.js';
J
Jean Carlo Deconto 已提交
77

S
sunag 已提交
78
			console.log( "Visit https://github.com/sunag/sea3d to all codes and builds under development." );
S
sunag 已提交
79

J
Jean Carlo Deconto 已提交
80
			var container, raycaster, stats;
J
Jean Carlo Deconto 已提交
81 82 83 84 85

			var camera, scene, renderer, composer, controls, velocity;
			var blocker, instructions;
			var moveLeft, moveForward, moveBackward, moveRight;
			var loader;
S
sunag 已提交
86 87
			var audioListener, soundFilter, soundAreaAnalyser, soundOutsideAnalyser;
			var soundArea, collisionArea, lightArea, lightOutside;
J
Jean Carlo Deconto 已提交
88 89 90 91 92

			// Initialize Three.JS

			initPointerLock();
			initKeyDown();
M
Mr.doob 已提交
93
			init();
J
Jean Carlo Deconto 已提交
94 95 96 97 98

			//
			// SEA3D Loader
			//

S
sunag 已提交
99
			loader = new SEA3D( {
J
Jean Carlo Deconto 已提交
100

S
sunag 已提交
101 102
				autoPlay: true, // Auto play animations
				container: scene // Container to add models
J
Jean Carlo Deconto 已提交
103 104 105

			} );

106
			loader.onComplete = function () {
J
Jean Carlo Deconto 已提交
107 108 109

				audioListener = loader.audioListener;

S
sunag 已提交
110 111
				// sound filter

J
Jean Carlo Deconto 已提交
112 113 114
				soundFilter = audioListener.context.createBiquadFilter();
				soundFilter.type = 'lowpass';
				soundFilter.Q.value = 10;
M
Mr.doob 已提交
115
				soundFilter.frequency.value = 440;
J
Jean Carlo Deconto 已提交
116

S
sunag 已提交
117 118
				// sound asset 1

S
sunag 已提交
119
				lightOutside = loader.getLight( "Light1" );
S
sunag 已提交
120

S
sunag 已提交
121
				var soundOutside = loader.getSound3D( "Point001" );
M
Mr.doob 已提交
122
				soundOutsideAnalyser = new THREE.AudioAnalyser( soundOutside, 32 );
S
sunag 已提交
123 124 125

				// sound asset 2 + area

S
sunag 已提交
126
				lightArea = loader.getLight( "Light2" );
S
sunag 已提交
127

S
sunag 已提交
128
				soundArea = loader.getSound3D( "Point002" );
M
Mr.doob 已提交
129
				soundAreaAnalyser = new THREE.AudioAnalyser( soundArea, 512 );
S
sunag 已提交
130

S
sunag 已提交
131
				collisionArea = loader.getMesh( "Torus003" );
J
Jean Carlo Deconto 已提交
132

J
Jean Carlo Deconto 已提交
133 134 135 136
				animate();

			};

J
Jean Carlo Deconto 已提交
137
			loader.load( './models/sea3d/sound.tjs.sea' );
J
Jean Carlo Deconto 已提交
138 139 140 141 142 143 144

			//

			function initPointerLock() {

				blocker = document.getElementById( 'blocker' );
				instructions = document.getElementById( 'instructions' );
M
Mr.doob 已提交
145

J
Jean Carlo Deconto 已提交
146 147 148 149 150 151 152 153
				// http://www.html5rocks.com/en/tutorials/pointerlock/intro/

				var havePointerLock = 'pointerLockElement' in document || 'mozPointerLockElement' in document || 'webkitPointerLockElement' in document;

				if ( havePointerLock ) {

					var element = document.body;

154
					var pointerlockchange = function () {
J
Jean Carlo Deconto 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175

						if ( document.pointerLockElement === element || document.mozPointerLockElement === element || document.webkitPointerLockElement === element ) {

							controls.enabled = true;

							blocker.style.display = 'none';

						} else {

							controls.enabled = false;

							blocker.style.display = '-webkit-box';
							blocker.style.display = '-moz-box';
							blocker.style.display = 'box';

							instructions.style.display = '';

						}

					};

176
					var pointerlockerror = function () {
J
Jean Carlo Deconto 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190

						instructions.style.display = '';

					};

					// Hook pointer lock state change events
					document.addEventListener( 'pointerlockchange', pointerlockchange, false );
					document.addEventListener( 'mozpointerlockchange', pointerlockchange, false );
					document.addEventListener( 'webkitpointerlockchange', pointerlockchange, false );

					document.addEventListener( 'pointerlockerror', pointerlockerror, false );
					document.addEventListener( 'mozpointerlockerror', pointerlockerror, false );
					document.addEventListener( 'webkitpointerlockerror', pointerlockerror, false );

191
					instructions.addEventListener( 'click', function () {
J
Jean Carlo Deconto 已提交
192 193 194 195 196

						instructions.style.display = 'none';

						// Ask the browser to lock the pointer
						element.requestPointerLock = element.requestPointerLock || element.mozRequestPointerLock || element.webkitRequestPointerLock;
197
						element.requestPointerLock();
J
Jean Carlo Deconto 已提交
198 199 200 201 202 203 204 205 206 207

					}, false );

				} else {

					instructions.innerHTML = 'Your browser doesn\'t seem to support Pointer Lock API';

				}

			}
M
Mr.doob 已提交
208

J
Jean Carlo Deconto 已提交
209 210
			function initKeyDown() {

M
Mr.doob 已提交
211
				function onKeyDown( event ) {
J
Jean Carlo Deconto 已提交
212 213 214 215 216 217 218 219 220 221

					switch ( event.keyCode ) {

						case 38: // up
						case 87: // w
							moveForward = true;
							break;

						case 37: // left
						case 65: // a
M
Mr.doob 已提交
222
							moveLeft = true;
J
Jean Carlo Deconto 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236
							break;

						case 40: // down
						case 83: // s
							moveBackward = true;
							break;

						case 39: // right
						case 68: // d
							moveRight = true;
							break;

					}

237
				}
J
Jean Carlo Deconto 已提交
238

M
Mr.doob 已提交
239
				function onKeyUp( event ) {
J
Jean Carlo Deconto 已提交
240

M
Mr.doob 已提交
241
					switch ( event.keyCode ) {
J
Jean Carlo Deconto 已提交
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264

						case 38: // up
						case 87: // w
							moveForward = false;
							break;

						case 37: // left
						case 65: // a
							moveLeft = false;
							break;

						case 40: // down
						case 83: // s
							moveBackward = false;
							break;

						case 39: // right
						case 68: // d
							moveRight = false;
							break;

					}

265
				}
J
Jean Carlo Deconto 已提交
266 267 268

				document.addEventListener( 'keydown', onKeyDown, false );
				document.addEventListener( 'keyup', onKeyUp, false );
M
Mr.doob 已提交
269

J
Jean Carlo Deconto 已提交
270
			}
M
Mr.doob 已提交
271

J
Jean Carlo Deconto 已提交
272 273
			function init() {

M
Mr.doob 已提交
274
				raycaster = new THREE.Raycaster();
J
Jean Carlo Deconto 已提交
275

M
Mr.doob 已提交
276 277
				scene = new THREE.Scene();
				scene.background = new THREE.Color( 0x333333 );
278

M
Mr.doob 已提交
279
				velocity = new THREE.Vector3();
M
Mr.doob 已提交
280

J
Jean Carlo Deconto 已提交
281 282 283
				container = document.createElement( 'div' );
				document.body.appendChild( container );

M
Mr.doob 已提交
284
				camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
M
Mr.doob 已提交
285

S
sunag 已提交
286
				controls = new PointerLockControls( camera );
J
Jean Carlo Deconto 已提交
287
				scene.add( controls.getObject() );
M
Mr.doob 已提交
288 289

				controls.getObject().translateX( 250 );
J
Jean Carlo Deconto 已提交
290
				controls.getObject().translateZ( 250 );
M
Mr.doob 已提交
291

M
Mr.doob 已提交
292
				renderer = new THREE.WebGLRenderer();
J
Jean Carlo Deconto 已提交
293 294 295 296 297
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				container.appendChild( renderer.domElement );

				stats = new Stats();
M
Mr.doob 已提交
298
				container.appendChild( stats.dom );
J
Jean Carlo Deconto 已提交
299 300 301

				// post-processing

S
sunag 已提交
302
				composer = new EffectComposer( renderer );
J
Jean Carlo Deconto 已提交
303

S
sunag 已提交
304 305
				var renderPass = new RenderPass( scene, camera );
				var copyPass = new ShaderPass( CopyShader );
J
Jean Carlo Deconto 已提交
306 307 308 309
				composer.addPass( renderPass );

				var vh = 1.4, vl = 1.2;

S
sunag 已提交
310
				var colorCorrectionPass = new ShaderPass( ColorCorrectionShader );
M
Mr.doob 已提交
311 312
				colorCorrectionPass.uniforms[ "powRGB" ].value = new THREE.Vector3( vh, vh, vh );
				colorCorrectionPass.uniforms[ "mulRGB" ].value = new THREE.Vector3( vl, vl, vl );
J
Jean Carlo Deconto 已提交
313 314
				composer.addPass( colorCorrectionPass );

S
sunag 已提交
315
				var vignettePass = new ShaderPass( VignetteShader );
J
Jean Carlo Deconto 已提交
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
				vignettePass.uniforms[ "darkness" ].value = 1.0;
				composer.addPass( vignettePass );

				composer.addPass( copyPass );

				// events

				window.addEventListener( 'resize', onWindowResize, false );

			}


			function onWindowResize() {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

M
Mr.doob 已提交
333
				composer.setSize( window.innerWidth, window.innerHeight );
J
Jean Carlo Deconto 已提交
334 335 336 337 338 339 340
				renderer.setSize( window.innerWidth, window.innerHeight );

			}

			//

			function animateCamera( delta ) {
M
Mr.doob 已提交
341

J
Jean Carlo Deconto 已提交
342 343 344 345 346 347 348 349 350 351 352 353 354
				var scale = 1400;

				velocity.x -= velocity.x * 10.0 * delta;
				velocity.z -= velocity.z * 10.0 * delta;

				velocity.y -= 9.8 * 100.0 * delta; // 100.0 = mass

				if ( moveForward ) velocity.z -= scale * delta;
				if ( moveBackward ) velocity.z += scale * delta;

				if ( moveLeft ) velocity.x -= scale * delta;
				if ( moveRight ) velocity.x += scale * delta;

M
Mr.doob 已提交
355
				controls.getObject().translateX( velocity.x * delta );
J
Jean Carlo Deconto 已提交
356 357 358
				controls.getObject().translateZ( velocity.z * delta );

			}
J
Jean Carlo Deconto 已提交
359

M
Mr.doob 已提交
360 361 362
			var clock = new THREE.Clock();
			var audioPos = new THREE.Vector3();
			var audioRot = new THREE.Euler();
J
Jean Carlo Deconto 已提交
363

S
sunag 已提交
364
			function updateSoundFilter() {
J
Jean Carlo Deconto 已提交
365 366

				// difference position between sound and listener
M
Mr.doob 已提交
367
				var difPos = new THREE.Vector3().setFromMatrixPosition( soundArea.matrixWorld ).sub( audioPos );
J
Jean Carlo Deconto 已提交
368 369 370 371 372 373
				var length = difPos.length();

				// pick a vector from camera to sound
				raycaster.set( audioPos, difPos.normalize() );

				// intersecting sound1
S
sunag 已提交
374
				if ( length > 50 && raycaster.intersectObjects( [ collisionArea ] ).length ) {
J
Jean Carlo Deconto 已提交
375

S
sunag 已提交
376
					if ( soundArea.getFilters()[ 0 ] !== soundFilter ) soundArea.setFilters( [ soundFilter ] );
J
Jean Carlo Deconto 已提交
377

S
sunag 已提交
378
				} else if ( soundArea.getFilters()[ 0 ] === soundFilter ) {
J
Jean Carlo Deconto 已提交
379

S
sunag 已提交
380
					soundArea.setFilters( [] );
J
Jean Carlo Deconto 已提交
381 382 383 384 385 386 387

				}

			}

			//

J
Jean Carlo Deconto 已提交
388 389 390
			function animate() {

				var delta = clock.getDelta();
M
Mr.doob 已提交
391

J
Jean Carlo Deconto 已提交
392
				animateCamera( delta );
J
Jean Carlo Deconto 已提交
393 394 395 396

				// Sound3D Spatial Transform Update
				loader.audioListener.position.copy( audioPos.setFromMatrixPosition( camera.matrixWorld ) );
				loader.audioListener.rotation.copy( audioRot.setFromRotationMatrix( camera.matrixWorld ) );
J
Jean Carlo Deconto 已提交
397

M
Mr.doob 已提交
398
				// Update sound filter from raycaster intersecting
S
sunag 已提交
399 400 401
				updateSoundFilter();

				// light intensity from sound amplitude
S
sunag 已提交
402
				lightOutside.intensity = soundOutsideAnalyser.getAverageFrequency() / 100;
403
				lightArea.intensity = soundAreaAnalyser.getAverageFrequency() / 50;
J
Jean Carlo Deconto 已提交
404 405

				// Update SEA3D Animations
S
sunag 已提交
406
				SEA3D.AnimationHandler.update( delta );
J
Jean Carlo Deconto 已提交
407

J
Jean Carlo Deconto 已提交
408 409 410
				render( delta );

				stats.update();
M
Mr.doob 已提交
411

J
Jean Carlo Deconto 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425
				requestAnimationFrame( animate );

			}

			function render( delta ) {

				//renderer.render( scene, camera );
				composer.render( delta );

			}

		</script>
	</body>
</html>