nmfisher / flutter_filament

3D rendering layer for Flutter/Filament
Other
44 stars 7 forks source link

Displaying multiple FilamentWidgets #18

Open Hannnes1 opened 2 months ago

Hannnes1 commented 2 months ago

Hi, I tried to display multiple models at once, by creating multiple FilamentWidgets, but it seems like that is not possible at the moment. All widgets except the first throws the following error:

E/flutter (17528): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(TEXTURE_EXISTS, Texture already exist. Make sure you call destroyTexture first, null, null)
E/flutter (17528): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:651:7)
E/flutter (17528): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:334:18)
E/flutter (17528): <asynchronous suspension>
E/flutter (17528): #2      FilamentControllerFFI._createRenderingSurface (package:flutter_filament/filament_controller_ffi.dart:242:34)
E/flutter (17528): <asynchronous suspension>
E/flutter (17528): #3      FilamentControllerFFI.createViewer (package:flutter_filament/filament_controller_ffi.dart:203:28)
E/flutter (17528): <asynchronous suspension>
E/flutter (17528): #4      _MedalState.initState.<anonymous closure> (package:filament_playground/main.dart:98:7)
E/flutter (17528): <asynchronous suspension>

I also got the same error after hot restarting, if .createViewer() is called in initState(). Not entirely sure if the cause is the same though.

createViewer() has the doc comment "You do not need to call this yourself.", so I tried removing it. However that caused the exception "No viewer available". I guess that the documentation isn't entirely up to date in the development branch?

Thanks for a great package!

nmfisher commented 2 months ago

At the moment only a single FilamentWidget is supported, so to load multiple models, you just load them into the same scene/view and translate them (or the camera) as necessary.

I could restructure things to accommodate multiple FilamentWidget (on the backend we would create a texture/view/camera for each new widget). What use case did you have in mind for multiple widgets?

Hannnes1 commented 2 months ago

The end goal I'm looking for is something like this, where the yellow squares will be 3D models.

https://github.com/nmfisher/flutter_filament/assets/47989573/96002a5f-6e19-4662-a54b-6a9aef45b9fe

The grid I can probably solve with only one FilamentWidget, but the Hero animation to the second screen is trickier. Even two FilamentWidgets on different screens causes the error, since the first screen is still in the navigation stack.

Although. Now that I think about it, I might be able to use one widget for everything, and do a hero animation entirely in the 3D viewer. Will require more math though.

nmfisher commented 2 months ago

That's possible, but would need quite a bit of work to implement on both the C++ and Dart sides. Everything so far has been written with the assumption of a single engine/scene/view/render target (texture)/render loop, so we'd need to go back in and refactor some things to allow creating new views/textures/etc. If you need something now, you're probably better off doing the animation in the viewer itself - I've done similar things (basically hard-coding animations in Blender then just triggering them in the app). That might be an easier short term solution.

Hannnes1 commented 2 months ago

Okay, I get it. I will see what I can do within the viewer itself instead.