add setter for frame interval, separate FilamentController initialization from widget

This commit is contained in:
Nick Fisher
2022-09-01 14:26:31 +10:00
parent 998da3ea57
commit e6d5556077
8 changed files with 134 additions and 33 deletions

View File

@@ -7,7 +7,10 @@ typedef FilamentAsset = int;
abstract class FilamentController {
late int textureId;
Future get initialized;
Future initialize(int width, int height, { double devicePixelRatio = 1});
Stream get onInitializationRequested;
Future initialize();
Future createTextureViewer(int width, int height, { double devicePixelRatio = 1});
Future setFrameRate(int framerate);
Future resize(int width, int height, { double devicePixelRatio = 1, double contentScaleFactor=1});
Future setBackgroundImage(String path);
Future loadSkybox(String skyboxPath);
@@ -64,6 +67,10 @@ class PolyvoxFilamentController extends FilamentController {
late MethodChannel _channel = MethodChannel("app.polyvox.filament/event");
late double _devicePixelRatio;
final _onInitRequestedController = StreamController();
Stream get onInitializationRequested => _onInitRequestedController.stream;
final _initialized = Completer();
Future get initialized => _initialized.future;
@@ -74,7 +81,16 @@ class PolyvoxFilamentController extends FilamentController {
});
}
Future initialize(int width, int height, { double devicePixelRatio=1 }) async {
Future initialize() async {
_onInitRequestedController.add(true);
return _initialized.future;
}
Future setFrameRate(int framerate) async {
await _channel.invokeMethod("setFrameInterval", 1/ framerate);
}
Future createTextureViewer(int width, int height, { double devicePixelRatio=1 }) async {
_devicePixelRatio = devicePixelRatio;
textureId = await _channel.invokeMethod("initialize", [width*devicePixelRatio, height*devicePixelRatio]);
_initialized.complete(true);

View File

@@ -55,14 +55,17 @@ class _FilamentWidgetState extends State<FilamentWidget> {
@override
void initState() {
WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
var size = ((context.findRenderObject()) as RenderBox).size;
print("Requesting texture creation for Filament of size $size");
await widget.controller
.initialize(size.width.toInt(), size.height.toInt());
print("Filament texture available");
setState(() {
_ready = true;
widget.controller.onInitializationRequested.listen((_) {
WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
var size = ((context.findRenderObject()) as RenderBox).size;
print("Requesting texture creation for Filament of size $size");
await widget.controller
.createTextureViewer(size.width.toInt(), size.height.toInt());
print("Filament texture available");
setState(() {
_ready = true;
});
});
});

View File

@@ -95,20 +95,22 @@ class _GestureDetectingFilamentViewState
},
child: FilamentWidget(controller: widget.controller))),
widget.showControls
? 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))),
))
? 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()
]);
}

View File

@@ -0,0 +1,63 @@
// import 'package:flutter/widgets.dart';
// import 'package:polyvox_filament/filament_controller.dart';
// import 'package:polyvox_filament/filament_controller.dart';
// import 'package:vector_math/vector_math_64.dart';
// class Position {
// final double x;
// final double y;
// final double z;
// Position(this.x, this.y, this.z);
// Position copy({double? x, double? y, double? z}) {
// return Position(x ?? this.x, y ?? this.y, z ?? this.z);
// }
// factory Position.zero() {
// return Position(0,0,0);
// }
// }
// class Rotation {
// final double rads;
// final double x;
// final double y;
// final double z;
// Rotation(this.x, this.y, this.z, this.rads);
// Rotation copy({double? rads, double? x, double? y, double? z}) {
// return Rotation(x ?? this.x, y ?? this.y, z ?? this.z, rads ?? this.rads);
// }
// factory Rotation.zero() {
// return Rotation(0, 1,0,0);
// }
// }
// ///
// /// Handles local transforms for assets/cameras.
// ///
// class TransformManager {
// final FilamentController _controller;
// Matrix4 transform = Matrix4.identity();
// TransformManager(this._controller);
// void scale(double scale) {
// transform.scale(scale, scale, scale);
// }
// void translate(double x, double y, double z) {
// transform.translate(x,y,z);
// }
// void rotate(double x, double y, double z) {
// transform.rotate(Vector3(x,y,z));
// }
// }