Flutter 3D物体展示插件flutter_3d_objects的使用

Flutter 3D物体展示插件flutter_3d_objects的使用

本插件允许Flutter开发者在他们的应用中轻松地展示3D模型。它支持.obj文件格式,并提供了场景操作的功能,包括设置相机位置和添加包含子对象的对象。

特性

  • .obj文件中显示3D模型。
  • 在场景中添加和操作对象。
  • 设置相机位置以调整视图。
  • 支持分层对象结构。

配置

如何使用此插件

1. 在pubspec.yaml中添加依赖项

dependencies:
  flutter:
    sdk: flutter
  flutter_3d_objects:

2. 在资源文件中添加3D模型

flutter:
  assets:
    - assets/p.obj
    - assets/c.obj

使用方法

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Cube(
            onSceneCreated: (Scene scene) {
              scene.world.add(
                Object(
                  fileName: 'assets/c.obj',
                  position: Vector3(1, 1, 1),
                  scale: Vector3(1, 1, 1),
                ),
              );
            },
          ),
        ),
      ),
    );
  }
}

示例

// 创建变量
late Scene _scene;
double x = 0;
double y = 3;
double z = 8;
late AnimationController _controller;
late Animation<double> _animation;
Object? _p;

// 创建函数
void _onSceneCreated(Scene scene) {
  _scene = scene;
  scene.camera.position.z = z;
  scene.camera.position.y = y;
  scene.camera.position.x = x;
  _p = Object(
    position: Vector3(0, -2.5, 0),
    scale: Vector3(4, 4, 4),
    fileName: 'assets/p.obj',
    children: [
      Object(
        fileName: 'assets/c.obj',
        position: Vector3(0.25, 0.15, -0.24),
        scale: Vector3(0.8, 0.8, 0.8),
      ),
    ],
  );
  scene.world.add(_p!);
}

// 旋转功能
void rotateLeft() {
  _animateRotation(90);
}

void rotateRight() {
  _animateRotation(-90);
}

void _animateRotation(double angle) {
  final double begin = _p!.rotation.y;
  final double end = _p!.rotation.y + angle;

  _animation = Tween<double>(begin: begin, end: end).animate(_controller)
    ..addListener(() {
      setState(() {
        _p!.rotation.y = _animation.value;
        _p!.updateTransform();
        _scene.update();
      });
    });

  _controller.forward(from: 0);
}

// 在主小部件函数中
Scaffold(
  appBar: AppBar(
    title: const Text('3D Object Viewer'),
  ),
  body: Stack(
    children: [
      Cube(
        interactive: false,
        zoom: false,
        onSceneCreated: _onSceneCreated,
      ),
      Positioned(
        bottom: 10,
        left: 0,
        right: 0,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            ElevatedButton(
              onPressed: rotateLeft,
              child: const Text('左旋'),
            ),
            ElevatedButton(
              onPressed: rotateRight,
              child: const Text('右旋'),
            ),
          ],
        ),
      ),
    ],
  ),
);

示例代码

import 'package:flutter_3d_objects/flutter_3d_objects.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: '3D Scene with Flutter Cube',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  [@override](/user/override)
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {
  late Scene _scene;
  double x = 0;
  double y = 3;
  double z = 8;
  late AnimationController _controller;
  late Animation<double> _animation;
  Object? _p;

  [@override](/user/override)
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: const Duration(milliseconds: 500),
      vsync: this,
    );
  }

  void _onSceneCreated(Scene scene) {
    _scene = scene;
    scene.camera.position.z = z;
    scene.camera.position.y = y;
    scene.camera.position.x = x;
    _p = Object(
      position: Vector3(0, -2.5, 0),
      scale: Vector3(4, 4, 4),
      fileName: 'assets/p.obj',
      children: [
        Object(
          fileName: 'assets/c.obj',
          position: Vector3(0.25, 0.15, -0.24),
          scale: Vector3(0.8, 0.8, 0.8),
        ),
        Object(
          fileName: 'assets/d.obj',
          position: Vector3(-0.3, 0.1, 0.2),
          scale: Vector3(0.6, 0.6, 0.6),
        ),
        Object(
          fileName: 'assets/e.obj',
          position: Vector3(0.0, 0.3, -0.3),
          scale: Vector3(0.7, 0.7, 0.7),
        ),
      ],
    );
    scene.world.add(_p!);
  }

  void rotateLeft() {
    _animateRotation(90);
  }

  void rotateRight() {
    _animateRotation(-90);
  }

  void _animateRotation(double angle) {
    final double begin = _p!.rotation.y;
    final double end = _p!.rotation.y + angle;

    _animation = Tween<double>(begin: begin, end: end).animate(_controller)
      ..addListener(() {
        setState(() {
          _p!.rotation.y = _animation.value;
          _p!.updateTransform();
          _scene.update();
        });
      });

    _controller.forward(from: 0);
  }

  [@override](/user/override)
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('3D Object Viewer'),
      ),
      body: Stack(
        children: [
          Cube(
            interactive: false,
            zoom: false,
            onSceneCreated: _onSceneCreated,
          ),
          Positioned(
            bottom: 10,
            left: 0,
            right: 0,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                ElevatedButton(
                  onPressed: rotateLeft,
                  child: const Text('左旋'),
                ),
                ElevatedButton(
                  onPressed: rotateRight,
                  child: const Text('右旋'),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

更多关于Flutter 3D物体展示插件flutter_3d_objects的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter 3D物体展示插件flutter_3d_objects的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


flutter_3d_objects 是一个用于在 Flutter 应用中展示 3D 物体的插件。它允许你在应用中嵌入和交互 3D 模型,通常以 .obj.glb 格式的文件。以下是如何使用 flutter_3d_objects 插件的基本步骤。

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加 flutter_3d_objects 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  flutter_3d_objects: ^0.1.0  # 请检查最新版本

然后运行 flutter pub get 来安装依赖。

2. 导入插件

在你的 Dart 文件中导入 flutter_3d_objects 插件:

import 'package:flutter_3d_objects/flutter_3d_objects.dart';

3. 使用 ThreeDObject 组件

ThreeDObjectflutter_3d_objects 插件中的主要组件,用于展示 3D 物体。你可以通过指定 path 参数来加载 3D 模型文件。

class My3DObjectViewer extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('3D Object Viewer'),
      ),
      body: Center(
        child: ThreeDObject(
          path: 'assets/models/your_model.obj', // 3D 模型文件的路径
          scale: Vector3(1.0, 1.0, 1.0), // 缩放比例
          rotation: Vector3(0.0, 0.0, 0.0), // 旋转角度
          position: Vector3(0.0, 0.0, 0.0), // 位置
        ),
      ),
    );
  }
}

4. 添加 3D 模型文件

将你的 3D 模型文件(如 .obj.glb 文件)放入 assets 文件夹中,并在 pubspec.yaml 文件中声明这些资源:

flutter:
  assets:
    - assets/models/your_model.obj

5. 运行应用

现在你可以运行你的 Flutter 应用,并查看 3D 物体的展示。你可以通过调整 scalerotationposition 参数来控制 3D 物体的显示效果。

6. 交互功能

flutter_3d_objects 插件通常还支持一些交互功能,例如通过手势旋转、缩放 3D 物体。你可以查阅插件的文档或示例代码来了解更多关于交互功能的实现。

示例代码

以下是一个完整的示例代码:

import 'package:flutter/material.dart';
import 'package:flutter_3d_objects/flutter_3d_objects.dart';
import 'package:vector_math/vector_math_64.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter 3D Objects Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: My3DObjectViewer(),
    );
  }
}

class My3DObjectViewer extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('3D Object Viewer'),
      ),
      body: Center(
        child: ThreeDObject(
          path: 'assets/models/your_model.obj',
          scale: Vector3(1.0, 1.0, 1.0),
          rotation: Vector3(0.0, 0.0, 0.0),
          position: Vector3(0.0, 0.0, 0.0),
        ),
      ),
    );
  }
}
回到顶部