teleportxr 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # teleport-nodejs
2
- A minimal Teleport server on node.js
1
+ # TeleportXR
2
+ Teleport virtual reality server for node.js
3
3
 
4
4
  1. Ensure node.js is installed.
5
- 2. Run npm install in this directory to create the node_modules folder.
6
- 3. Run server.js using the provided .vscode/launch.json
5
+ 2. Run npm install teleportxr.
6
+ 3. See https://github.com/teleportxr/teleport-nodejs-server-example.git for example usage.
package/client/client.js CHANGED
@@ -8,6 +8,7 @@ const node_encoder= require("../protocol/encoders/node_encoder.js");
8
8
  const resource_encoder= require("../protocol/encoders/resource_encoder.js");
9
9
  const WebRtcConnectionManager = require('../connections/webrtcconnectionmanager');
10
10
  const resources= require("../scene/resources.js");
11
+ const { BackgroundMode } = require("../core/core.js");
11
12
 
12
13
 
13
14
  class OriginState
@@ -110,6 +111,14 @@ class Client {
110
111
  Start()
111
112
  {
112
113
  this.setupCommand=new command.SetupCommand();
114
+ if(this.scene)
115
+ {
116
+ if(this.scene.backgroundTexturePath&&this.scene.backgroundTexturePath!="")
117
+ {
118
+ this.setupCommand.BackgroundMode_backgroundMode=BackgroundMode.TEXTURE;
119
+ this.setupCommand.uid_backgroundTexture=resources.AddTexture(this.scene.backgroundTexturePath);
120
+ }
121
+ }
113
122
  this.SendCommand(this.setupCommand);
114
123
  }
115
124
  SendCommand(command){
@@ -223,27 +232,30 @@ class Client {
223
232
  console.log("Sending node "+uid+" "+node.name+" to Client "+this.clientID+", size: "+nodeSize+" bytes");
224
233
  this.webRtcConnection.sendGeometry(view2);
225
234
  }
226
- SendMesh(uid)
227
- {
228
- var mesh=resources.GetResource(uid);
229
- }
230
- SendTexture(uid)
235
+ SendGenericResource(uid)
231
236
  {
232
- var texture=resources.GetResourceFromUid(uid);
233
- if(!texture)
237
+ var resource=resources.GetResourceFromUid(uid);
238
+ if(!resource)
234
239
  {
235
- console.warn("No texture of uid ",uid," was found.")
240
+ console.warn("No resource of uid ",uid," was found.")
236
241
  return;
237
242
  }
238
- const MAX_TEXTURE_SIZE=500;
239
- const buffer = new ArrayBuffer(MAX_TEXTURE_SIZE);
240
- const textureSize=resource_encoder.EncodeResource(texture,buffer);
243
+ const MAX_BUFFER_SIZE=500;
244
+ const buffer = new ArrayBuffer(MAX_BUFFER_SIZE);
245
+ const resourceSize=resource_encoder.EncodeResource(resource,buffer);
241
246
  this.geometryService.EncodedResource(uid);
242
- const view2 = new DataView(buffer, 0, textureSize);
243
- console.log("Sending texture "+uid+" "+texture.url+" to Client "+this.clientID+", size: "+textureSize+" bytes");
247
+ const view2 = new DataView(buffer, 0, resourceSize);
248
+ console.log("Sending resource "+uid+" "+resource.url+" to Client "+this.clientID+", size: "+resourceSize+" bytes");
244
249
  this.webRtcConnection.sendGeometry(view2);
245
250
  }
246
-
251
+ SendMesh(uid)
252
+ {
253
+ this.SendGenericResource(uid);
254
+ }
255
+ SendTexture(uid)
256
+ {
257
+ this.SendGenericResource(uid);
258
+ }
247
259
  receiveHandshake(data)
248
260
  {
249
261
  if(data.length<message.HandshakeMessage.sizeof()) {
@@ -130,7 +130,22 @@ class GeometryService
130
130
  this.streamedNodes.delete(uid);
131
131
  // TODO: now reduce the counts for all the dependent resources.
132
132
  }
133
-
133
+ StreamOrUnstream(resourceMap,uid,diff)
134
+ {
135
+ if(uid==BigInt(0))
136
+ return;
137
+ if(uid==0)
138
+ return;
139
+ if(!resourceMap.has(uid))
140
+ {
141
+ resourceMap.set(uid,0);
142
+ }
143
+ resourceMap.set(uid,resourceMap.get(uid)+diff);
144
+ }
145
+ AddOrRemoveTexture(thisTextureUid,diff){
146
+ this.StreamOrUnstream(this.streamedTextures,thisTextureUid,diff);
147
+ }
148
+
134
149
  AddMeshComponentResources(meshComponent,diff)
135
150
  {
136
151
  if (meshComponent.getType() != nd.NodeDataType.Mesh)
@@ -141,7 +156,7 @@ class GeometryService
141
156
  {
142
157
  return;
143
158
  }
144
- this.streamedMeshes.set(meshComponent.data_uid,this.streamedMeshes.get(meshComponent.data_uid)+diff);
159
+ this.StreamOrUnstream(this.streamedMeshes,meshComponent.data_uid,diff);
145
160
  //meshNode.skeletonID = node.skeletonNodeID;
146
161
 
147
162
  //Get joint/bone IDs, if the skeletonID is not zero.
@@ -154,7 +169,7 @@ class GeometryService
154
169
  }
155
170
  if(meshComponent.renderState.globalIlluminationUid != BigInt(0))
156
171
  {
157
- this.streamedTextures.set(meshComponent.renderState.globalIlluminationUid,this.streamedTextures.get(meshComponent.renderState.globalIlluminationUid)+diff);
172
+ this.StreamOrUnstream(this.streamedTextures,meshComponent.renderState.globalIlluminationUid,diff);
158
173
  }
159
174
  }
160
175
 
@@ -171,7 +186,7 @@ class GeometryService
171
186
  {
172
187
  continue;
173
188
  }
174
- this.streamedMaterials.set(material_uid,this.streamedMaterials.get(material_uid)+diff);
189
+ this.StreamOrUnstream(this.streamedMaterials,material_uid,diff);
175
190
 
176
191
  var texture_uids =
177
192
  [
@@ -184,7 +199,7 @@ class GeometryService
184
199
  for(const tex_uid of texture_uids)
185
200
  {
186
201
  if(tex_uid!=0)
187
- this.streamedTextures.set(tex_uid,this.streamedTextures.get(tex_uid)+diff);
202
+ this.StreamOrUnstream(this.streamedTextures,tex_uid,diff);
188
203
  }
189
204
  }
190
205
  }
@@ -254,11 +269,11 @@ class GeometryService
254
269
  if(f)
255
270
  {
256
271
  if(node.data_uid)
257
- this.streamedTextCanvases.set(f.data_uid,this.streamedTextCanvases.get(f.data_uid)+diff);
272
+ this.StreamOrUnstream(this.streamedTextCanvases,f.data_uid,diff);
258
273
  if(c.font_uid)
259
- this.streamedFontAtlases.set(f.font_uid,this.streamedFontAtlases.get(f.font_uid)+diff);
274
+ this.StreamOrUnstream(this.streamedFontAtlases,f.font_uid,diff);
260
275
  if(f.font_texture_uid)
261
- this.streamedTextures.set(f.font_texture_uid,this.streamedTextures.get(f.font_texture_uid)+diff);
276
+ this.StreamOrUnstream(this.streamedTextures,f.font_texture_uid,diff);
262
277
  }
263
278
  }
264
279
  }
@@ -303,22 +318,6 @@ class GeometryService
303
318
  this.AddOrRemoveNodeAndResources(uid,1);
304
319
  }
305
320
  }
306
- AddOrRemoveTexture(thisTextureUid,diff){
307
- if(thisTextureUid==BigInt(0))
308
- return;
309
- if(thisTextureUid==0)
310
- return;
311
- if(this.streamedTextures.has(thisTextureUid))
312
- {
313
-
314
- }
315
- else
316
- {
317
- this.streamedTextures.set(thisTextureUid,0);
318
- }
319
- this.streamedTextures.set(thisTextureUid,this.streamedTextures.get(thisTextureUid)+diff);
320
- }
321
-
322
321
  UpdateTexturesToStream() {
323
322
  // scene background?
324
323
  var bg_uid=resources.GetResourceUidFromUrl(core.GeometryPayloadType.TexturePointer,this.scene.backgroundTexturePath);
@@ -375,10 +374,10 @@ class GeometryService
375
374
  GetMeshesToStream()
376
375
  {
377
376
  var resource_uids=[];
378
- this.streamedMeshes.forEach(uid => {
377
+ let time_now_us=core.getTimestampUs();
378
+ for(const [uid, count] of this.streamedMeshes)
379
+ {
379
380
  //is mesh streamed
380
- if(!GeometryService.trackedResources.has(uid))
381
- return;
382
381
  var res=GeometryService.GetOrCreateTrackedResource(uid);
383
382
  res.Sent(this.clientID,time_now_us);
384
383
  if(res.WasSentToClient(this.clientID))
@@ -395,7 +394,7 @@ class GeometryService
395
394
  // if it hasn't been sent at all to our client, we add its resources.
396
395
  resource_uids.push(uid);
397
396
  }
398
- });
397
+ };
399
398
  return resource_uids;
400
399
  }
401
400
  EncodedResource(resource_uid)
package/core/core.js CHANGED
@@ -428,7 +428,7 @@ function put_vec4(dataView, byteOffset, value) {
428
428
  //! Insert a string to the dataView.
429
429
  function put_string(dataView, byteOffset, name) {
430
430
  byteOffset = put_uint16(dataView, byteOffset, name.length);
431
- //Push name.
431
+ // Push name.
432
432
  for (var i = 0; i < name.length; i++) {
433
433
  var char = name[i];
434
434
  var code = char.charCodeAt(0);
package/package.json CHANGED
@@ -13,7 +13,7 @@
13
13
  },
14
14
  "name": "teleportxr",
15
15
  "description": "A minimal Teleport server on node.js",
16
- "version": "1.0.0",
16
+ "version": "1.0.1",
17
17
  "repository": {
18
18
  "type": "git",
19
19
  "url": "git+https://github.com/simul/teleport-nodejs.git"
@@ -56,7 +56,13 @@ function GetResourceFromUid(uid)
56
56
  //! Add the texture url as a resource.
57
57
  function AddTexture(url)
58
58
  {
59
- GetResourceUidFromUrl(core.GeometryPayloadType.TexturePointer,url);
59
+ return GetResourceUidFromUrl(core.GeometryPayloadType.TexturePointer,url);
60
60
  }
61
61
 
62
- module.exports = {Resource,GetResourceFromUrl,GetResourceUidFromUrl,GetResourceFromUid,AddTexture};
62
+ //! Add the texture url as a resource.
63
+ function AddMesh(url)
64
+ {
65
+ return GetResourceUidFromUrl(core.GeometryPayloadType.MeshPointer,url);
66
+ }
67
+
68
+ module.exports = {Resource,GetResourceFromUrl,GetResourceUidFromUrl,GetResourceFromUid,AddTexture,AddMesh};
package/scene/scene.js CHANGED
@@ -65,7 +65,11 @@ class Scene {
65
65
  if (components) {
66
66
  for (let c of components) {
67
67
  if (c["type"] == "mesh")
68
- n.setMeshComponent(c["url"]);
68
+ {
69
+ var mesh_url=c["url"];
70
+ resources.AddMesh(mesh_url);
71
+ n.setMeshComponent(mesh_url);
72
+ }
69
73
  }
70
74
  }
71
75
  }
@@ -0,0 +1,35 @@
1
+ 'use strict';
2
+
3
+ const WebRtcConnectionManager = require('./connections/webrtcconnectionmanager');
4
+ const cm = require('./client/client_manager.js');
5
+
6
+ const signaling=require("./signaling.js");
7
+ const scene=require("./scene/scene.js");
8
+
9
+ var sc=new scene.Scene();
10
+
11
+ const fs = require('fs');
12
+ const path = require('path');
13
+
14
+ const assetsPath = path.join(__dirname,'assets');
15
+
16
+ sc.Load(path.join(assetsPath,'scene.json'));
17
+
18
+ // This is our app's callback for when a new client is to be created.
19
+ // It must return the origin uid for the client.
20
+ function createNewClient(clientID) {
21
+ var origin_uid=sc.CreateNode();
22
+ return origin_uid;
23
+ }
24
+
25
+ // This will be called AFTER a client has been created, so we can access it from the clientManager.
26
+ function onClientPostCreate(clientID) {
27
+ var client=cm.getInstance().GetClient(clientID);
28
+ client.SetScene(sc);
29
+ }
30
+
31
+ const webRtcConnectionManager = WebRtcConnectionManager.getInstance();
32
+ webRtcConnectionManager.SetSendConfigMessage(signaling.sendConfigMessage);
33
+ cm.getInstance().SetNewClientCallback(createNewClient);
34
+ cm.getInstance().SetClientPostCreationCallback(onClientPostCreate);
35
+ signaling.init(webRtcConnectionManager,cm.getInstance().newClient.bind(cm.getInstance()));