start using menu for example project & add methods for getting camera model/view matrices

This commit is contained in:
Nick Fisher
2023-11-03 22:17:39 +08:00
parent f5cc7a8174
commit 48be185bba
15 changed files with 895 additions and 729 deletions

View File

@@ -7,7 +7,7 @@ import 'package:flutter_filament/filament_controller.dart';
class CameraMatrixOverlay extends StatefulWidget {
final FilamentController controller;
CameraMatrixOverlay({super.key, required this.controller});
const CameraMatrixOverlay({super.key, required this.controller});
@override
State<StatefulWidget> createState() => _CameraMatrixOverlayState();
@@ -18,23 +18,37 @@ class _CameraMatrixOverlayState extends State<CameraMatrixOverlay> {
String? _cameraPosition;
String? _cameraRotation;
void _updateTimer() {
_cameraTimer?.cancel();
if (widget.controller.hasViewer.value) {
_cameraTimer =
Timer.periodic(const Duration(milliseconds: 50), (timer) async {
var cameraPosition = await widget.controller.getCameraPosition();
var cameraRotation = await widget.controller.getCameraRotation();
_cameraPosition =
"${cameraPosition.storage.map((v) => v.toStringAsFixed(2))}";
_cameraRotation =
"${cameraRotation.storage.map((v) => v.toStringAsFixed(2))}";
setState(() {});
});
}
}
@override
void initState() {
super.initState();
_cameraTimer =
Timer.periodic(const Duration(milliseconds: 50), (timer) async {
var cameraPosition = await widget.controller.getCameraPosition();
var cameraRotation = await widget.controller.getCameraRotation();
_cameraPosition = cameraPosition.toString();
_cameraRotation = cameraRotation.toString();
setState(() {});
});
_updateTimer();
widget.controller.hasViewer.addListener(_updateTimer);
}
@override
void dispose() {
super.dispose();
widget.controller.hasViewer.removeListener(_updateTimer);
_cameraTimer?.cancel();
}

View File

@@ -1,58 +0,0 @@
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_filament/filament_controller.dart';
class CameraMenu extends StatefulWidget {
final FilamentController? controller;
CameraMenu({super.key, required this.controller});
@override
State<StatefulWidget> createState() {
return _CameraMenuState();
}
}
class _CameraMenuState extends State<CameraMenu> {
bool _frustumCulling = true;
final FocusNode _buttonFocusNode = FocusNode(debugLabel: 'Camera Menu');
@override
void didUpdateWidget(CameraMenu oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.controller != oldWidget.controller) {
setState(() {});
}
}
@override
Widget build(BuildContext context) {
return MenuAnchor(
childFocusNode: _buttonFocusNode,
menuChildren: <Widget>[
MenuItemButton(
child: Text("Camera"),
onPressed: () {},
),
],
builder:
(BuildContext context, MenuController controller, Widget? child) {
return Align(
alignment: Alignment.bottomLeft,
child: TextButton(
onPressed: widget.controller?.hasViewer != true
? null
: () {
if (controller.isOpen) {
controller.close();
} else {
controller.open();
}
},
child: const Text("Camera"),
));
},
);
}
}

View File

@@ -6,11 +6,14 @@ import 'package:flutter_filament/filament_controller.dart';
import 'package:flutter_filament/filament_controller_ffi.dart';
class ControllerMenu extends StatefulWidget {
final FilamentController? controller;
final void Function(FilamentController controller) onControllerCreated;
final void Function() onControllerDestroyed;
ControllerMenu(
{required this.onControllerCreated, required this.onControllerDestroyed});
{this.controller,
required this.onControllerCreated,
required this.onControllerDestroyed});
@override
State<StatefulWidget> createState() => _ControllerMenuState();
@@ -21,11 +24,30 @@ class _ControllerMenuState extends State<ControllerMenu> {
final FocusNode _buttonFocusNode = FocusNode(debugLabel: 'Camera Menu');
void _createController({String? uberArchivePath}) {
if (_filamentController != null) {
throw Exception("Controller already exists");
}
_filamentController =
FilamentControllerFFI(uberArchivePath: uberArchivePath);
widget.onControllerCreated(_filamentController!);
}
@override
void initState() {
super.initState();
_filamentController = widget.controller;
}
@override
void didUpdateWidget(ControllerMenu oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.controller != _filamentController) {
setState(() {
_filamentController = widget.controller;
});
}
}
@override
Widget build(BuildContext context) {
var items = <Widget>[];

View File

@@ -1,27 +1,18 @@
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter_filament/filament_controller_ffi.dart';
import 'package:flutter_filament_example/camera_matrix_overlay.dart';
import 'package:flutter_filament_example/controller_menu.dart';
import 'package:flutter_filament_example/example_viewport.dart';
import 'package:flutter_filament_example/picker_result_widget.dart';
import 'package:flutter_filament_example/scene_menu.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:flutter_filament/animations/animation_data.dart';
import 'package:flutter_filament/filament_controller.dart';
import 'package:flutter_filament/filament_controller_ffi.dart';
import 'package:flutter_filament/animations/animation_builder.dart';
import 'package:flutter_filament/widgets/filament_gesture_detector.dart';
import 'package:flutter_filament/widgets/filament_widget.dart';
import 'camera_menu.dart';
const loadDefaultScene = bool.hasEnvironment('--load-default-scene');
void main() async {
runApp(const MyApp());
print(loadDefaultScene);
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@@ -42,6 +33,8 @@ class _MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
}
class ExampleWidget extends StatefulWidget {
const ExampleWidget({super.key});
@override
State<StatefulWidget> createState() {
return _ExampleWidgetState();
@@ -53,26 +46,14 @@ enum MenuType { controller, assets, camera, misc }
class _ExampleWidgetState extends State<ExampleWidget> {
FilamentController? _filamentController;
FilamentEntity? _shapes;
FilamentEntity? _flightHelmet;
FilamentEntity? _buster;
FilamentEntity? _light;
List<String>? _animations;
final weights = List.filled(255, 0.0);
bool _loop = false;
EdgeInsets _viewportMargin = EdgeInsets.zero;
bool _hasViewer = false;
bool _rendering = false;
int _framerate = 60;
bool _postProcessing = true;
bool _coneHidden = false;
Widget _item(void Function() onTap, String text) {
return GestureDetector(
onTap: () {
@@ -86,7 +67,25 @@ class _ExampleWidgetState extends State<ExampleWidget> {
child: Text(text)));
}
final FocusNode _buttonFocusNode = FocusNode(debugLabel: 'Menu Button');
@override
void initState() {
super.initState();
if (loadDefaultScene) {
WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
setState(() {
_filamentController = FilamentControllerFFI();
});
await Future.delayed(const Duration(milliseconds: 100));
WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
await _filamentController!.createViewer();
await _filamentController!
.loadSkybox("assets/default_env/default_env_skybox.ktx");
await _filamentController!.setRendering(true);
await _filamentController!.loadGlb("assets/shapes/shapes.glb");
});
});
}
}
@override
Widget build(BuildContext context) {
@@ -104,6 +103,7 @@ class _ExampleWidgetState extends State<ExampleWidget> {
color: Colors.white,
child: Row(children: [
ControllerMenu(
controller: _filamentController,
onControllerDestroyed: () {},
onControllerCreated: (controller) {
setState(() {
@@ -114,24 +114,22 @@ class _ExampleWidgetState extends State<ExampleWidget> {
controller: _filamentController,
)
]))),
_filamentController == null
? Container()
: Align(
alignment: Alignment.topLeft,
child: CameraMatrixOverlay(controller: _filamentController!),
),
_filamentController == null
? Container()
: Align(
alignment: Alignment.topRight,
child: PickerResultWidget(controller: _filamentController!),
)
]);
// _item(() {
// _item(() async {
// _light = await _filamentController!
// .addLight(1, 6500, 150000, 0, 1, 0, 0, -1, 0, true);
// }, "add directional light"),
// _item(() async {
// await _filamentController!.clearLights();
// }, "clear lights"),
// _item(() {
// setState(() {
// _postProcessing = !_postProcessing;
// });
// _filamentController!.setPostProcessing(_postProcessing);
// }, "${_postProcessing ? "Disable" : "Enable"} postprocessing"),
// _item(() async {
// _animations = await _filamentController!.setCamera(_shapes!, null);
// setState(() {});
@@ -281,21 +279,6 @@ class _ExampleWidgetState extends State<ExampleWidget> {
// }, "play animation ${_animations!.indexOf(a)} (noreplace)")));
// }
// children.add(_item(() {
// _filamentController!.setToneMapping(ToneMapper.LINEAR);
// }, "Set tone mapping to linear"));
// children.add(_item(() {
// _filamentController!.moveCameraToAsset(_shapes!);
// }, "Move camera to shapes asset"));
// children.add(_item(() {
// setState(() {
// _frustumCulling = !_frustumCulling;
// });
// _filamentController!.setViewFrustumCulling(_frustumCulling);
// }, "${_frustumCulling ? "Disable" : "Enable"} frustum culling"));
// children.addAll([
// _item(() async {
// await Permission.microphone.request();
@@ -350,37 +333,37 @@ class _ExampleWidgetState extends State<ExampleWidget> {
}
}
// _item(24 () async { 'rotate by pi around Y axis'),
// _item(5 () async { 'load flight helmet'),
// _item(24 () async { 'rotate by pi around Y axis'),
// _item(5 () async { 'load flight helmet'),
// _item(7 () async { 'set all weights to 1'),
// _item(8 () async { 'set all weights to 0'),
// _item(9 () async { 'play all animations'),
// _item(34 () async { 'play animation 0'),
// _item(34 () async { 'play animation 0 (noreplace)'),
// _item(35 () async { 'play animation 1'),
// _item(34 () async { 'play animation 0 (noreplace)'),
// _item(36 () async { 'play animation 2'),
// _item(34 () async { 'play animation 0 (noreplace)'),
// _item(36 () async { 'play animation 3'),
// _item(34 () async { 'play animation 3 (noreplace)'),
// _item(37 () async { 'stop animation 0'),
// _item(7 () async { 'set all weights to 1'),
// _item(8 () async { 'set all weights to 0'),
// _item(9 () async { 'play all animations'),
// _item(34 () async { 'play animation 0'),
// _item(34 () async { 'play animation 0 (noreplace)'),
// _item(35 () async { 'play animation 1'),
// _item(34 () async { 'play animation 0 (noreplace)'),
// _item(36 () async { 'play animation 2'),
// _item(34 () async { 'play animation 0 (noreplace)'),
// _item(36 () async { 'play animation 3'),
// _item(34 () async { 'play animation 3 (noreplace)'),
// _item(37 () async { 'stop animation 0'),
// _item(14 () async { 'set camera'),
// _item(15 () async { 'animate weights'),
// _item(16 () async { 'get target names'),
// _item(17 () async { 'get animation names'),
// _item(18 () async { 'pan left'),
// _item(19 () async { 'pan right'),
// _item(25 () async {
// Text(_vertical ? 'set horizontal' : 'set vertical')),
// _item(26 () async { 'set camera pos to 0,0,3'),
// _item(27 () async { 'toggle framerate'),
// _item(28 () async { 'set bg image pos'),
// _item(29 () async { 'add light'),
// _item(30 () async { 'remove light'),
// _item(31 () async { 'clear all lights'),
// _item(32 () async { 'set camera model matrix'),
// _item(14 () async { 'set camera'),
// _item(15 () async { 'animate weights'),
// _item(16 () async { 'get target names'),
// _item(17 () async { 'get animation names'),
// _item(18 () async { 'pan left'),
// _item(19 () async { 'pan right'),
// _item(25 () async {
// Text(_vertical ? 'set horizontal' : 'set vertical')),
// _item(26 () async { 'set camera pos to 0,0,3'),
// _item(27 () async { 'toggle framerate'),
// _item(28 () async { 'set bg image pos'),
// _item(29 () async { 'add light'),
// _item(30 () async { 'remove light'),
// _item(31 () async { 'clear all lights'),
// _item(32 () async { 'set camera model matrix'),
// case -1:

View File

@@ -1,11 +1,10 @@
import 'package:flutter/material.dart';
import 'package:flutter_filament/filament_controller.dart';
import 'package:flutter_filament/generated_bindings.dart';
class PickerResultWidget extends StatelessWidget {
final FilamentController controller;
const PickerResultWidget({super.key, required this.controller});
const PickerResultWidget({required this.controller, super.key});
@override
Widget build(BuildContext context) {

View File

@@ -1,12 +1,11 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_filament/filament_controller.dart';
class SceneMenu extends StatefulWidget {
final FilamentController? controller;
SceneMenu({super.key, required this.controller});
const SceneMenu({super.key, required this.controller});
@override
State<StatefulWidget> createState() {
@@ -15,9 +14,8 @@ class SceneMenu extends StatefulWidget {
}
class _SceneMenuState extends State<SceneMenu> {
FilamentEntity? _shapes;
List<String>? _animations;
bool _hasSkybox = false;
bool _postProcessing = true;
final FocusNode _buttonFocusNode = FocusNode(debugLabel: 'Camera Menu');
@override
@@ -29,6 +27,14 @@ class _SceneMenuState extends State<SceneMenu> {
}
}
bool _coneHidden = false;
FilamentEntity? _shapes;
FilamentEntity? _directionalLight;
List<String>? _animations;
bool _loop = false;
bool _hasSkybox = false;
List<MenuItemButton> _assetMenu() {
return [
MenuItemButton(
@@ -84,6 +90,101 @@ class _SceneMenuState extends State<SceneMenu> {
];
}
bool _frustumCulling = true;
ManipulatorMode _cameraManipulatorMode = ManipulatorMode.ORBIT;
double _zoomSpeed = 0.01;
double _orbitSpeedX = 0.01;
double _orbitSpeedY = 0.01;
List<Widget> _cameraMenu() {
return [
MenuItemButton(
onPressed: () {
widget.controller!.moveCameraToAsset(_shapes!);
},
child: const Text("Move camera to shapes asset"),
),
MenuItemButton(
onPressed: () {
setState(() {
_frustumCulling = !_frustumCulling;
});
widget.controller!.setViewFrustumCulling(_frustumCulling);
},
child:
Text("${_frustumCulling ? "Disable" : "Enable"} frustum culling"),
),
SubmenuButton(
menuChildren: ManipulatorMode.values.map((mm) {
return MenuItemButton(
onPressed: () {
_cameraManipulatorMode = mm;
widget.controller!.setCameraManipulatorOptions(
mode: _cameraManipulatorMode,
orbitSpeedX: _orbitSpeedX,
orbitSpeedY: _orbitSpeedY,
zoomSpeed: _zoomSpeed);
setState(() {});
},
child: Text(
mm.name,
style: TextStyle(
fontWeight: _cameraManipulatorMode == mm
? FontWeight.bold
: FontWeight.normal),
),
);
}).toList(),
child: Text("Manipulator mode")),
SubmenuButton(
menuChildren: [0.01, 0.1, 1.0, 10.0, 100.0].map((speed) {
return MenuItemButton(
onPressed: () {
_zoomSpeed = speed;
widget.controller!.setCameraManipulatorOptions(
mode: _cameraManipulatorMode,
orbitSpeedX: _orbitSpeedX,
orbitSpeedY: _orbitSpeedY,
zoomSpeed: _zoomSpeed);
setState(() {});
},
child: Text(
speed.toString(),
style: TextStyle(
fontWeight: (speed - _zoomSpeed).abs() < 0.0001
? FontWeight.bold
: FontWeight.normal),
),
);
}).toList(),
child: const Text("Zoom speed")),
SubmenuButton(
menuChildren: [0.001, 0.01, 0.1, 1.0].map((speed) {
return MenuItemButton(
onPressed: () {
_orbitSpeedX = speed;
_orbitSpeedY = speed;
widget.controller!.setCameraManipulatorOptions(
mode: _cameraManipulatorMode,
orbitSpeedX: _orbitSpeedX,
orbitSpeedY: _orbitSpeedY,
zoomSpeed: _zoomSpeed);
setState(() {});
},
child: Text(
speed.toString(),
style: TextStyle(
fontWeight: (speed - _orbitSpeedX).abs() < 0.0001
? FontWeight.bold
: FontWeight.normal),
),
);
}).toList(),
child: const Text("Orbit speed (X & Y)"))
];
}
bool _rendering = false;
int _framerate = 60;
@@ -107,7 +208,35 @@ class _SceneMenuState extends State<SceneMenu> {
_framerate = _framerate == 60 ? 30 : 60;
widget.controller!.setFrameRate(_framerate);
},
child: const Text("Toggle framerate (currently ) "),
child: Text("Toggle framerate (currently $_framerate) "),
),
MenuItemButton(
onPressed: () {
widget.controller!.setToneMapping(ToneMapper.LINEAR);
},
child: const Text("Set tone mapping to linear"),
),
MenuItemButton(
onPressed: () {
setState(() {
_postProcessing = !_postProcessing;
});
widget.controller!.setPostProcessing(_postProcessing);
},
child: Text("${_postProcessing ? "Disable" : "Enable"} postprocessing"),
),
MenuItemButton(
onPressed: () async {
_directionalLight = await widget.controller!
.addLight(1, 6500, 150000, 0, 1, 0, 0, -1, 0, true);
},
child: const Text("add directional light"),
),
MenuItemButton(
onPressed: () async {
await widget.controller!.clearLights();
},
child: const Text("clear all lights"),
),
];
}
@@ -129,9 +258,9 @@ class _SceneMenuState extends State<SceneMenu> {
menuChildren: _assetMenu(),
child: const Text("Assets"),
),
const SubmenuButton(
menuChildren: <Widget>[],
child: Text("Camera"),
SubmenuButton(
menuChildren: _cameraMenu(),
child: const Text("Camera"),
),
],
builder: (BuildContext context, MenuController controller,