@@ -6,37 +6,68 @@ export class WebSocketRepresentationManager {
6
6
this . rerpresentations = new Map ( ) ;
7
7
}
8
8
9
- addRepresentation ( key , threeObj ) {
9
+ AddRepresentation ( key , threeObj ) {
10
10
if ( this . rerpresentations . has ( key ) ) {
11
- throw new Error ( "representation manager already has a rep for key: " + key )
11
+ console . warn ( "representation manager already has a rep for key: " + key )
12
+ // throw new Error("representation manager already has a rep for key: " + key)
12
13
}
13
14
this . rerpresentations . set ( key , threeObj )
14
15
}
15
16
16
- removeRepresentation ( key , threeObj ) {
17
- if ( this . rerpresentations . has ( key ) ) {
18
- throw new Error ( "representation manager already has a rep for key: " + key )
17
+ RemoveRepresentation ( key , threeObj ) {
18
+ if ( ! this . rerpresentations . has ( key ) ) {
19
+ throw new Error ( "representation manager does not have a rep for key: " + key )
19
20
}
20
21
this . rerpresentations . set ( key , threeObj )
21
22
}
22
23
23
- toMessage ( ) {
24
+ ToMessage ( ) {
24
25
const message = [ ] ;
26
+
27
+ let pos = new THREE . Vector3 ( ) ;
28
+ let rot = new THREE . Quaternion ( ) ;
29
+
25
30
this . rerpresentations . forEach ( ( threeObj , key ) => {
26
- message . push ( {
27
- type : key ,
28
- "position" : {
29
- "x" : threeObj . position . x ,
30
- "y" : threeObj . position . y ,
31
- "z" : threeObj . position . z ,
32
- } ,
33
- "rotation" : {
34
- "x" : threeObj . quaternion . x ,
35
- "y" : threeObj . quaternion . y ,
36
- "z" : threeObj . quaternion . z ,
37
- "w" : threeObj . quaternion . w ,
31
+ if ( ! threeObj ) {
32
+ return ;
33
+ }
34
+
35
+ if ( ! threeObj . matrixWorld ) {
36
+ return ;
37
+ }
38
+
39
+ // Somethings wrong with the matrix world, let's not include this
40
+ // representation
41
+ const eles = threeObj . matrixWorld . elements ;
42
+ for ( let i = 0 ; i < eles . length ; i ++ ) {
43
+ if ( isNaN ( eles [ i ] ) || ! isFinite ( eles [ i ] ) ) {
44
+ return ;
38
45
}
39
- } )
46
+ }
47
+
48
+ try {
49
+ const worldMatrix = threeObj . matrixWorld ;
50
+ pos . setFromMatrixPosition ( worldMatrix )
51
+ rot . setFromRotationMatrix ( worldMatrix )
52
+ message . push ( {
53
+ type : key ,
54
+ "position" : {
55
+ "x" : pos . x ,
56
+ "y" : pos . y ,
57
+ "z" : pos . z ,
58
+ } ,
59
+ "rotation" : {
60
+ "x" : rot . x ,
61
+ "y" : rot . y ,
62
+ "z" : rot . z ,
63
+ "w" : rot . w ,
64
+ }
65
+ } )
66
+ } catch ( error ) {
67
+ console . error ( error ) ;
68
+ // Expected output: ReferenceError: nonExistentFunction is not defined
69
+ // (Note: the exact output may be browser-dependent)
70
+ }
40
71
} ) ;
41
72
return message ;
42
73
}
@@ -103,6 +134,7 @@ export class WebSocketManager {
103
134
104
135
createPlayerObject ( name , playerData ) {
105
136
const newPlayer = new THREE . Group ( ) ;
137
+ newPlayer . name = "player" ;
106
138
107
139
const sphere = new THREE . Mesh (
108
140
this . playerConfiguration . playerGeometry ,
@@ -152,6 +184,8 @@ export class WebSocketManager {
152
184
newPlayer . position . x = playerData . position . x ;
153
185
newPlayer . position . y = playerData . position . y ;
154
186
newPlayer . position . z = playerData . position . z ;
187
+
188
+ newPlayer . scale . set ( 0.25 , 0.25 , 0.25 )
155
189
this . scene . add ( newPlayer ) ;
156
190
157
191
return {
@@ -165,18 +199,53 @@ export class WebSocketManager {
165
199
} ;
166
200
}
167
201
202
+ createHandObject ( handData ) {
203
+ console . log ( "creating hand" )
204
+ const hand = new THREE . Group ( ) ;
205
+ hand . name = "hand" ;
206
+
207
+ const sphere = new THREE . Mesh (
208
+ this . playerConfiguration . playerGeometry ,
209
+ this . playerConfiguration . playerMaterial
210
+ ) ;
211
+ sphere . scale . set ( 0.1 , 0.1 , 0.1 )
212
+ hand . add ( sphere ) ;
213
+
214
+ hand . position . x = handData . position . x ;
215
+ hand . position . y = handData . position . y ;
216
+ hand . position . z = handData . position . z ;
217
+
218
+ this . scene . add ( hand ) ;
219
+
220
+ return {
221
+ desiredPosition : handData . position ,
222
+ desiredRotation : handData . rotation ,
223
+ obj : hand ,
224
+ cleanup : ( ) => {
225
+ this . scene . remove ( hand ) ;
226
+ }
227
+ } ;
228
+ }
229
+
230
+
168
231
setupPlayer ( key , playerData ) {
169
232
const resps = [ ]
170
233
171
- console . log ( playerData ) ;
172
-
173
234
if ( playerData . representation ) {
174
235
playerData . representation . forEach ( ( rep ) => {
175
236
switch ( rep . type ) {
176
237
case "player" :
177
238
resps . push ( this . createPlayerObject ( playerData . name , rep ) )
178
239
break ;
179
240
241
+ case "left-hand" :
242
+ resps . push ( this . createHandObject ( rep ) ) ;
243
+ break ;
244
+
245
+ case "right-hand" :
246
+ resps . push ( this . createHandObject ( rep ) ) ;
247
+ break ;
248
+
180
249
default :
181
250
break ;
182
251
}
@@ -203,7 +272,7 @@ export class WebSocketManager {
203
272
204
273
const q = new THREE . Quaternion ( dr . x , dr . y , dr . z , dr . w ) ;
205
274
if ( ! rep . obj . quaternion . equals ( q ) ) {
206
- rep . obj . quaternion . rotateTowards ( q , delta * 2 ) ;
275
+ rep . obj . quaternion . rotateTowards ( q , delta * 2 ) ;
207
276
}
208
277
209
278
const pp = rep . obj . position ;
@@ -260,9 +329,9 @@ export class WebSocketManager {
260
329
playersUpdated [ playerID ] = true ;
261
330
262
331
if ( playerID in this . connectedPlayers && this . connectedPlayers [ playerID ] . representation . length === serverPlayer . representation . length ) {
263
- console . log ( "updating..." )
264
332
const player = this . connectedPlayers [ playerID ] ;
265
333
for ( let i = 0 ; i < player . representation . length ; i ++ ) {
334
+ console . log ( "updating " + serverPlayer . representation [ i ] . type )
266
335
player . representation [ i ] . desiredPosition . x = serverPlayer . representation [ i ] . position . x ;
267
336
player . representation [ i ] . desiredPosition . y = serverPlayer . representation [ i ] . position . y ;
268
337
player . representation [ i ] . desiredPosition . z = serverPlayer . representation [ i ] . position . z ;
@@ -274,6 +343,10 @@ export class WebSocketManager {
274
343
}
275
344
276
345
} else {
346
+ if ( playerID in this . connectedPlayers ) {
347
+ this . removePlayer ( playerID ) ;
348
+ }
349
+
277
350
// Create a new Player!
278
351
this . setupPlayer ( playerID , serverPlayer ) ;
279
352
}
@@ -284,12 +357,15 @@ export class WebSocketManager {
284
357
if ( updated ) {
285
358
continue ;
286
359
}
287
-
288
- this . connectedPlayers [ playerID ] . representation . forEach ( rep => rep . cleanup ( ) ) ;
289
- delete this . connectedPlayers [ playerID ] ;
360
+ this . removePlayer ( playerID ) ;
290
361
}
291
362
}
292
363
364
+ removePlayer ( playerID ) {
365
+ this . connectedPlayers [ playerID ] . representation . forEach ( rep => rep . cleanup ( ) ) ;
366
+ delete this . connectedPlayers [ playerID ] ;
367
+ }
368
+
293
369
onMessage ( evt ) {
294
370
const message = JSON . parse ( evt . data ) ;
295
371
@@ -317,7 +393,7 @@ export class WebSocketManager {
317
393
this . conn . send ( JSON . stringify ( {
318
394
"type" : "Client-SetOrientation" ,
319
395
"data" : {
320
- "representation" : this . representationManager . toMessage ( ) ,
396
+ "representation" : this . representationManager . ToMessage ( ) ,
321
397
}
322
398
} ) ) ;
323
399
}
0 commit comments