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 { class CameraMatrixOverlay extends StatefulWidget {
final FilamentController controller; final FilamentController controller;
CameraMatrixOverlay({super.key, required this.controller}); const CameraMatrixOverlay({super.key, required this.controller});
@override @override
State<StatefulWidget> createState() => _CameraMatrixOverlayState(); State<StatefulWidget> createState() => _CameraMatrixOverlayState();
@@ -18,23 +18,37 @@ class _CameraMatrixOverlayState extends State<CameraMatrixOverlay> {
String? _cameraPosition; String? _cameraPosition;
String? _cameraRotation; String? _cameraRotation;
@override void _updateTimer() {
void initState() { _cameraTimer?.cancel();
super.initState(); if (widget.controller.hasViewer.value) {
_cameraTimer = _cameraTimer =
Timer.periodic(const Duration(milliseconds: 50), (timer) async { Timer.periodic(const Duration(milliseconds: 50), (timer) async {
var cameraPosition = await widget.controller.getCameraPosition(); var cameraPosition = await widget.controller.getCameraPosition();
var cameraRotation = await widget.controller.getCameraRotation(); var cameraRotation = await widget.controller.getCameraRotation();
_cameraPosition = cameraPosition.toString();
_cameraRotation = cameraRotation.toString(); _cameraPosition =
"${cameraPosition.storage.map((v) => v.toStringAsFixed(2))}";
_cameraRotation =
"${cameraRotation.storage.map((v) => v.toStringAsFixed(2))}";
setState(() {}); setState(() {});
}); });
} }
}
@override
void initState() {
super.initState();
_updateTimer();
widget.controller.hasViewer.addListener(_updateTimer);
}
@override @override
void dispose() { void dispose() {
super.dispose(); super.dispose();
widget.controller.hasViewer.removeListener(_updateTimer);
_cameraTimer?.cancel(); _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'; import 'package:flutter_filament/filament_controller_ffi.dart';
class ControllerMenu extends StatefulWidget { class ControllerMenu extends StatefulWidget {
final FilamentController? controller;
final void Function(FilamentController controller) onControllerCreated; final void Function(FilamentController controller) onControllerCreated;
final void Function() onControllerDestroyed; final void Function() onControllerDestroyed;
ControllerMenu( ControllerMenu(
{required this.onControllerCreated, required this.onControllerDestroyed}); {this.controller,
required this.onControllerCreated,
required this.onControllerDestroyed});
@override @override
State<StatefulWidget> createState() => _ControllerMenuState(); State<StatefulWidget> createState() => _ControllerMenuState();
@@ -21,11 +24,30 @@ class _ControllerMenuState extends State<ControllerMenu> {
final FocusNode _buttonFocusNode = FocusNode(debugLabel: 'Camera Menu'); final FocusNode _buttonFocusNode = FocusNode(debugLabel: 'Camera Menu');
void _createController({String? uberArchivePath}) { void _createController({String? uberArchivePath}) {
if (_filamentController != null) {
throw Exception("Controller already exists");
}
_filamentController = _filamentController =
FilamentControllerFFI(uberArchivePath: uberArchivePath); FilamentControllerFFI(uberArchivePath: uberArchivePath);
widget.onControllerCreated(_filamentController!); 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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
var items = <Widget>[]; 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/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/controller_menu.dart';
import 'package:flutter_filament_example/example_viewport.dart'; import 'package:flutter_filament_example/example_viewport.dart';
import 'package:flutter_filament_example/picker_result_widget.dart'; import 'package:flutter_filament_example/picker_result_widget.dart';
import 'package:flutter_filament_example/scene_menu.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.dart';
import 'package:flutter_filament/filament_controller_ffi.dart'; const loadDefaultScene = bool.hasEnvironment('--load-default-scene');
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';
void main() async { void main() async {
runApp(const MyApp()); print(loadDefaultScene);
runApp(MyApp());
} }
class MyApp extends StatefulWidget { class MyApp extends StatefulWidget {
@@ -42,6 +33,8 @@ class _MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
} }
class ExampleWidget extends StatefulWidget { class ExampleWidget extends StatefulWidget {
const ExampleWidget({super.key});
@override @override
State<StatefulWidget> createState() { State<StatefulWidget> createState() {
return _ExampleWidgetState(); return _ExampleWidgetState();
@@ -53,26 +46,14 @@ enum MenuType { controller, assets, camera, misc }
class _ExampleWidgetState extends State<ExampleWidget> { class _ExampleWidgetState extends State<ExampleWidget> {
FilamentController? _filamentController; FilamentController? _filamentController;
FilamentEntity? _shapes;
FilamentEntity? _flightHelmet; FilamentEntity? _flightHelmet;
FilamentEntity? _buster; FilamentEntity? _buster;
FilamentEntity? _light; FilamentEntity? _light;
List<String>? _animations;
final weights = List.filled(255, 0.0); final weights = List.filled(255, 0.0);
bool _loop = false;
EdgeInsets _viewportMargin = EdgeInsets.zero; 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) { Widget _item(void Function() onTap, String text) {
return GestureDetector( return GestureDetector(
onTap: () { onTap: () {
@@ -86,7 +67,25 @@ class _ExampleWidgetState extends State<ExampleWidget> {
child: Text(text))); 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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@@ -104,6 +103,7 @@ class _ExampleWidgetState extends State<ExampleWidget> {
color: Colors.white, color: Colors.white,
child: Row(children: [ child: Row(children: [
ControllerMenu( ControllerMenu(
controller: _filamentController,
onControllerDestroyed: () {}, onControllerDestroyed: () {},
onControllerCreated: (controller) { onControllerCreated: (controller) {
setState(() { setState(() {
@@ -114,24 +114,22 @@ class _ExampleWidgetState extends State<ExampleWidget> {
controller: _filamentController, 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(() {
// _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 { // _item(() async {
// _animations = await _filamentController!.setCamera(_shapes!, null); // _animations = await _filamentController!.setCamera(_shapes!, null);
// setState(() {}); // setState(() {});
@@ -281,21 +279,6 @@ class _ExampleWidgetState extends State<ExampleWidget> {
// }, "play animation ${_animations!.indexOf(a)} (noreplace)"))); // }, "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([ // children.addAll([
// _item(() async { // _item(() async {
// await Permission.microphone.request(); // await Permission.microphone.request();

View File

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

View File

@@ -1,12 +1,11 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_filament/filament_controller.dart'; import 'package:flutter_filament/filament_controller.dart';
class SceneMenu extends StatefulWidget { class SceneMenu extends StatefulWidget {
final FilamentController? controller; final FilamentController? controller;
SceneMenu({super.key, required this.controller}); const SceneMenu({super.key, required this.controller});
@override @override
State<StatefulWidget> createState() { State<StatefulWidget> createState() {
@@ -15,9 +14,8 @@ class SceneMenu extends StatefulWidget {
} }
class _SceneMenuState extends State<SceneMenu> { class _SceneMenuState extends State<SceneMenu> {
FilamentEntity? _shapes; bool _postProcessing = true;
List<String>? _animations;
bool _hasSkybox = false;
final FocusNode _buttonFocusNode = FocusNode(debugLabel: 'Camera Menu'); final FocusNode _buttonFocusNode = FocusNode(debugLabel: 'Camera Menu');
@override @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() { List<MenuItemButton> _assetMenu() {
return [ return [
MenuItemButton( 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; bool _rendering = false;
int _framerate = 60; int _framerate = 60;
@@ -107,7 +208,35 @@ class _SceneMenuState extends State<SceneMenu> {
_framerate = _framerate == 60 ? 30 : 60; _framerate = _framerate == 60 ? 30 : 60;
widget.controller!.setFrameRate(_framerate); 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(), menuChildren: _assetMenu(),
child: const Text("Assets"), child: const Text("Assets"),
), ),
const SubmenuButton( SubmenuButton(
menuChildren: <Widget>[], menuChildren: _cameraMenu(),
child: Text("Camera"), child: const Text("Camera"),
), ),
], ],
builder: (BuildContext context, MenuController controller, builder: (BuildContext context, MenuController controller,

View File

@@ -163,6 +163,7 @@ namespace polyvox
double _zoomSpeed = 0.01; double _zoomSpeed = 0.01;
math::mat4f _cameraPosition; math::mat4f _cameraPosition;
math::mat4f _cameraRotation; math::mat4f _cameraRotation;
void _createManipulator();
ColorGrading *colorGrading = nullptr; ColorGrading *colorGrading = nullptr;

View File

@@ -984,7 +984,8 @@ namespace polyvox
_frameCount++; _frameCount++;
// if a manipulator is active, update the active camera orientation // if a manipulator is active, update the active camera orientation
if(_manipulator) { if (_manipulator)
{
math::double3 eye, target, upward; math::double3 eye, target, upward;
Camera &cam = _view->getCamera(); Camera &cam = _view->getCamera();
_manipulator->getLookAt(&eye, &target, &upward); _manipulator->getLookAt(&eye, &target, &upward);
@@ -1123,21 +1124,27 @@ namespace polyvox
void FilamentViewer::_createManipulator() void FilamentViewer::_createManipulator()
{ {
Camera &cam = _view->getCamera(); Camera &cam = _view->getCamera();
auto &tm = _engine->getTransformManager();
auto transformInstance = tm.getInstance(cam.getEntity());
auto transform = tm.getTransform(transformInstance);
math::double3 home = cam.getPosition(); math::double3 home = cam.getPosition();
math::double3 up = cam.getUpVector(); math::double3 up = cam.getUpVector();
math::double3 target = home + cam.getForwardVector(); auto fv = cam.getForwardVector();
math::double3 target = home + fv;
Viewport const &vp = _view->getViewport(); Viewport const &vp = _view->getViewport();
Log("Creating manipulator for viewport size %dx%d with camera norm %f", vp.width, vp.height, norm(home)); Log("Creating manipulator for viewport size %dx%d at home %f %f %f, fv %f %f %f, up %f %f %f target %f %f %f (norm %f) with _zoomSpeed %f", vp.width, vp.height, home[0], home[1], home[2], fv[0], fv[1], fv[2], up[0], up[1], up[2], target[0], target[1], target[2], norm(home), _zoomSpeed);
float zoomSpeed = norm(home) / 10;
zoomSpeed = math::clamp(zoomSpeed, 0.1f, 100000.0f);
_manipulator = Manipulator<double>::Builder() _manipulator = Manipulator<double>::Builder()
.viewport(vp.width, vp.height) .viewport(vp.width, vp.height)
.orbitHomePosition(home[0], home[1], home[2])
.upVector(up.x, up.y, up.z) .upVector(up.x, up.y, up.z)
.zoomSpeed(_zoomSpeed) .zoomSpeed(_zoomSpeed)
.orbitSpeed(_orbitSpeedX, _orbitSpeedY)
.targetPosition(target[0], target[1], target[2]) .targetPosition(target[0], target[1], target[2])
// only applicable to Mode::ORBIT
.orbitHomePosition(home[0], home[1], home[2])
.orbitSpeed(_orbitSpeedX, _orbitSpeedY)
// only applicable to Mode::FREE_FLIGHT
.flightStartPosition(home[0], home[1], home[2])
.build(_manipulatorMode); .build(_manipulatorMode);
} }
@@ -1146,7 +1153,7 @@ namespace polyvox
_manipulatorMode = mode; _manipulatorMode = mode;
_orbitSpeedX = orbitSpeedX; _orbitSpeedX = orbitSpeedX;
_orbitSpeedY = orbitSpeedY; _orbitSpeedY = orbitSpeedY;
_zoomSpeed = zoomSpeedY; _zoomSpeed = zoomSpeed;
} }
void FilamentViewer::grabBegin(float x, float y, bool pan) void FilamentViewer::grabBegin(float x, float y, bool pan)
@@ -1160,9 +1167,12 @@ namespace polyvox
{ {
_createManipulator(); _createManipulator();
} }
if(pan) { if (pan)
{
Log("Beginning pan at %f %f", x, y); Log("Beginning pan at %f %f", x, y);
} else { }
else
{
Log("Beginning rotate at %f %f", x, y); Log("Beginning rotate at %f %f", x, y);
} }
@@ -1234,9 +1244,8 @@ namespace polyvox
void FilamentViewer::pick(uint32_t x, uint32_t y, EntityId *entityId) void FilamentViewer::pick(uint32_t x, uint32_t y, EntityId *entityId)
{ {
_view->pick(x, y, [=](filament::View::PickingQueryResult const &result) { _view->pick(x, y, [=](filament::View::PickingQueryResult const &result)
*entityId = Entity::smuggle(result.renderable); { *entityId = Entity::smuggle(result.renderable); });
});
} }
} // namespace polyvox } // namespace polyvox

View File

@@ -130,7 +130,7 @@ extern "C"
return modelMatrix.asArray(); return modelMatrix.asArray();
} }
FLUTTER_PLUGIN_EXPORT void set_camera_manipulator_options(const void *const viewer, ManipulatorMode mode, double orbitSpeedX, double orbitSpeedY, double zoomSpeed) FLUTTER_PLUGIN_EXPORT void set_camera_manipulator_options(const void *const viewer, _ManipulatorMode mode, double orbitSpeedX, double orbitSpeedY, double zoomSpeed)
{ {
((FilamentViewer *)viewer)->setCameraManipulatorOptions((filament::camutils::Mode)mode, orbitSpeedX, orbitSpeedY, zoomSpeed); ((FilamentViewer *)viewer)->setCameraManipulatorOptions((filament::camutils::Mode)mode, orbitSpeedX, orbitSpeedY, zoomSpeed);
} }

View File

@@ -440,9 +440,10 @@ abstract class FilamentController {
/// ///
/// Sets the options for manipulating the camera via the viewport. /// Sets the options for manipulating the camera via the viewport.
/// ManipulatorMode.FREE_FLIGHT and ManipulatorMode.MAP are currently unsupported and will throw an exception.
/// ///
Future setCameraManipulatorOptions( Future setCameraManipulatorOptions(
{ManipulatorMode mode = ManipulatorMode.FREE_FLIGHT, {ManipulatorMode mode = ManipulatorMode.ORBIT,
double orbitSpeedX = 0.01, double orbitSpeedX = 0.01,
double orbitSpeedY = 0.01, double orbitSpeedY = 0.01,
double zoomSpeed = 0.01}); double zoomSpeed = 0.01});

View File

@@ -1012,8 +1012,11 @@ class FilamentControllerFFI extends FilamentController {
var arrayPtr = _lib.get_camera_model_matrix(_viewer!); var arrayPtr = _lib.get_camera_model_matrix(_viewer!);
var doubleList = arrayPtr.asTypedList(16); var doubleList = arrayPtr.asTypedList(16);
var modelMatrix = Matrix4.fromFloat64List(doubleList); var modelMatrix = Matrix4.fromFloat64List(doubleList);
var position = modelMatrix.getColumn(3).xyz;
calloc.free(arrayPtr); calloc.free(arrayPtr);
return modelMatrix.getColumn(3).xyz; return position;
} }
@override @override
@@ -1032,13 +1035,16 @@ class FilamentControllerFFI extends FilamentController {
@override @override
Future setCameraManipulatorOptions( Future setCameraManipulatorOptions(
{ManipulatorMode mode = ManipulatorMode.FREE_FLIGHT, {ManipulatorMode mode = ManipulatorMode.ORBIT,
double orbitSpeedX = 0.01, double orbitSpeedX = 0.01,
double orbitSpeedY = 0.01, double orbitSpeedY = 0.01,
double zoomSpeed = 0.01}) async { double zoomSpeed = 0.01}) async {
if (_viewer == null) { if (_viewer == null) {
throw Exception("No viewer available"); throw Exception("No viewer available");
} }
if (mode != ManipulatorMode.ORBIT) {
throw Exception("Manipulator mode $mode not yet implemented");
}
_lib.set_camera_manipulator_options( _lib.set_camera_manipulator_options(
_viewer!, mode.index, orbitSpeedX, orbitSpeedX, zoomSpeed); _viewer!, mode.index, orbitSpeedX, orbitSpeedX, zoomSpeed);
} }

View File

@@ -41,13 +41,18 @@ using namespace camutils;
typedef int32_t EntityId; typedef int32_t EntityId;
namespace polyvox { namespace polyvox
{
enum ToneMapping { enum ToneMapping
ACES, FILMIC, LINEAR {
ACES,
FILMIC,
LINEAR
}; };
class FilamentViewer { class FilamentViewer
{
public: public:
FilamentViewer(const void *context, const ResourceLoaderWrapper *const resourceLoaderWrapper, void *const platform = nullptr, const char *uberArchivePath = nullptr); FilamentViewer(const void *context, const ResourceLoaderWrapper *const resourceLoaderWrapper, void *const platform = nullptr, const char *uberArchivePath = nullptr);
~FilamentViewer(); ~FilamentViewer();
@@ -61,7 +66,6 @@ namespace polyvox {
void removeIbl(); void removeIbl();
void removeAsset(EntityId asset); void removeAsset(EntityId asset);
// removes all add assets from the current scene
void clearAssets(); void clearAssets();
void updateViewportAndCameraProjection(int height, int width, float scaleFactor); void updateViewportAndCameraProjection(int height, int width, float scaleFactor);
@@ -69,13 +73,11 @@ namespace polyvox {
uint64_t frameTimeInNanos, uint64_t frameTimeInNanos,
void *pixelBuffer, void *pixelBuffer,
void (*callback)(void *buf, size_t size, void *data), void (*callback)(void *buf, size_t size, void *data),
void* data void *data);
);
void setFrameInterval(float interval); void setFrameInterval(float interval);
bool setCamera(EntityId asset, const char *nodeName); bool setCamera(EntityId asset, const char *nodeName);
void createSwapChain(const void *surface, uint32_t width, uint32_t height); void createSwapChain(const void *surface, uint32_t width, uint32_t height);
void destroySwapChain(); void destroySwapChain();
@@ -87,6 +89,9 @@ namespace polyvox {
void setBackgroundImage(const char *resourcePath, bool fillHeight); void setBackgroundImage(const char *resourcePath, bool fillHeight);
void clearBackgroundImage(); void clearBackgroundImage();
void setBackgroundImagePosition(float x, float y, bool clamp); void setBackgroundImagePosition(float x, float y, bool clamp);
// Camera methods
void moveCameraToAsset(EntityId entityId); void moveCameraToAsset(EntityId entityId);
void setViewFrustumCulling(bool enabled); void setViewFrustumCulling(bool enabled);
void setCameraExposure(float aperture, float shutterSpeed, float sensitivity); void setCameraExposure(float aperture, float shutterSpeed, float sensitivity);
@@ -97,45 +102,33 @@ namespace polyvox {
void setCameraModelMatrix(const float *const matrix); void setCameraModelMatrix(const float *const matrix);
void setCameraFocalLength(float fl); void setCameraFocalLength(float fl);
void setCameraFocusDistance(float focusDistance); void setCameraFocusDistance(float focusDistance);
void setCameraManipulatorOptions(filament::camutils::Mode mode, double orbitSpeedX, double orbitSpeedY, double zoomSpeed);
void grabBegin(float x, float y, bool pan); void grabBegin(float x, float y, bool pan);
void grabUpdate(float x, float y); void grabUpdate(float x, float y);
void grabEnd(); void grabEnd();
void scrollBegin(); void scrollBegin();
void scrollUpdate(float x, float y, float delta); void scrollUpdate(float x, float y, float delta);
void scrollEnd(); void scrollEnd();
void pick(uint32_t x, uint32_t y, EntityId *entityId);
EntityId addLight(LightManager::Type t, float colour, float intensity, float posX, float posY, float posZ, float dirX, float dirY, float dirZ, bool shadows); EntityId addLight(LightManager::Type t, float colour, float intensity, float posX, float posY, float posZ, float dirX, float dirY, float dirZ, bool shadows);
void removeLight(EntityId entityId); void removeLight(EntityId entityId);
void clearLights(); void clearLights();
void setPostProcessing(bool enabled); void setPostProcessing(bool enabled);
void pick(uint32_t x, uint32_t y, EntityId* entityId);
AssetManager* const getAssetManager() { AssetManager *const getAssetManager()
{
return (AssetManager *const)_assetManager; return (AssetManager *const)_assetManager;
} }
private: private:
void createImageRenderable();
void loadResources(std::string relativeResourcePath);
void cleanup();
bool _panning = false;
float _startX;
float _startY;
math::mat4f _cameraPosition;
math::mat4f _cameraRotation;
const ResourceLoaderWrapper *const _resourceLoaderWrapper; const ResourceLoaderWrapper *const _resourceLoaderWrapper;
Scene *_scene = nullptr; Scene *_scene = nullptr;
View *_view = nullptr; View *_view = nullptr;
Engine *_engine = nullptr; Engine *_engine = nullptr;
// a default camera that we add to every scene
Camera* _mainCamera = nullptr;
Renderer *_renderer = nullptr; Renderer *_renderer = nullptr;
RenderTarget *_rt = nullptr; RenderTarget *_rt = nullptr;
Texture *_rtColor = nullptr; Texture *_rtColor = nullptr;
@@ -159,8 +152,18 @@ namespace polyvox {
bool _actualSize = false; bool _actualSize = false;
// Camera properties
Camera *_mainCamera = nullptr; // the default camera added to every scene. If you want the *active* camera, access via View.
float _cameraFocalLength = 28.0f; float _cameraFocalLength = 28.0f;
float _cameraFocusDistance = 0.0f; float _cameraFocusDistance = 0.0f;
Manipulator<double> *_manipulator = nullptr;
filament::camutils::Mode _manipulatorMode;
double _orbitSpeedX = 0.01;
double _orbitSpeedY = 0.01;
double _zoomSpeed = 0.01;
math::mat4f _cameraPosition;
math::mat4f _cameraRotation;
void _createManipulator();
ColorGrading *colorGrading = nullptr; ColorGrading *colorGrading = nullptr;
@@ -179,13 +182,8 @@ namespace polyvox {
void loadPngTexture(string path, ResourceBuffer data); void loadPngTexture(string path, ResourceBuffer data);
void loadTextureFromPath(string path); void loadTextureFromPath(string path);
Manipulator<double>* _manipulator = nullptr;
void _createManipulator();
uint32_t _lastFrameTimeInNanos; uint32_t _lastFrameTimeInNanos;
}; };
} }

View File

@@ -47,11 +47,11 @@
#include "ResourceBuffer.hpp" #include "ResourceBuffer.hpp"
typedef int32_t EntityId; typedef int32_t EntityId;
typedef int32_t _ManipulatorMode;
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
typedef int32_t EntityId;
FLUTTER_PLUGIN_EXPORT const void* create_filament_viewer(const void* const context, const ResourceLoaderWrapper* const loader, void* const platform, const char* uberArchivePath); FLUTTER_PLUGIN_EXPORT const void* create_filament_viewer(const void* const context, const ResourceLoaderWrapper* const loader, void* const platform, const char* uberArchivePath);
FLUTTER_PLUGIN_EXPORT void destroy_filament_viewer(const void* const viewer); FLUTTER_PLUGIN_EXPORT void destroy_filament_viewer(const void* const viewer);
@@ -140,6 +140,8 @@ FLUTTER_PLUGIN_EXPORT void transform_to_unit_cube(void* assetManager, EntityId a
FLUTTER_PLUGIN_EXPORT void set_position(void* assetManager, EntityId asset, float x, float y, float z); FLUTTER_PLUGIN_EXPORT void set_position(void* assetManager, EntityId asset, float x, float y, float z);
FLUTTER_PLUGIN_EXPORT void set_rotation(void* assetManager, EntityId asset, float rads, float x, float y, float z); FLUTTER_PLUGIN_EXPORT void set_rotation(void* assetManager, EntityId asset, float rads, float x, float y, float z);
FLUTTER_PLUGIN_EXPORT void set_scale(void* assetManager, EntityId asset, float scale); FLUTTER_PLUGIN_EXPORT void set_scale(void* assetManager, EntityId asset, float scale);
// Camera methods
FLUTTER_PLUGIN_EXPORT void move_camera_to_asset(const void* const viewer, EntityId asset); FLUTTER_PLUGIN_EXPORT void move_camera_to_asset(const void* const viewer, EntityId asset);
FLUTTER_PLUGIN_EXPORT void set_view_frustum_culling(const void* const viewer, bool enabled); FLUTTER_PLUGIN_EXPORT void set_view_frustum_culling(const void* const viewer, bool enabled);
FLUTTER_PLUGIN_EXPORT void set_camera_exposure(const void* const viewer, float aperture, float shutterSpeed, float sensitivity); FLUTTER_PLUGIN_EXPORT void set_camera_exposure(const void* const viewer, float aperture, float shutterSpeed, float sensitivity);
@@ -152,6 +154,9 @@ FLUTTER_PLUGIN_EXPORT const double* const get_camera_view_matrix(const void* con
FLUTTER_PLUGIN_EXPORT const double* const get_camera_projection_matrix(const void* const viewer); FLUTTER_PLUGIN_EXPORT const double* const get_camera_projection_matrix(const void* const viewer);
FLUTTER_PLUGIN_EXPORT void set_camera_focal_length(const void* const viewer, float focalLength); FLUTTER_PLUGIN_EXPORT void set_camera_focal_length(const void* const viewer, float focalLength);
FLUTTER_PLUGIN_EXPORT void set_camera_focus_distance(const void* const viewer, float focusDistance); FLUTTER_PLUGIN_EXPORT void set_camera_focus_distance(const void* const viewer, float focusDistance);
FLUTTER_PLUGIN_EXPORT void set_camera_manipulator_options(const void* const viewer, _ManipulatorMode mode, double orbitSpeedX, double orbitSpeedY, double zoomSpeed);
FLUTTER_PLUGIN_EXPORT int hide_mesh(void* assetManager, EntityId asset, const char* meshName); FLUTTER_PLUGIN_EXPORT int hide_mesh(void* assetManager, EntityId asset, const char* meshName);
FLUTTER_PLUGIN_EXPORT int reveal_mesh(void* assetManager, EntityId asset, const char* meshName); FLUTTER_PLUGIN_EXPORT int reveal_mesh(void* assetManager, EntityId asset, const char* meshName);
FLUTTER_PLUGIN_EXPORT void set_post_processing(void* const viewer, bool enabled); FLUTTER_PLUGIN_EXPORT void set_post_processing(void* const viewer, bool enabled);

View File

@@ -984,7 +984,8 @@ namespace polyvox
_frameCount++; _frameCount++;
// if a manipulator is active, update the active camera orientation // if a manipulator is active, update the active camera orientation
if(_manipulator) { if (_manipulator)
{
math::double3 eye, target, upward; math::double3 eye, target, upward;
Camera &cam = _view->getCamera(); Camera &cam = _view->getCamera();
_manipulator->getLookAt(&eye, &target, &upward); _manipulator->getLookAt(&eye, &target, &upward);
@@ -1123,22 +1124,36 @@ namespace polyvox
void FilamentViewer::_createManipulator() void FilamentViewer::_createManipulator()
{ {
Camera &cam = _view->getCamera(); Camera &cam = _view->getCamera();
auto &tm = _engine->getTransformManager();
auto transformInstance = tm.getInstance(cam.getEntity());
auto transform = tm.getTransform(transformInstance);
math::double3 home = cam.getPosition(); math::double3 home = cam.getPosition();
math::double3 up = cam.getUpVector(); math::double3 up = cam.getUpVector();
math::double3 target = home + cam.getForwardVector(); auto fv = cam.getForwardVector();
math::double3 target = home + fv;
Viewport const &vp = _view->getViewport(); Viewport const &vp = _view->getViewport();
Log("Creating manipulator for viewport size %dx%d with camera norm %f", vp.width, vp.height, norm(home)); Log("Creating manipulator for viewport size %dx%d at home %f %f %f, fv %f %f %f, up %f %f %f target %f %f %f (norm %f) with _zoomSpeed %f", vp.width, vp.height, home[0], home[1], home[2], fv[0], fv[1], fv[2], up[0], up[1], up[2], target[0], target[1], target[2], norm(home), _zoomSpeed);
float zoomSpeed = norm(home) / 10;
zoomSpeed = math::clamp(zoomSpeed, 0.1f, 100000.0f);
_manipulator = Manipulator<double>::Builder() _manipulator = Manipulator<double>::Builder()
.viewport(vp.width, vp.height) .viewport(vp.width, vp.height)
.orbitHomePosition(home[0], home[1], home[2])
.upVector(up.x, up.y, up.z) .upVector(up.x, up.y, up.z)
.zoomSpeed(zoomSpeed) .zoomSpeed(_zoomSpeed)
// .orbitSpeed(0.0001, 0.0001)
.targetPosition(target[0], target[1], target[2]) .targetPosition(target[0], target[1], target[2])
.build(Mode::ORBIT); // only applicable to Mode::ORBIT
.orbitHomePosition(home[0], home[1], home[2])
.orbitSpeed(_orbitSpeedX, _orbitSpeedY)
// only applicable to Mode::FREE_FLIGHT
.flightStartPosition(home[0], home[1], home[2])
.build(_manipulatorMode);
}
void FilamentViewer::setCameraManipulatorOptions(filament::camutils::Mode mode, double orbitSpeedX, double orbitSpeedY, double zoomSpeed)
{
_manipulatorMode = mode;
_orbitSpeedX = orbitSpeedX;
_orbitSpeedY = orbitSpeedY;
_zoomSpeed = zoomSpeed;
} }
void FilamentViewer::grabBegin(float x, float y, bool pan) void FilamentViewer::grabBegin(float x, float y, bool pan)
@@ -1152,9 +1167,12 @@ namespace polyvox
{ {
_createManipulator(); _createManipulator();
} }
if(pan) { if (pan)
{
Log("Beginning pan at %f %f", x, y); Log("Beginning pan at %f %f", x, y);
} else { }
else
{
Log("Beginning rotate at %f %f", x, y); Log("Beginning rotate at %f %f", x, y);
} }
@@ -1226,9 +1244,8 @@ namespace polyvox
void FilamentViewer::pick(uint32_t x, uint32_t y, EntityId *entityId) void FilamentViewer::pick(uint32_t x, uint32_t y, EntityId *entityId)
{ {
_view->pick(x, y, [=](filament::View::PickingQueryResult const &result) { _view->pick(x, y, [=](filament::View::PickingQueryResult const &result)
*entityId = Entity::smuggle(result.renderable); { *entityId = Entity::smuggle(result.renderable); });
});
} }
} // namespace polyvox } // namespace polyvox

View File

@@ -10,133 +10,168 @@
using namespace polyvox; using namespace polyvox;
extern "C"
extern "C" { {
#include "FlutterFilamentApi.h" #include "FlutterFilamentApi.h"
FLUTTER_PLUGIN_EXPORT const void* create_filament_viewer(const void* context, const ResourceLoaderWrapper* const loader, void* const platform, const char* uberArchivePath) { FLUTTER_PLUGIN_EXPORT const void *create_filament_viewer(const void *context, const ResourceLoaderWrapper *const loader, void *const platform, const char *uberArchivePath)
{
return (const void *)new FilamentViewer(context, loader, platform, uberArchivePath); return (const void *)new FilamentViewer(context, loader, platform, uberArchivePath);
} }
FLUTTER_PLUGIN_EXPORT ResourceLoaderWrapper* make_resource_loader(LoadFilamentResourceFromOwner loadFn, FreeFilamentResourceFromOwner freeFn, void* const owner) { FLUTTER_PLUGIN_EXPORT ResourceLoaderWrapper *make_resource_loader(LoadFilamentResourceFromOwner loadFn, FreeFilamentResourceFromOwner freeFn, void *const owner)
{
return new ResourceLoaderWrapper(loadFn, freeFn, owner); return new ResourceLoaderWrapper(loadFn, freeFn, owner);
} }
FLUTTER_PLUGIN_EXPORT void create_render_target(const void* const viewer, intptr_t texture, uint32_t width, uint32_t height) { FLUTTER_PLUGIN_EXPORT void create_render_target(const void *const viewer, intptr_t texture, uint32_t width, uint32_t height)
{
((FilamentViewer *)viewer)->createRenderTarget(texture, width, height); ((FilamentViewer *)viewer)->createRenderTarget(texture, width, height);
} }
FLUTTER_PLUGIN_EXPORT void destroy_filament_viewer(const void* const viewer) { FLUTTER_PLUGIN_EXPORT void destroy_filament_viewer(const void *const viewer)
{
delete ((FilamentViewer *)viewer); delete ((FilamentViewer *)viewer);
} }
FLUTTER_PLUGIN_EXPORT void set_background_color(const void* const viewer, const float r, const float g, const float b, const float a) { FLUTTER_PLUGIN_EXPORT void set_background_color(const void *const viewer, const float r, const float g, const float b, const float a)
{
((FilamentViewer *)viewer)->setBackgroundColor(r, g, b, a); ((FilamentViewer *)viewer)->setBackgroundColor(r, g, b, a);
} }
FLUTTER_PLUGIN_EXPORT void clear_background_image(const void* const viewer) { FLUTTER_PLUGIN_EXPORT void clear_background_image(const void *const viewer)
{
((FilamentViewer *)viewer)->clearBackgroundImage(); ((FilamentViewer *)viewer)->clearBackgroundImage();
} }
FLUTTER_PLUGIN_EXPORT void set_background_image(const void* const viewer, const char* path, bool fillHeight) { FLUTTER_PLUGIN_EXPORT void set_background_image(const void *const viewer, const char *path, bool fillHeight)
{
((FilamentViewer *)viewer)->setBackgroundImage(path, fillHeight); ((FilamentViewer *)viewer)->setBackgroundImage(path, fillHeight);
} }
FLUTTER_PLUGIN_EXPORT void set_background_image_position(const void* const viewer, float x, float y, bool clamp) { FLUTTER_PLUGIN_EXPORT void set_background_image_position(const void *const viewer, float x, float y, bool clamp)
{
((FilamentViewer *)viewer)->setBackgroundImagePosition(x, y, clamp); ((FilamentViewer *)viewer)->setBackgroundImagePosition(x, y, clamp);
} }
FLUTTER_PLUGIN_EXPORT void set_tone_mapping(const void* const viewer, int toneMapping) { FLUTTER_PLUGIN_EXPORT void set_tone_mapping(const void *const viewer, int toneMapping)
{
((FilamentViewer *)viewer)->setToneMapping((ToneMapping)toneMapping); ((FilamentViewer *)viewer)->setToneMapping((ToneMapping)toneMapping);
} }
FLUTTER_PLUGIN_EXPORT void set_bloom(const void* const viewer, float strength) { FLUTTER_PLUGIN_EXPORT void set_bloom(const void *const viewer, float strength)
{
Log("Setting bloom to %f", strength); Log("Setting bloom to %f", strength);
((FilamentViewer *)viewer)->setBloom(strength); ((FilamentViewer *)viewer)->setBloom(strength);
} }
FLUTTER_PLUGIN_EXPORT void load_skybox(const void* const viewer, const char* skyboxPath) { FLUTTER_PLUGIN_EXPORT void load_skybox(const void *const viewer, const char *skyboxPath)
{
((FilamentViewer *)viewer)->loadSkybox(skyboxPath); ((FilamentViewer *)viewer)->loadSkybox(skyboxPath);
} }
FLUTTER_PLUGIN_EXPORT void load_ibl(const void* const viewer, const char* iblPath, float intensity) { FLUTTER_PLUGIN_EXPORT void load_ibl(const void *const viewer, const char *iblPath, float intensity)
{
((FilamentViewer *)viewer)->loadIbl(iblPath, intensity); ((FilamentViewer *)viewer)->loadIbl(iblPath, intensity);
} }
FLUTTER_PLUGIN_EXPORT void remove_skybox(const void* const viewer) { FLUTTER_PLUGIN_EXPORT void remove_skybox(const void *const viewer)
{
((FilamentViewer *)viewer)->removeSkybox(); ((FilamentViewer *)viewer)->removeSkybox();
} }
FLUTTER_PLUGIN_EXPORT void remove_ibl(const void* const viewer) { FLUTTER_PLUGIN_EXPORT void remove_ibl(const void *const viewer)
{
((FilamentViewer *)viewer)->removeIbl(); ((FilamentViewer *)viewer)->removeIbl();
} }
EntityId add_light(const void* const viewer, uint8_t type, float colour, float intensity, float posX, float posY, float posZ, float dirX, float dirY, float dirZ, bool shadows) { EntityId add_light(const void *const viewer, uint8_t type, float colour, float intensity, float posX, float posY, float posZ, float dirX, float dirY, float dirZ, bool shadows)
{
return ((FilamentViewer *)viewer)->addLight((LightManager::Type)type, colour, intensity, posX, posY, posZ, dirX, dirY, dirZ, shadows); return ((FilamentViewer *)viewer)->addLight((LightManager::Type)type, colour, intensity, posX, posY, posZ, dirX, dirY, dirZ, shadows);
} }
FLUTTER_PLUGIN_EXPORT void remove_light(const void* const viewer, int32_t entityId) { FLUTTER_PLUGIN_EXPORT void remove_light(const void *const viewer, int32_t entityId)
{
((FilamentViewer *)viewer)->removeLight(entityId); ((FilamentViewer *)viewer)->removeLight(entityId);
} }
FLUTTER_PLUGIN_EXPORT void clear_lights(const void* const viewer) { FLUTTER_PLUGIN_EXPORT void clear_lights(const void *const viewer)
{
((FilamentViewer *)viewer)->clearLights(); ((FilamentViewer *)viewer)->clearLights();
} }
FLUTTER_PLUGIN_EXPORT EntityId load_glb(void* assetManager, const char* assetPath, bool unlit) { FLUTTER_PLUGIN_EXPORT EntityId load_glb(void *assetManager, const char *assetPath, bool unlit)
{
return ((AssetManager *)assetManager)->loadGlb(assetPath, unlit); return ((AssetManager *)assetManager)->loadGlb(assetPath, unlit);
} }
FLUTTER_PLUGIN_EXPORT EntityId load_gltf(void* assetManager, const char* assetPath, const char* relativePath) { FLUTTER_PLUGIN_EXPORT EntityId load_gltf(void *assetManager, const char *assetPath, const char *relativePath)
{
return ((AssetManager *)assetManager)->loadGltf(assetPath, relativePath); return ((AssetManager *)assetManager)->loadGltf(assetPath, relativePath);
} }
FLUTTER_PLUGIN_EXPORT bool set_camera(const void* const viewer, EntityId asset, const char* nodeName) { FLUTTER_PLUGIN_EXPORT bool set_camera(const void *const viewer, EntityId asset, const char *nodeName)
{
return ((FilamentViewer *)viewer)->setCamera(asset, nodeName); return ((FilamentViewer *)viewer)->setCamera(asset, nodeName);
} }
const double* const get_camera_model_matrix(const void* const viewer) { const double *const get_camera_model_matrix(const void *const viewer)
{
const auto &modelMatrix = ((FilamentViewer *)viewer)->getCameraModelMatrix(); const auto &modelMatrix = ((FilamentViewer *)viewer)->getCameraModelMatrix();
double *array = (double *)calloc(16, sizeof(double)); double *array = (double *)calloc(16, sizeof(double));
memcpy(array, modelMatrix.asArray(), 16 * sizeof(double)); memcpy(array, modelMatrix.asArray(), 16 * sizeof(double));
return array; return array;
} }
const double* const get_camera_view_matrix(const void* const viewer) { const double *const get_camera_view_matrix(const void *const viewer)
{
const auto &modelMatrix = ((FilamentViewer *)viewer)->getCameraViewMatrix(); const auto &modelMatrix = ((FilamentViewer *)viewer)->getCameraViewMatrix();
return modelMatrix.asArray(); return modelMatrix.asArray();
} }
FLUTTER_PLUGIN_EXPORT void set_view_frustum_culling(const void* const viewer, bool enabled) { FLUTTER_PLUGIN_EXPORT void set_camera_manipulator_options(const void *const viewer, _ManipulatorMode mode, double orbitSpeedX, double orbitSpeedY, double zoomSpeed)
{
((FilamentViewer *)viewer)->setCameraManipulatorOptions((filament::camutils::Mode)mode, orbitSpeedX, orbitSpeedY, zoomSpeed);
}
FLUTTER_PLUGIN_EXPORT void set_view_frustum_culling(const void *const viewer, bool enabled)
{
((FilamentViewer *)viewer)->setViewFrustumCulling(enabled); ((FilamentViewer *)viewer)->setViewFrustumCulling(enabled);
} }
FLUTTER_PLUGIN_EXPORT void move_camera_to_asset(const void* const viewer, EntityId asset) { FLUTTER_PLUGIN_EXPORT void move_camera_to_asset(const void *const viewer, EntityId asset)
{
((FilamentViewer *)viewer)->moveCameraToAsset(asset); ((FilamentViewer *)viewer)->moveCameraToAsset(asset);
} }
FLUTTER_PLUGIN_EXPORT void set_camera_focus_distance(const void* const viewer, float distance) { FLUTTER_PLUGIN_EXPORT void set_camera_focus_distance(const void *const viewer, float distance)
{
((FilamentViewer *)viewer)->setCameraFocusDistance(distance); ((FilamentViewer *)viewer)->setCameraFocusDistance(distance);
} }
FLUTTER_PLUGIN_EXPORT void set_camera_exposure(const void* const viewer, float aperture, float shutterSpeed, float sensitivity) { FLUTTER_PLUGIN_EXPORT void set_camera_exposure(const void *const viewer, float aperture, float shutterSpeed, float sensitivity)
{
((FilamentViewer *)viewer)->setCameraExposure(aperture, shutterSpeed, sensitivity); ((FilamentViewer *)viewer)->setCameraExposure(aperture, shutterSpeed, sensitivity);
} }
FLUTTER_PLUGIN_EXPORT void set_camera_position(const void* const viewer, float x, float y, float z) { FLUTTER_PLUGIN_EXPORT void set_camera_position(const void *const viewer, float x, float y, float z)
{
((FilamentViewer *)viewer)->setCameraPosition(x, y, z); ((FilamentViewer *)viewer)->setCameraPosition(x, y, z);
} }
FLUTTER_PLUGIN_EXPORT void set_camera_rotation(const void* const viewer, float rads, float x, float y, float z) { FLUTTER_PLUGIN_EXPORT void set_camera_rotation(const void *const viewer, float rads, float x, float y, float z)
{
((FilamentViewer *)viewer)->setCameraRotation(rads, x, y, z); ((FilamentViewer *)viewer)->setCameraRotation(rads, x, y, z);
} }
FLUTTER_PLUGIN_EXPORT void set_camera_model_matrix(const void* const viewer, const float* const matrix) { FLUTTER_PLUGIN_EXPORT void set_camera_model_matrix(const void *const viewer, const float *const matrix)
{
((FilamentViewer *)viewer)->setCameraModelMatrix(matrix); ((FilamentViewer *)viewer)->setCameraModelMatrix(matrix);
} }
FLUTTER_PLUGIN_EXPORT void set_camera_focal_length(const void* const viewer, float focalLength) { FLUTTER_PLUGIN_EXPORT void set_camera_focal_length(const void *const viewer, float focalLength)
{
((FilamentViewer *)viewer)->setCameraFocalLength(focalLength); ((FilamentViewer *)viewer)->setCameraFocalLength(focalLength);
} }
@@ -145,54 +180,65 @@ extern "C" {
uint64_t frameTimeInNanos, uint64_t frameTimeInNanos,
void *pixelBuffer, void *pixelBuffer,
void (*callback)(void *buf, size_t size, void *data), void (*callback)(void *buf, size_t size, void *data),
void* data) { void *data)
{
((FilamentViewer *)viewer)->render(frameTimeInNanos, pixelBuffer, callback, data); ((FilamentViewer *)viewer)->render(frameTimeInNanos, pixelBuffer, callback, data);
} }
FLUTTER_PLUGIN_EXPORT void set_frame_interval( FLUTTER_PLUGIN_EXPORT void set_frame_interval(
const void *const viewer, const void *const viewer,
float frameInterval float frameInterval)
) { {
((FilamentViewer *)viewer)->setFrameInterval(frameInterval); ((FilamentViewer *)viewer)->setFrameInterval(frameInterval);
} }
FLUTTER_PLUGIN_EXPORT void destroy_swap_chain(const void* const viewer) { FLUTTER_PLUGIN_EXPORT void destroy_swap_chain(const void *const viewer)
{
((FilamentViewer *)viewer)->destroySwapChain(); ((FilamentViewer *)viewer)->destroySwapChain();
} }
FLUTTER_PLUGIN_EXPORT void create_swap_chain(const void* const viewer, const void* const window, uint32_t width, uint32_t height) { FLUTTER_PLUGIN_EXPORT void create_swap_chain(const void *const viewer, const void *const window, uint32_t width, uint32_t height)
{
((FilamentViewer *)viewer)->createSwapChain(window, width, height); ((FilamentViewer *)viewer)->createSwapChain(window, width, height);
} }
FLUTTER_PLUGIN_EXPORT void update_viewport_and_camera_projection(const void* const viewer, uint32_t width, uint32_t height, float scaleFactor) { FLUTTER_PLUGIN_EXPORT void update_viewport_and_camera_projection(const void *const viewer, uint32_t width, uint32_t height, float scaleFactor)
{
return ((FilamentViewer *)viewer)->updateViewportAndCameraProjection(width, height, scaleFactor); return ((FilamentViewer *)viewer)->updateViewportAndCameraProjection(width, height, scaleFactor);
} }
FLUTTER_PLUGIN_EXPORT void scroll_update(const void* const viewer, float x, float y, float delta) { FLUTTER_PLUGIN_EXPORT void scroll_update(const void *const viewer, float x, float y, float delta)
{
((FilamentViewer *)viewer)->scrollUpdate(x, y, delta); ((FilamentViewer *)viewer)->scrollUpdate(x, y, delta);
} }
FLUTTER_PLUGIN_EXPORT void scroll_begin(const void* const viewer) { FLUTTER_PLUGIN_EXPORT void scroll_begin(const void *const viewer)
{
((FilamentViewer *)viewer)->scrollBegin(); ((FilamentViewer *)viewer)->scrollBegin();
} }
FLUTTER_PLUGIN_EXPORT void scroll_end(const void* const viewer) { FLUTTER_PLUGIN_EXPORT void scroll_end(const void *const viewer)
{
((FilamentViewer *)viewer)->scrollEnd(); ((FilamentViewer *)viewer)->scrollEnd();
} }
FLUTTER_PLUGIN_EXPORT void grab_begin(const void* const viewer, float x, float y, bool pan) { FLUTTER_PLUGIN_EXPORT void grab_begin(const void *const viewer, float x, float y, bool pan)
{
((FilamentViewer *)viewer)->grabBegin(x, y, pan); ((FilamentViewer *)viewer)->grabBegin(x, y, pan);
} }
FLUTTER_PLUGIN_EXPORT void grab_update(const void* const viewer, float x, float y) { FLUTTER_PLUGIN_EXPORT void grab_update(const void *const viewer, float x, float y)
{
((FilamentViewer *)viewer)->grabUpdate(x, y); ((FilamentViewer *)viewer)->grabUpdate(x, y);
} }
FLUTTER_PLUGIN_EXPORT void grab_end(const void* const viewer) { FLUTTER_PLUGIN_EXPORT void grab_end(const void *const viewer)
{
((FilamentViewer *)viewer)->grabEnd(); ((FilamentViewer *)viewer)->grabEnd();
} }
FLUTTER_PLUGIN_EXPORT void * get_asset_manager(const void* const viewer) { FLUTTER_PLUGIN_EXPORT void *get_asset_manager(const void *const viewer)
{
return (void *)((FilamentViewer *)viewer)->getAssetManager(); return (void *)((FilamentViewer *)viewer)->getAssetManager();
} }
@@ -201,7 +247,8 @@ extern "C" {
EntityId asset, EntityId asset,
const char *const entityName, const char *const entityName,
float *const weights, float *const weights,
int count) { int count)
{
// ((AssetManager*)assetManager)->setMorphTargetWeights(asset, entityName, weights, count); // ((AssetManager*)assetManager)->setMorphTargetWeights(asset, entityName, weights, count);
} }
@@ -210,19 +257,12 @@ extern "C" {
EntityId asset, EntityId asset,
const char *const entityName, const char *const entityName,
const float *const weights, const float *const weights,
const int numWeights const int numWeights)
) { {
return ((AssetManager*)assetManager)->setMorphTargetWeights( return ((AssetManager *)assetManager)->setMorphTargetWeights(asset, entityName, weights, numWeights);
asset,
entityName,
weights,
numWeights
);
} }
bool set_morph_animation( bool set_morph_animation(
void *assetManager, void *assetManager,
EntityId asset, EntityId asset,
@@ -231,17 +271,10 @@ extern "C" {
const int *const morphIndices, const int *const morphIndices,
int numMorphTargets, int numMorphTargets,
int numFrames, int numFrames,
float frameLengthInMs) { float frameLengthInMs)
{
return ((AssetManager*)assetManager)->setMorphAnimationBuffer( return ((AssetManager *)assetManager)->setMorphAnimationBuffer(asset, entityName, morphData, morphIndices, numMorphTargets, numFrames, frameLengthInMs);
asset,
entityName,
morphData,
morphIndices,
numMorphTargets,
numFrames,
frameLengthInMs
);
} }
FLUTTER_PLUGIN_EXPORT void set_bone_animation( FLUTTER_PLUGIN_EXPORT void set_bone_animation(
@@ -253,25 +286,16 @@ extern "C" {
const char **const boneNames, const char **const boneNames,
const char **const meshNames, const char **const meshNames,
int numMeshTargets, int numMeshTargets,
float frameLengthInMs) { float frameLengthInMs)
((AssetManager*)assetManager)->setBoneAnimationBuffer( {
asset, ((AssetManager *)assetManager)->setBoneAnimationBuffer(asset, frameData, numFrames, numBones, boneNames, meshNames, numMeshTargets, frameLengthInMs);
frameData,
numFrames,
numBones,
boneNames,
meshNames,
numMeshTargets,
frameLengthInMs
);
} }
FLUTTER_PLUGIN_EXPORT void set_post_processing(void* const viewer, bool enabled) { FLUTTER_PLUGIN_EXPORT void set_post_processing(void *const viewer, bool enabled)
{
((FilamentViewer *)viewer)->setPostProcessing(enabled); ((FilamentViewer *)viewer)->setPostProcessing(enabled);
} }
// void set_bone_transform( // void set_bone_transform(
// EntityId asset, // EntityId asset,
// const char* boneName, // const char* boneName,
@@ -299,7 +323,6 @@ extern "C" {
// } // }
FLUTTER_PLUGIN_EXPORT void play_animation( FLUTTER_PLUGIN_EXPORT void play_animation(
void *assetManager, void *assetManager,
EntityId asset, EntityId asset,
@@ -307,7 +330,8 @@ extern "C" {
bool loop, bool loop,
bool reverse, bool reverse,
bool replaceActive, bool replaceActive,
float crossfade) { float crossfade)
{
((AssetManager *)assetManager)->playAnimation(asset, index, loop, reverse, replaceActive, crossfade); ((AssetManager *)assetManager)->playAnimation(asset, index, loop, reverse, replaceActive, crossfade);
} }
@@ -315,18 +339,20 @@ extern "C" {
void *assetManager, void *assetManager,
EntityId asset, EntityId asset,
int animationIndex, int animationIndex,
int animationFrame) { int animationFrame)
{
// ((AssetManager*)assetManager)->setAnimationFrame(asset, animationIndex, animationFrame); // ((AssetManager*)assetManager)->setAnimationFrame(asset, animationIndex, animationFrame);
} }
float get_animation_duration(void *assetManager, EntityId asset, int animationIndex)
float get_animation_duration(void* assetManager, EntityId asset, int animationIndex) { {
return ((AssetManager *)assetManager)->getAnimationDuration(asset, animationIndex); return ((AssetManager *)assetManager)->getAnimationDuration(asset, animationIndex);
} }
int get_animation_count( int get_animation_count(
void *assetManager, void *assetManager,
EntityId asset) { EntityId asset)
{
auto names = ((AssetManager *)assetManager)->getAnimationNames(asset); auto names = ((AssetManager *)assetManager)->getAnimationNames(asset);
return (int)names->size(); return (int)names->size();
} }
@@ -335,74 +361,88 @@ extern "C" {
void *assetManager, void *assetManager,
EntityId asset, EntityId asset,
char *const outPtr, char *const outPtr,
int index int index)
) { {
auto names = ((AssetManager *)assetManager)->getAnimationNames(asset); auto names = ((AssetManager *)assetManager)->getAnimationNames(asset);
string name = names->at(index); string name = names->at(index);
strcpy(outPtr, name.c_str()); strcpy(outPtr, name.c_str());
} }
FLUTTER_PLUGIN_EXPORT int get_morph_target_name_count(void* assetManager, EntityId asset, const char* meshName) { FLUTTER_PLUGIN_EXPORT int get_morph_target_name_count(void *assetManager, EntityId asset, const char *meshName)
{
unique_ptr<vector<string>> names = ((AssetManager *)assetManager)->getMorphTargetNames(asset, meshName); unique_ptr<vector<string>> names = ((AssetManager *)assetManager)->getMorphTargetNames(asset, meshName);
return (int)names->size(); return (int)names->size();
} }
FLUTTER_PLUGIN_EXPORT void get_morph_target_name(void* assetManager, EntityId asset, const char* meshName, char* const outPtr, int index ) { FLUTTER_PLUGIN_EXPORT void get_morph_target_name(void *assetManager, EntityId asset, const char *meshName, char *const outPtr, int index)
{
unique_ptr<vector<string>> names = ((AssetManager *)assetManager)->getMorphTargetNames(asset, meshName); unique_ptr<vector<string>> names = ((AssetManager *)assetManager)->getMorphTargetNames(asset, meshName);
string name = names->at(index); string name = names->at(index);
strcpy(outPtr, name.c_str()); strcpy(outPtr, name.c_str());
} }
FLUTTER_PLUGIN_EXPORT void remove_asset(const void* const viewer, EntityId asset) { FLUTTER_PLUGIN_EXPORT void remove_asset(const void *const viewer, EntityId asset)
{
((FilamentViewer *)viewer)->removeAsset(asset); ((FilamentViewer *)viewer)->removeAsset(asset);
} }
FLUTTER_PLUGIN_EXPORT void clear_assets(const void* const viewer) { FLUTTER_PLUGIN_EXPORT void clear_assets(const void *const viewer)
{
((FilamentViewer *)viewer)->clearAssets(); ((FilamentViewer *)viewer)->clearAssets();
} }
bool set_material_color(void* assetManager, EntityId asset, const char* meshName, int materialIndex, const float r, const float g, const float b, const float a) { bool set_material_color(void *assetManager, EntityId asset, const char *meshName, int materialIndex, const float r, const float g, const float b, const float a)
{
return ((AssetManager *)assetManager)->setMaterialColor(asset, meshName, materialIndex, r, g, b, a); return ((AssetManager *)assetManager)->setMaterialColor(asset, meshName, materialIndex, r, g, b, a);
} }
FLUTTER_PLUGIN_EXPORT void transform_to_unit_cube(void* assetManager, EntityId asset) { FLUTTER_PLUGIN_EXPORT void transform_to_unit_cube(void *assetManager, EntityId asset)
{
((AssetManager *)assetManager)->transformToUnitCube(asset); ((AssetManager *)assetManager)->transformToUnitCube(asset);
} }
FLUTTER_PLUGIN_EXPORT void set_position(void* assetManager, EntityId asset, float x, float y, float z) { FLUTTER_PLUGIN_EXPORT void set_position(void *assetManager, EntityId asset, float x, float y, float z)
{
((AssetManager *)assetManager)->setPosition(asset, x, y, z); ((AssetManager *)assetManager)->setPosition(asset, x, y, z);
} }
FLUTTER_PLUGIN_EXPORT void set_rotation(void* assetManager, EntityId asset, float rads, float x, float y, float z) { FLUTTER_PLUGIN_EXPORT void set_rotation(void *assetManager, EntityId asset, float rads, float x, float y, float z)
{
((AssetManager *)assetManager)->setRotation(asset, rads, x, y, z); ((AssetManager *)assetManager)->setRotation(asset, rads, x, y, z);
} }
FLUTTER_PLUGIN_EXPORT void set_scale(void* assetManager, EntityId asset, float scale) { FLUTTER_PLUGIN_EXPORT void set_scale(void *assetManager, EntityId asset, float scale)
{
((AssetManager *)assetManager)->setScale(asset, scale); ((AssetManager *)assetManager)->setScale(asset, scale);
} }
FLUTTER_PLUGIN_EXPORT void stop_animation(void* assetManager, EntityId asset, int index) { FLUTTER_PLUGIN_EXPORT void stop_animation(void *assetManager, EntityId asset, int index)
{
((AssetManager *)assetManager)->stopAnimation(asset, index); ((AssetManager *)assetManager)->stopAnimation(asset, index);
} }
FLUTTER_PLUGIN_EXPORT int hide_mesh(void* assetManager, EntityId asset, const char* meshName) { FLUTTER_PLUGIN_EXPORT int hide_mesh(void *assetManager, EntityId asset, const char *meshName)
{
return ((AssetManager *)assetManager)->hide(asset, meshName); return ((AssetManager *)assetManager)->hide(asset, meshName);
} }
FLUTTER_PLUGIN_EXPORT int reveal_mesh(void* assetManager, EntityId asset, const char* meshName) { FLUTTER_PLUGIN_EXPORT int reveal_mesh(void *assetManager, EntityId asset, const char *meshName)
{
return ((AssetManager *)assetManager)->reveal(asset, meshName); return ((AssetManager *)assetManager)->reveal(asset, meshName);
} }
FLUTTER_PLUGIN_EXPORT void pick(void *const viewer, int x, int y, EntityId *entityId)
FLUTTER_PLUGIN_EXPORT void pick(void* const viewer, int x, int y, EntityId* entityId) { {
((FilamentViewer *)viewer)->pick(static_cast<uint32_t>(x), static_cast<uint32_t>(y), static_cast<int32_t *>(entityId)); ((FilamentViewer *)viewer)->pick(static_cast<uint32_t>(x), static_cast<uint32_t>(y), static_cast<int32_t *>(entityId));
} }
FLUTTER_PLUGIN_EXPORT const char* get_name_for_entity(void* const assetManager, const EntityId entityId) { FLUTTER_PLUGIN_EXPORT const char *get_name_for_entity(void *const assetManager, const EntityId entityId)
{
return ((AssetManager *)assetManager)->getNameForEntity(entityId); return ((AssetManager *)assetManager)->getNameForEntity(entityId);
} }
FLUTTER_PLUGIN_EXPORT void ios_dummy() { FLUTTER_PLUGIN_EXPORT void ios_dummy()
{
Log("Dummy called"); Log("Dummy called");
} }
} }