m-r-davari / flutter_3d_controller

A Flutter package for rendering interactive 3D models in different formats(glb, gltf, fbx, obj), with ability to control animations, textures and camera.
https://pub.dev/packages/flutter_3d_controller
MIT License
78 stars 8 forks source link

Load glb file from device internal storage in AOS & iOS #11

Closed sangjin-hash closed 2 months ago

sangjin-hash commented 2 months ago

In my app, glb files are downloaded from the server and saved in internal storage. I used getApplicationSupportDirectory() of the path_provider library to save the glb file. I confirmed that it was downloaded from the server and saved in that path(/data/user/0/[package name]/files/Model_1.glb).

The body of the scaffold is as follows

        color: Palette.darkGrey,
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        child: Flutter3DViewer(
          progressBarColor: Palette.valueSky,
          controller: controller,
          src: filePath,
        )
      )

The filePath means the path where the glb file is saved. Although the file works well and exists in the path, the following error occurs when loading it into the 3D viewer.

E/flutter (12723): The asset does not exist or has empty data.
E/flutter (12723): #0      PlatformAssetBundle.load.<anonymous closure> (package:flutter/src/services/asset_bundle.dart:324:9)
E/flutter (12723): <asynchronous suspension>
E/flutter (12723): #1      ModelViewerState._readAsset (package:flutter_3d_controller/src/modules/model_viewer/model_viewer_mobile.dart:313:18)
E/flutter (12723): <asynchronous suspension>
E/flutter (12723): #2      ModelViewerState._initProxy.<anonymous closure> (package:flutter_3d_controller/src/modules/model_viewer/model_viewer_mobile.dart:264:26)
E/flutter (12723): <asynchronous suspension>

Is it working by loading from internal storage in device of Android and iOS?

sangjin-hash commented 2 months ago

If you store 3d models in file system, use getApplicationDocumentsDirectory() this method. I tried to use getApplicationSupportDirectory(), but it doesn't work on iOS(In Android, it works). I think the viewer cannot find the file in iOS. If you use getApplicationDocumentsDirectory(), the viewer can access the file!

And the viewer's src must be configured as follows.

        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        child: Flutter3DViewer(
          progressBarColor: Palette.valueSky,
          controller: controller,
          src: file://$filePath,
        )
      )

I confirmed that it works well on Android and iOS.