refactoring
This commit is contained in:
@@ -11,40 +11,13 @@ import 'package:thermion_flutter_platform_interface/thermion_flutter_platform_in
|
||||
class ThermionFlutterPlugin {
|
||||
ThermionFlutterPlugin._();
|
||||
|
||||
static bool _initializing = false;
|
||||
|
||||
static ThermionViewer? _viewer;
|
||||
|
||||
static ThermionFlutterOptions? options;
|
||||
|
||||
static Future<ThermionViewer> createViewer(
|
||||
{ThermionFlutterOptions options =
|
||||
const ThermionFlutterOptions.empty()}) async {
|
||||
if (_initializing) {
|
||||
throw Exception("Existing call to createViewer has not completed.");
|
||||
}
|
||||
_initializing = true;
|
||||
const ThermionFlutterOptions()}) async {
|
||||
|
||||
if (_viewer != null) {
|
||||
throw Exception(
|
||||
"Instance of ThermionViewer has already been created. Ensure you call dispose() on that instance.");
|
||||
}
|
||||
|
||||
options = options;
|
||||
|
||||
_viewer =
|
||||
final viewer =
|
||||
await ThermionFlutterPlatform.instance.createViewer(options: options);
|
||||
|
||||
await _viewer!.initialized;
|
||||
|
||||
var camera = await _viewer!.getActiveCamera();
|
||||
await camera.setLensProjection();
|
||||
|
||||
_viewer!.onDispose(() async {
|
||||
_viewer = null;
|
||||
ThermionFlutterPlugin.options = null;
|
||||
});
|
||||
_initializing = false;
|
||||
return _viewer!;
|
||||
return viewer;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,13 @@
|
||||
import 'package:vector_math/vector_math_64.dart' as v;
|
||||
import 'package:vector_math/vector_math_64.dart';
|
||||
|
||||
class CameraOrientation {
|
||||
v.Vector3 position = v.Vector3(0, 0, 0);
|
||||
Vector3 position = Vector3.zero();
|
||||
Vector3 rotation = Vector3.zero();
|
||||
|
||||
var rotationX = 0.0;
|
||||
var rotationY = 0.0;
|
||||
var rotationZ = 0.0;
|
||||
|
||||
v.Quaternion compose() {
|
||||
return v.Quaternion.axisAngle(v.Vector3(0, 0, 1), rotationZ) *
|
||||
v.Quaternion.axisAngle(v.Vector3(0, 1, 0), rotationY) *
|
||||
v.Quaternion.axisAngle(v.Vector3(1, 0, 0), rotationX);
|
||||
Matrix4 compose() {
|
||||
final quat = Quaternion.axisAngle(Vector3(0, 0, 1), rotation.z) *
|
||||
Quaternion.axisAngle(Vector3(0, 1, 0), rotation.y) *
|
||||
Quaternion.axisAngle(Vector3(1, 0, 0), rotation.x);
|
||||
return Matrix4.compose(position, quat, Vector3.all(1));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +1,15 @@
|
||||
|
||||
import 'package:thermion_dart/thermion_dart.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import '../../../utils/camera_orientation.dart';
|
||||
|
||||
import 'dart:math';
|
||||
|
||||
|
||||
class CameraOptionsWidget extends StatefulWidget {
|
||||
final ThermionViewer controller;
|
||||
final Camera camera;
|
||||
final CameraOrientation cameraOrientation;
|
||||
final List<({ThermionEntity entity, String name})> cameras;
|
||||
|
||||
CameraOptionsWidget(
|
||||
{super.key,
|
||||
required this.controller,
|
||||
required this.cameras,
|
||||
required this.cameraOrientation}) {}
|
||||
{super.key, required this.camera, required this.cameraOrientation}) {}
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() => _CameraOptionsWidgetState();
|
||||
@@ -51,24 +45,17 @@ class _CameraOptionsWidgetState extends State<CameraOptionsWidget> {
|
||||
@override
|
||||
void didUpdateWidget(CameraOptionsWidget oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.cameras.length != widget.cameras.length) {
|
||||
if (oldWidget.camera != widget.camera) {
|
||||
setState(() {});
|
||||
}
|
||||
}
|
||||
|
||||
Future _set() async {
|
||||
await widget.controller.setCameraExposure(
|
||||
await widget.camera.setCameraExposure(
|
||||
double.parse(_apertureController.text),
|
||||
double.parse(_speedController.text),
|
||||
double.parse(_sensitivityController.text));
|
||||
await widget.controller.setCameraPosition(
|
||||
widget.cameraOrientation.position.x,
|
||||
widget.cameraOrientation.position.y,
|
||||
widget.cameraOrientation.position.z);
|
||||
var rotation = widget.cameraOrientation.compose();
|
||||
await widget.controller.setCameraRotation(rotation);
|
||||
print(
|
||||
"Camera : ${widget.cameraOrientation.position} ${widget.cameraOrientation.rotationX} ${widget.cameraOrientation.rotationY} ${widget.cameraOrientation.rotationZ}");
|
||||
await widget.camera.setModelMatrix(widget.cameraOrientation.compose());
|
||||
setState(() {});
|
||||
}
|
||||
|
||||
@@ -98,34 +85,20 @@ class _CameraOptionsWidgetState extends State<CameraOptionsWidget> {
|
||||
Expanded(
|
||||
child: TextField(controller: _sensitivityController)),
|
||||
]),
|
||||
Row(children: [
|
||||
Text("Bloom: ${_bloom.toStringAsFixed(2)}"),
|
||||
Slider(
|
||||
value: _bloom,
|
||||
min: 0.0,
|
||||
max: 1.0,
|
||||
onChanged: (v) async {
|
||||
setState(() {
|
||||
_bloom = v;
|
||||
});
|
||||
await widget.controller.setBloom(_bloom);
|
||||
})
|
||||
]),
|
||||
Row(children: [
|
||||
const Text("Focal length"),
|
||||
Slider(
|
||||
label: _focalLength.toString(),
|
||||
value: _focalLength,
|
||||
min: 1.0,
|
||||
max: 100.0,
|
||||
onChanged: (v) async {
|
||||
setState(() {
|
||||
_focalLength = v;
|
||||
});
|
||||
await widget.controller
|
||||
.setCameraFocalLength(_focalLength);
|
||||
})
|
||||
]),
|
||||
// Row(children: [
|
||||
// const Text("Focal length"),
|
||||
// Slider(
|
||||
// label: _focalLength.toString(),
|
||||
// value: _focalLength,
|
||||
// min: 1.0,
|
||||
// max: 100.0,
|
||||
// onChanged: (v) async {
|
||||
// setState(() {
|
||||
// _focalLength = v;
|
||||
// });
|
||||
// await widget.camera.setLensProjection(near: kNear, far:kFar, _focalLength);
|
||||
// })
|
||||
// ]),
|
||||
Row(children: [
|
||||
const Text("X"),
|
||||
Slider(
|
||||
@@ -140,94 +113,6 @@ class _CameraOptionsWidgetState extends State<CameraOptionsWidget> {
|
||||
_set();
|
||||
})
|
||||
]),
|
||||
Row(children: [
|
||||
const Text("Y"),
|
||||
Slider(
|
||||
label: widget.cameraOrientation.position.y.toString(),
|
||||
value: widget.cameraOrientation.position.y,
|
||||
min: -100.0,
|
||||
max: 100.0,
|
||||
onChanged: (v) async {
|
||||
setState(() {
|
||||
widget.cameraOrientation.position.y = v;
|
||||
});
|
||||
_set();
|
||||
})
|
||||
]),
|
||||
Row(children: [
|
||||
const Text("Z"),
|
||||
Slider(
|
||||
label: widget.cameraOrientation.position.z.toString(),
|
||||
value: widget.cameraOrientation.position.z,
|
||||
min: -100.0,
|
||||
max: 100.0,
|
||||
onChanged: (v) async {
|
||||
setState(() {
|
||||
widget.cameraOrientation.position.z = v;
|
||||
});
|
||||
_set();
|
||||
})
|
||||
]),
|
||||
Row(children: [
|
||||
const Text("ROTX"),
|
||||
Slider(
|
||||
label: widget.cameraOrientation.rotationX.toString(),
|
||||
value: widget.cameraOrientation.rotationX,
|
||||
min: -pi,
|
||||
max: pi,
|
||||
onChanged: (value) async {
|
||||
setState(() {
|
||||
widget.cameraOrientation.rotationX = value;
|
||||
});
|
||||
_set();
|
||||
})
|
||||
]),
|
||||
Row(children: [
|
||||
const Text("ROTY"),
|
||||
Slider(
|
||||
label: widget.cameraOrientation.rotationY.toString(),
|
||||
value: widget.cameraOrientation.rotationY,
|
||||
min: -pi,
|
||||
max: pi,
|
||||
onChanged: (v) async {
|
||||
setState(() {
|
||||
widget.cameraOrientation.rotationY = v;
|
||||
});
|
||||
_set();
|
||||
}),
|
||||
]),
|
||||
Row(children: [
|
||||
const Text("ROTZ"),
|
||||
Slider(
|
||||
label: widget.cameraOrientation.rotationZ.toString(),
|
||||
value: widget.cameraOrientation.rotationZ,
|
||||
min: -pi,
|
||||
max: pi,
|
||||
onChanged: (v) async {
|
||||
setState(() {
|
||||
widget.cameraOrientation.rotationZ = v;
|
||||
});
|
||||
_set();
|
||||
})
|
||||
]),
|
||||
Wrap(
|
||||
children: [
|
||||
GestureDetector(
|
||||
child: const Text("Main "),
|
||||
onTap: () {
|
||||
widget.controller.setMainCamera();
|
||||
},
|
||||
),
|
||||
...widget.cameras
|
||||
.map((camera) => GestureDetector(
|
||||
onTap: () {
|
||||
widget.controller
|
||||
.setCamera(camera.entity, camera.name);
|
||||
},
|
||||
child: Text(camera.name)))
|
||||
.toList()
|
||||
],
|
||||
)
|
||||
]))));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,13 +7,16 @@ import 'package:vector_math/vector_math_64.dart' as v64;
|
||||
class CameraOrientationWidget extends StatefulWidget {
|
||||
final ThermionViewer viewer;
|
||||
|
||||
const CameraOrientationWidget({Key? key, required this.viewer}) : super(key: key);
|
||||
const CameraOrientationWidget({Key? key, required this.viewer})
|
||||
: super(key: key);
|
||||
|
||||
@override
|
||||
_CameraOrientationWidgetState createState() => _CameraOrientationWidgetState();
|
||||
_CameraOrientationWidgetState createState() =>
|
||||
_CameraOrientationWidgetState();
|
||||
}
|
||||
|
||||
class _CameraOrientationWidgetState extends State<CameraOrientationWidget> with SingleTickerProviderStateMixin {
|
||||
class _CameraOrientationWidgetState extends State<CameraOrientationWidget>
|
||||
with SingleTickerProviderStateMixin {
|
||||
late AnimationController _controller;
|
||||
v64.Vector3? _position;
|
||||
v64.Matrix3? _rotation;
|
||||
@@ -30,6 +33,7 @@ class _CameraOrientationWidgetState extends State<CameraOrientationWidget> with
|
||||
}
|
||||
|
||||
void _updateCameraInfo() async {
|
||||
final camera = await widget.viewer.getActiveCamera();
|
||||
final position = await widget.viewer.getCameraPosition();
|
||||
final rotation = await widget.viewer.getCameraRotation();
|
||||
setState(() {
|
||||
@@ -91,4 +95,4 @@ class _CameraOrientationWidgetState extends State<CameraOrientationWidget> with
|
||||
double _getRoll(v64.Matrix3 matrix) {
|
||||
return atan2(matrix[3], matrix[4]) * 180 / pi;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,7 +75,6 @@ class _ThermionWidgetState extends State<ThermionWidget> {
|
||||
key: ObjectKey(widget.viewer),
|
||||
initial: widget.initial,
|
||||
viewer: widget.viewer,
|
||||
view: widget.viewer.view,
|
||||
showFpsCounter: widget.showFpsCounter,
|
||||
onResize: widget.onResize);
|
||||
}
|
||||
|
||||
@@ -102,7 +102,7 @@ class _ViewerWidgetState extends State<ViewerWidget> {
|
||||
Future _tearDown() async {
|
||||
await viewer!.dispose();
|
||||
if (widget.options.destroyAppOnUnload) {
|
||||
await viewer!.app.destroy();
|
||||
await FilamentApp.instance!.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user