-
Notifications
You must be signed in to change notification settings - Fork 490
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to load glb as byte[] or MemoryStream #732
Comments
hey, should be possible with |
SOLUTION If anyone is still interested, this is how I did it for my webGL Application:
If anyone has a better approach, please let me know!
Old Comment History. Original comment:
But at initialization, in the constructor of GltfSceneImporter the "VerifyDataLoader" Method gets called, which checks for a directoryname - which I dont have if I am using a stream as input:
If I am trying to use it, unity throws a NullReferenceException, because the "_gltfFileName" is empty. For now I dont know how to use it - any recommendations? . EDIT: Okay I tried with the UnityWebRequestLoader but it only works in editor playmode, built to webGL it doesnt. This is my code so far, only working in Editor:
And this was my working code for uploading a file to my WebGL APP, using a different gltf plugin, which worked by loading from byte[]:
I feel like something in between is missing. Maybe it needs to copy the buffer? . EDIT 2: Okay I am pretty sure it is the "System.Threading.Tasks" include, that WebGL can't handle.
B) Use the Stream approach, but need to work around the filename check @pfcDorn What would you suggest? |
I am trying again now like this with glb files and the "fake" UnityWebRequestLoader: private async UniTask<GameObject> LoadWithUnityGLTF(byte[] bytes)
{
using (var stream = new MemoryStream(bytes))
{
var glb = GLBBuilder.ConstructFromStream(stream);
_unityGltf.DataLoader = new UnityGLTF.Loader.UnityWebRequestLoader("");
var importer = new UnityGLTF.GLTFSceneImporter(glb.Root, stream, _unityGltf);
await importer.LoadScene(-1, showSceneObj: true);
return importer.LastLoadedScene;
}
} But I get lots of mesh errors like these and of course no valid model:
I also tried creating my own MemoryLoader with the IDataLoader interface but this didn't work as well. private class MemoryDataLoader : UnityGLTF.Loader.IDataLoader
{
private readonly MemoryStream _memoryStream;
public MemoryDataLoader(MemoryStream memoryStream)
{
_memoryStream = memoryStream;
}
public async Task<Stream> LoadStreamAsync(string relativeFilePath)
{
return _memoryStream;
}
} Any ideas? |
Hey, can you take a look on this branch: https://github.com/KhronosGroup/UnityGLTF/tree/fix/load-from-stream var stream = new FileStream(filePath, FileMode.Open);
var importOptions = new ImportOptions();
var importer = new GLTFSceneImporter(stream, importOptions);
await importer.LoadSceneAsync();
stream.Dispose(); Generally, you should only load GLBs which contains all textures and data. References to files would not work. Btw: You already has found a working solution with a custom DataLoader: private class StreamDataLoader : UnityGLTF.Loader.IDataLoader
{
private readonly Stream _stream;
public StreamDataLoader(Stream stream)
{
_stream = stream;
}
public async Task<Stream> LoadStreamAsync(string relativeFilePath)
{
return _stream;
}
}
var stream = new FileStream(filePath, FileMode.Open);
var importOptions = new ImportOptions();
importOptions.DataLoader = new StreamDataLoader(stream);
GLTFParser.ParseJson(stream, out var gltfRoot);
var importer = new GLTFSceneImporter(gltfRoot, stream, importOptions);
await importer.LoadSceneAsync();
stream.Dispose(); |
Works like a charm! I didn't manage to get it to work with the custom DataLoader but with your first example on the separate branch. |
Hello,
I'd like to load a byte[] or MemoryStream as a glb file without a file path from script (not using Inspector at all).
Is this possible? Didn't find anything in examples or tests.
The text was updated successfully, but these errors were encountered: