prefrontalcortex / UnityGLTF

Runtime GLTF Loader for Unity3D
MIT License
163 stars 41 forks source link

Add ability to export files and buffer views #73

Open robertlong opened 1 year ago

robertlong commented 1 year ago

This PR supersedes #60.

I'm developing an exporter plugin for Third Room that has support for the KHR_audio extension. This includes exporting mp3 files. Files are either exported to disk and referenced via uri or exported as a buffer view. This PR adds two new methods for exporting arbitrary files and buffer views as well as exposing the shouldUseInternalBuffer flag for determining which method to use.

When exporting as a .glb or if you just want to export arbitrary buffer views, you can now use the exporter.ExportBufferView(byte[] bytes) function.

When exporting as a .gltf and you want to export an external file you can now use the exporter.ExportFile(string path) function which returns the file uri.

Here's an example of this in use:

KHR_AudioData audio;

if (exporter.shouldUseInternalBuffer) {
  var data = File.ReadAllBytes(path);
  var bufferView = exporter.ExportBufferView(data);

  audio = new KHR_AudioData() {
    mimeType = "audio/mpeg",
    bufferView = bufferView,
  };
} else {
  audio = new KHR_AudioData() {
    uri = exporter.ExportFile(path),
  };
}
hybridherbst commented 1 year ago

Thanks! It feels a bit weird that now I need to handle things different depending on if I want to export a glTF or a glb (exposing these internals, basically).

Just an idea, what do you think about:

robertlong commented 1 year ago

Here's where I'm using it right now: https://github.com/matrix-org/thirdroom-unity-exporter/blob/main/Packages/thirdroom-unity-exporter/Runtime/Scripts/KHR_audio/KHR_AudioExtension.cs#L123

I think that approach would work well 👍 I'll switch over to it and update this PR when I do.

robertlong commented 1 year ago

A couple months later, but I finally made the change @hybridherbst