use decompose/composeMatrix for transforms and add mouse controls for rotation
This commit is contained in:
@@ -19,54 +19,103 @@ class EntityTransformController {
|
||||
bool _back = false;
|
||||
bool _rotateLeft = false;
|
||||
bool _rotateRight = false;
|
||||
double _rotY = 0;
|
||||
|
||||
int? forwardAnimationIndex;
|
||||
int? backwardAnimationIndex;
|
||||
int? strafeLeftAnimationIndex;
|
||||
int? strafeRightAnimationIndex;
|
||||
|
||||
EntityTransformController(this.controller, this._entity,
|
||||
{this.translationSpeed = 1, this.rotationRadsPerSecond = pi / 2}) {
|
||||
{this.translationSpeed = 1,
|
||||
this.rotationRadsPerSecond = pi / 2,
|
||||
this.forwardAnimationIndex,
|
||||
this.backwardAnimationIndex,
|
||||
this.strafeLeftAnimationIndex,
|
||||
this.strafeRightAnimationIndex}) {
|
||||
var translationSpeedPerTick = translationSpeed / (1000 / 16.667);
|
||||
var rotationRadsPerTick = rotationRadsPerSecond / (1000 / 16.667);
|
||||
_ticker = Timer.periodic(const Duration(milliseconds: 16), (timer) {
|
||||
_update(translationSpeedPerTick);
|
||||
_update(translationSpeedPerTick, rotationRadsPerTick);
|
||||
});
|
||||
}
|
||||
|
||||
void _update(double translationSpeedPerTick) async {
|
||||
bool _enabled = true;
|
||||
void enable() {
|
||||
_enabled = true;
|
||||
}
|
||||
|
||||
void disable() {
|
||||
_enabled = false;
|
||||
}
|
||||
|
||||
void _update(
|
||||
double translationSpeedPerTick, double rotationRadsPerTick) async {
|
||||
if (!_enabled) {
|
||||
return;
|
||||
}
|
||||
var _position = v.Vector3.zero();
|
||||
var _rotation = v.Quaternion.identity();
|
||||
bool requiresUpdate = false;
|
||||
bool updateTranslation = false;
|
||||
if (_forward) {
|
||||
_position.add(v.Vector3(0, 0, -translationSpeedPerTick));
|
||||
requiresUpdate = true;
|
||||
updateTranslation = true;
|
||||
}
|
||||
if (_back) {
|
||||
_position.add(v.Vector3(0, 0, translationSpeedPerTick));
|
||||
requiresUpdate = true;
|
||||
updateTranslation = true;
|
||||
}
|
||||
if (_strafeLeft) {
|
||||
_position.add(v.Vector3(-translationSpeedPerTick, 0, 0));
|
||||
requiresUpdate = true;
|
||||
updateTranslation = true;
|
||||
}
|
||||
if (_strafeRight) {
|
||||
_position.add(v.Vector3(translationSpeedPerTick, 0, 0));
|
||||
requiresUpdate = true;
|
||||
updateTranslation = true;
|
||||
}
|
||||
|
||||
// todo - better to use pitch/yaw/roll
|
||||
if (_rotateLeft) {}
|
||||
if (_rotateRight) {}
|
||||
bool updateRotation = false;
|
||||
var _rotation = v.Quaternion.identity();
|
||||
|
||||
if (requiresUpdate) {
|
||||
double rads = 0.0;
|
||||
if (_rotY != 0) {
|
||||
rads = _rotY! * pi / 1000;
|
||||
var rotY = v.Quaternion.axisAngle(v.Vector3(0, 1, 0), rads).normalized();
|
||||
_rotation = rotY;
|
||||
updateRotation = true;
|
||||
_rotY = 0;
|
||||
}
|
||||
|
||||
if (updateTranslation) {
|
||||
await controller.setPosition(
|
||||
_entity, _position.x, _position.y, _position.z,
|
||||
relative: true);
|
||||
}
|
||||
if (updateRotation) {
|
||||
var axis = _rotation.axis;
|
||||
await controller.setRotationQuat(_entity, _rotation, relative: true);
|
||||
}
|
||||
}
|
||||
|
||||
void look(double deltaX) async {
|
||||
_rotY -= deltaX;
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
_ticker.cancel();
|
||||
}
|
||||
|
||||
void forwardPressed() {
|
||||
print("forward");
|
||||
bool _playingForwardAnimation = false;
|
||||
|
||||
void forwardPressed() async {
|
||||
_forward = true;
|
||||
if (forwardAnimationIndex != null) {
|
||||
if (!_playingForwardAnimation) {
|
||||
await controller.playAnimation(_entity, forwardAnimationIndex!,
|
||||
loop: true);
|
||||
_playingForwardAnimation = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Timer? _forwardTimer;
|
||||
@@ -76,8 +125,12 @@ class EntityTransformController {
|
||||
|
||||
void forwardReleased() async {
|
||||
_forwardTimer?.cancel();
|
||||
_forwardTimer = Timer(Duration(milliseconds: 50), () {
|
||||
_forwardTimer = Timer(Duration(milliseconds: 50), () async {
|
||||
_forward = false;
|
||||
_playingForwardAnimation = false;
|
||||
if (forwardAnimationIndex != null) {
|
||||
await controller.stopAnimation(_entity, forwardAnimationIndex!);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import 'dart:ui' as ui;
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
import 'package:flutter_filament/animations/animation_data.dart';
|
||||
import 'package:flutter_filament/entities/entity_transform_controller.dart';
|
||||
import 'package:vector_math/vector_math_64.dart';
|
||||
|
||||
// a handle that can be safely passed back to the rendering layer to manipulate an Entity
|
||||
@@ -467,7 +468,14 @@ abstract class FilamentController {
|
||||
/// Sets the rotation for [entity] to [rads] around the axis {x,y,z}.
|
||||
///
|
||||
Future setRotation(
|
||||
FilamentEntity entity, double rads, double x, double y, double z);
|
||||
FilamentEntity entity, double rads, double x, double y, double z,
|
||||
{bool relative = false});
|
||||
|
||||
///
|
||||
/// Sets the rotation for [entity] to the specified quaternion.
|
||||
///
|
||||
Future setRotationQuat(FilamentEntity entity, Quaternion rotation,
|
||||
{bool relative = false});
|
||||
|
||||
///
|
||||
/// Reveal the node [meshName] under [entity]. Only applicable if [hide] had previously been called; this is a no-op otherwise.
|
||||
@@ -528,5 +536,6 @@ abstract class FilamentController {
|
||||
// Stream get keyboardFocusRequested;
|
||||
// void requestKeyboardFocus();
|
||||
|
||||
void control(FilamentEntity entity, {double? translationSpeed});
|
||||
Future<EntityTransformController> control(FilamentEntity entity,
|
||||
{double? translationSpeed, String? forwardAnimation});
|
||||
}
|
||||
|
||||
@@ -1069,11 +1069,23 @@ class FilamentControllerFFI extends FilamentController {
|
||||
|
||||
@override
|
||||
Future setRotation(
|
||||
FilamentEntity entity, double rads, double x, double y, double z) async {
|
||||
FilamentEntity entity, double rads, double x, double y, double z,
|
||||
{bool relative = false}) async {
|
||||
if (_viewer == null) {
|
||||
throw Exception("No viewer available, ignoring");
|
||||
}
|
||||
set_rotation(_assetManager!, entity, rads, x, y, z);
|
||||
var quat = Quaternion.axisAngle(Vector3(x, y, z), rads);
|
||||
await setRotationQuat(entity, quat, relative: relative);
|
||||
}
|
||||
|
||||
@override
|
||||
Future setRotationQuat(FilamentEntity entity, Quaternion rotation,
|
||||
{bool relative = false}) async {
|
||||
if (_viewer == null) {
|
||||
throw Exception("No viewer available, ignoring");
|
||||
}
|
||||
set_rotation(_assetManager!, entity, rotation.radians, rotation.x,
|
||||
rotation.y, rotation.z, rotation.w, relative);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -1325,10 +1337,23 @@ class FilamentControllerFFI extends FilamentController {
|
||||
}
|
||||
|
||||
HardwareKeyboardListener? _keyboardListener;
|
||||
void control(FilamentEntity entity, {double? translationSpeed}) {
|
||||
Future<EntityTransformController> control(FilamentEntity entity,
|
||||
{double? translationSpeed, String? forwardAnimation}) async {
|
||||
int? forwardAnimationIndex;
|
||||
if (forwardAnimation != null) {
|
||||
final animationNames = await getAnimationNames(entity);
|
||||
forwardAnimationIndex = animationNames.indexOf(forwardAnimation);
|
||||
}
|
||||
|
||||
if (forwardAnimationIndex == -1) {
|
||||
throw Exception("Invalid animation : $forwardAnimation");
|
||||
}
|
||||
|
||||
_keyboardListener?.dispose();
|
||||
_keyboardListener = HardwareKeyboardListener(EntityTransformController(
|
||||
this, entity,
|
||||
translationSpeed: translationSpeed ?? 1.0));
|
||||
var transformController = EntityTransformController(this, entity,
|
||||
translationSpeed: translationSpeed ?? 1.0,
|
||||
forwardAnimationIndex: forwardAnimationIndex);
|
||||
_keyboardListener = HardwareKeyboardListener(transformController);
|
||||
return transformController;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -564,7 +564,9 @@ external void set_position(
|
||||
ffi.Float,
|
||||
ffi.Float,
|
||||
ffi.Float,
|
||||
ffi.Float)>(symbol: 'set_rotation', assetId: 'flutter_filament_plugin')
|
||||
ffi.Float,
|
||||
ffi.Float,
|
||||
ffi.Bool)>(symbol: 'set_rotation', assetId: 'flutter_filament_plugin')
|
||||
external void set_rotation(
|
||||
ffi.Pointer<ffi.Void> assetManager,
|
||||
int asset,
|
||||
@@ -572,6 +574,8 @@ external void set_rotation(
|
||||
double x,
|
||||
double y,
|
||||
double z,
|
||||
double w,
|
||||
bool relative,
|
||||
);
|
||||
|
||||
@ffi.Native<ffi.Void Function(ffi.Pointer<ffi.Void>, EntityId, ffi.Float)>(
|
||||
|
||||
23
lib/widgets/entity_controller_mouse_widget.dart
Normal file
23
lib/widgets/entity_controller_mouse_widget.dart
Normal file
@@ -0,0 +1,23 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_filament/entities/entity_transform_controller.dart';
|
||||
|
||||
///
|
||||
/// A widget that translates mouse gestures to zoom/pan/rotate actions.
|
||||
///
|
||||
class EntityTransformMouseControllerWidget extends StatelessWidget {
|
||||
final EntityTransformController? transformController;
|
||||
final Widget? child;
|
||||
|
||||
const EntityTransformMouseControllerWidget(
|
||||
{Key? key, required this.transformController, this.child})
|
||||
: super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Listener(
|
||||
onPointerHover: (event) {
|
||||
transformController?.look(event.delta.dx);
|
||||
},
|
||||
child: child);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user