GithubHelp home page GithubHelp logo

babakcode / o3d Goto Github PK

View Code? Open in Web Editor NEW
33.0 2.0 12.0 42.21 MB

The Flutter 3D objects easy controller ( glb format )

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

License: BSD 3-Clause "New" or "Revised" License

Java 0.41% Shell 0.28% Objective-C 0.52% Dart 72.50% C++ 12.81% C 0.79% CMake 10.37% Swift 1.02% Kotlin 0.07% HTML 1.23%
3d ar flutter model model3d flutter3d models3d

o3d's Introduction

O3D - Model Viewer for Flutter

This is a Flutter widget for rendering interactive 3D models in the glTF and GLB formats. The widget embeds Google's <model-viewer> web component in a WebView.

Flutter 3D model viewer with O3D

Screenshot

online demo 1: https://babakcode.github.io/ui_3d_test/ / source code

online demo 2: https://babakcode.github.io/ui_3d_flutter/ / source code

Flutter 3d model Flutter 3d model

Features

  1. O3DController controller = O3DController();
  2. cameraTarget: use controller.cameraTarget(20, 20, 5) x, y, z
  3. cameraOrbit: use controller.cameraOrbit(1.2, 1, 4) (theta)deg, (phi)deg, (radius)m
  4. availableAnimations: use controller.availableAnimations().then((animations) => log("Available animations: $animations"));
  5. play: use controller.play() [optional] repetitions => play(repetitions: 2)
  6. pause: use controller.pause()
  7. Renders glTF and GLB models. (Also, USDZ models on iOS 12+.)
  8. Supports animated models, with a configurable auto-play setting.
  9. Optionally supports launching the model into an AR viewer.
  10. Optionally auto-rotates the model, with a configurable delay.
  11. Supports a configurable background color for the widget.

Installation

in pubspec.yaml

dependencies:
  o3d: ^3.1.0

AndroidManifest.xml (Android 9+ only)

Test on real device and 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:

+    <uses-permission android:name="android.permission.INTERNET"/>

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

This does not affect Android 8 and earlier.

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/o3d/assets/model-viewer.min.js" defer></script>
</head>

Examples

Importing the library

import 'package:o3d/o3d.dart';

Creating a O3D widget

class _MyHomePageState extends State<MyHomePage> {
  
  // to control the animation
  O3DController controller = O3DController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
        actions: [
          IconButton(
              onPressed: () => 
                  controller.cameraOrbit(20, 20, 5),
              icon: const Icon(Icons.change_circle)),
          IconButton(
              onPressed: () =>
                  controller.cameraTarget(1.2, 1, 4), 
              icon: const Icon(Icons.change_circle_outlined)),
        ],
      ),
      body: O3D.asset(
         src: 'assets/glb/jeff_johansen_idle.glb', 
         controller: controller,
      ),
    );
  }
}

Loading a bundled Flutter asset

O3D.asset(
  src: 'assets/MyModel.glb',
  // ...
),

Loading a model from the web

body: O3D.network(
   src:'https://modelviewer.dev/shared-assets/models/Astronaut.glb',
   // ...
),

Loading a model from the file system

This is not available on Web.

class HomePage extends StatelessWidget {
   const HomePage({super.key});
   
   @override
   Widget build(BuildContext context) {
      return O3D(src: 'file:///path/to/MyModel.glb',
      // ...
      );
   }
}

projects

babak code Tassio Gustavo Vaibhav Chandolia
BabakCode BabakCode Vaibhav Chandolia youtube video
youtube video link linkedin post link Youtube channel

Compatibility

Notes

We use the Google APP , com.google.android.googlequicksearchbox to display interactive 3D models on Android. The model displays in 'ar_preferred' mode by default, Scene Viewer launches in AR native mode as the entry mode. If Google Play Services for AR (ARCore, com.google.ar.core) isn't present, Scene Viewer gracefully falls back to 3D mode as the entry mode.

Note that due to browsers' CORS security restrictions, the model file must be served with a Access-Control-Allow-Origin: * HTTP header.

Frequently Asked Questions

Q: Why doesn't my 3D model load and/or render?

A: There are several reasons why your model URL could fail to load and render:

  1. It might not be possible to parse the provided glTF or GLB file. Some tools can produce invalid files when exporting glTF. Always run your model files through the glTF Validator to check for this.

o3d's People

Contributors

babakcode avatar jamal-o 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

Watchers

 avatar  avatar

o3d's Issues

pubspec fails when adding o3d to project

Getting this error on a brand new project:

$ flutter pub add o3d
Because o3d depends on o3d, version solving failed.
flutter pub get
Resolving dependencies...
Error on line 34, column 3 of pubspec.yaml: A package may not list itself as a dependency.
   ╷
34 │   o3d: 3.0.0
   │   ^^^
   ╵
Process finished with exit code 65

Provide ability to pass http headers in network call

Hi Babak,

I was wondering if you plan on including the ability to add http headers within the O3D.network method? Because the nature of our 3D models we require users to be authenticated before we return the GLB model. In our other API calls we require the Authorization header to be present with a valid token.

Do you plan on adding the ability for headers to be passed to the network call?

Suggestion for Regular Updates to Dependency Libraries

Hello,

First of all, thank you for your work on the [o3d] plugin for Flutter. It's a valuable tool for the community, and I appreciate your efforts in maintaining it.

I am writing to suggest regular updates for the dependency libraries used in the project, specifically [model_view] and [flutter_webpage]. While I haven't encountered specific issues, I believe that keeping these libraries up-to-date could prevent potential compatibility and security problems, and possibly unlock new features and improvements that could benefit all users.

Thank you for considering this suggestion. Your contributions to the open-source community are greatly valued.

Cannot use origin without a scheme

Loading the GLB from asset folder i cant view on Android Build:

Loading like this:
O3D(
src: 'assets/3d/refix_spray_ORBIT_cam.glb',
controller: o3dcontroller,
ar: false,
autoPlay: true,
autoRotate: true,
),

E/flutter ( 2836): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Bad state: Cannot use origin without a scheme: assets/3d/refix_spray_ORBIT_cam.glb

ModelViewerState._initProxy. package:model_viewer_plus/src/model_viewer_plus_mobile.dart:289

Not working on iOS

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

animations = await _controller. availableAnimations();

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.

availableAnimations works on Android, not iOS

availableAnimations feature is not working on iOS. I took this same code from the pub dev page:

availableAnimations: use controller.availableAnimations().then((animations) => log("Available animations: $animations"));

It gets a parse error:

flutter: ModelViewer initializing... <http://127.0.0.1:60021/>
flutter: ModelViewer wants to load: http://127.0.0.1:60021/
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: FormatException: Unexpected character (at character 1)
(
^

#0      _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1380:5)
#1      _ChunkedJsonParser.parseNumber (dart:convert-patch/convert_patch.dart:1247:9)
#2      _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:912:22)
#3      _parseJson (dart:convert-patch/convert_patch.dart:35:10)
#4      JsonDecoder.convert (dart:convert/json.dart:610:36)
#5      JsonCodec.decode (dart:convert/json.dart:216:41)
#6      jsonDecode (dart:convert/json.dart:155:10)
#7      O3dImp.availableAnimations (package:o3d/src/controllers/implementation/o3d_stub_impl.dart:86:12)
<asynchronous suspension>
#8      _MyHomePageState.build.<anonymous closure> (package:animationsample/main.dart:148:52)
<asynchronous suspension>

I know this GLB has animations because it works on Android:

I/flutter (25510): Available animations: [Idle, Jump, Land, Run]

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.