Giter Site home page Giter Site logo

m-r-davari / flutter_3d_controller Goto Github PK

View Code? Open in Web Editor NEW
71.0 2.0 5.0 26.81 MB

A Flutter package for rendering interactive 3D models in different formats(glb, gltf, fbx, obj), with ability to control animations, textures and camera.

Home Page: https://pub.dev/packages/flutter_3d_controller

License: MIT License

Kotlin 0.09% Swift 1.32% Objective-C 0.84% Dart 61.46% CMake 13.98% C++ 16.73% C 1.24% HTML 1.29% Shell 0.41% Ruby 2.01% Java 0.65%
model-viewer models-3d models3d objects-3d viewer-3d 3d-animation 3d-controller 3d-objects dart flutter

flutter_3d_controller's Introduction

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.

Features

  • Mobile and Web stable version (support glb & gltf format)
  • Play animation
  • Switch between animations
  • Pause animation
  • Reset animation
  • Get available animations list
  • Switch between textures
  • Get available textures list
  • Set camera target
  • Reset camera target
  • Set camera orbit
  • Reset camera orbit
  • Set default loading progressbar color

Samples

Model1 Model2 Model3 Model3 Model1 Model2 Model3 Model3

Compatibility

  • Android
  • iOS
  • Web

Notes

  • For now this package only support GLB & glTF format, other 3d formats coming soon.
  • Visit the full example to see how to use this package

Brief Example

//Create controller object to control 3D model.
Flutter3DController controller = Flutter3DController();

//It will play 3D model animation, you can use it to play or switch between animations.
controller.playAnimation();

//If you pass specific animation name it will play that specific animation.
//If you pass null and your model has at least 1 animation it will play first animation.
controller.playAnimation(animationName: chosenAnimation);

//It will pause the animation at current frame.
controller.pauseAnimation();

//It will reset and play animation from first frame (from beginning).
controller.resetAnimation();

//It will return available animation list of 3D model.
await controller.getAvailableAnimations();

//It will load desired texture of 3D model, you need to pass texture name.
controller.setTexture(textureName: chosenTexture);

//It will return available textures list of 3D model.
await controller.getAvailableTextures();

//It will set your desired camera target.
controller.setCameraTarget(0.3, 0.2, 0.4);

//It will reset the camera target to default.
controller.resetCameraTarget();

//It will set your desired camera orbit.
controller.setCameraOrbit(20, 20, 5);

//It will reset the camera orbit to default.
controller.resetCameraOrbit();
//The 3D viewer widget
Flutter3DViewer(
    //If you don't pass progressBarColor the color of defaultLoadingProgressBar will be grey.
    //You can set your custom color or use [Colors.transparent] for hiding the loadingProgressBar.
    progressBarColor: Colors.blue,
    controller: controller,
    src: 'assets/business_man.glb',
    //src: 'assets/sheen_chair.glb',
)

Installation

pubspec.yaml

dependencies:
  flutter_3d_controller: ^1.3.0

AndroidManifest.xml (Android 9+ only)

To use this widget on Android 9+ devices, your app must be permitted to make an HTTP connection to http://localhost:XXXXX. Android 9 (API level 28) changed the default for [android:usesCleartextTraffic] from true to false, so you will need to configure your app's android/app/src/main/AndroidManifest.xml as follows:

     <application
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher"
-       android:label="example">
+       android:label="example"
+       android:usesCleartextTraffic="true">
        <activity
            android:name=".MainActivity"

This does not affect Android 8 and earlier. See [#7] for more information.

app/build.gradle (Android only)

Change minSdkVersion to 21.

defaultConfig {
    ...
    minSdkVersion 21
    ...
}

Info.plist (iOS only)

To use this widget on iOS, you need to opt-in to the embedded views preview by adding a boolean property to your app's ios/Runner/Info.plist file, with the key io.flutter.embedded_views_preview and the value YES:

  <key>io.flutter.embedded_views_preview</key>
  <true/>

web/index.html (Web only)

Modify the <head> tag of your web/index.html to load the JavaScript, like so:

<head>

  <!-- Other stuff -->

  <script type="module" src="./assets/packages/flutter_3d_controller/assets/model-viewer.min.js" defer></script>
</head>

More Info

This package use 'Model Viewer' to render 3D models and it may have some issue in rendering some models/textures, the core of package (Model Viewer) will change in future to support all type of 3D models

flutter_3d_controller's People

Contributors

m-r-davari avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

flutter_3d_controller's Issues

Unable to fetch Animations and textures on iOS.

After the model is loaded, I am calling the controller.getAvailableTextures() and controller.getAvailableAnimations(). This works on Android and it returns the list of Textures and Animations. But on iOS, I don't get any response, instead see this printed on the console - Return type of evaluateJavaScript is not directly supported: __NSArrayM. Returned description of value. Also the code terminates there and doesn't go further. There is no crash though, but the execution just stops their.

Flutter doctor -v

[!] Flutter (Channel stable, 3.19.2, on macOS 13.6.4 22G513 darwin-arm64, locale en-IN)
• Flutter version 3.19.2 on channel stable at /Users/vishakakamath/development/flutter
! Warning: dart on your path resolves to /opt/homebrew/Cellar/dart/2.18.6/libexec/bin/dart, which is not inside your current Flutter SDK checkout at /Users/vishakakamath/development/flutter. Consider adding
/Users/vishakakamath/development/flutter/bin to the front of your path.
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 7482962148 (3 months ago), 2024-02-27 16:51:22 -0500
• Engine revision 04817c99c9
• Dart version 3.3.0
• DevTools version 2.31.1

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
• Android SDK at /Users/vishakakamath/Library/Android/sdk
• Platform android-34, build-tools 33.0.1
• Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)
• All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.2)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Build 15C500b
• CocoaPods version 1.15.2

Dynamically add textures from a path

I have a simple sofa with white background in my .glb file. I want to change the fabric based on the option selected by the user, I have url's for the fabric images. Is this possible?

View Application Directory Models

I want to download models from the network and preview them after saving them in the application's directory. Is this approach feasible? I noticed that in your example, you passed a static file path; can it instead reference files within the application's directory?

Not working on iOS

When I try to load a glb object in iOS and invoke this method:

animations = await _controller.getAvailableAnimations();

I get the following error:

Exception has occurred.
FormatException (FormatException: Unexpected character (at character 1)
(
^
)

Any idea how I can solve this issue?

Note that its working well on Android.

Formats Support

Will you please tell that this package currently support how many formats?

DIspose not present

Hi I was trying out the package for the POC and I've managed to expirience the memory related issues.
After using the hot reload feature the app leaks the controller and setState is called after the widget is not mounted in the tree.

[log] setState() called after dispose(): ModelViewerState#c07b1(lifecycle state: defunct, not mounted)
      This error happens if you call setState() on a State object for a widget that no longer appears in the widget tree (e.g., whose parent widget no longer includes the widget in its build). This error can occur when code calls setState() from a timer or an animation callback.
      The preferred solution is to cancel the timer or stop listening to the animation in the dispose() callback. Another solution is to check the "mounted" property of this object before calling setState() to ensure the object is still in the tree.
      This error might indicate a memory leak if setState() is being called because another object is retaining a reference to this State object after it has been removed from the tree. To avoid memory leaks, consider breaking the reference to this object during dispose().
[log] #0      State.setState.<anonymous closure> (package:flutter/src/widgets/framework.dart:1167:9)
      #1      State.setState (package:flutter/src/widgets/framework.dart:1202:6)
      #2      ModelViewerState._initController (package:flutter_3d_controller/src/modules/model_viewer/model_viewer_mobile.dart:219:5)
      <asynchronous suspension>

Load glb file from device internal storage in AOS & iOS

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?

Clicking on 3D Models and Feature to Zoom In on the Clicked Model Part

First of all, congratulations on creating a successful library. Upon inspecting the library, I noticed that it only covers basic features such as viewing, animation, etc. However, I would like to request the addition of features like clicking on a multi-part object in OBJ format and displaying a selected effect when clicked. Thank you.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.