fix camera manipulator/gesture detector, add explicit functions to set camera pos/rot, add unlitshader material provider

This commit is contained in:
Nick Fisher
2022-09-01 13:03:15 +10:00
parent 91038bc0d7
commit 26d916fc40
13 changed files with 2085 additions and 118 deletions

View File

@@ -7,8 +7,8 @@ typedef FilamentAsset = int;
abstract class FilamentController {
late int textureId;
Future get initialized;
Future initialize(int width, int height);
Future resize(int width, int height, {double contentScaleFactor=1});
Future initialize(int width, int height, { double devicePixelRatio = 1});
Future resize(int width, int height, { double devicePixelRatio = 1, double contentScaleFactor=1});
Future setBackgroundImage(String path);
Future loadSkybox(String skyboxPath);
Future removeSkybox();
@@ -38,11 +38,16 @@ abstract class FilamentController {
Future setPosition(FilamentAsset asset, double x, double y, double z);
Future setRotation(
FilamentAsset asset, double rads, double x, double y, double z);
Future setScale(
FilamentAsset asset, double scale);
Future setCameraFocalLength(
double focalLength);
Future setCameraFocusDistance(
double focusDistance);
Future setCameraPosition(
double x, double y, double z);
Future setCameraRotation(
double rads, double x, double y, double z);
///
/// Set the weights of all morph targets in the mesh to the specified weights at successive frames (where each frame requires a duration of [frameLengthInMs].
/// Accepts a list of doubles representing a sequence of "frames", stacked end-to-end.
@@ -55,7 +60,9 @@ abstract class FilamentController {
}
class PolyvoxFilamentController extends FilamentController {
late MethodChannel _channel = MethodChannel("app.polyvox.filament/event");
late double _devicePixelRatio;
final _initialized = Completer();
Future get initialized => _initialized.future;
@@ -67,13 +74,15 @@ class PolyvoxFilamentController extends FilamentController {
});
}
Future initialize(int width, int height) async {
textureId = await _channel.invokeMethod("initialize", [width, height]);
Future initialize(int width, int height, { double devicePixelRatio=1 }) async {
_devicePixelRatio = devicePixelRatio;
textureId = await _channel.invokeMethod("initialize", [width*devicePixelRatio, height*devicePixelRatio]);
_initialized.complete(true);
}
Future resize(int width, int height, { double contentScaleFactor=1.0}) async {
await _channel.invokeMethod("resize", [width, height, contentScaleFactor]);
Future resize(int width, int height, { double devicePixelRatio=1, double contentScaleFactor=1.0}) async {
_devicePixelRatio = devicePixelRatio;
await _channel.invokeMethod("resize", [width*devicePixelRatio, height*devicePixelRatio, contentScaleFactor]);
}
@override
@@ -118,11 +127,11 @@ class PolyvoxFilamentController extends FilamentController {
}
Future panStart(double x, double y) async {
await _channel.invokeMethod("panStart", [x.toInt(), y.toInt()]);
await _channel.invokeMethod("panStart", [x * _devicePixelRatio, y * _devicePixelRatio]);
}
Future panUpdate(double x, double y) async {
await _channel.invokeMethod("panUpdate", [x.toInt(), y.toInt()]);
await _channel.invokeMethod("panUpdate", [x * _devicePixelRatio, y * _devicePixelRatio]);
}
Future panEnd() async {
@@ -130,11 +139,11 @@ class PolyvoxFilamentController extends FilamentController {
}
Future rotateStart(double x, double y) async {
await _channel.invokeMethod("rotateStart", [x.toInt(), y.toInt()]);
await _channel.invokeMethod("rotateStart", [x * _devicePixelRatio, y * _devicePixelRatio]);
}
Future rotateUpdate(double x, double y) async {
await _channel.invokeMethod("rotateUpdate", [x.toInt(), y.toInt()]);
await _channel.invokeMethod("rotateUpdate", [x * _devicePixelRatio, y * _devicePixelRatio]);
}
Future rotateEnd() async {
@@ -174,7 +183,7 @@ class PolyvoxFilamentController extends FilamentController {
}
Future zoom(double z) async {
await _channel.invokeMethod("zoom", z);
await _channel.invokeMethod("zoom", [0.0,0.0,z]);
}
Future playAnimation(FilamentAsset asset, int index,
@@ -205,6 +214,16 @@ class PolyvoxFilamentController extends FilamentController {
await _channel.invokeMethod("setCameraFocusDistance", focusDistance);
}
Future setCameraPosition(
double x, double y, double z) async {
await _channel.invokeMethod("setCameraPosition", [x,y,z]);
}
Future setCameraRotation(
double rads, double x, double y, double z) async {
await _channel.invokeMethod("setCameraRotation", [rads, x,y,z]);
}
Future setTexture(FilamentAsset asset, String assetPath,
{int renderableIndex = 0}) async {
await _channel
@@ -219,6 +238,11 @@ class PolyvoxFilamentController extends FilamentController {
await _channel.invokeMethod("setPosition", [asset, x, y, z]);
}
Future setScale(
FilamentAsset asset, double scale) async {
await _channel.invokeMethod("setScale", [asset, scale]);
}
Future setRotation(
FilamentAsset asset, double rads, double x, double y, double z) async {
await _channel.invokeMethod("setRotation", [asset, rads, x, y, z]);

View File

@@ -1,6 +1,4 @@
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'filament_controller.dart';
import 'filament_widget.dart';
@@ -20,6 +18,29 @@ class GestureDetectingFilamentView extends StatefulWidget {
class _GestureDetectingFilamentViewState
extends State<GestureDetectingFilamentView> {
bool _rotate = false;
late Future Function(double x, double y) _functionStart;
late Future Function(double x, double y) _functionUpdate;
late Future Function() _functionEnd;
double _lastScale = 0;
@override
void initState() {
_setFunction();
super.initState();
}
void _setFunction() {
if (_rotate) {
_functionStart = widget.controller.rotateStart;
_functionUpdate = widget.controller.rotateUpdate;
_functionEnd = widget.controller.rotateEnd;
} else {
_functionStart = widget.controller.panStart;
_functionUpdate = widget.controller.panUpdate;
_functionEnd = widget.controller.panEnd;
}
}
@override
void didUpdateWidget(Widget oldWidget) {
@@ -27,6 +48,7 @@ class _GestureDetectingFilamentViewState
(oldWidget as GestureDetectingFilamentView).showControls) {
setState(() {});
}
super.didUpdateWidget(oldWidget);
}
@@ -36,67 +58,58 @@ class _GestureDetectingFilamentViewState
Positioned.fill(
child: GestureDetector(
behavior: HitTestBehavior.opaque,
onPanDown: (details) async {
await widget.controller.panEnd();
await widget.controller.rotateEnd();
_rotate
? widget.controller.rotateStart(
details.globalPosition.dx, details.globalPosition.dy)
: widget.controller.panStart(
details.globalPosition.dx, details.globalPosition.dy);
onScaleStart: (d) {
if (d.pointerCount == 2) {
// _lastScale = d.
} else {
// print("start ${d.focalPoint}");
_functionStart(d.focalPoint.dx, d.focalPoint.dy);
}
},
onPanUpdate: (details) {
_rotate
? widget.controller.rotateUpdate(
details.globalPosition.dx, details.globalPosition.dy)
: widget.controller.panUpdate(
details.globalPosition.dx, details.globalPosition.dy);
onScaleEnd: (d) {
if (d.pointerCount == 2) {
_lastScale = 0;
} else {
// print("end ${d.velocity}");
_functionEnd();
}
},
onPanEnd: (d) {
_rotate
? widget.controller.rotateEnd()
: widget.controller.panEnd();
onScaleUpdate: (d) {
if (d.pointerCount == 2) {
if (_lastScale == 0) {
_lastScale = d.scale;
} else {
// var zoomFactor = ;
// if(zoomFactor < 0) {
// zoomFactor *= 10;
// }
// print(zoomFactor);
print(d.horizontalScale);
// print(d.focalPoint.dx);
widget.controller.zoom(d.scale > 1 ? 10 : -10);
}
} else {
// print("update ${d.focalPoint}");
_functionUpdate(d.focalPoint.dx, d.focalPoint.dy);
}
},
child: FilamentWidget(controller: widget.controller))),
widget.showControls
? Padding(
padding: const EdgeInsets.all(50),
child: Row(children: [
Checkbox(
value: _rotate,
onChanged: (v) {
setState(() {
_rotate = v == true;
});
}),
ElevatedButton(
onPressed: () => widget.controller.zoom(30.0),
child: const Text('-')),
ElevatedButton(
onPressed: () => widget.controller.zoom(-30.0),
child: const Text('+'))
]))
? Align(alignment:Alignment.bottomRight, child:GestureDetector(
onTap: () {
setState(() {
_rotate = !_rotate;
_setFunction();
});
},
child: Container(
padding: const EdgeInsets.all(50),
child: Icon(Icons.rotate_90_degrees_ccw,
color: _rotate
? Colors.white
: Colors.white.withOpacity(0.5))),
))
: Container()
]);
}
}
// behavior: HitTestBehavior.opaque,
// onPanDown: (details) {
// _rotate
// ? _filamentController.rotateStart(
// details.globalPosition.dx, details.globalPosition.dy)
// : _filamentController.panStart(
// details.globalPosition.dx, details.globalPosition.dy);
// },
// onPanUpdate: (details) {
// _rotate
// ? _filamentController.rotateUpdate(
// details.globalPosition.dx, details.globalPosition.dy)
// : _filamentController.panUpdate(
// details.globalPosition.dx, details.globalPosition.dy);
// },
// onPanEnd: (d) {
// _rotate
// ? _filamentController.rotateEnd()
// : _filamentController.panEnd();
// },