Skip to content

Commit b83e85b

Browse files
authored
Merge pull request #182 from WhitestormJS/#77
#77 Merge audio module
2 parents 721bb7f + 5d52b4c commit b83e85b

File tree

10 files changed

+360
-0
lines changed

10 files changed

+360
-0
lines changed

examples/assets/sounds/folk.mp3

6.36 MB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
extends ../../layout.pug
2+
3+
block additional
4+
script(src='../../assets/modules/StatsModule.js')
5+
script(src='../../../modules/whs-module-audio/build/AudioModule.js')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import * as UTILS from '../../globals';
2+
3+
const controlsModule = new WHS.controls.OrbitModule();
4+
5+
const cameraModule = new WHS.app.CameraModule({
6+
position: {
7+
z: 250,
8+
y: 100
9+
},
10+
11+
far: 30000,
12+
near: 1
13+
});
14+
15+
const world = new WHS.App([
16+
...UTILS.appModules({
17+
position: new THREE.Vector3(0, 10, 200)
18+
}),
19+
20+
controlsModule,
21+
cameraModule
22+
]);
23+
24+
controlsModule.controls.autoRotate = true;
25+
26+
const audioModule = new AudioModule({
27+
loop: true
28+
});
29+
30+
const sphere = new WHS.Sphere({
31+
geometry: {
32+
radius: 20
33+
},
34+
35+
shadow: {
36+
cast: false
37+
},
38+
39+
material: new THREE.MeshBasicMaterial({
40+
color: 0xffffff
41+
}),
42+
43+
modules: [
44+
audioModule
45+
],
46+
47+
position: {
48+
y: 5
49+
}
50+
51+
});
52+
sphere.addTo(world);
53+
54+
audioModule.addListener(cameraModule.camera);
55+
audioModule.playAudio(`${process.assetsPath}/sounds/folk.mp3`);
56+
57+
new WHS.PointLight({
58+
light: {
59+
color: 0xffffff,
60+
intensity: 1,
61+
distance: 1000
62+
},
63+
64+
position: [10, 40, 10]
65+
}).addTo(world);
66+
67+
new WHS.Box({
68+
geometry: {
69+
width: 2000,
70+
height: 0.1,
71+
depth: 2000
72+
},
73+
74+
shadow: {
75+
cast: false
76+
},
77+
78+
material: new THREE.MeshPhongMaterial({
79+
color: 0xffffff
80+
}),
81+
82+
position: [0, -60, 0],
83+
rotation: [0, 0, 25]
84+
}).addTo(world);
85+
86+
world.start();

gulp/dev.js

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ gulp.task('dev', () => {
4848
});
4949

5050
app.use('/assets', express.static(path.resolve(__dirname, `${examples}/assets`)));
51+
app.use('/modules', express.static('modules'));
5152

5253
app.set('views', path.resolve(__dirname, `./${examples}`));
5354
app.set('view engine', 'pug');

modules/.babelrc

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"presets": [
3+
"es2015"
4+
],
5+
"plugins": [
6+
"add-module-exports",
7+
"transform-decorators-legacy",
8+
"transform-class-properties",
9+
"transform-object-rest-spread"
10+
]
11+
}

modules/whs-module-audio/build/AudioModule.js

+154
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

modules/whs-module-audio/build/AudioModule.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

modules/whs-module-audio/package.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "whs-module-audio",
3+
"version": "0.0.1",
4+
"description": "Audio module for whs",
5+
"main": "build/AudioPlugin.js",
6+
"scripts": {
7+
"start": "webpack --watch",
8+
"build": "webpack",
9+
"test": "echo \"Error: no test specified\" && exit 1"
10+
},
11+
"keywords": [
12+
"whs-module",
13+
"audio"
14+
],
15+
"author": "hirako2000",
16+
"license": "MIT"
17+
}

modules/whs-module-audio/src/index.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import {
2+
PositionalAudio,
3+
AudioListener,
4+
AudioLoader
5+
} from 'three';
6+
7+
export default class PositionalAudioModule {
8+
constructor(params = {}) {
9+
this.params = Object.assign({
10+
loop: true
11+
}, params);
12+
13+
this.audioListener = new AudioListener();
14+
this.audioLoader = new AudioLoader();
15+
16+
this.positionalAudio = new PositionalAudio(this.audioListener);
17+
this.positionalAudio.setLoop(this.params.loop);
18+
}
19+
20+
addListener(object) {
21+
object.native.add(this.audioListener);
22+
};
23+
24+
playAudio(path) {
25+
const sound = this.positionalAudio;
26+
27+
this.audioLoader.load(path, buffer => {
28+
sound.setBuffer(buffer);
29+
sound.setRefDistance(50);
30+
sound.play();
31+
});
32+
};
33+
34+
bridge = {
35+
mesh(mesh, self) {
36+
mesh.add(self.positionalAudio);
37+
return mesh;
38+
}
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import path from 'path';
2+
import webpack from 'webpack';
3+
4+
process.env.BABEL_ENV = 'browser';
5+
6+
const isProduction = process.env.NODE_ENV === 'production';
7+
8+
console.log(
9+
isProduction
10+
? 'Production mode'
11+
: 'Development mode'
12+
);
13+
14+
export default {
15+
devtool: isProduction ? false : 'source-map',
16+
entry: './src/index.js',
17+
target: 'web',
18+
output: {
19+
path: path.join(__dirname, './build/'),
20+
filename: 'AudioModule.js',
21+
libraryTarget: 'umd',
22+
library: 'AudioModule'
23+
},
24+
externals: {
25+
whs: 'WHS',
26+
three: 'THREE'
27+
},
28+
module: {
29+
loaders: [
30+
{
31+
test: /\.js$/,
32+
exclude: /node_modules/,
33+
loader: 'babel-loader'
34+
}
35+
]
36+
},
37+
plugins: isProduction
38+
? [
39+
new webpack.optimize.UglifyJsPlugin({
40+
compress: {warnings: false},
41+
minimize: true
42+
})
43+
]
44+
: []
45+
};

0 commit comments

Comments
 (0)