-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.html
293 lines (264 loc) · 9.66 KB
/
index.html
1
2
3
4
5
6
7
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<!doctype html>
<html manifest="manifest.appcache">
<!-- <html> -->
<title>WebGL Video Chat</title>
<script src="vendor/tquery/build/tquery-bundle-require.js"></script>
<body>
<div style="position: absolute; font-size: 200%; font-weight: bolder; right: 1em; bottom: 0.5em;" id='uiContainer'>
<form action="javascript:void(0)" id='roomNameForm' style='display: inline-block'>
<div style='float: right'>
<input type='button' class='butRoulette' value='roulette'/>
<input type='button' class='butRandom' value='random' />
</div>
<span>ROOM :</span>
<input type="text" class='roomName' size='10' style='font-size: inherit;'/>
</form>
</div><div id="info" style='color: white'>
WebGL Video Chat
-
a video chat in WebGL with WebRTC
by <a href='http://twitter.com/jerome_etienne' target='_blank'>@jerome_etienne</a>
<br/>
<a href='https://github.com/mrdoob/three.js/' target='_blank'>three.js</a> with
<a href='http://jeromeetienne.github.com/tquery/' target='_blank'>tQuery API</a> for 3D
-
<a href='https://github.com/webRTC/webRTC.io/' target='_blank'>webrtc.io</a> for WebRTC
- hosted on <a href='http://nodejitsu.com/' target='_blank'>nodejitsu</a>
</div><script>
requirejs.config({
paths : {
'build' : 'vendor/tquery/build',
'plugins' : 'vendor/tquery/plugins',
'threex' : 'vendor/tquery/vendor/threex',
'three.js' : 'vendor/tquery/vendor/three.js',
},
});
require([ 'tquery.loaders'
, 'tquery.checkerboard'
, 'tquery.pproc'
, 'tquery.fog'
, 'tquery.shadowmap'
, 'tquery.videos'
, 'tquery.webrtcio'
], function(){
var world = tQuery.createWorld().boilerplate({
screenshot : false,
fullscreen : false
}).pageTitle('#info').start()
world.shadowMapEnabled(true)
world.camera().positionY(0.3).positionZ(1.5)
var target = world.scene().position().clone().add(tQuery.createVector3(0,0,-1))
world.camera().lookAt(target);
world.removeCameraControls();
world.tRenderer().setClearColorHex( 0x000000, 1 );
// add the fog
world.addFogExp2({
density : 0.3
});
//////////////////////////////////////////////////////////////////////////////////
// add ground //
//////////////////////////////////////////////////////////////////////////////////
tQuery.createCheckerboard({
segmentsW : 60,
segmentsH : 60
}).addTo(world)
.scaleBy(20)
.receiveShadow(true);
//////////////////////////////////////////////////////////////////////////////////
// add some lights //
//////////////////////////////////////////////////////////////////////////////////
tQuery.createAmbientLight().addTo(world)
.color(0x444444)
tQuery.createDirectionalLight().addTo(world)
.position(4,2,4)
.color(0xcccccc)
.castShadow(true)
.shadowDarkness(0.8)
.shadowMap(512*2,512*2)
.shadowCamera(3, -3, 3, -3, 0.1, 30)
//.shadowCameraVisible(true)
//////////////////////////////////////////////////////////////////////////////////
// load the model //
//////////////////////////////////////////////////////////////////////////////////
var loader = new THREE.ColladaLoader();
var tvSetModel = null;
loader.options.convertUpAxis = true;
var modelUrl = 'models/OldTelevisionSet01/models/Old Television Set 01.dae';
loader.load(modelUrl, function(collada){
tvSetModel = tQuery(collada.scene);
// set castShadow
tQuery('mesh', tvSetModel).castShadow(true)
// notify everybody
if( location.hash === '#roulette'){
onModelLoadedRoulette();
}else{
onModelLoaded();
}
});
//////////////////////////////////////////////////////////////////////////////////
// comment //
//////////////////////////////////////////////////////////////////////////////////
rtc.on('connect', function(){
console.log('connected to server')
})
rtc.on('connections', function(connections){
console.log('received connections event', connections)
})
rtc.on('add remote stream', function(mediaStream, socketId) {
createScreen(mediaStream, 'socketId-'+socketId)
.addClass('socketId-'+socketId)
reflowScreens();
})
rtc.on('disconnect stream', function(socketId) {
// get the disconnected screen
var screen = tQuery('.socketId-'+socketId);
console.assert( screen.length === 1);
// detach the screen from the scene
screen.detach();
// get the video texture and stop
var plane = tQuery('.plane', screen);
var texture = plane.get(0).material.map;
texture.poorlyCodedClose();
// reflow the screens
reflowScreens();
})
//////////////////////////////////////////////////////////////////////////////////
// comment //
//////////////////////////////////////////////////////////////////////////////////
// determine roomName
location.hash = location.hash || 'room-' + Math.floor((Math.random()) * 0x10000).toString(16);
var roomName = location.hash.slice(1).match(/^(.*)-?/)[0];
var nostream = location.hash.slice(1).match(/-nostream$/) ? true : false;
// set the input value
document.querySelector('#roomNameForm .roomName').value = roomName;
// handle roomNameForm
var roomNameForm = document.querySelector('#roomNameForm')
roomNameForm.addEventListener('submit', function(){
roomName = roomNameForm[0].value;
location.hash = '#' + roomName;
location.reload();
});
document.querySelector('#roomNameForm .butRandom').addEventListener('click', function(){
roomName = 'room-' + Math.floor((Math.random()) * 0x10000).toString(16);
location.hash = '#' + roomName;
location.reload();
});
document.querySelector('#roomNameForm .butRoulette').addEventListener('click', function(){
location.hash = '#roulette';
location.reload();
});
//////////////////////////////////////////////////////////////////////////////////
// comment //
//////////////////////////////////////////////////////////////////////////////////
function onModelLoadedRoulette(){
var request = new XMLHttpRequest();
request.onload = function(){
var response = JSON.parse(this.responseText)
roomName = response.roomName;
// set the input value
document.querySelector('#roomNameForm .roomName').value = roomName;
onModelLoaded();
}
var url = '/rouletteRoomName?'+Math.floor((Math.random()) * 0x10000).toString(16)
request.open("GET", url, true);
request.send();
}
function onModelLoaded(){
// connect the server
var serverUrl = 'ws://' + location.host + location.pathname;
rtc.connect(serverUrl, roomName);
// log to debug
console.log('connecting to server', serverUrl, 'in room', roomName)
// honor nostream
if( nostream ) return;
// create a local stream
rtc.createStream({'video': true, 'audio': false}, function(mediaStream){
console.log('created stream', arguments)
createScreen(mediaStream)
reflowScreens();
}, function(){
console.log('createStream failed', arguments)
});
}
/**
* adapt screens layout depending on their numbers
*/
function reflowScreens(){
var screens = tQuery('.screen');
var nScreens = screens.length;
var offsetX = -(screens.length - 1)/2
var width = 0.7
screens.each(function(tObject3D, idx){
tObject3D.position.x = width * (idx + offsetX);
})
// set camera position depending on nScreens
var camera = world.camera();
if( nScreens === 0 || nScreens === 1 || nScreens === 2 ){
camera.positionZ( 1.5 );
}else if( nScreens === 3 ){
camera.positionZ( 2.0 );
}else if( nScreens === 4 ){
camera.positionZ( 2.5 );
}else if( nScreens === 5 ){
camera.positionZ( 3.5 );
}else if( nScreens === 6 ){
camera.positionZ( 4.0 );
}else if( nScreens === 7 ){
camera.positionZ( 4.5 );
}else if( nScreens === 8 ){
camera.positionZ( 5 );
}else if( nScreens === 9 ){
camera.positionZ( 5.5 );
}else if( nScreens === 10 ){
camera.positionZ( 6 );
}
}
/**
* create a 3d screen for a mediaStream
* @param {[type]} mediaStream [description]
* @return {tQuery.Mesh} the created tQuery.Mesh
*/
function createScreen(mediaStream){
// create the object3d
var container = tQuery.createObject3D().addTo(world)
.addClass('screen')
// add the tvset
tvSetModel.clone().addTo(container)
.positionY(-0.4)
.addClass('tvSet')
// create the plane of the screen
var url = URL.createObjectURL(mediaStream);
var texture = tQuery.createVideoTexture(url);
tQuery.createPlane().addTo(container)
.addClass('plane')
.setBasicMaterial()
.map(texture)
.back()
.positionY(0.5).positionY(0.31).positionZ(0.16)
.scaleX(1/2.2).scaleY(1/2.85)
// return container
return container;
}
});
</script>
<!-- github ribbon -->
<a href="https://github.com/jeromeetienne/webglvideochat" target='_blank'><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png" alt="Fork me on GitHub"></a>
<!-- share buttons -->
<div style='position: absolute; margin: 10px; top: 0px'>
<!-- twitter share button -->
<a href="https://twitter.com/share" class="twitter-share-button" data-text='WebGLVideoChat is a video chat in webgl with webrtc. Check it out!' data-via="jerome_etienne">Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
<br/>
<!-- google plus button -->
<!-- Place this tag where you want the +1 button to render. -->
<div class="g-plusone" data-size="small"></div>
<!-- Place this tag after the last +1 button tag. -->
<script type="text/javascript">
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/plusone.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
</script>
</div>
</body></html>