refactoring
This commit is contained in:
@@ -247,7 +247,8 @@ abstract class FilamentApp<T> {
|
||||
///
|
||||
///
|
||||
///
|
||||
Future setClearColor(double r, double g, double b, double a);
|
||||
Future setClearOptions(double r, double g, double b, double a,
|
||||
{int clearStencil = 0, bool discard = false, bool clear = true});
|
||||
|
||||
///
|
||||
///
|
||||
@@ -264,4 +265,9 @@ abstract class FilamentApp<T> {
|
||||
///
|
||||
///
|
||||
Future<T> createColorGrading(ToneMapper mapper);
|
||||
|
||||
///
|
||||
///
|
||||
///
|
||||
Future<GizmoAsset> createGizmo(covariant View view, T animationManager, GizmoType type);
|
||||
}
|
||||
|
||||
@@ -1,371 +1,371 @@
|
||||
import 'dart:async';
|
||||
import 'dart:math';
|
||||
import 'package:thermion_dart/thermion_dart.dart';
|
||||
|
||||
class _Gizmo {
|
||||
final ThermionViewer viewer;
|
||||
|
||||
final GizmoAsset _gizmo;
|
||||
|
||||
final transformUpdates = StreamController<({Matrix4 transform})>.broadcast();
|
||||
|
||||
Axis? _active;
|
||||
final GizmoType type;
|
||||
|
||||
_Gizmo(this._gizmo, this.viewer, this.type);
|
||||
|
||||
static Future<_Gizmo> forType(ThermionViewer viewer, GizmoType type) async {
|
||||
final view = await viewer.view;
|
||||
return _Gizmo(await viewer.createGizmo(view, type), viewer, type);
|
||||
}
|
||||
|
||||
Future dispose() async {
|
||||
await transformUpdates.close();
|
||||
await viewer.destroyAsset(_gizmo);
|
||||
}
|
||||
|
||||
Future hide() async {
|
||||
final scene = await viewer.view.getScene();
|
||||
await scene.remove(_gizmo);
|
||||
}
|
||||
|
||||
Future reveal() async {
|
||||
final scene = await viewer.view.getScene();
|
||||
await scene.add(_gizmo);
|
||||
gizmoTransform = await _gizmo.getWorldTransform();
|
||||
}
|
||||
|
||||
double _getAngleBetweenVectors(Vector2 v1, Vector2 v2) {
|
||||
// Normalize vectors to ensure consistent rotation regardless of distance from center
|
||||
v1.normalize();
|
||||
v2.normalize();
|
||||
|
||||
// Calculate angle using atan2
|
||||
double angle = atan2(v2.y, v2.x) - atan2(v1.y, v1.x);
|
||||
|
||||
// Ensure angle is between -π and π
|
||||
if (angle > pi) angle -= 2 * pi;
|
||||
if (angle < -pi) angle += 2 * pi;
|
||||
|
||||
return angle;
|
||||
}
|
||||
|
||||
void checkHover(int x, int y) async {
|
||||
_gizmo.pick(x, y, handler: (result, coords) async {
|
||||
switch (result) {
|
||||
case GizmoPickResultType.None:
|
||||
await _gizmo.unhighlight();
|
||||
_active = null;
|
||||
break;
|
||||
case GizmoPickResultType.AxisX:
|
||||
_active = Axis.X;
|
||||
case GizmoPickResultType.AxisY:
|
||||
_active = Axis.Y;
|
||||
case GizmoPickResultType.AxisZ:
|
||||
_active = Axis.Z;
|
||||
default:
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Matrix4? gizmoTransform;
|
||||
|
||||
void _updateTransform(Vector2 currentPosition, Vector2 delta) async {
|
||||
if (type == GizmoType.translation) {
|
||||
await _updateTranslation(currentPosition, delta);
|
||||
} else if (type == GizmoType.rotation) {
|
||||
await _updateRotation(currentPosition, delta);
|
||||
}
|
||||
|
||||
await _gizmo.setTransform(gizmoTransform!);
|
||||
|
||||
transformUpdates.add((transform: gizmoTransform!));
|
||||
}
|
||||
|
||||
Future<void>? _updateTranslation(
|
||||
Vector2 currentPosition, Vector2 delta) async {
|
||||
var view = await viewer.view;
|
||||
var camera = await viewer.getActiveCamera();
|
||||
var viewport = await view.getViewport();
|
||||
var projectionMatrix = await camera.getProjectionMatrix();
|
||||
var viewMatrix = await camera.getViewMatrix();
|
||||
var inverseViewMatrix = await camera.getModelMatrix();
|
||||
var inverseProjectionMatrix = projectionMatrix.clone()..invert();
|
||||
|
||||
// get gizmo position in screenspace
|
||||
var gizmoPositionWorldSpace = gizmoTransform!.getTranslation();
|
||||
Vector4 gizmoClipSpace = projectionMatrix *
|
||||
viewMatrix *
|
||||
Vector4(gizmoPositionWorldSpace.x, gizmoPositionWorldSpace.y,
|
||||
gizmoPositionWorldSpace.z, 1.0);
|
||||
|
||||
var gizmoNdc = gizmoClipSpace / gizmoClipSpace.w;
|
||||
|
||||
var gizmoScreenSpace = Vector2(((gizmoNdc.x / 2) + 0.5) * viewport.width,
|
||||
viewport.height - (((gizmoNdc.y / 2) + 0.5) * viewport.height));
|
||||
|
||||
gizmoScreenSpace += delta;
|
||||
|
||||
gizmoNdc = Vector4(((gizmoScreenSpace.x / viewport.width) - 0.5) * 2,
|
||||
(((gizmoScreenSpace.y / viewport.height)) - 0.5) * -2, gizmoNdc.z, 1.0);
|
||||
|
||||
var gizmoViewSpace = inverseProjectionMatrix * gizmoNdc;
|
||||
gizmoViewSpace /= gizmoViewSpace.w;
|
||||
|
||||
var newPosition = (inverseViewMatrix * gizmoViewSpace).xyz;
|
||||
|
||||
Vector3 worldSpaceDelta = newPosition - gizmoTransform!.getTranslation();
|
||||
worldSpaceDelta.multiply(_active!.asVector());
|
||||
|
||||
gizmoTransform!
|
||||
.setTranslation(gizmoTransform!.getTranslation() + worldSpaceDelta);
|
||||
}
|
||||
|
||||
Future<void>? _updateRotation(Vector2 currentPosition, Vector2 delta) async {
|
||||
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
|
||||
var gizmoPositionWorldSpace = gizmoTransform!.getTranslation();
|
||||
Vector4 gizmoClipSpace = projectionMatrix *
|
||||
viewMatrix *
|
||||
Vector4(gizmoPositionWorldSpace.x, gizmoPositionWorldSpace.y,
|
||||
gizmoPositionWorldSpace.z, 1.0);
|
||||
|
||||
var gizmoNdc = gizmoClipSpace / gizmoClipSpace.w;
|
||||
var gizmoScreenSpace = Vector2(((gizmoNdc.x / 2) + 0.5) * viewport.width,
|
||||
viewport.height - (((gizmoNdc.y / 2) + 0.5) * viewport.height));
|
||||
|
||||
// Calculate vectors from gizmo center to previous and current mouse positions
|
||||
var prevVector = (currentPosition - delta) - gizmoScreenSpace;
|
||||
var currentVector = currentPosition - gizmoScreenSpace;
|
||||
|
||||
// Calculate rotation angle based on the active axis
|
||||
double rotationAngle = 0.0;
|
||||
switch (_active) {
|
||||
case Axis.X:
|
||||
// For X axis, project onto YZ plane
|
||||
var prev = Vector2(prevVector.y, -prevVector.x);
|
||||
var curr = Vector2(currentVector.y, -currentVector.x);
|
||||
rotationAngle = _getAngleBetweenVectors(prev, curr);
|
||||
break;
|
||||
case Axis.Y:
|
||||
// For Y axis, project onto XZ plane
|
||||
var prev = Vector2(prevVector.x, -prevVector.y);
|
||||
var curr = Vector2(currentVector.x, -currentVector.y);
|
||||
rotationAngle = _getAngleBetweenVectors(prev, curr);
|
||||
break;
|
||||
case Axis.Z:
|
||||
// For Z axis, use screen plane directly
|
||||
rotationAngle = -1 * _getAngleBetweenVectors(prevVector, currentVector);
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
// Create rotation matrix based on the active axis
|
||||
var rotationMatrix = Matrix4.identity();
|
||||
switch (_active) {
|
||||
case Axis.X:
|
||||
rotationMatrix.setRotationX(rotationAngle);
|
||||
break;
|
||||
case Axis.Y:
|
||||
rotationMatrix.setRotationY(rotationAngle);
|
||||
break;
|
||||
case Axis.Z:
|
||||
rotationMatrix.setRotationZ(rotationAngle);
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
// Apply rotation to the current transform
|
||||
gizmoTransform = gizmoTransform! * rotationMatrix;
|
||||
}
|
||||
}
|
||||
|
||||
class GizmoInputHandler extends InputHandler {
|
||||
final ThermionViewer viewer;
|
||||
|
||||
late final _gizmos = <GizmoType, _Gizmo>{};
|
||||
|
||||
_Gizmo? _active;
|
||||
|
||||
ThermionEntity? _attached;
|
||||
|
||||
Future attach(ThermionEntity entity) async {
|
||||
if (_attached != null) {
|
||||
await detach();
|
||||
}
|
||||
_attached = entity;
|
||||
if (_active != null) {
|
||||
await FilamentApp.instance!.setParent(_attached!, _active!._gizmo.entity);
|
||||
await _active!.reveal();
|
||||
}
|
||||
}
|
||||
|
||||
Future<Matrix4?> getGizmoTransform() async {
|
||||
return _active?.gizmoTransform;
|
||||
}
|
||||
|
||||
Future detach() async {
|
||||
if (_attached == null) {
|
||||
return;
|
||||
}
|
||||
await FilamentApp.instance!.setParent(_attached!, null);
|
||||
await _active?.hide();
|
||||
_attached = null;
|
||||
}
|
||||
|
||||
final _initialized = Completer<bool>();
|
||||
|
||||
final _transformController = StreamController<Matrix4>.broadcast();
|
||||
Stream<Matrix4> get transformUpdated => _transformController.stream;
|
||||
|
||||
final _pickResultController = StreamController<ThermionEntity?>.broadcast();
|
||||
Stream<ThermionEntity?> get onPickResult => _pickResultController.stream;
|
||||
|
||||
GizmoInputHandler({required this.viewer, required GizmoType initialType}) {
|
||||
initialize().then((_) {
|
||||
setGizmoType(initialType);
|
||||
});
|
||||
}
|
||||
|
||||
GizmoType? getGizmoType() {
|
||||
return _active?.type;
|
||||
}
|
||||
|
||||
Future setGizmoType(GizmoType? type) async {
|
||||
if (type == null) {
|
||||
await detach();
|
||||
_active?.hide();
|
||||
_active = null;
|
||||
} else {
|
||||
_active?.hide();
|
||||
_active = _gizmos[type]!;
|
||||
_active!.reveal();
|
||||
if (_attached != null) {
|
||||
await attach(_attached!);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future initialize() async {
|
||||
if (_initialized.isCompleted) {
|
||||
throw Exception("Already initialized");
|
||||
}
|
||||
await viewer.initialized;
|
||||
|
||||
_gizmos[GizmoType.translation] =
|
||||
await _Gizmo.forType(viewer, GizmoType.translation);
|
||||
_gizmos[GizmoType.rotation] =
|
||||
await _Gizmo.forType(viewer, GizmoType.rotation);
|
||||
await setGizmoType(GizmoType.translation);
|
||||
for (final gizmo in _gizmos.values) {
|
||||
gizmo.transformUpdates.stream.listen((update) {
|
||||
_transformController.add(update.transform);
|
||||
});
|
||||
}
|
||||
_initialized.complete(true);
|
||||
}
|
||||
|
||||
@override
|
||||
Future dispose() async {
|
||||
_gizmos[GizmoType.rotation]!.dispose();
|
||||
_gizmos[GizmoType.translation]!.dispose();
|
||||
_gizmos.clear();
|
||||
}
|
||||
|
||||
@override
|
||||
InputAction? getActionForType(InputType gestureType) {
|
||||
if (gestureType == InputType.LMB_DOWN) {
|
||||
return InputAction.PICK;
|
||||
}
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> get initialized => _initialized.future;
|
||||
|
||||
@override
|
||||
void keyDown(PhysicalKey key) {}
|
||||
|
||||
@override
|
||||
void keyUp(PhysicalKey key) {}
|
||||
|
||||
@override
|
||||
Future<void>? onPointerDown(Vector2 localPosition, bool isMiddle) async {
|
||||
if (!_initialized.isCompleted) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isMiddle) {
|
||||
return;
|
||||
}
|
||||
|
||||
await viewer.pick(localPosition.x.toInt(), localPosition.y.toInt(),
|
||||
(result) async {
|
||||
if (_active?._gizmo.isNonPickable(result.entity) == true ||
|
||||
result.entity == FILAMENT_ENTITY_NULL) {
|
||||
_pickResultController.add(null);
|
||||
return;
|
||||
}
|
||||
if (_active?._gizmo.isGizmoEntity(result.entity) != true) {
|
||||
_pickResultController.add(result.entity);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void>? onPointerHover(Vector2 localPosition, Vector2 delta) async {
|
||||
if (!_initialized.isCompleted) {
|
||||
return;
|
||||
}
|
||||
_active?.checkHover(localPosition.x.floor(), localPosition.y.floor());
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void>? onPointerMove(
|
||||
Vector2 localPosition, Vector2 delta, bool isMiddle) async {
|
||||
if (!isMiddle && _active?._active != null) {
|
||||
final scaledDelta = Vector2(
|
||||
delta.x,
|
||||
delta.y,
|
||||
);
|
||||
_active!._updateTransform(localPosition, scaledDelta);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void>? onPointerScroll(
|
||||
Vector2 localPosition, double scrollDelta) async {}
|
||||
|
||||
@override
|
||||
Future<void>? onPointerUp(bool isMiddle) async {}
|
||||
|
||||
@override
|
||||
Future<void>? onScaleEnd(int pointerCount, double velocity) {}
|
||||
|
||||
@override
|
||||
Future<void>? onScaleStart(
|
||||
Vector2 focalPoint, int pointerCount, Duration? sourceTimestamp) {}
|
||||
|
||||
@override
|
||||
Future<void>? onScaleUpdate(
|
||||
Vector2 focalPoint,
|
||||
Vector2 focalPointDelta,
|
||||
double horizontalScale,
|
||||
double verticalScale,
|
||||
double scale,
|
||||
int pointerCount,
|
||||
double rotation,
|
||||
Duration? sourceTimestamp) {}
|
||||
|
||||
@override
|
||||
void setActionForType(InputType gestureType, InputAction gestureAction) {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
}
|
||||
// import 'dart:async';
|
||||
// import 'dart:math';
|
||||
// import 'package:thermion_dart/thermion_dart.dart';
|
||||
|
||||
// class _Gizmo {
|
||||
// final ThermionViewer viewer;
|
||||
|
||||
// final GizmoAsset _gizmo;
|
||||
|
||||
// final transformUpdates = StreamController<({Matrix4 transform})>.broadcast();
|
||||
|
||||
// Axis? _active;
|
||||
// final GizmoType type;
|
||||
|
||||
// _Gizmo(this._gizmo, this.viewer, this.type);
|
||||
|
||||
// static Future<_Gizmo> forType(ThermionViewer viewer, GizmoType type) async {
|
||||
// final view = await viewer.view;
|
||||
// return _Gizmo(await viewer.createGizmo(view, type), viewer, type);
|
||||
// }
|
||||
|
||||
// Future dispose() async {
|
||||
// await transformUpdates.close();
|
||||
// await viewer.destroyAsset(_gizmo);
|
||||
// }
|
||||
|
||||
// Future hide() async {
|
||||
// final scene = await viewer.view.getScene();
|
||||
// await scene.remove(_gizmo);
|
||||
// }
|
||||
|
||||
// Future reveal() async {
|
||||
// final scene = await viewer.view.getScene();
|
||||
// await scene.add(_gizmo);
|
||||
// gizmoTransform = await _gizmo.getWorldTransform();
|
||||
// }
|
||||
|
||||
// double _getAngleBetweenVectors(Vector2 v1, Vector2 v2) {
|
||||
// // Normalize vectors to ensure consistent rotation regardless of distance from center
|
||||
// v1.normalize();
|
||||
// v2.normalize();
|
||||
|
||||
// // Calculate angle using atan2
|
||||
// double angle = atan2(v2.y, v2.x) - atan2(v1.y, v1.x);
|
||||
|
||||
// // Ensure angle is between -π and π
|
||||
// if (angle > pi) angle -= 2 * pi;
|
||||
// if (angle < -pi) angle += 2 * pi;
|
||||
|
||||
// return angle;
|
||||
// }
|
||||
|
||||
// void checkHover(int x, int y) async {
|
||||
// _gizmo.pick(x, y, handler: (result, coords) async {
|
||||
// switch (result) {
|
||||
// case GizmoPickResultType.None:
|
||||
// await _gizmo.unhighlight();
|
||||
// _active = null;
|
||||
// break;
|
||||
// case GizmoPickResultType.AxisX:
|
||||
// _active = Axis.X;
|
||||
// case GizmoPickResultType.AxisY:
|
||||
// _active = Axis.Y;
|
||||
// case GizmoPickResultType.AxisZ:
|
||||
// _active = Axis.Z;
|
||||
// default:
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
|
||||
// Matrix4? gizmoTransform;
|
||||
|
||||
// void _updateTransform(Vector2 currentPosition, Vector2 delta) async {
|
||||
// if (type == GizmoType.translation) {
|
||||
// await _updateTranslation(currentPosition, delta);
|
||||
// } else if (type == GizmoType.rotation) {
|
||||
// await _updateRotation(currentPosition, delta);
|
||||
// }
|
||||
|
||||
// await _gizmo.setTransform(gizmoTransform!);
|
||||
|
||||
// transformUpdates.add((transform: gizmoTransform!));
|
||||
// }
|
||||
|
||||
// Future<void>? _updateTranslation(
|
||||
// Vector2 currentPosition, Vector2 delta) async {
|
||||
// var view = await viewer.view;
|
||||
// var camera = await viewer.getActiveCamera();
|
||||
// var viewport = await view.getViewport();
|
||||
// var projectionMatrix = await camera.getProjectionMatrix();
|
||||
// var viewMatrix = await camera.getViewMatrix();
|
||||
// var inverseViewMatrix = await camera.getModelMatrix();
|
||||
// var inverseProjectionMatrix = projectionMatrix.clone()..invert();
|
||||
|
||||
// // get gizmo position in screenspace
|
||||
// var gizmoPositionWorldSpace = gizmoTransform!.getTranslation();
|
||||
// Vector4 gizmoClipSpace = projectionMatrix *
|
||||
// viewMatrix *
|
||||
// Vector4(gizmoPositionWorldSpace.x, gizmoPositionWorldSpace.y,
|
||||
// gizmoPositionWorldSpace.z, 1.0);
|
||||
|
||||
// var gizmoNdc = gizmoClipSpace / gizmoClipSpace.w;
|
||||
|
||||
// var gizmoScreenSpace = Vector2(((gizmoNdc.x / 2) + 0.5) * viewport.width,
|
||||
// viewport.height - (((gizmoNdc.y / 2) + 0.5) * viewport.height));
|
||||
|
||||
// gizmoScreenSpace += delta;
|
||||
|
||||
// gizmoNdc = Vector4(((gizmoScreenSpace.x / viewport.width) - 0.5) * 2,
|
||||
// (((gizmoScreenSpace.y / viewport.height)) - 0.5) * -2, gizmoNdc.z, 1.0);
|
||||
|
||||
// var gizmoViewSpace = inverseProjectionMatrix * gizmoNdc;
|
||||
// gizmoViewSpace /= gizmoViewSpace.w;
|
||||
|
||||
// var newPosition = (inverseViewMatrix * gizmoViewSpace).xyz;
|
||||
|
||||
// Vector3 worldSpaceDelta = newPosition - gizmoTransform!.getTranslation();
|
||||
// worldSpaceDelta.multiply(_active!.asVector());
|
||||
|
||||
// gizmoTransform!
|
||||
// .setTranslation(gizmoTransform!.getTranslation() + worldSpaceDelta);
|
||||
// }
|
||||
|
||||
// Future<void>? _updateRotation(Vector2 currentPosition, Vector2 delta) async {
|
||||
// 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
|
||||
// var gizmoPositionWorldSpace = gizmoTransform!.getTranslation();
|
||||
// Vector4 gizmoClipSpace = projectionMatrix *
|
||||
// viewMatrix *
|
||||
// Vector4(gizmoPositionWorldSpace.x, gizmoPositionWorldSpace.y,
|
||||
// gizmoPositionWorldSpace.z, 1.0);
|
||||
|
||||
// var gizmoNdc = gizmoClipSpace / gizmoClipSpace.w;
|
||||
// var gizmoScreenSpace = Vector2(((gizmoNdc.x / 2) + 0.5) * viewport.width,
|
||||
// viewport.height - (((gizmoNdc.y / 2) + 0.5) * viewport.height));
|
||||
|
||||
// // Calculate vectors from gizmo center to previous and current mouse positions
|
||||
// var prevVector = (currentPosition - delta) - gizmoScreenSpace;
|
||||
// var currentVector = currentPosition - gizmoScreenSpace;
|
||||
|
||||
// // Calculate rotation angle based on the active axis
|
||||
// double rotationAngle = 0.0;
|
||||
// switch (_active) {
|
||||
// case Axis.X:
|
||||
// // For X axis, project onto YZ plane
|
||||
// var prev = Vector2(prevVector.y, -prevVector.x);
|
||||
// var curr = Vector2(currentVector.y, -currentVector.x);
|
||||
// rotationAngle = _getAngleBetweenVectors(prev, curr);
|
||||
// break;
|
||||
// case Axis.Y:
|
||||
// // For Y axis, project onto XZ plane
|
||||
// var prev = Vector2(prevVector.x, -prevVector.y);
|
||||
// var curr = Vector2(currentVector.x, -currentVector.y);
|
||||
// rotationAngle = _getAngleBetweenVectors(prev, curr);
|
||||
// break;
|
||||
// case Axis.Z:
|
||||
// // For Z axis, use screen plane directly
|
||||
// rotationAngle = -1 * _getAngleBetweenVectors(prevVector, currentVector);
|
||||
// break;
|
||||
// default:
|
||||
// return;
|
||||
// }
|
||||
|
||||
// // Create rotation matrix based on the active axis
|
||||
// var rotationMatrix = Matrix4.identity();
|
||||
// switch (_active) {
|
||||
// case Axis.X:
|
||||
// rotationMatrix.setRotationX(rotationAngle);
|
||||
// break;
|
||||
// case Axis.Y:
|
||||
// rotationMatrix.setRotationY(rotationAngle);
|
||||
// break;
|
||||
// case Axis.Z:
|
||||
// rotationMatrix.setRotationZ(rotationAngle);
|
||||
// break;
|
||||
// default:
|
||||
// return;
|
||||
// }
|
||||
|
||||
// // Apply rotation to the current transform
|
||||
// gizmoTransform = gizmoTransform! * rotationMatrix;
|
||||
// }
|
||||
// }
|
||||
|
||||
// class GizmoInputHandler extends InputHandler {
|
||||
// final ThermionViewer viewer;
|
||||
|
||||
// late final _gizmos = <GizmoType, _Gizmo>{};
|
||||
|
||||
// _Gizmo? _active;
|
||||
|
||||
// ThermionEntity? _attached;
|
||||
|
||||
// Future attach(ThermionEntity entity) async {
|
||||
// if (_attached != null) {
|
||||
// await detach();
|
||||
// }
|
||||
// _attached = entity;
|
||||
// if (_active != null) {
|
||||
// await FilamentApp.instance!.setParent(_attached!, _active!._gizmo.entity);
|
||||
// await _active!.reveal();
|
||||
// }
|
||||
// }
|
||||
|
||||
// Future<Matrix4?> getGizmoTransform() async {
|
||||
// return _active?.gizmoTransform;
|
||||
// }
|
||||
|
||||
// Future detach() async {
|
||||
// if (_attached == null) {
|
||||
// return;
|
||||
// }
|
||||
// await FilamentApp.instance!.setParent(_attached!, null);
|
||||
// await _active?.hide();
|
||||
// _attached = null;
|
||||
// }
|
||||
|
||||
// final _initialized = Completer<bool>();
|
||||
|
||||
// final _transformController = StreamController<Matrix4>.broadcast();
|
||||
// Stream<Matrix4> get transformUpdated => _transformController.stream;
|
||||
|
||||
// final _pickResultController = StreamController<ThermionEntity?>.broadcast();
|
||||
// Stream<ThermionEntity?> get onPickResult => _pickResultController.stream;
|
||||
|
||||
// GizmoInputHandler({required this.viewer, required GizmoType initialType}) {
|
||||
// initialize().then((_) {
|
||||
// setGizmoType(initialType);
|
||||
// });
|
||||
// }
|
||||
|
||||
// GizmoType? getGizmoType() {
|
||||
// return _active?.type;
|
||||
// }
|
||||
|
||||
// Future setGizmoType(GizmoType? type) async {
|
||||
// if (type == null) {
|
||||
// await detach();
|
||||
// _active?.hide();
|
||||
// _active = null;
|
||||
// } else {
|
||||
// _active?.hide();
|
||||
// _active = _gizmos[type]!;
|
||||
// _active!.reveal();
|
||||
// if (_attached != null) {
|
||||
// await attach(_attached!);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// Future initialize() async {
|
||||
// if (_initialized.isCompleted) {
|
||||
// throw Exception("Already initialized");
|
||||
// }
|
||||
// await viewer.initialized;
|
||||
|
||||
// _gizmos[GizmoType.translation] =
|
||||
// await _Gizmo.forType(viewer, GizmoType.translation);
|
||||
// _gizmos[GizmoType.rotation] =
|
||||
// await _Gizmo.forType(viewer, GizmoType.rotation);
|
||||
// await setGizmoType(GizmoType.translation);
|
||||
// for (final gizmo in _gizmos.values) {
|
||||
// gizmo.transformUpdates.stream.listen((update) {
|
||||
// _transformController.add(update.transform);
|
||||
// });
|
||||
// }
|
||||
// _initialized.complete(true);
|
||||
// }
|
||||
|
||||
// @override
|
||||
// Future dispose() async {
|
||||
// _gizmos[GizmoType.rotation]!.dispose();
|
||||
// _gizmos[GizmoType.translation]!.dispose();
|
||||
// _gizmos.clear();
|
||||
// }
|
||||
|
||||
// @override
|
||||
// InputAction? getActionForType(InputType gestureType) {
|
||||
// if (gestureType == InputType.LMB_DOWN) {
|
||||
// return InputAction.PICK;
|
||||
// }
|
||||
// throw UnimplementedError();
|
||||
// }
|
||||
|
||||
// @override
|
||||
// Future<bool> get initialized => _initialized.future;
|
||||
|
||||
// @override
|
||||
// void keyDown(PhysicalKey key) {}
|
||||
|
||||
// @override
|
||||
// void keyUp(PhysicalKey key) {}
|
||||
|
||||
// @override
|
||||
// Future<void>? onPointerDown(Vector2 localPosition, bool isMiddle) async {
|
||||
// if (!_initialized.isCompleted) {
|
||||
// return;
|
||||
// }
|
||||
|
||||
// if (isMiddle) {
|
||||
// return;
|
||||
// }
|
||||
|
||||
// await viewer.pick(localPosition.x.toInt(), localPosition.y.toInt(),
|
||||
// (result) async {
|
||||
// if (_active?._gizmo.isNonPickable(result.entity) == true ||
|
||||
// result.entity == FILAMENT_ENTITY_NULL) {
|
||||
// _pickResultController.add(null);
|
||||
// return;
|
||||
// }
|
||||
// if (_active?._gizmo.isGizmoEntity(result.entity) != true) {
|
||||
// _pickResultController.add(result.entity);
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
|
||||
// @override
|
||||
// Future<void>? onPointerHover(Vector2 localPosition, Vector2 delta) async {
|
||||
// if (!_initialized.isCompleted) {
|
||||
// return;
|
||||
// }
|
||||
// _active?.checkHover(localPosition.x.floor(), localPosition.y.floor());
|
||||
// }
|
||||
|
||||
// @override
|
||||
// Future<void>? onPointerMove(
|
||||
// Vector2 localPosition, Vector2 delta, bool isMiddle) async {
|
||||
// if (!isMiddle && _active?._active != null) {
|
||||
// final scaledDelta = Vector2(
|
||||
// delta.x,
|
||||
// delta.y,
|
||||
// );
|
||||
// _active!._updateTransform(localPosition, scaledDelta);
|
||||
// return;
|
||||
// }
|
||||
// }
|
||||
|
||||
// @override
|
||||
// Future<void>? onPointerScroll(
|
||||
// Vector2 localPosition, double scrollDelta) async {}
|
||||
|
||||
// @override
|
||||
// Future<void>? onPointerUp(bool isMiddle) async {}
|
||||
|
||||
// @override
|
||||
// Future<void>? onScaleEnd(int pointerCount, double velocity) {}
|
||||
|
||||
// @override
|
||||
// Future<void>? onScaleStart(
|
||||
// Vector2 focalPoint, int pointerCount, Duration? sourceTimestamp) {}
|
||||
|
||||
// @override
|
||||
// Future<void>? onScaleUpdate(
|
||||
// Vector2 focalPoint,
|
||||
// Vector2 focalPointDelta,
|
||||
// double horizontalScale,
|
||||
// double verticalScale,
|
||||
// double scale,
|
||||
// int pointerCount,
|
||||
// double rotation,
|
||||
// Duration? sourceTimestamp) {}
|
||||
|
||||
// @override
|
||||
// void setActionForType(InputType gestureType, InputAction gestureAction) {
|
||||
// throw UnimplementedError();
|
||||
// }
|
||||
// }
|
||||
|
||||
@@ -6,6 +6,7 @@ import 'package:thermion_dart/src/filament/src/engine.dart';
|
||||
import 'package:thermion_dart/src/filament/src/scene.dart';
|
||||
import 'package:thermion_dart/src/viewer/src/ffi/src/callbacks.dart';
|
||||
import 'package:thermion_dart/src/viewer/src/ffi/src/ffi_asset.dart';
|
||||
import 'package:thermion_dart/src/viewer/src/ffi/src/ffi_gizmo.dart';
|
||||
import 'package:thermion_dart/src/viewer/src/ffi/src/ffi_material.dart';
|
||||
import 'package:thermion_dart/src/viewer/src/ffi/src/ffi_render_target.dart';
|
||||
import 'package:thermion_dart/src/viewer/src/ffi/src/ffi_scene.dart';
|
||||
@@ -609,14 +610,6 @@ class FFIFilamentApp extends FilamentApp<Pointer> {
|
||||
.firstWhere((x) => _swapChains[x]?.contains(view) == true);
|
||||
final out = Uint8List(viewport.width * viewport.height * 4);
|
||||
|
||||
await withVoidCallback((cb) {
|
||||
Engine_flushAndWaitRenderThead(engine, cb);
|
||||
});
|
||||
|
||||
var fence = await withPointerCallback<TFence>((cb) {
|
||||
Engine_createFenceRenderThread(engine, cb);
|
||||
});
|
||||
|
||||
await withBoolCallback((cb) {
|
||||
Renderer_beginFrameRenderThread(renderer, swapChain.swapChain, 0, cb);
|
||||
});
|
||||
@@ -647,20 +640,18 @@ class FFIFilamentApp extends FilamentApp<Pointer> {
|
||||
});
|
||||
|
||||
await withVoidCallback((cb) {
|
||||
Engine_destroyFenceRenderThread(engine, fence, cb);
|
||||
Engine_flushAndWaitRenderThead(engine, cb);
|
||||
});
|
||||
|
||||
// await withVoidCallback((cb) {
|
||||
// Engine_flushAndWaitRenderThead(engine, cb);
|
||||
// });
|
||||
return out;
|
||||
}
|
||||
|
||||
///
|
||||
///
|
||||
///
|
||||
Future setClearColor(double r, double g, double b, double a) async {
|
||||
Renderer_setClearOptions(renderer, r, g, b, a, 0, true, false);
|
||||
Future setClearOptions(double r, double g, double b, double a,
|
||||
{int clearStencil = 0, bool discard = false, bool clear = true}) async {
|
||||
Renderer_setClearOptions(
|
||||
renderer, r, g, b, a, clearStencil, clear, discard);
|
||||
}
|
||||
|
||||
///
|
||||
@@ -735,7 +726,7 @@ class FFIFilamentApp extends FilamentApp<Pointer> {
|
||||
View_setColorGrading(view.view, nullptr);
|
||||
for (final cg in view.colorGrading.entries) {
|
||||
await withVoidCallback(
|
||||
(cb) => Engine_destroyColorGradingRenderThread(engine, cg.value, cb));
|
||||
(cb) => Engine_destroyColorGradingRenderThread(engine, cg.value, cb));
|
||||
}
|
||||
await withVoidCallback(
|
||||
(cb) => Engine_destroyViewRenderThread(engine, view.view, cb));
|
||||
@@ -751,6 +742,43 @@ class FFIFilamentApp extends FilamentApp<Pointer> {
|
||||
ColorGrading_createRenderThread(
|
||||
engine, TToneMapping.values[mapper.index], cb));
|
||||
}
|
||||
|
||||
FFIMaterial? _gizmoMaterial;
|
||||
|
||||
///
|
||||
///
|
||||
///
|
||||
Future<GizmoAsset> createGizmo(covariant FFIView view,
|
||||
Pointer animationManager, GizmoType gizmoType) async {
|
||||
if (_gizmoMaterial == null) {
|
||||
final materialPtr = await withPointerCallback<TMaterial>((cb) {
|
||||
Material_createGizmoMaterialRenderThread(engine, cb);
|
||||
});
|
||||
_gizmoMaterial ??= FFIMaterial(materialPtr, this);
|
||||
}
|
||||
|
||||
var gltfResourceLoader = await withPointerCallback<TGltfResourceLoader>(
|
||||
(cb) => GltfResourceLoader_createRenderThread(engine, nullptr, cb));
|
||||
|
||||
final gizmo = await withPointerCallback<TGizmo>((cb) {
|
||||
Gizmo_createRenderThread(engine, gltfAssetLoader, gltfResourceLoader, nameComponentManager,
|
||||
view.view, _gizmoMaterial!.pointer, TGizmoType.values[gizmoType.index], cb);
|
||||
});
|
||||
if (gizmo == nullptr) {
|
||||
throw Exception("Failed to create gizmo");
|
||||
}
|
||||
final gizmoEntityCount =
|
||||
SceneAsset_getChildEntityCount(gizmo.cast<TSceneAsset>());
|
||||
final gizmoEntities = Int32List(gizmoEntityCount);
|
||||
SceneAsset_getChildEntities(
|
||||
gizmo.cast<TSceneAsset>(), gizmoEntities.address);
|
||||
|
||||
return FFIGizmo(gizmo.cast<TSceneAsset>(), this,
|
||||
animationManager.cast<TAnimationManager>(),
|
||||
view: view,
|
||||
entities: gizmoEntities.toSet()
|
||||
..add(SceneAsset_getEntity(gizmo.cast<TSceneAsset>())));
|
||||
}
|
||||
}
|
||||
|
||||
class FinalizableUint8List implements Finalizable {
|
||||
|
||||
@@ -25,9 +25,9 @@ class FFIGizmo extends FFIAsset implements GizmoAsset {
|
||||
bool isGizmoEntity(ThermionEntity entity) => entities.contains(entity);
|
||||
|
||||
FFIGizmo(
|
||||
super.asset,
|
||||
super.app,
|
||||
super.animationManager,
|
||||
super.asset,
|
||||
super.app,
|
||||
super.animationManager,
|
||||
{
|
||||
required this.view,
|
||||
required this.entities,
|
||||
|
||||
@@ -35,6 +35,11 @@ external ffi.Pointer<TMaterial> Material_createGridMaterial(
|
||||
ffi.Pointer<TEngine> tEngine,
|
||||
);
|
||||
|
||||
@ffi.Native<ffi.Pointer<TMaterial> Function(ffi.Pointer<TEngine>)>(isLeaf: true)
|
||||
external ffi.Pointer<TMaterial> Material_createGizmoMaterial(
|
||||
ffi.Pointer<TEngine> tEngine,
|
||||
);
|
||||
|
||||
@ffi.Native<ffi.Bool Function(ffi.Pointer<TMaterial>, ffi.Pointer<ffi.Char>)>(
|
||||
isLeaf: true)
|
||||
external bool Material_hasParameter(
|
||||
@@ -1057,22 +1062,40 @@ external void TextureSampler_destroy(
|
||||
);
|
||||
|
||||
@ffi.Native<
|
||||
ffi.Pointer<TGizmo> Function(ffi.Pointer<TEngine>, ffi.Pointer<TView>,
|
||||
ffi.Pointer<TGizmo> Function(
|
||||
ffi.Pointer<TEngine>,
|
||||
ffi.Pointer<TGltfAssetLoader>,
|
||||
ffi.Pointer<TGltfResourceLoader>,
|
||||
ffi.Pointer<TNameComponentManager>,
|
||||
ffi.Pointer<TView>,
|
||||
ffi.Pointer<TMaterial>,
|
||||
ffi.UnsignedInt)>(symbol: "Gizmo_create", isLeaf: true)
|
||||
external ffi.Pointer<TGizmo> _Gizmo_create(
|
||||
ffi.Pointer<TEngine> tEngine,
|
||||
ffi.Pointer<TGltfAssetLoader> assetLoader,
|
||||
ffi.Pointer<TGltfResourceLoader> tGltfResourceLoader,
|
||||
ffi.Pointer<TNameComponentManager> tNameComponentManager,
|
||||
ffi.Pointer<TView> tView,
|
||||
ffi.Pointer<TMaterial> tMaterial,
|
||||
int tGizmoType,
|
||||
);
|
||||
|
||||
ffi.Pointer<TGizmo> Gizmo_create(
|
||||
ffi.Pointer<TEngine> tEngine,
|
||||
ffi.Pointer<TGltfAssetLoader> assetLoader,
|
||||
ffi.Pointer<TGltfResourceLoader> tGltfResourceLoader,
|
||||
ffi.Pointer<TNameComponentManager> tNameComponentManager,
|
||||
ffi.Pointer<TView> tView,
|
||||
ffi.Pointer<TMaterial> tMaterial,
|
||||
TGizmoType tGizmoType,
|
||||
) =>
|
||||
_Gizmo_create(
|
||||
tEngine,
|
||||
assetLoader,
|
||||
tGltfResourceLoader,
|
||||
tNameComponentManager,
|
||||
tView,
|
||||
tMaterial,
|
||||
tGizmoType.value,
|
||||
);
|
||||
|
||||
@@ -1991,6 +2014,19 @@ external void Material_createImageMaterialRenderThread(
|
||||
onComplete,
|
||||
);
|
||||
|
||||
@ffi.Native<
|
||||
ffi.Void Function(
|
||||
ffi.Pointer<TEngine>,
|
||||
ffi.Pointer<
|
||||
ffi
|
||||
.NativeFunction<ffi.Void Function(ffi.Pointer<TMaterial>)>>)>(
|
||||
isLeaf: true)
|
||||
external void Material_createGizmoMaterialRenderThread(
|
||||
ffi.Pointer<TEngine> tEngine,
|
||||
ffi.Pointer<ffi.NativeFunction<ffi.Void Function(ffi.Pointer<TMaterial>)>>
|
||||
onComplete,
|
||||
);
|
||||
|
||||
@ffi.Native<
|
||||
ffi.Void Function(
|
||||
ffi.Pointer<TEngine>,
|
||||
@@ -2151,17 +2187,6 @@ external void MaterialProvider_createMaterialInstanceRenderThread(
|
||||
callback,
|
||||
);
|
||||
|
||||
@ffi.Native<
|
||||
ffi.Void Function(
|
||||
ffi.Pointer<TSceneManager>,
|
||||
ffi.Pointer<TMaterialInstance>,
|
||||
ffi.Pointer<ffi.NativeFunction<ffi.Void Function()>>)>(isLeaf: true)
|
||||
external void SceneManager_destroyMaterialInstanceRenderThread(
|
||||
ffi.Pointer<TSceneManager> tSceneManager,
|
||||
ffi.Pointer<TMaterialInstance> tMaterialInstance,
|
||||
ffi.Pointer<ffi.NativeFunction<ffi.Void Function()>> callback,
|
||||
);
|
||||
|
||||
@ffi.Native<
|
||||
ffi.Void Function(
|
||||
ffi.Pointer<TAnimationManager>,
|
||||
@@ -2741,6 +2766,52 @@ external void Scene_addFilamentAssetRenderThread(
|
||||
ffi.Pointer<ffi.NativeFunction<ffi.Void Function()>> callback,
|
||||
);
|
||||
|
||||
@ffi.Native<
|
||||
ffi.Void Function(
|
||||
ffi.Pointer<TEngine>,
|
||||
ffi.Pointer<TGltfAssetLoader>,
|
||||
ffi.Pointer<TGltfResourceLoader>,
|
||||
ffi.Pointer<TNameComponentManager>,
|
||||
ffi.Pointer<TView>,
|
||||
ffi.Pointer<TMaterial>,
|
||||
ffi.UnsignedInt,
|
||||
ffi.Pointer<
|
||||
ffi.NativeFunction<ffi.Void Function(ffi.Pointer<TGizmo>)>>)>(
|
||||
symbol: "Gizmo_createRenderThread", isLeaf: true)
|
||||
external void _Gizmo_createRenderThread(
|
||||
ffi.Pointer<TEngine> tEngine,
|
||||
ffi.Pointer<TGltfAssetLoader> tAssetLoader,
|
||||
ffi.Pointer<TGltfResourceLoader> tGltfResourceLoader,
|
||||
ffi.Pointer<TNameComponentManager> tNameComponentManager,
|
||||
ffi.Pointer<TView> tView,
|
||||
ffi.Pointer<TMaterial> tMaterial,
|
||||
int tGizmoType,
|
||||
ffi.Pointer<ffi.NativeFunction<ffi.Void Function(ffi.Pointer<TGizmo>)>>
|
||||
callback,
|
||||
);
|
||||
|
||||
void Gizmo_createRenderThread(
|
||||
ffi.Pointer<TEngine> tEngine,
|
||||
ffi.Pointer<TGltfAssetLoader> tAssetLoader,
|
||||
ffi.Pointer<TGltfResourceLoader> tGltfResourceLoader,
|
||||
ffi.Pointer<TNameComponentManager> tNameComponentManager,
|
||||
ffi.Pointer<TView> tView,
|
||||
ffi.Pointer<TMaterial> tMaterial,
|
||||
TGizmoType tGizmoType,
|
||||
ffi.Pointer<ffi.NativeFunction<ffi.Void Function(ffi.Pointer<TGizmo>)>>
|
||||
callback,
|
||||
) =>
|
||||
_Gizmo_createRenderThread(
|
||||
tEngine,
|
||||
tAssetLoader,
|
||||
tGltfResourceLoader,
|
||||
tNameComponentManager,
|
||||
tView,
|
||||
tMaterial,
|
||||
tGizmoType.value,
|
||||
callback,
|
||||
);
|
||||
|
||||
@ffi.Native<
|
||||
ffi.Pointer<TGltfResourceLoader> Function(
|
||||
ffi.Pointer<TEngine>, ffi.Pointer<ffi.Char>)>(isLeaf: true)
|
||||
@@ -3824,15 +3895,15 @@ final class Aabb3 extends ffi.Struct {
|
||||
}
|
||||
|
||||
enum TGizmoType {
|
||||
TRANSLATION(0),
|
||||
ROTATION(1);
|
||||
GIZMO_TYPE_TRANSLATION(0),
|
||||
GIZMO_TYPE_ROTATION(1);
|
||||
|
||||
final int value;
|
||||
const TGizmoType(this.value);
|
||||
|
||||
static TGizmoType fromValue(int value) => switch (value) {
|
||||
0 => TRANSLATION,
|
||||
1 => ROTATION,
|
||||
0 => GIZMO_TYPE_TRANSLATION,
|
||||
1 => GIZMO_TYPE_ROTATION,
|
||||
_ => throw ArgumentError("Unknown value for TGizmoType: $value"),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -90,9 +90,8 @@ class ThermionViewerFFI extends ThermionViewer {
|
||||
View_setAntiAliasing(view.view, false, false, false);
|
||||
View_setDitheringEnabled(view.view, false);
|
||||
View_setRenderQuality(view.view, TQualityLevel.MEDIUM);
|
||||
|
||||
|
||||
await FilamentApp.instance!.setClearColor(1.0, 0.0, 0.0, 1.0);
|
||||
|
||||
await FilamentApp.instance!.setClearOptions(0.0, 0.0, 0.0, 0.0);
|
||||
scene = FFIScene(Engine_createScene(app.engine));
|
||||
await view.setScene(scene);
|
||||
final camera = FFICamera(
|
||||
@@ -431,8 +430,6 @@ class ThermionViewerFFI extends ThermionViewer {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
///
|
||||
///
|
||||
///
|
||||
@@ -602,10 +599,15 @@ class ThermionViewerFFI extends ThermionViewer {
|
||||
///
|
||||
///
|
||||
///
|
||||
Future showGridOverlay() async {
|
||||
Future setGridOverlayVisibility(bool visible) async {
|
||||
_grid ??= _grid = await GridOverlay.create(app, animationManager);
|
||||
await scene.add(_grid!);
|
||||
await view.setLayerVisibility(VisibilityLayers.OVERLAY, true);
|
||||
if (visible) {
|
||||
await scene.add(_grid!);
|
||||
await view.setLayerVisibility(VisibilityLayers.OVERLAY, true);
|
||||
} else {
|
||||
await scene.remove(_grid!);
|
||||
await view.setLayerVisibility(VisibilityLayers.OVERLAY, true);
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
@@ -720,36 +722,18 @@ class ThermionViewerFFI extends ThermionViewer {
|
||||
return asset;
|
||||
}
|
||||
|
||||
////
|
||||
final _gizmos = <GizmoType, GizmoAsset>{};
|
||||
|
||||
///
|
||||
///
|
||||
///
|
||||
//
|
||||
@override
|
||||
Future<GizmoAsset> createGizmo(FFIView view, GizmoType gizmoType) async {
|
||||
throw UnimplementedError();
|
||||
// var scene = View_getScene(view.view);
|
||||
// final gizmo = await withPointerCallback<TGizmo>((cb) {
|
||||
// SceneManager_createGizmoRenderThread(_sceneManager!, view.view, scene,
|
||||
// TGizmoType.values[gizmoType.index], cb);
|
||||
// });
|
||||
// if (gizmo == nullptr) {
|
||||
// throw Exception("Failed to create gizmo");
|
||||
// }
|
||||
|
||||
// final gizmoEntityCount =
|
||||
// SceneAsset_getChildEntityCount(gizmo.cast<TSceneAsset>());
|
||||
// final gizmoEntities = Int32List(gizmoEntityCount);
|
||||
// SceneAsset_getChildEntities(
|
||||
// gizmo.cast<TSceneAsset>(), gizmoEntities.address);
|
||||
|
||||
// return FFIGizmo(
|
||||
// view,
|
||||
// gizmo.cast<TSceneAsset>(),
|
||||
// _sceneManager!,
|
||||
// app.engine!,
|
||||
// nullptr,
|
||||
// this,
|
||||
// gizmoEntities.toSet()
|
||||
// ..add(SceneAsset_getEntity(gizmo.cast<TSceneAsset>())));
|
||||
Future<GizmoAsset> getGizmo(GizmoType gizmoType) async {
|
||||
if (_gizmos[gizmoType] == null) {
|
||||
_gizmos[gizmoType] =
|
||||
await FilamentApp.instance!.createGizmo(view, animationManager, gizmoType);
|
||||
}
|
||||
return _gizmos[gizmoType]!;
|
||||
}
|
||||
|
||||
Future addToScene(covariant FFIAsset asset) async {
|
||||
|
||||
@@ -239,9 +239,10 @@ abstract class ThermionViewer {
|
||||
{List<MaterialInstance>? materialInstances, bool keepData = false, bool addToScene=true});
|
||||
|
||||
///
|
||||
/// The gizmo for translating/rotating objects. Only one gizmo can be active for a given view.
|
||||
/// Returns a gizmo for translating/rotating objects.
|
||||
/// Only one gizmo can be visible at any given time for this viewer.
|
||||
///
|
||||
Future<GizmoAsset> createGizmo(covariant View view, GizmoType type);
|
||||
Future<GizmoAsset> getGizmo(GizmoType type);
|
||||
|
||||
///
|
||||
/// Register a callback to be invoked when this viewer is disposed.
|
||||
@@ -261,12 +262,7 @@ abstract class ThermionViewer {
|
||||
///
|
||||
///
|
||||
///
|
||||
Future showGridOverlay();
|
||||
|
||||
///
|
||||
///
|
||||
///
|
||||
Future removeGridOverlay();
|
||||
Future setGridOverlayVisibility(bool visible);
|
||||
|
||||
///
|
||||
///
|
||||
@@ -293,6 +289,14 @@ abstract class ThermionViewer {
|
||||
///
|
||||
int getCameraCount();
|
||||
|
||||
///
|
||||
/// Adds the asset to the scene, meaning the asset will be rendered/visible.
|
||||
///
|
||||
Future addToScene(covariant ThermionAsset asset);
|
||||
|
||||
///
|
||||
/// Removes the asset from the scene, meaning the asset will not be rendered/visible.
|
||||
/// The asset itself will remain valid.
|
||||
///
|
||||
Future removeFromScene(covariant ThermionAsset asset);
|
||||
}
|
||||
|
||||
@@ -36,6 +36,7 @@ set(MATERIAL_SOURCES
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/include/material/image.c"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/include/material/grid.c"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/include/material/unlit.c"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/include/material/unlit_fixed_size.c"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/include/material/gizmo.c"
|
||||
)
|
||||
|
||||
|
||||
@@ -153,8 +153,8 @@ extern "C"
|
||||
typedef struct Aabb3 Aabb3;
|
||||
|
||||
enum TGizmoType {
|
||||
TRANSLATION,
|
||||
ROTATION
|
||||
GIZMO_TYPE_TRANSLATION,
|
||||
GIZMO_TYPE_ROTATION
|
||||
};
|
||||
|
||||
enum TPrimitiveType {
|
||||
|
||||
@@ -16,7 +16,15 @@ enum TGizmoPickResultType { AxisX, AxisY, AxisZ, Parent, None };
|
||||
|
||||
typedef void (*GizmoPickCallback)(TGizmoPickResultType resultType, float x, float y, float z);
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE TGizmo *Gizmo_create(TEngine *tEngine, TView *tView, TGizmoType tGizmoType);
|
||||
EMSCRIPTEN_KEEPALIVE TGizmo *Gizmo_create(
|
||||
TEngine *tEngine,
|
||||
TGltfAssetLoader *assetLoader,
|
||||
TGltfResourceLoader *tGltfResourceLoader,
|
||||
TNameComponentManager *tNameComponentManager,
|
||||
TView *tView,
|
||||
TMaterial *tMaterial,
|
||||
TGizmoType tGizmoType
|
||||
);
|
||||
EMSCRIPTEN_KEEPALIVE void Gizmo_pick(TGizmo *tGizmo, uint32_t x, uint32_t y, GizmoPickCallback callback);
|
||||
EMSCRIPTEN_KEEPALIVE void Gizmo_highlight(TGizmo *tGizmo, TGizmoAxis axis);
|
||||
EMSCRIPTEN_KEEPALIVE void Gizmo_unhighlight(TGizmo *tGizmo);
|
||||
|
||||
@@ -13,7 +13,7 @@ EMSCRIPTEN_KEEPALIVE TGltfAssetLoader *GltfAssetLoader_create(TEngine *tEngine,
|
||||
EMSCRIPTEN_KEEPALIVE TFilamentAsset *GltfAssetLoader_load(
|
||||
TEngine *tEngine,
|
||||
TGltfAssetLoader *tAssetLoader,
|
||||
uint8_t *data,
|
||||
const uint8_t *data,
|
||||
size_t length,
|
||||
uint8_t numInstances
|
||||
);
|
||||
|
||||
@@ -70,6 +70,7 @@ extern "C"
|
||||
EMSCRIPTEN_KEEPALIVE TMaterialInstance *Material_createInstance(TMaterial *tMaterial);
|
||||
EMSCRIPTEN_KEEPALIVE TMaterial *Material_createImageMaterial(TEngine *tEngine);
|
||||
EMSCRIPTEN_KEEPALIVE TMaterial *Material_createGridMaterial(TEngine *tEngine);
|
||||
EMSCRIPTEN_KEEPALIVE TMaterial *Material_createGizmoMaterial(TEngine *tEngine);
|
||||
EMSCRIPTEN_KEEPALIVE bool Material_hasParameter(TMaterial *tMaterial, const char *propertyName);
|
||||
EMSCRIPTEN_KEEPALIVE bool MaterialInstance_isStencilWriteEnabled(TMaterialInstance *materialInstance);
|
||||
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setStencilWrite(TMaterialInstance *materialInstance, bool enabled);
|
||||
|
||||
@@ -27,7 +27,7 @@ extern "C"
|
||||
TEngine *tEngine,
|
||||
TGltfAssetLoader *tAssetLoader,
|
||||
TNameComponentManager *tNameComponentManager,
|
||||
uint8_t *data,
|
||||
const uint8_t *data,
|
||||
size_t length,
|
||||
size_t numInstances
|
||||
);
|
||||
@@ -37,7 +37,7 @@ extern "C"
|
||||
TGltfResourceLoader *tResourceLoader,
|
||||
TEngine *tEngine,
|
||||
TNameComponentManager *tNameComponentManager,
|
||||
uint8_t *data,
|
||||
const uint8_t *data,
|
||||
size_t length,
|
||||
size_t numInstances
|
||||
);
|
||||
|
||||
@@ -81,6 +81,7 @@ namespace thermion
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE void Material_createInstanceRenderThread(TMaterial *tMaterial, void (*onComplete)(TMaterialInstance *));
|
||||
EMSCRIPTEN_KEEPALIVE void Material_createImageMaterialRenderThread(TEngine *tEngine, void (*onComplete)(TMaterial *));
|
||||
EMSCRIPTEN_KEEPALIVE void Material_createGizmoMaterialRenderThread(TEngine *tEngine, void (*onComplete)(TMaterial *));
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE void ColorGrading_createRenderThread(TEngine *tEngine, TToneMapping toneMapping, void (*callback)(TColorGrading *));
|
||||
EMSCRIPTEN_KEEPALIVE void View_setColorGradingRenderThread(TView *tView, TColorGrading *tColorGrading, void (*callback)());
|
||||
@@ -116,7 +117,6 @@ namespace thermion
|
||||
void (*callback)(TSceneAsset *)
|
||||
);
|
||||
EMSCRIPTEN_KEEPALIVE void MaterialProvider_createMaterialInstanceRenderThread(TMaterialProvider *tMaterialProvider, TMaterialKey *tKey, void (*callback)(TMaterialInstance *));
|
||||
EMSCRIPTEN_KEEPALIVE void SceneManager_destroyMaterialInstanceRenderThread(TSceneManager *tSceneManager, TMaterialInstance *tMaterialInstance, void (*callback)());
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE void AnimationManager_updateBoneMatricesRenderThread(
|
||||
TAnimationManager *tAnimationManager,
|
||||
@@ -269,6 +269,16 @@ namespace thermion
|
||||
void (*callback)(TFilamentAsset *)
|
||||
);
|
||||
EMSCRIPTEN_KEEPALIVE void Scene_addFilamentAssetRenderThread(TScene* tScene, TFilamentAsset *tAsset, void (*callback)());
|
||||
EMSCRIPTEN_KEEPALIVE void Gizmo_createRenderThread(
|
||||
TEngine *tEngine,
|
||||
TGltfAssetLoader *tAssetLoader,
|
||||
TGltfResourceLoader *tGltfResourceLoader,
|
||||
TNameComponentManager *tNameComponentManager,
|
||||
TView *tView,
|
||||
TMaterial *tMaterial,
|
||||
TGizmoType tGizmoType,
|
||||
void (*callback)(TGizmo *)
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -37,7 +37,6 @@ namespace thermion
|
||||
SceneAsset *sceneAsset,
|
||||
Engine *engine,
|
||||
View *view,
|
||||
Scene *scene,
|
||||
Material *material) noexcept;
|
||||
|
||||
Gizmo(Gizmo &&other) noexcept;
|
||||
|
||||
@@ -137,7 +137,7 @@ namespace thermion
|
||||
Entity findEntityByName(const char* name) override {
|
||||
TRACE("Searching for entity with name %s", name);
|
||||
Entity entities[1];
|
||||
auto found = _asset->getEntitiesByName(name, entities, 1);
|
||||
auto found = _asset->getEntitiesByName(name, entities, 1);
|
||||
return entities[0];
|
||||
}
|
||||
|
||||
|
||||
@@ -117,6 +117,9 @@ namespace thermion
|
||||
for(int i = 0; i < getChildEntityCount(); i++) {
|
||||
auto entity = getChildEntities()[i];
|
||||
auto nameInstance = _ncm->getInstance(entity);
|
||||
if(!nameInstance.isValid()) {
|
||||
continue;
|
||||
}
|
||||
auto entityName = _ncm->getName(nameInstance);
|
||||
|
||||
if (strcmp(entityName, name) == 0) {
|
||||
|
||||
@@ -3,7 +3,11 @@
|
||||
#include <filament/Scene.h>
|
||||
|
||||
#include "c_api/TGizmo.h"
|
||||
#include "c_api/TSceneAsset.h"
|
||||
#include "scene/Gizmo.hpp"
|
||||
#include "scene/GltfSceneAsset.hpp"
|
||||
#include "resources/translation_gizmo_glb.h"
|
||||
#include "resources/rotation_gizmo_glb.h"
|
||||
#include "Log.hpp"
|
||||
|
||||
#ifdef __cplusplus
|
||||
@@ -14,6 +18,61 @@ namespace thermion
|
||||
using namespace filament;
|
||||
#endif
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE TGizmo *Gizmo_create(
|
||||
TEngine *tEngine,
|
||||
TGltfAssetLoader *assetLoader,
|
||||
TGltfResourceLoader *tGltfResourceLoader,
|
||||
TNameComponentManager *tNameComponentManager,
|
||||
TView *tView,
|
||||
TMaterial *tMaterial,
|
||||
TGizmoType tGizmoType) {
|
||||
|
||||
auto *engine = reinterpret_cast<Engine *>(tEngine);
|
||||
auto *view = reinterpret_cast<View *>(tView);
|
||||
auto *material = reinterpret_cast<Material *>(tMaterial);
|
||||
auto *gltfResourceLoader = reinterpret_cast<gltfio::ResourceLoader *>(tGltfResourceLoader);
|
||||
TSceneAsset *sceneAsset;
|
||||
switch (tGizmoType)
|
||||
{
|
||||
case GIZMO_TYPE_TRANSLATION:
|
||||
{
|
||||
TRACE("Building translation gizmo");
|
||||
sceneAsset = SceneAsset_loadGlb(
|
||||
tEngine,
|
||||
assetLoader,
|
||||
tNameComponentManager,
|
||||
TRANSLATION_GIZMO_GLB_TRANSLATION_GIZMO_DATA,
|
||||
TRANSLATION_GIZMO_GLB_TRANSLATION_GIZMO_SIZE,
|
||||
3
|
||||
);
|
||||
break;
|
||||
}
|
||||
case GIZMO_TYPE_ROTATION:
|
||||
{
|
||||
TRACE("Building rotation gizmo");
|
||||
sceneAsset = SceneAsset_loadGlb(
|
||||
tEngine,
|
||||
assetLoader,
|
||||
tNameComponentManager,
|
||||
ROTATION_GIZMO_GLB_ROTATION_GIZMO_DATA,
|
||||
ROTATION_GIZMO_GLB_ROTATION_GIZMO_SIZE,
|
||||
3
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
auto *gltfSceneAsset = reinterpret_cast<GltfSceneAsset *>(sceneAsset);
|
||||
auto *filamentAsset = gltfSceneAsset->getAsset();
|
||||
gltfResourceLoader->loadResources(filamentAsset);
|
||||
auto *gizmo = new Gizmo(
|
||||
gltfSceneAsset,
|
||||
engine,
|
||||
view,
|
||||
material
|
||||
);
|
||||
return reinterpret_cast<TGizmo *>(gizmo);
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE void Gizmo_pick(TGizmo *tGizmo, uint32_t x, uint32_t y, GizmoPickCallback callback)
|
||||
{
|
||||
auto *gizmo = reinterpret_cast<Gizmo *>(tGizmo);
|
||||
|
||||
@@ -56,7 +56,7 @@ EMSCRIPTEN_KEEPALIVE TGltfAssetLoader *GltfAssetLoader_create(TEngine *tEngine,
|
||||
EMSCRIPTEN_KEEPALIVE TFilamentAsset *GltfAssetLoader_load(
|
||||
TEngine *tEngine,
|
||||
TGltfAssetLoader *tAssetLoader,
|
||||
uint8_t *data,
|
||||
const uint8_t *data,
|
||||
size_t length,
|
||||
uint8_t numInstances)
|
||||
{
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "MathUtils.hpp"
|
||||
#include "material/image.h"
|
||||
#include "material/grid.h"
|
||||
#include "material/unlit_fixed_size.h"
|
||||
|
||||
#include "c_api/TMaterialInstance.h"
|
||||
|
||||
@@ -45,7 +46,13 @@ namespace thermion
|
||||
return reinterpret_cast<TMaterial *>(material);
|
||||
}
|
||||
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE TMaterial *Material_createGizmoMaterial(TEngine *tEngine) {
|
||||
auto *engine = reinterpret_cast<filament::Engine *>(tEngine);
|
||||
auto *material = filament::Material::Builder()
|
||||
.package(UNLIT_FIXED_SIZE_UNLIT_FIXED_SIZE_DATA, UNLIT_FIXED_SIZE_UNLIT_FIXED_SIZE_SIZE)
|
||||
.build(*engine);
|
||||
return reinterpret_cast<TMaterial *>(material);
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE bool Material_hasParameter(TMaterial *tMaterial, const char *propertyName) {
|
||||
auto *material = reinterpret_cast<filament::Material *>(tMaterial);
|
||||
|
||||
@@ -70,7 +70,7 @@ extern "C"
|
||||
TEngine *tEngine,
|
||||
TGltfAssetLoader *tAssetLoader,
|
||||
TNameComponentManager *tNameComponentManager,
|
||||
uint8_t *data,
|
||||
const uint8_t *data,
|
||||
size_t length,
|
||||
size_t numInstances
|
||||
) {
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include "c_api/APIBoundaryTypes.h"
|
||||
#include "c_api/TAnimationManager.h"
|
||||
#include "c_api/TEngine.h"
|
||||
#include "c_api/TGizmo.h"
|
||||
#include "c_api/TGltfAssetLoader.h"
|
||||
#include "c_api/TGltfResourceLoader.h"
|
||||
#include "c_api/TRenderer.h"
|
||||
@@ -380,6 +381,17 @@ extern "C"
|
||||
});
|
||||
auto fut = _renderThread->add_task(lambda);
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE void Material_createGizmoMaterialRenderThread(TEngine *tEngine, void (*onComplete)(TMaterial *)) {
|
||||
std::packaged_task<void()> lambda(
|
||||
[=]() mutable
|
||||
{
|
||||
auto *instance = Material_createGizmoMaterial(tEngine);
|
||||
onComplete(instance);
|
||||
});
|
||||
auto fut = _renderThread->add_task(lambda);
|
||||
}
|
||||
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE void Material_createInstanceRenderThread(TMaterial *tMaterial, void (*onComplete)(TMaterialInstance *))
|
||||
{
|
||||
@@ -1010,4 +1022,23 @@ extern "C"
|
||||
});
|
||||
auto fut = _renderThread->add_task(lambda);
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE void Gizmo_createRenderThread(
|
||||
TEngine *tEngine,
|
||||
TGltfAssetLoader *tAssetLoader,
|
||||
TGltfResourceLoader *tGltfResourceLoader,
|
||||
TNameComponentManager *tNameComponentManager,
|
||||
TView *tView,
|
||||
TMaterial *tMaterial,
|
||||
TGizmoType tGizmoType,
|
||||
void (*callback)(TGizmo *)
|
||||
) {
|
||||
std::packaged_task<void()> lambda(
|
||||
[=]() mutable
|
||||
{
|
||||
auto *gizmo = Gizmo_create(tEngine, tAssetLoader,tGltfResourceLoader, tNameComponentManager, tView, tMaterial, tGizmoType);
|
||||
callback(gizmo);
|
||||
});
|
||||
auto fut = _renderThread->add_task(lambda);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user