refactoring
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
import 'dart:async';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:thermion_dart/thermion_dart.dart';
|
||||
import 'package:vector_math/vector_math_64.dart';
|
||||
|
||||
import 'implementations/fixed_orbit_camera_rotation_delegate.dart';
|
||||
import 'implementations/free_flight_camera_delegate.dart';
|
||||
|
||||
@@ -64,7 +62,7 @@ class DelegateInputHandler implements InputHandler {
|
||||
_inputDeltas[gestureType] = Vector3.zero();
|
||||
}
|
||||
|
||||
viewer.registerRequestFrameHook(process);
|
||||
FilamentApp.instance!.registerRequestFrameHook(process);
|
||||
}
|
||||
|
||||
factory DelegateInputHandler.fixedOrbit(ThermionViewer viewer,
|
||||
@@ -75,7 +73,7 @@ class DelegateInputHandler implements InputHandler {
|
||||
DelegateInputHandler(
|
||||
viewer: viewer,
|
||||
pickDelegate: pickDelegate,
|
||||
transformDelegate: FixedOrbitRotateInputHandlerDelegate(viewer,
|
||||
transformDelegate: FixedOrbitRotateInputHandlerDelegate(viewer.view,
|
||||
minimumDistance: minimumDistance),
|
||||
actions: {
|
||||
InputType.MMB_HOLD_AND_MOVE: InputAction.ROTATE,
|
||||
@@ -96,9 +94,8 @@ class DelegateInputHandler implements InputHandler {
|
||||
DelegateInputHandler(
|
||||
viewer: viewer,
|
||||
pickDelegate: pickDelegate,
|
||||
transformDelegate: FreeFlightInputHandlerDelegate(viewer,
|
||||
transformDelegate: FreeFlightInputHandlerDelegate(viewer.view,
|
||||
clampY: clampY,
|
||||
entity: entity,
|
||||
rotationSensitivity: rotateSensitivity,
|
||||
zoomSensitivity: zoomSensitivity,
|
||||
panSensitivity: panSensitivity,
|
||||
@@ -245,7 +242,7 @@ class DelegateInputHandler implements InputHandler {
|
||||
|
||||
@override
|
||||
Future dispose() async {
|
||||
viewer.unregisterRequestFrameHook(process);
|
||||
FilamentApp.instance!.unregisterRequestFrameHook(process);
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -10,8 +10,7 @@ import '../input_handler.dart';
|
||||
/// point.
|
||||
///
|
||||
class FixedOrbitRotateInputHandlerDelegate implements InputHandlerDelegate {
|
||||
final ThermionViewer viewer;
|
||||
late Future<Camera> _camera;
|
||||
final View view;
|
||||
final double minimumDistance;
|
||||
late final Vector3 target;
|
||||
|
||||
@@ -24,20 +23,17 @@ class FixedOrbitRotateInputHandlerDelegate implements InputHandlerDelegate {
|
||||
Timer? _updateTimer;
|
||||
|
||||
FixedOrbitRotateInputHandlerDelegate(
|
||||
this.viewer, {
|
||||
this.view, {
|
||||
Vector3? target,
|
||||
this.minimumDistance = 10.0,
|
||||
this.rotationSensitivity = 0.01,
|
||||
this.zoomSensitivity = 0.1,
|
||||
}) {
|
||||
this.target = target ?? Vector3.zero();
|
||||
_camera = viewer.getMainCamera().then((Camera cam) async {
|
||||
var viewMatrix = makeViewMatrix(Vector3(0.0, 0, -minimumDistance),
|
||||
this.target, Vector3(0.0, 1.0, 0.0));
|
||||
viewMatrix.invert();
|
||||
|
||||
await cam.setTransform(viewMatrix);
|
||||
return cam;
|
||||
view.getCamera().then((camera) {
|
||||
camera.lookAt(Vector3(0.0, 0, -minimumDistance),
|
||||
focus: this.target, up: Vector3(0.0, 1.0, 0.0));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -81,12 +77,13 @@ class FixedOrbitRotateInputHandlerDelegate implements InputHandlerDelegate {
|
||||
|
||||
_executing = true;
|
||||
|
||||
final view = await viewer.getViewAt(0);
|
||||
final camera = await view.getCamera();
|
||||
|
||||
final viewport = await view.getViewport();
|
||||
|
||||
var viewMatrix = await viewer.getCameraViewMatrix();
|
||||
var modelMatrix = await viewer.getCameraModelMatrix();
|
||||
var projectionMatrix = await viewer.getCameraProjectionMatrix();
|
||||
var viewMatrix = await camera.getViewMatrix();
|
||||
var modelMatrix = await camera.getModelMatrix();
|
||||
var projectionMatrix = await camera.getProjectionMatrix();
|
||||
var inverseProjectionMatrix = projectionMatrix.clone()..invert();
|
||||
Vector3 currentPosition = modelMatrix.getTranslation();
|
||||
|
||||
@@ -117,14 +114,14 @@ class FixedOrbitRotateInputHandlerDelegate implements InputHandlerDelegate {
|
||||
Matrix4 newViewMatrix = makeViewMatrix(currentPosition, target, up);
|
||||
newViewMatrix.invert();
|
||||
|
||||
await (await _camera).setModelMatrix(newViewMatrix);
|
||||
await camera.setModelMatrix(newViewMatrix);
|
||||
updatedModelMatrix = newViewMatrix;
|
||||
}
|
||||
} else if (_queuedRotationDelta.length != 0) {
|
||||
double rotateX = _queuedRotationDelta.x * rotationSensitivity;
|
||||
double rotateY = _queuedRotationDelta.y * rotationSensitivity;
|
||||
|
||||
var modelMatrix = await viewer.getCameraModelMatrix();
|
||||
var modelMatrix = await camera.getModelMatrix();
|
||||
|
||||
// for simplicity, we always assume a fixed coordinate system where
|
||||
// we are rotating around world Y and camera X
|
||||
@@ -136,7 +133,7 @@ class FixedOrbitRotateInputHandlerDelegate implements InputHandlerDelegate {
|
||||
.asRotationMatrix());
|
||||
|
||||
modelMatrix = rot1 * rot2 * modelMatrix;
|
||||
await (await _camera).setModelMatrix(modelMatrix);
|
||||
await camera.setModelMatrix(modelMatrix);
|
||||
updatedModelMatrix = modelMatrix;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,8 +5,8 @@ import '../delegates.dart';
|
||||
import '../input_handler.dart';
|
||||
|
||||
class FreeFlightInputHandlerDelegate implements InputHandlerDelegate {
|
||||
final ThermionViewer viewer;
|
||||
late Future<ThermionEntity> entity;
|
||||
final View view;
|
||||
|
||||
final Vector3? minBounds;
|
||||
final Vector3? maxBounds;
|
||||
final double rotationSensitivity;
|
||||
@@ -20,21 +20,14 @@ class FreeFlightInputHandlerDelegate implements InputHandlerDelegate {
|
||||
double _queuedZoomDelta = 0.0;
|
||||
Vector3 _queuedMoveDelta = Vector3.zero();
|
||||
|
||||
FreeFlightInputHandlerDelegate(this.viewer,
|
||||
FreeFlightInputHandlerDelegate(this.view,
|
||||
{this.minBounds,
|
||||
this.maxBounds,
|
||||
this.rotationSensitivity = 0.001,
|
||||
this.movementSensitivity = 0.1,
|
||||
this.zoomSensitivity = 0.1,
|
||||
this.panSensitivity = 0.1,
|
||||
this.clampY,
|
||||
ThermionEntity? entity}) {
|
||||
if (entity != null) {
|
||||
this.entity = Future.value(entity);
|
||||
} else {
|
||||
this.entity = viewer.getMainCameraEntity();
|
||||
}
|
||||
}
|
||||
this.clampY}) {}
|
||||
|
||||
@override
|
||||
Future<void> queue(InputAction action, Vector3? delta) async {
|
||||
@@ -76,9 +69,9 @@ class FreeFlightInputHandlerDelegate implements InputHandlerDelegate {
|
||||
return null;
|
||||
}
|
||||
|
||||
final activeCamera = await viewer.getActiveCamera();
|
||||
final activeCamera = await view.getCamera();
|
||||
|
||||
Matrix4 current = await viewer.getLocalTransform(await entity);
|
||||
Matrix4 current = await activeCamera.getModelMatrix();
|
||||
|
||||
Vector3 relativeTranslation = Vector3.zero();
|
||||
Quaternion relativeRotation = Quaternion.identity();
|
||||
@@ -121,17 +114,18 @@ class FreeFlightInputHandlerDelegate implements InputHandlerDelegate {
|
||||
_queuedMoveDelta = Vector3.zero();
|
||||
}
|
||||
|
||||
// If the managed entity is not the active camera, we need to apply the rotation from the current camera model matrix
|
||||
// to the entity's translation
|
||||
if (await entity != activeCamera.getEntity()) {
|
||||
Matrix4 modelMatrix = await activeCamera.getModelMatrix();
|
||||
relativeTranslation = modelMatrix.getRotation() * relativeTranslation;
|
||||
}
|
||||
// // If the managed entity is not the active camera, we need to apply the rotation from the current camera model matrix
|
||||
// // to the entity's translation
|
||||
// if (await entity != activeCamera.getEntity()) {
|
||||
// Matrix4 modelMatrix = await activeCamera.getModelMatrix();
|
||||
// relativeTranslation = modelMatrix.getRotation() * relativeTranslation;
|
||||
// }
|
||||
|
||||
var updated = Matrix4.compose(
|
||||
relativeTranslation, relativeRotation, Vector3(1, 1, 1)) *
|
||||
current;
|
||||
await viewer.setTransform(await entity, updated);
|
||||
|
||||
await activeCamera.setModelMatrix(updated);
|
||||
|
||||
_executing = false;
|
||||
return updated;
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import 'dart:async';
|
||||
import 'dart:math';
|
||||
import 'package:thermion_dart/thermion_dart.dart';
|
||||
import 'package:vector_math/vector_math_64.dart';
|
||||
|
||||
class _Gizmo {
|
||||
final ThermionViewer viewer;
|
||||
@@ -16,7 +15,7 @@ class _Gizmo {
|
||||
_Gizmo(this._gizmo, this.viewer, this.type);
|
||||
|
||||
static Future<_Gizmo> forType(ThermionViewer viewer, GizmoType type) async {
|
||||
final view = await viewer.getViewAt(0);
|
||||
final view = await viewer.view;
|
||||
return _Gizmo(await viewer.createGizmo(view, type), viewer, type);
|
||||
}
|
||||
|
||||
@@ -26,12 +25,14 @@ class _Gizmo {
|
||||
}
|
||||
|
||||
Future hide() async {
|
||||
await _gizmo.removeFromScene();
|
||||
final scene = await viewer.view.getScene();
|
||||
await scene.remove(_gizmo);
|
||||
}
|
||||
|
||||
Future reveal() async {
|
||||
await _gizmo.addToScene();
|
||||
gizmoTransform = await viewer.getWorldTransform(_gizmo.entity);
|
||||
final scene = await viewer.view.getScene();
|
||||
await scene.add(_gizmo);
|
||||
gizmoTransform = await _gizmo.getWorldTransform();
|
||||
}
|
||||
|
||||
double _getAngleBetweenVectors(Vector2 v1, Vector2 v2) {
|
||||
@@ -76,17 +77,17 @@ class _Gizmo {
|
||||
await _updateRotation(currentPosition, delta);
|
||||
}
|
||||
|
||||
await viewer.setTransform(_gizmo.entity, gizmoTransform!);
|
||||
await _gizmo.setTransform(gizmoTransform!);
|
||||
|
||||
transformUpdates.add((transform: gizmoTransform!));
|
||||
}
|
||||
|
||||
Future<void>? _updateTranslation(
|
||||
Vector2 currentPosition, Vector2 delta) async {
|
||||
var view = await viewer.getViewAt(0);
|
||||
var view = await viewer.view;
|
||||
var camera = await viewer.getActiveCamera();
|
||||
var viewport = await view.getViewport();
|
||||
var projectionMatrix = await viewer.getCameraProjectionMatrix();
|
||||
var projectionMatrix = await camera.getProjectionMatrix();
|
||||
var viewMatrix = await camera.getViewMatrix();
|
||||
var inverseViewMatrix = await camera.getModelMatrix();
|
||||
var inverseProjectionMatrix = projectionMatrix.clone()..invert();
|
||||
@@ -121,10 +122,9 @@ class _Gizmo {
|
||||
}
|
||||
|
||||
Future<void>? _updateRotation(Vector2 currentPosition, Vector2 delta) async {
|
||||
var view = await viewer.getViewAt(0);
|
||||
var camera = await viewer.getActiveCamera();
|
||||
var viewport = await view.getViewport();
|
||||
var projectionMatrix = await viewer.getCameraProjectionMatrix();
|
||||
var camera = await viewer.view.getCamera();
|
||||
var viewport = await viewer.view.getViewport();
|
||||
var projectionMatrix = await camera.getProjectionMatrix();
|
||||
var viewMatrix = await camera.getViewMatrix();
|
||||
|
||||
// Get gizmo center in screen space
|
||||
@@ -187,7 +187,6 @@ class _Gizmo {
|
||||
}
|
||||
|
||||
class GizmoInputHandler extends InputHandler {
|
||||
|
||||
final ThermionViewer viewer;
|
||||
|
||||
late final _gizmos = <GizmoType, _Gizmo>{};
|
||||
@@ -202,7 +201,7 @@ class GizmoInputHandler extends InputHandler {
|
||||
}
|
||||
_attached = entity;
|
||||
if (_active != null) {
|
||||
await viewer.setParent(_attached!, _active!._gizmo.entity);
|
||||
await FilamentApp.instance!.setParent(_attached!, _active!._gizmo.entity);
|
||||
await _active!.reveal();
|
||||
}
|
||||
}
|
||||
@@ -215,7 +214,7 @@ class GizmoInputHandler extends InputHandler {
|
||||
if (_attached == null) {
|
||||
return;
|
||||
}
|
||||
await viewer.setParent(_attached!, 0);
|
||||
await FilamentApp.instance!.setParent(_attached!, null);
|
||||
await _active?.hide();
|
||||
_attached = null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user