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,

View File

@@ -110,7 +110,7 @@ namespace polyvox
void scrollUpdate(float x, float y, float delta);
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);
void removeLight(EntityId entityId);
void clearLights();
@@ -163,6 +163,7 @@ namespace polyvox
double _zoomSpeed = 0.01;
math::mat4f _cameraPosition;
math::mat4f _cameraRotation;
void _createManipulator();
ColorGrading *colorGrading = nullptr;

View File

@@ -984,9 +984,10 @@ namespace polyvox
_frameCount++;
// if a manipulator is active, update the active camera orientation
if(_manipulator) {
if (_manipulator)
{
math::double3 eye, target, upward;
Camera& cam =_view->getCamera();
Camera &cam = _view->getCamera();
_manipulator->getLookAt(&eye, &target, &upward);
cam.lookAt(eye, target, upward);
}
@@ -1007,17 +1008,17 @@ namespace polyvox
// }
// else
// {
// Render the scene, unless the renderer wants to skip the frame.
if (_renderer->beginFrame(_swapChain, frameTimeInNanos))
{
_renderer->render(_view);
_renderer->endFrame();
}
else
{
// std::cout << "Skipped" << std::endl;
// skipped frame
}
// Render the scene, unless the renderer wants to skip the frame.
if (_renderer->beginFrame(_swapChain, frameTimeInNanos))
{
_renderer->render(_view);
_renderer->endFrame();
}
else
{
// std::cout << "Skipped" << std::endl;
// skipped frame
}
// }
}
@@ -1110,34 +1111,40 @@ namespace polyvox
const math::mat4 FilamentViewer::getCameraModelMatrix()
{
const auto& cam = _view->getCamera();
const auto &cam = _view->getCamera();
return cam.getModelMatrix();
}
const math::mat4 FilamentViewer::getCameraViewMatrix()
{
const auto& cam = _view->getCamera();
const auto &cam = _view->getCamera();
return cam.getViewMatrix();
}
void FilamentViewer::_createManipulator()
{
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 up = cam.getUpVector();
math::double3 target = home + cam.getForwardVector();
auto fv = cam.getForwardVector();
math::double3 target = home + fv;
Viewport const &vp = _view->getViewport();
Log("Creating manipulator for viewport size %dx%d with camera norm %f", vp.width, vp.height, norm(home));
float zoomSpeed = norm(home) / 10;
zoomSpeed = math::clamp(zoomSpeed, 0.1f, 100000.0f);
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);
_manipulator = Manipulator<double>::Builder()
.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)
.orbitSpeed(_orbitSpeedX, _orbitSpeedY)
.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);
}
@@ -1146,7 +1153,7 @@ namespace polyvox
_manipulatorMode = mode;
_orbitSpeedX = orbitSpeedX;
_orbitSpeedY = orbitSpeedY;
_zoomSpeed = zoomSpeedY;
_zoomSpeed = zoomSpeed;
}
void FilamentViewer::grabBegin(float x, float y, bool pan)
@@ -1160,12 +1167,15 @@ namespace polyvox
{
_createManipulator();
}
if(pan) {
if (pan)
{
Log("Beginning pan at %f %f", x, y);
} else {
}
else
{
Log("Beginning rotate at %f %f", x, y);
}
_manipulator->grabBegin(x, y, pan);
}
@@ -1234,9 +1244,8 @@ namespace polyvox
void FilamentViewer::pick(uint32_t x, uint32_t y, EntityId *entityId)
{
_view->pick(x, y, [=](filament::View::PickingQueryResult const &result) {
*entityId = Entity::smuggle(result.renderable);
});
_view->pick(x, y, [=](filament::View::PickingQueryResult const &result)
{ *entityId = Entity::smuggle(result.renderable); });
}
} // namespace polyvox

View File

@@ -130,7 +130,7 @@ extern "C"
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);
}

View File

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

View File

@@ -41,151 +41,149 @@ using namespace camutils;
typedef int32_t EntityId;
namespace polyvox {
namespace polyvox
{
enum ToneMapping {
ACES, FILMIC, LINEAR
};
class FilamentViewer {
public:
FilamentViewer(const void* context, const ResourceLoaderWrapper* const resourceLoaderWrapper, void* const platform=nullptr, const char* uberArchivePath=nullptr);
~FilamentViewer();
void setToneMapping(ToneMapping toneMapping);
void setBloom(float strength);
void loadSkybox(const char* const skyboxUri);
void removeSkybox();
void loadIbl(const char* const iblUri, float intensity);
void removeIbl();
void removeAsset(EntityId asset);
// removes all add assets from the current scene
void clearAssets();
void updateViewportAndCameraProjection(int height, int width, float scaleFactor);
void render(
uint64_t frameTimeInNanos,
void* pixelBuffer,
void (*callback)(void *buf, size_t size, void *data),
void* data
);
void setFrameInterval(float interval);
bool setCamera(EntityId asset, const char* nodeName);
void createSwapChain(const void* surface, uint32_t width, uint32_t height);
void destroySwapChain();
void createRenderTarget(intptr_t textureId, uint32_t width,uint32_t height);
Renderer* getRenderer();
void setBackgroundColor(const float r, const float g, const float b, const float a);
void setBackgroundImage(const char* resourcePath, bool fillHeight);
void clearBackgroundImage();
void setBackgroundImagePosition(float x, float y, bool clamp);
void moveCameraToAsset(EntityId entityId);
void setViewFrustumCulling(bool enabled);
void setCameraExposure(float aperture, float shutterSpeed, float sensitivity);
void setCameraPosition(float x, float y, float z);
void setCameraRotation(float rads, float x, float y, float z);
const math::mat4 getCameraModelMatrix();
const math::mat4 getCameraViewMatrix();
void setCameraModelMatrix(const float* const matrix);
void setCameraFocalLength(float fl);
void setCameraFocusDistance(float focusDistance);
void grabBegin(float x, float y, bool pan);
void grabUpdate(float x, float y);
void grabEnd();
void scrollBegin();
void scrollUpdate(float x, float y, float delta);
void scrollEnd();
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 clearLights();
void setPostProcessing(bool enabled);
void pick(uint32_t x, uint32_t y, EntityId* entityId);
AssetManager* const getAssetManager() {
return (AssetManager* const) _assetManager;
}
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;
Scene* _scene = nullptr;
View* _view = nullptr;
Engine* _engine = nullptr;
// a default camera that we add to every scene
Camera* _mainCamera = nullptr;
Renderer* _renderer = nullptr;
RenderTarget* _rt = nullptr;
Texture* _rtColor = nullptr;
Texture* _rtDepth = nullptr;
SwapChain* _swapChain = nullptr;
AssetManager* _assetManager = nullptr;
NameComponentManager* _ncm = nullptr;
std::mutex mtx; // mutex to ensure thread safety when removing assets
vector<utils::Entity> _lights;
Texture* _skyboxTexture = nullptr;
Skybox* _skybox = nullptr;
Texture* _iblTexture = nullptr;
IndirectLight* _indirectLight = nullptr;
bool _recomputeAabb = false;
bool _actualSize = false;
float _cameraFocalLength = 28.0f;
float _cameraFocusDistance = 0.0f;
ColorGrading *colorGrading = nullptr;
// background image properties
uint32_t _imageHeight = 0;
uint32_t _imageWidth = 0;
mat4f _imageScale;
Texture* _imageTexture = nullptr;
utils::Entity* _imageEntity = nullptr;
VertexBuffer* _imageVb = nullptr;
IndexBuffer* _imageIb = nullptr;
Material* _imageMaterial = nullptr;
TextureSampler _imageSampler;
void loadKtx2Texture(string path, ResourceBuffer data);
void loadKtxTexture(string path, ResourceBuffer data);
void loadPngTexture(string path, ResourceBuffer data);
void loadTextureFromPath(string path);
Manipulator<double>* _manipulator = nullptr;
void _createManipulator();
uint32_t _lastFrameTimeInNanos;
enum ToneMapping
{
ACES,
FILMIC,
LINEAR
};
class FilamentViewer
{
public:
FilamentViewer(const void *context, const ResourceLoaderWrapper *const resourceLoaderWrapper, void *const platform = nullptr, const char *uberArchivePath = nullptr);
~FilamentViewer();
void setToneMapping(ToneMapping toneMapping);
void setBloom(float strength);
void loadSkybox(const char *const skyboxUri);
void removeSkybox();
void loadIbl(const char *const iblUri, float intensity);
void removeIbl();
void removeAsset(EntityId asset);
void clearAssets();
void updateViewportAndCameraProjection(int height, int width, float scaleFactor);
void render(
uint64_t frameTimeInNanos,
void *pixelBuffer,
void (*callback)(void *buf, size_t size, void *data),
void *data);
void setFrameInterval(float interval);
bool setCamera(EntityId asset, const char *nodeName);
void createSwapChain(const void *surface, uint32_t width, uint32_t height);
void destroySwapChain();
void createRenderTarget(intptr_t textureId, uint32_t width, uint32_t height);
Renderer *getRenderer();
void setBackgroundColor(const float r, const float g, const float b, const float a);
void setBackgroundImage(const char *resourcePath, bool fillHeight);
void clearBackgroundImage();
void setBackgroundImagePosition(float x, float y, bool clamp);
// Camera methods
void moveCameraToAsset(EntityId entityId);
void setViewFrustumCulling(bool enabled);
void setCameraExposure(float aperture, float shutterSpeed, float sensitivity);
void setCameraPosition(float x, float y, float z);
void setCameraRotation(float rads, float x, float y, float z);
const math::mat4 getCameraModelMatrix();
const math::mat4 getCameraViewMatrix();
void setCameraModelMatrix(const float *const matrix);
void setCameraFocalLength(float fl);
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 grabUpdate(float x, float y);
void grabEnd();
void scrollBegin();
void scrollUpdate(float x, float y, float delta);
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);
void removeLight(EntityId entityId);
void clearLights();
void setPostProcessing(bool enabled);
AssetManager *const getAssetManager()
{
return (AssetManager *const)_assetManager;
}
private:
const ResourceLoaderWrapper *const _resourceLoaderWrapper;
Scene *_scene = nullptr;
View *_view = nullptr;
Engine *_engine = nullptr;
Renderer *_renderer = nullptr;
RenderTarget *_rt = nullptr;
Texture *_rtColor = nullptr;
Texture *_rtDepth = nullptr;
SwapChain *_swapChain = nullptr;
AssetManager *_assetManager = nullptr;
NameComponentManager *_ncm = nullptr;
std::mutex mtx; // mutex to ensure thread safety when removing assets
vector<utils::Entity> _lights;
Texture *_skyboxTexture = nullptr;
Skybox *_skybox = nullptr;
Texture *_iblTexture = nullptr;
IndirectLight *_indirectLight = nullptr;
bool _recomputeAabb = 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 _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;
// background image properties
uint32_t _imageHeight = 0;
uint32_t _imageWidth = 0;
mat4f _imageScale;
Texture *_imageTexture = nullptr;
utils::Entity *_imageEntity = nullptr;
VertexBuffer *_imageVb = nullptr;
IndexBuffer *_imageIb = nullptr;
Material *_imageMaterial = nullptr;
TextureSampler _imageSampler;
void loadKtx2Texture(string path, ResourceBuffer data);
void loadKtxTexture(string path, ResourceBuffer data);
void loadPngTexture(string path, ResourceBuffer data);
void loadTextureFromPath(string path);
uint32_t _lastFrameTimeInNanos;
};
}

View File

@@ -47,11 +47,11 @@
#include "ResourceBuffer.hpp"
typedef int32_t EntityId;
typedef int32_t _ManipulatorMode;
#ifdef __cplusplus
extern "C" {
#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 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_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);
// Camera methods
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_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 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_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 reveal_mesh(void* assetManager, EntityId asset, const char* meshName);
FLUTTER_PLUGIN_EXPORT void set_post_processing(void* const viewer, bool enabled);

View File

@@ -984,9 +984,10 @@ namespace polyvox
_frameCount++;
// if a manipulator is active, update the active camera orientation
if(_manipulator) {
if (_manipulator)
{
math::double3 eye, target, upward;
Camera& cam =_view->getCamera();
Camera &cam = _view->getCamera();
_manipulator->getLookAt(&eye, &target, &upward);
cam.lookAt(eye, target, upward);
}
@@ -1007,17 +1008,17 @@ namespace polyvox
// }
// else
// {
// Render the scene, unless the renderer wants to skip the frame.
if (_renderer->beginFrame(_swapChain, frameTimeInNanos))
{
_renderer->render(_view);
_renderer->endFrame();
}
else
{
// std::cout << "Skipped" << std::endl;
// skipped frame
}
// Render the scene, unless the renderer wants to skip the frame.
if (_renderer->beginFrame(_swapChain, frameTimeInNanos))
{
_renderer->render(_view);
_renderer->endFrame();
}
else
{
// std::cout << "Skipped" << std::endl;
// skipped frame
}
// }
}
@@ -1110,35 +1111,49 @@ namespace polyvox
const math::mat4 FilamentViewer::getCameraModelMatrix()
{
const auto& cam = _view->getCamera();
const auto &cam = _view->getCamera();
return cam.getModelMatrix();
}
const math::mat4 FilamentViewer::getCameraViewMatrix()
{
const auto& cam = _view->getCamera();
const auto &cam = _view->getCamera();
return cam.getViewMatrix();
}
void FilamentViewer::_createManipulator()
{
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 up = cam.getUpVector();
math::double3 target = home + cam.getForwardVector();
auto fv = cam.getForwardVector();
math::double3 target = home + fv;
Viewport const &vp = _view->getViewport();
Log("Creating manipulator for viewport size %dx%d with camera norm %f", vp.width, vp.height, norm(home));
float zoomSpeed = norm(home) / 10;
zoomSpeed = math::clamp(zoomSpeed, 0.1f, 100000.0f);
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);
_manipulator = Manipulator<double>::Builder()
.viewport(vp.width, vp.height)
.orbitHomePosition(home[0], home[1], home[2])
.upVector(up.x, up.y, up.z)
.zoomSpeed(zoomSpeed)
// .orbitSpeed(0.0001, 0.0001)
.upVector(up.x, up.y, up.z)
.zoomSpeed(_zoomSpeed)
.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)
@@ -1152,12 +1167,15 @@ namespace polyvox
{
_createManipulator();
}
if(pan) {
if (pan)
{
Log("Beginning pan at %f %f", x, y);
} else {
}
else
{
Log("Beginning rotate at %f %f", x, y);
}
_manipulator->grabBegin(x, y, pan);
}
@@ -1226,9 +1244,8 @@ namespace polyvox
void FilamentViewer::pick(uint32_t x, uint32_t y, EntityId *entityId)
{
_view->pick(x, y, [=](filament::View::PickingQueryResult const &result) {
*entityId = Entity::smuggle(result.renderable);
});
_view->pick(x, y, [=](filament::View::PickingQueryResult const &result)
{ *entityId = Entity::smuggle(result.renderable); });
}
} // namespace polyvox

View File

@@ -10,399 +10,439 @@
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) {
return (const void*) new FilamentViewer(context, loader, platform, 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);
}
FLUTTER_PLUGIN_EXPORT ResourceLoaderWrapper* make_resource_loader(LoadFilamentResourceFromOwner loadFn, FreeFilamentResourceFromOwner freeFn, void* const owner) {
return new ResourceLoaderWrapper(loadFn, freeFn, owner);
}
FLUTTER_PLUGIN_EXPORT ResourceLoaderWrapper *make_resource_loader(LoadFilamentResourceFromOwner loadFn, FreeFilamentResourceFromOwner freeFn, void *const 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) {
((FilamentViewer*)viewer)->createRenderTarget(texture, width, height);
}
FLUTTER_PLUGIN_EXPORT void destroy_filament_viewer(const void* const viewer) {
delete((FilamentViewer*)viewer);
}
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);
}
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);
}
FLUTTER_PLUGIN_EXPORT void destroy_filament_viewer(const void *const viewer)
{
delete ((FilamentViewer *)viewer);
}
FLUTTER_PLUGIN_EXPORT void clear_background_image(const void* const viewer) {
((FilamentViewer*)viewer)->clearBackgroundImage();
}
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);
}
FLUTTER_PLUGIN_EXPORT void set_background_image(const void* const viewer, const char* path, bool fillHeight) {
((FilamentViewer*)viewer)->setBackgroundImage(path, fillHeight);
}
FLUTTER_PLUGIN_EXPORT void clear_background_image(const void *const viewer)
{
((FilamentViewer *)viewer)->clearBackgroundImage();
}
FLUTTER_PLUGIN_EXPORT void set_background_image_position(const void* const viewer, float x, float y, bool clamp) {
((FilamentViewer*)viewer)->setBackgroundImagePosition(x, y, clamp);
}
FLUTTER_PLUGIN_EXPORT void set_background_image(const void *const viewer, const char *path, bool fillHeight)
{
((FilamentViewer *)viewer)->setBackgroundImage(path, fillHeight);
}
FLUTTER_PLUGIN_EXPORT void set_tone_mapping(const void* const viewer, int toneMapping) {
((FilamentViewer*)viewer)->setToneMapping((ToneMapping)toneMapping);
}
FLUTTER_PLUGIN_EXPORT void set_background_image_position(const void *const viewer, float x, float y, bool clamp)
{
((FilamentViewer *)viewer)->setBackgroundImagePosition(x, y, clamp);
}
FLUTTER_PLUGIN_EXPORT void set_bloom(const void* const viewer, float strength) {
FLUTTER_PLUGIN_EXPORT void set_tone_mapping(const void *const viewer, int toneMapping)
{
((FilamentViewer *)viewer)->setToneMapping((ToneMapping)toneMapping);
}
FLUTTER_PLUGIN_EXPORT void set_bloom(const void *const viewer, float strength)
{
Log("Setting bloom to %f", strength);
((FilamentViewer*)viewer)->setBloom(strength);
}
FLUTTER_PLUGIN_EXPORT void load_skybox(const void* const viewer, const char* skyboxPath) {
((FilamentViewer*)viewer)->loadSkybox(skyboxPath);
}
FLUTTER_PLUGIN_EXPORT void load_ibl(const void* const viewer, const char* iblPath, float intensity) {
((FilamentViewer*)viewer)->loadIbl(iblPath, intensity);
}
FLUTTER_PLUGIN_EXPORT void remove_skybox(const void* const viewer) {
((FilamentViewer*)viewer)->removeSkybox();
}
FLUTTER_PLUGIN_EXPORT void remove_ibl(const void* const viewer) {
((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) {
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) {
((FilamentViewer*)viewer)->removeLight(entityId);
}
FLUTTER_PLUGIN_EXPORT void clear_lights(const void* const viewer) {
((FilamentViewer*)viewer)->clearLights();
}
FLUTTER_PLUGIN_EXPORT EntityId load_glb(void* assetManager, const char* assetPath, bool unlit) {
return ((AssetManager*)assetManager)->loadGlb(assetPath, unlit);
}
FLUTTER_PLUGIN_EXPORT EntityId load_gltf(void* assetManager, const char* assetPath, const char* relativePath) {
return ((AssetManager*)assetManager)->loadGltf(assetPath, relativePath);
}
FLUTTER_PLUGIN_EXPORT bool set_camera(const void* const viewer, EntityId asset, const char* nodeName) {
return ((FilamentViewer*)viewer)->setCamera(asset, nodeName);
}
const double* const get_camera_model_matrix(const void* const viewer) {
const auto& modelMatrix = ((FilamentViewer*)viewer)->getCameraModelMatrix();
double* array = (double*)calloc(16, sizeof(double));
memcpy(array, modelMatrix.asArray(), 16 * sizeof(double));
return array;
}
const double* const get_camera_view_matrix(const void* const viewer) {
const auto& modelMatrix = ((FilamentViewer*)viewer)->getCameraViewMatrix();
return modelMatrix.asArray();
}
FLUTTER_PLUGIN_EXPORT void set_view_frustum_culling(const void* const viewer, bool enabled) {
((FilamentViewer*)viewer)->setViewFrustumCulling(enabled);
}
FLUTTER_PLUGIN_EXPORT void move_camera_to_asset(const void* const viewer, EntityId asset) {
((FilamentViewer*)viewer)->moveCameraToAsset(asset);
}
FLUTTER_PLUGIN_EXPORT void set_camera_focus_distance(const void* const viewer, float distance) {
((FilamentViewer*)viewer)->setCameraFocusDistance(distance);
}
FLUTTER_PLUGIN_EXPORT void set_camera_exposure(const void* const viewer, float aperture, float shutterSpeed, float sensitivity) {
((FilamentViewer*)viewer)->setCameraExposure(aperture, shutterSpeed, sensitivity);
}
FLUTTER_PLUGIN_EXPORT void set_camera_position(const void* const viewer, float x, float y, float 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) {
((FilamentViewer*)viewer)->setCameraRotation(rads, x, y, z);
}
FLUTTER_PLUGIN_EXPORT void set_camera_model_matrix(const void* const viewer, const float* const matrix) {
((FilamentViewer*)viewer)->setCameraModelMatrix(matrix);
}
FLUTTER_PLUGIN_EXPORT void set_camera_focal_length(const void* const viewer, float focalLength) {
((FilamentViewer*)viewer)->setCameraFocalLength(focalLength);
}
FLUTTER_PLUGIN_EXPORT void render(
const void* const viewer,
uint64_t frameTimeInNanos,
void* pixelBuffer,
void (*callback)(void *buf, size_t size, void *data),
void* data) {
((FilamentViewer*)viewer)->render(frameTimeInNanos, pixelBuffer, callback, data);
}
FLUTTER_PLUGIN_EXPORT void set_frame_interval(
const void* const viewer,
float frameInterval
) {
((FilamentViewer*)viewer)->setFrameInterval(frameInterval);
}
FLUTTER_PLUGIN_EXPORT void destroy_swap_chain(const void* const viewer) {
((FilamentViewer*)viewer)->destroySwapChain();
}
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);
}
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);
}
FLUTTER_PLUGIN_EXPORT void scroll_update(const void* const viewer, float x, float y, float delta) {
((FilamentViewer*)viewer)->scrollUpdate(x, y, delta);
}
FLUTTER_PLUGIN_EXPORT void scroll_begin(const void* const viewer) {
((FilamentViewer*)viewer)->scrollBegin();
}
FLUTTER_PLUGIN_EXPORT void scroll_end(const void* const viewer) {
((FilamentViewer*)viewer)->scrollEnd();
}
FLUTTER_PLUGIN_EXPORT void grab_begin(const void* const viewer, float x, float y, bool pan) {
((FilamentViewer*)viewer)->grabBegin(x, y, pan);
}
FLUTTER_PLUGIN_EXPORT void grab_update(const void* const viewer, float x, float y) {
((FilamentViewer*)viewer)->grabUpdate(x, y);
}
FLUTTER_PLUGIN_EXPORT void grab_end(const void* const viewer) {
((FilamentViewer*)viewer)->grabEnd();
}
FLUTTER_PLUGIN_EXPORT void * get_asset_manager(const void* const viewer) {
return (void*)((FilamentViewer*)viewer)->getAssetManager();
}
FLUTTER_PLUGIN_EXPORT void apply_weights(
void* assetManager,
EntityId asset,
const char* const entityName,
float* const weights,
int count) {
// ((AssetManager*)assetManager)->setMorphTargetWeights(asset, entityName, weights, count);
}
FLUTTER_PLUGIN_EXPORT void set_morph_target_weights(
void* assetManager,
EntityId asset,
const char* const entityName,
const float* const weights,
const int numWeights
) {
return ((AssetManager*)assetManager)->setMorphTargetWeights(
asset,
entityName,
weights,
numWeights
);
}
bool set_morph_animation(
void* assetManager,
EntityId asset,
const char* const entityName,
const float* const morphData,
const int* const morphIndices,
int numMorphTargets,
int numFrames,
float frameLengthInMs) {
return ((AssetManager*)assetManager)->setMorphAnimationBuffer(
asset,
entityName,
morphData,
morphIndices,
numMorphTargets,
numFrames,
frameLengthInMs
);
}
FLUTTER_PLUGIN_EXPORT void set_bone_animation(
void* assetManager,
EntityId asset,
const float* const frameData,
int numFrames,
int numBones,
const char** const boneNames,
const char** const meshNames,
int numMeshTargets,
float frameLengthInMs) {
((AssetManager*)assetManager)->setBoneAnimationBuffer(
asset,
frameData,
numFrames,
numBones,
boneNames,
meshNames,
numMeshTargets,
frameLengthInMs
);
((FilamentViewer *)viewer)->setBloom(strength);
}
FLUTTER_PLUGIN_EXPORT void set_post_processing(void* const viewer, bool enabled) {
((FilamentViewer*)viewer)->setPostProcessing(enabled);
FLUTTER_PLUGIN_EXPORT void load_skybox(const void *const viewer, const char *skyboxPath)
{
((FilamentViewer *)viewer)->loadSkybox(skyboxPath);
}
FLUTTER_PLUGIN_EXPORT void load_ibl(const void *const viewer, const char *iblPath, float intensity)
{
((FilamentViewer *)viewer)->loadIbl(iblPath, intensity);
}
FLUTTER_PLUGIN_EXPORT void remove_skybox(const void *const viewer)
{
((FilamentViewer *)viewer)->removeSkybox();
}
// void set_bone_transform(
// EntityId asset,
// const char* boneName,
// const char* entityName,
// float transX,
// float transY,
// float transZ,
// float quatX,
// float quatY,
// float quatZ,
// float quatW
// ) {
// ((AssetManager*)assetManager)->setBoneTransform(
// boneName,
// entityName,
// transX,
// transY,
// transZ,
// quatX,
// quatY,
// quatZ,
// quatW,
// false
// );
FLUTTER_PLUGIN_EXPORT void remove_ibl(const void *const viewer)
{
((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)
{
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)
{
((FilamentViewer *)viewer)->removeLight(entityId);
}
FLUTTER_PLUGIN_EXPORT void play_animation(
void* assetManager,
EntityId asset,
int index,
bool loop,
bool reverse,
bool replaceActive,
float crossfade) {
((AssetManager*)assetManager)->playAnimation(asset, index, loop, reverse, replaceActive, crossfade);
}
FLUTTER_PLUGIN_EXPORT void clear_lights(const void *const viewer)
{
((FilamentViewer *)viewer)->clearLights();
}
FLUTTER_PLUGIN_EXPORT void set_animation_frame(
void* assetManager,
EntityId asset,
int animationIndex,
int animationFrame) {
// ((AssetManager*)assetManager)->setAnimationFrame(asset, animationIndex, animationFrame);
}
FLUTTER_PLUGIN_EXPORT EntityId load_glb(void *assetManager, const char *assetPath, bool unlit)
{
return ((AssetManager *)assetManager)->loadGlb(assetPath, unlit);
}
FLUTTER_PLUGIN_EXPORT EntityId load_gltf(void *assetManager, const char *assetPath, const char *relativePath)
{
return ((AssetManager *)assetManager)->loadGltf(assetPath, relativePath);
}
float get_animation_duration(void* assetManager, EntityId asset, int animationIndex) {
return ((AssetManager*)assetManager)->getAnimationDuration(asset, animationIndex);
}
FLUTTER_PLUGIN_EXPORT bool set_camera(const void *const viewer, EntityId asset, const char *nodeName)
{
return ((FilamentViewer *)viewer)->setCamera(asset, nodeName);
}
int get_animation_count(
void* assetManager,
EntityId asset) {
auto names = ((AssetManager*)assetManager)->getAnimationNames(asset);
return (int)names->size();
}
const double *const get_camera_model_matrix(const void *const viewer)
{
const auto &modelMatrix = ((FilamentViewer *)viewer)->getCameraModelMatrix();
double *array = (double *)calloc(16, sizeof(double));
memcpy(array, modelMatrix.asArray(), 16 * sizeof(double));
return array;
}
FLUTTER_PLUGIN_EXPORT void get_animation_name(
void* assetManager,
EntityId asset,
char* const outPtr,
int index
) {
auto names = ((AssetManager*)assetManager)->getAnimationNames(asset);
string name = names->at(index);
strcpy(outPtr, name.c_str());
}
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);
return (int)names->size();
}
const double *const get_camera_view_matrix(const void *const viewer)
{
const auto &modelMatrix = ((FilamentViewer *)viewer)->getCameraViewMatrix();
return modelMatrix.asArray();
}
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);
string name = names->at(index);
strcpy(outPtr, name.c_str());
}
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 remove_asset(const void* const viewer, EntityId asset) {
((FilamentViewer*)viewer)->removeAsset(asset);
}
FLUTTER_PLUGIN_EXPORT void set_view_frustum_culling(const void *const viewer, bool enabled)
{
((FilamentViewer *)viewer)->setViewFrustumCulling(enabled);
}
FLUTTER_PLUGIN_EXPORT void clear_assets(const void* const viewer) {
((FilamentViewer*)viewer)->clearAssets();
}
FLUTTER_PLUGIN_EXPORT void move_camera_to_asset(const void *const viewer, EntityId asset)
{
((FilamentViewer *)viewer)->moveCameraToAsset(asset);
}
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);
}
FLUTTER_PLUGIN_EXPORT void set_camera_focus_distance(const void *const viewer, float distance)
{
((FilamentViewer *)viewer)->setCameraFocusDistance(distance);
}
FLUTTER_PLUGIN_EXPORT void transform_to_unit_cube(void* assetManager, EntityId asset) {
((AssetManager*)assetManager)->transformToUnitCube(asset);
}
FLUTTER_PLUGIN_EXPORT void set_camera_exposure(const void *const viewer, float aperture, float shutterSpeed, float sensitivity)
{
((FilamentViewer *)viewer)->setCameraExposure(aperture, shutterSpeed, sensitivity);
}
FLUTTER_PLUGIN_EXPORT void set_position(void* assetManager, EntityId asset, float x, float y, float z) {
((AssetManager*)assetManager)->setPosition(asset, x, y, z);
}
FLUTTER_PLUGIN_EXPORT void set_camera_position(const void *const viewer, float x, float y, float z)
{
((FilamentViewer *)viewer)->setCameraPosition(x, y, 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);
}
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);
}
FLUTTER_PLUGIN_EXPORT void set_scale(void* assetManager, EntityId asset, float scale) {
((AssetManager*)assetManager)->setScale(asset, scale);
}
FLUTTER_PLUGIN_EXPORT void set_camera_model_matrix(const void *const viewer, const float *const matrix)
{
((FilamentViewer *)viewer)->setCameraModelMatrix(matrix);
}
FLUTTER_PLUGIN_EXPORT void stop_animation(void* assetManager, EntityId asset, int index) {
((AssetManager*)assetManager)->stopAnimation(asset, index);
}
FLUTTER_PLUGIN_EXPORT void set_camera_focal_length(const void *const viewer, float focalLength)
{
((FilamentViewer *)viewer)->setCameraFocalLength(focalLength);
}
FLUTTER_PLUGIN_EXPORT int hide_mesh(void* assetManager, EntityId asset, const char* meshName) {
return ((AssetManager*)assetManager)->hide(asset, meshName);
}
FLUTTER_PLUGIN_EXPORT void render(
const void *const viewer,
uint64_t frameTimeInNanos,
void *pixelBuffer,
void (*callback)(void *buf, size_t size, void *data),
void *data)
{
((FilamentViewer *)viewer)->render(frameTimeInNanos, pixelBuffer, callback, data);
}
FLUTTER_PLUGIN_EXPORT int reveal_mesh(void* assetManager, EntityId asset, const char* meshName) {
return ((AssetManager*)assetManager)->reveal(asset, meshName);
}
FLUTTER_PLUGIN_EXPORT void set_frame_interval(
const void *const viewer,
float frameInterval)
{
((FilamentViewer *)viewer)->setFrameInterval(frameInterval);
}
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));
}
FLUTTER_PLUGIN_EXPORT void destroy_swap_chain(const void *const viewer)
{
((FilamentViewer *)viewer)->destroySwapChain();
}
FLUTTER_PLUGIN_EXPORT const char* get_name_for_entity(void* const assetManager, const EntityId entityId) {
return ((AssetManager*)assetManager)->getNameForEntity(entityId);
}
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);
}
FLUTTER_PLUGIN_EXPORT void ios_dummy() {
Log("Dummy called");
}
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);
}
FLUTTER_PLUGIN_EXPORT void scroll_update(const void *const viewer, float x, float y, float delta)
{
((FilamentViewer *)viewer)->scrollUpdate(x, y, delta);
}
FLUTTER_PLUGIN_EXPORT void scroll_begin(const void *const viewer)
{
((FilamentViewer *)viewer)->scrollBegin();
}
FLUTTER_PLUGIN_EXPORT void scroll_end(const void *const viewer)
{
((FilamentViewer *)viewer)->scrollEnd();
}
FLUTTER_PLUGIN_EXPORT void grab_begin(const void *const viewer, float x, float y, bool pan)
{
((FilamentViewer *)viewer)->grabBegin(x, y, pan);
}
FLUTTER_PLUGIN_EXPORT void grab_update(const void *const viewer, float x, float y)
{
((FilamentViewer *)viewer)->grabUpdate(x, y);
}
FLUTTER_PLUGIN_EXPORT void grab_end(const void *const viewer)
{
((FilamentViewer *)viewer)->grabEnd();
}
FLUTTER_PLUGIN_EXPORT void *get_asset_manager(const void *const viewer)
{
return (void *)((FilamentViewer *)viewer)->getAssetManager();
}
FLUTTER_PLUGIN_EXPORT void apply_weights(
void *assetManager,
EntityId asset,
const char *const entityName,
float *const weights,
int count)
{
// ((AssetManager*)assetManager)->setMorphTargetWeights(asset, entityName, weights, count);
}
FLUTTER_PLUGIN_EXPORT void set_morph_target_weights(
void *assetManager,
EntityId asset,
const char *const entityName,
const float *const weights,
const int numWeights)
{
return ((AssetManager *)assetManager)->setMorphTargetWeights(asset, entityName, weights, numWeights);
}
bool set_morph_animation(
void *assetManager,
EntityId asset,
const char *const entityName,
const float *const morphData,
const int *const morphIndices,
int numMorphTargets,
int numFrames,
float frameLengthInMs)
{
return ((AssetManager *)assetManager)->setMorphAnimationBuffer(asset, entityName, morphData, morphIndices, numMorphTargets, numFrames, frameLengthInMs);
}
FLUTTER_PLUGIN_EXPORT void set_bone_animation(
void *assetManager,
EntityId asset,
const float *const frameData,
int numFrames,
int numBones,
const char **const boneNames,
const char **const meshNames,
int numMeshTargets,
float frameLengthInMs)
{
((AssetManager *)assetManager)->setBoneAnimationBuffer(asset, frameData, numFrames, numBones, boneNames, meshNames, numMeshTargets, frameLengthInMs);
}
FLUTTER_PLUGIN_EXPORT void set_post_processing(void *const viewer, bool enabled)
{
((FilamentViewer *)viewer)->setPostProcessing(enabled);
}
// void set_bone_transform(
// EntityId asset,
// const char* boneName,
// const char* entityName,
// float transX,
// float transY,
// float transZ,
// float quatX,
// float quatY,
// float quatZ,
// float quatW
// ) {
// ((AssetManager*)assetManager)->setBoneTransform(
// boneName,
// entityName,
// transX,
// transY,
// transZ,
// quatX,
// quatY,
// quatZ,
// quatW,
// false
// );
// }
FLUTTER_PLUGIN_EXPORT void play_animation(
void *assetManager,
EntityId asset,
int index,
bool loop,
bool reverse,
bool replaceActive,
float crossfade)
{
((AssetManager *)assetManager)->playAnimation(asset, index, loop, reverse, replaceActive, crossfade);
}
FLUTTER_PLUGIN_EXPORT void set_animation_frame(
void *assetManager,
EntityId asset,
int animationIndex,
int animationFrame)
{
// ((AssetManager*)assetManager)->setAnimationFrame(asset, animationIndex, animationFrame);
}
float get_animation_duration(void *assetManager, EntityId asset, int animationIndex)
{
return ((AssetManager *)assetManager)->getAnimationDuration(asset, animationIndex);
}
int get_animation_count(
void *assetManager,
EntityId asset)
{
auto names = ((AssetManager *)assetManager)->getAnimationNames(asset);
return (int)names->size();
}
FLUTTER_PLUGIN_EXPORT void get_animation_name(
void *assetManager,
EntityId asset,
char *const outPtr,
int index)
{
auto names = ((AssetManager *)assetManager)->getAnimationNames(asset);
string name = names->at(index);
strcpy(outPtr, name.c_str());
}
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);
return (int)names->size();
}
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);
string name = names->at(index);
strcpy(outPtr, name.c_str());
}
FLUTTER_PLUGIN_EXPORT void remove_asset(const void *const viewer, EntityId asset)
{
((FilamentViewer *)viewer)->removeAsset(asset);
}
FLUTTER_PLUGIN_EXPORT void clear_assets(const void *const viewer)
{
((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)
{
return ((AssetManager *)assetManager)->setMaterialColor(asset, meshName, materialIndex, r, g, b, a);
}
FLUTTER_PLUGIN_EXPORT void transform_to_unit_cube(void *assetManager, EntityId asset)
{
((AssetManager *)assetManager)->transformToUnitCube(asset);
}
FLUTTER_PLUGIN_EXPORT void set_position(void *assetManager, EntityId asset, float x, float y, float 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)
{
((AssetManager *)assetManager)->setRotation(asset, rads, x, y, z);
}
FLUTTER_PLUGIN_EXPORT void set_scale(void *assetManager, EntityId asset, float scale)
{
((AssetManager *)assetManager)->setScale(asset, scale);
}
FLUTTER_PLUGIN_EXPORT void stop_animation(void *assetManager, EntityId asset, int index)
{
((AssetManager *)assetManager)->stopAnimation(asset, index);
}
FLUTTER_PLUGIN_EXPORT int hide_mesh(void *assetManager, EntityId asset, const char *meshName)
{
return ((AssetManager *)assetManager)->hide(asset, meshName);
}
FLUTTER_PLUGIN_EXPORT int reveal_mesh(void *assetManager, EntityId asset, const char *meshName)
{
return ((AssetManager *)assetManager)->reveal(asset, meshName);
}
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));
}
FLUTTER_PLUGIN_EXPORT const char *get_name_for_entity(void *const assetManager, const EntityId entityId)
{
return ((AssetManager *)assetManager)->getNameForEntity(entityId);
}
FLUTTER_PLUGIN_EXPORT void ios_dummy()
{
Log("Dummy called");
}
}