I will start by giving some context: I want to share a model via socket.io.
My model can be composed as follow:
1 mtl file
1 obj file.
several jpgs images being the textures
I was wondering if the scene: Scene parameter created in the sceneLoadedCallback of the FilesInput class contains the data of the obj+mtl+jpgs therefore I don’t need to broadcast all these files to all the clients. OR
If I still need to broadcast these files because the Scene object is actually just using these jpegs + mtl+ obj files: my idea was to use JSZip to compress all the files in one document then broadcast it via the sever and then unzip it on every client side.
Just to clarify my question, let’s say my model have
JPEG texture (xx KB of data)
Obj file (yy KB of data)
Mtl file (zz KB of data)
Is the xx + yy + zz data present in the resulting Scene parameter in the sceneLoadedCallback?
Yes, the scene will contain the meshes + materials (+textures) created by reading the obj / mtl / jpeg files if that is your question. Note that this scene is the one you passed to the constructor of FilesInput.
Note also that the files themselves are not present per see: it’s the result of their processing that’s there (meshes, materials, textures).
Thank your for your answer, that is what I want to replicate in every client browser. I just need the result of the processing not the actual files used to generate the model.
The problem I’m facing now is that I am unable to broadcast this object via socket.io. For some reason it can’t be created as a JSON because of circular reference. Do you know a way I can broadcast and then recreate the scene in the other clients session ?
Is sending the meshes,materials,textures of the scene as 3 separated JSON objects could work ?
Error when using JSON.stringify(scene) to broadcast the scene:
You won’t be able to directly send the scene object over the wire as there are too much dependencies / circular references, you should first serialize it with something like:
var serializedScene = BABYLON.SceneSerializer.Serialize(scene);
var strScene = JSON.stringify(serializedScene);
and send the string.
On the other side, load the data as a .babylon file with the scene loader.
I’ve tried what you suggested, so the shape is broadcasted correctly but it seems that the textures are not. Below you can see on the right the view in the browser of the client that need to broadcast the model and on the left the one receiving it
And in the console I have errors saying Local ressource not found File://texture1.jpg Local ressource not found File://texture2.jpg Local ressource not found File://texture3.jpg
To recreate the scene I’ve used the following line: BABYLON.SceneLoader.Append('', 'data:' + newScene, this.scene, this.updateScene.bind(this), null , this.appendError.bind(this));
newScene being the serialized scene that is broadcasted through socket.io
When you said load the data as .babylon file what did you meant by this ?
I meant what you did, using the SceneLoader to parse the data.
Regarding the missing images, I thought it was possible to serialize a scene with the pictures embedded in, I think I have already seen .babylon files with embedded picture data but I don’t recall where / how those data were embedded. It’s also possible I’m wrong… Maybe others will know better.
[…] Maybe the data are embedded only when the urls you pass to the texture constructor are data urls…
@aWeirdo I’m using Babylons 4.2.0-rc.6 and when I tried what you suggested the model is not loading even in the client who uploaded the model. I have this error
If I comment the line you suggested, it works in the local client.
I just saw that there is 4.2.0 that just got released, I’ll try to update and I’ll let you know. Thanks for your help
@aWeirdo If I comment the part where I serialize the scene it works
So the error comes when ForceSerializeBuffers is true and this serializing part as mentionned by @Evgeni_Popov is executed: BABYLON.SceneSerializer.Serialize(babylonScene);
Do I need to set some attributes of the SceneSerializer to true when the BABYLON.Texture.ForceSerializeBuffers is set to true ?
Hummm I just tried the exact same operations (with the exact same files) in the sandbox and it works, even the serialization. I’m not sure why though ?
As you can see below the base64string after the serialization
The error was that the serialization was being executed before the scene was actually ready to be serialized. The solution was to place it inside a executeWhenReady block