refactoring

This commit is contained in:
Nick Fisher
2025-03-22 10:49:24 +08:00
parent a67f42f0de
commit 0cbbc058e0
22 changed files with 675 additions and 463 deletions

View File

@@ -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<T> createColorGrading(ToneMapper mapper);
///
///
///
Future<GizmoAsset> createGizmo(covariant View view, T animationManager, GizmoType type);
} }

View File

@@ -1,371 +1,371 @@
import 'dart:async'; // import 'dart:async';
import 'dart:math'; // import 'dart:math';
import 'package:thermion_dart/thermion_dart.dart'; // import 'package:thermion_dart/thermion_dart.dart';
class _Gizmo { // class _Gizmo {
final ThermionViewer viewer; // final ThermionViewer viewer;
final GizmoAsset _gizmo; // final GizmoAsset _gizmo;
final transformUpdates = StreamController<({Matrix4 transform})>.broadcast(); // final transformUpdates = StreamController<({Matrix4 transform})>.broadcast();
Axis? _active; // Axis? _active;
final GizmoType type; // final GizmoType type;
_Gizmo(this._gizmo, this.viewer, this.type); // _Gizmo(this._gizmo, this.viewer, this.type);
static Future<_Gizmo> forType(ThermionViewer viewer, GizmoType type) async { // static Future<_Gizmo> forType(ThermionViewer viewer, GizmoType type) async {
final view = await viewer.view; // final view = await viewer.view;
return _Gizmo(await viewer.createGizmo(view, type), viewer, type); // return _Gizmo(await viewer.createGizmo(view, type), viewer, type);
} // }
Future dispose() async { // Future dispose() async {
await transformUpdates.close(); // await transformUpdates.close();
await viewer.destroyAsset(_gizmo); // await viewer.destroyAsset(_gizmo);
} // }
Future hide() async { // Future hide() async {
final scene = await viewer.view.getScene(); // final scene = await viewer.view.getScene();
await scene.remove(_gizmo); // await scene.remove(_gizmo);
} // }
Future reveal() async { // Future reveal() async {
final scene = await viewer.view.getScene(); // final scene = await viewer.view.getScene();
await scene.add(_gizmo); // await scene.add(_gizmo);
gizmoTransform = await _gizmo.getWorldTransform(); // gizmoTransform = await _gizmo.getWorldTransform();
} // }
double _getAngleBetweenVectors(Vector2 v1, Vector2 v2) { // double _getAngleBetweenVectors(Vector2 v1, Vector2 v2) {
// Normalize vectors to ensure consistent rotation regardless of distance from center // // Normalize vectors to ensure consistent rotation regardless of distance from center
v1.normalize(); // v1.normalize();
v2.normalize(); // v2.normalize();
// Calculate angle using atan2 // // Calculate angle using atan2
double angle = atan2(v2.y, v2.x) - atan2(v1.y, v1.x); // double angle = atan2(v2.y, v2.x) - atan2(v1.y, v1.x);
// Ensure angle is between -π and π // // Ensure angle is between -π and π
if (angle > pi) angle -= 2 * pi; // if (angle > pi) angle -= 2 * pi;
if (angle < -pi) angle += 2 * pi; // if (angle < -pi) angle += 2 * pi;
return angle; // return angle;
} // }
void checkHover(int x, int y) async { // void checkHover(int x, int y) async {
_gizmo.pick(x, y, handler: (result, coords) async { // _gizmo.pick(x, y, handler: (result, coords) async {
switch (result) { // switch (result) {
case GizmoPickResultType.None: // case GizmoPickResultType.None:
await _gizmo.unhighlight(); // await _gizmo.unhighlight();
_active = null; // _active = null;
break; // break;
case GizmoPickResultType.AxisX: // case GizmoPickResultType.AxisX:
_active = Axis.X; // _active = Axis.X;
case GizmoPickResultType.AxisY: // case GizmoPickResultType.AxisY:
_active = Axis.Y; // _active = Axis.Y;
case GizmoPickResultType.AxisZ: // case GizmoPickResultType.AxisZ:
_active = Axis.Z; // _active = Axis.Z;
default: // default:
} // }
}); // });
} // }
Matrix4? gizmoTransform; // Matrix4? gizmoTransform;
void _updateTransform(Vector2 currentPosition, Vector2 delta) async { // void _updateTransform(Vector2 currentPosition, Vector2 delta) async {
if (type == GizmoType.translation) { // if (type == GizmoType.translation) {
await _updateTranslation(currentPosition, delta); // await _updateTranslation(currentPosition, delta);
} else if (type == GizmoType.rotation) { // } else if (type == GizmoType.rotation) {
await _updateRotation(currentPosition, delta); // await _updateRotation(currentPosition, delta);
} // }
await _gizmo.setTransform(gizmoTransform!); // await _gizmo.setTransform(gizmoTransform!);
transformUpdates.add((transform: gizmoTransform!)); // transformUpdates.add((transform: gizmoTransform!));
} // }
Future<void>? _updateTranslation( // Future<void>? _updateTranslation(
Vector2 currentPosition, Vector2 delta) async { // Vector2 currentPosition, Vector2 delta) async {
var view = await viewer.view; // var view = await viewer.view;
var camera = await viewer.getActiveCamera(); // var camera = await viewer.getActiveCamera();
var viewport = await view.getViewport(); // var viewport = await view.getViewport();
var projectionMatrix = await camera.getProjectionMatrix(); // var projectionMatrix = await camera.getProjectionMatrix();
var viewMatrix = await camera.getViewMatrix(); // var viewMatrix = await camera.getViewMatrix();
var inverseViewMatrix = await camera.getModelMatrix(); // var inverseViewMatrix = await camera.getModelMatrix();
var inverseProjectionMatrix = projectionMatrix.clone()..invert(); // var inverseProjectionMatrix = projectionMatrix.clone()..invert();
// get gizmo position in screenspace // // get gizmo position in screenspace
var gizmoPositionWorldSpace = gizmoTransform!.getTranslation(); // var gizmoPositionWorldSpace = gizmoTransform!.getTranslation();
Vector4 gizmoClipSpace = projectionMatrix * // Vector4 gizmoClipSpace = projectionMatrix *
viewMatrix * // viewMatrix *
Vector4(gizmoPositionWorldSpace.x, gizmoPositionWorldSpace.y, // Vector4(gizmoPositionWorldSpace.x, gizmoPositionWorldSpace.y,
gizmoPositionWorldSpace.z, 1.0); // gizmoPositionWorldSpace.z, 1.0);
var gizmoNdc = gizmoClipSpace / gizmoClipSpace.w; // var gizmoNdc = gizmoClipSpace / gizmoClipSpace.w;
var gizmoScreenSpace = Vector2(((gizmoNdc.x / 2) + 0.5) * viewport.width, // var gizmoScreenSpace = Vector2(((gizmoNdc.x / 2) + 0.5) * viewport.width,
viewport.height - (((gizmoNdc.y / 2) + 0.5) * viewport.height)); // viewport.height - (((gizmoNdc.y / 2) + 0.5) * viewport.height));
gizmoScreenSpace += delta; // gizmoScreenSpace += delta;
gizmoNdc = Vector4(((gizmoScreenSpace.x / viewport.width) - 0.5) * 2, // gizmoNdc = Vector4(((gizmoScreenSpace.x / viewport.width) - 0.5) * 2,
(((gizmoScreenSpace.y / viewport.height)) - 0.5) * -2, gizmoNdc.z, 1.0); // (((gizmoScreenSpace.y / viewport.height)) - 0.5) * -2, gizmoNdc.z, 1.0);
var gizmoViewSpace = inverseProjectionMatrix * gizmoNdc; // var gizmoViewSpace = inverseProjectionMatrix * gizmoNdc;
gizmoViewSpace /= gizmoViewSpace.w; // gizmoViewSpace /= gizmoViewSpace.w;
var newPosition = (inverseViewMatrix * gizmoViewSpace).xyz; // var newPosition = (inverseViewMatrix * gizmoViewSpace).xyz;
Vector3 worldSpaceDelta = newPosition - gizmoTransform!.getTranslation(); // Vector3 worldSpaceDelta = newPosition - gizmoTransform!.getTranslation();
worldSpaceDelta.multiply(_active!.asVector()); // worldSpaceDelta.multiply(_active!.asVector());
gizmoTransform! // gizmoTransform!
.setTranslation(gizmoTransform!.getTranslation() + worldSpaceDelta); // .setTranslation(gizmoTransform!.getTranslation() + worldSpaceDelta);
} // }
Future<void>? _updateRotation(Vector2 currentPosition, Vector2 delta) async { // Future<void>? _updateRotation(Vector2 currentPosition, Vector2 delta) async {
var camera = await viewer.view.getCamera(); // var camera = await viewer.view.getCamera();
var viewport = await viewer.view.getViewport(); // var viewport = await viewer.view.getViewport();
var projectionMatrix = await camera.getProjectionMatrix(); // var projectionMatrix = await camera.getProjectionMatrix();
var viewMatrix = await camera.getViewMatrix(); // var viewMatrix = await camera.getViewMatrix();
// Get gizmo center in screen space // // Get gizmo center in screen space
var gizmoPositionWorldSpace = gizmoTransform!.getTranslation(); // var gizmoPositionWorldSpace = gizmoTransform!.getTranslation();
Vector4 gizmoClipSpace = projectionMatrix * // Vector4 gizmoClipSpace = projectionMatrix *
viewMatrix * // viewMatrix *
Vector4(gizmoPositionWorldSpace.x, gizmoPositionWorldSpace.y, // Vector4(gizmoPositionWorldSpace.x, gizmoPositionWorldSpace.y,
gizmoPositionWorldSpace.z, 1.0); // gizmoPositionWorldSpace.z, 1.0);
var gizmoNdc = gizmoClipSpace / gizmoClipSpace.w; // var gizmoNdc = gizmoClipSpace / gizmoClipSpace.w;
var gizmoScreenSpace = Vector2(((gizmoNdc.x / 2) + 0.5) * viewport.width, // var gizmoScreenSpace = Vector2(((gizmoNdc.x / 2) + 0.5) * viewport.width,
viewport.height - (((gizmoNdc.y / 2) + 0.5) * viewport.height)); // viewport.height - (((gizmoNdc.y / 2) + 0.5) * viewport.height));
// Calculate vectors from gizmo center to previous and current mouse positions // // Calculate vectors from gizmo center to previous and current mouse positions
var prevVector = (currentPosition - delta) - gizmoScreenSpace; // var prevVector = (currentPosition - delta) - gizmoScreenSpace;
var currentVector = currentPosition - gizmoScreenSpace; // var currentVector = currentPosition - gizmoScreenSpace;
// Calculate rotation angle based on the active axis // // Calculate rotation angle based on the active axis
double rotationAngle = 0.0; // double rotationAngle = 0.0;
switch (_active) { // switch (_active) {
case Axis.X: // case Axis.X:
// For X axis, project onto YZ plane // // For X axis, project onto YZ plane
var prev = Vector2(prevVector.y, -prevVector.x); // var prev = Vector2(prevVector.y, -prevVector.x);
var curr = Vector2(currentVector.y, -currentVector.x); // var curr = Vector2(currentVector.y, -currentVector.x);
rotationAngle = _getAngleBetweenVectors(prev, curr); // rotationAngle = _getAngleBetweenVectors(prev, curr);
break; // break;
case Axis.Y: // case Axis.Y:
// For Y axis, project onto XZ plane // // For Y axis, project onto XZ plane
var prev = Vector2(prevVector.x, -prevVector.y); // var prev = Vector2(prevVector.x, -prevVector.y);
var curr = Vector2(currentVector.x, -currentVector.y); // var curr = Vector2(currentVector.x, -currentVector.y);
rotationAngle = _getAngleBetweenVectors(prev, curr); // rotationAngle = _getAngleBetweenVectors(prev, curr);
break; // break;
case Axis.Z: // case Axis.Z:
// For Z axis, use screen plane directly // // For Z axis, use screen plane directly
rotationAngle = -1 * _getAngleBetweenVectors(prevVector, currentVector); // rotationAngle = -1 * _getAngleBetweenVectors(prevVector, currentVector);
break; // break;
default: // default:
return; // return;
} // }
// Create rotation matrix based on the active axis // // Create rotation matrix based on the active axis
var rotationMatrix = Matrix4.identity(); // var rotationMatrix = Matrix4.identity();
switch (_active) { // switch (_active) {
case Axis.X: // case Axis.X:
rotationMatrix.setRotationX(rotationAngle); // rotationMatrix.setRotationX(rotationAngle);
break; // break;
case Axis.Y: // case Axis.Y:
rotationMatrix.setRotationY(rotationAngle); // rotationMatrix.setRotationY(rotationAngle);
break; // break;
case Axis.Z: // case Axis.Z:
rotationMatrix.setRotationZ(rotationAngle); // rotationMatrix.setRotationZ(rotationAngle);
break; // break;
default: // default:
return; // return;
} // }
// Apply rotation to the current transform // // Apply rotation to the current transform
gizmoTransform = gizmoTransform! * rotationMatrix; // gizmoTransform = gizmoTransform! * rotationMatrix;
} // }
} // }
class GizmoInputHandler extends InputHandler { // class GizmoInputHandler extends InputHandler {
final ThermionViewer viewer; // final ThermionViewer viewer;
late final _gizmos = <GizmoType, _Gizmo>{}; // late final _gizmos = <GizmoType, _Gizmo>{};
_Gizmo? _active; // _Gizmo? _active;
ThermionEntity? _attached; // ThermionEntity? _attached;
Future attach(ThermionEntity entity) async { // Future attach(ThermionEntity entity) async {
if (_attached != null) { // if (_attached != null) {
await detach(); // await detach();
} // }
_attached = entity; // _attached = entity;
if (_active != null) { // if (_active != null) {
await FilamentApp.instance!.setParent(_attached!, _active!._gizmo.entity); // await FilamentApp.instance!.setParent(_attached!, _active!._gizmo.entity);
await _active!.reveal(); // await _active!.reveal();
} // }
} // }
Future<Matrix4?> getGizmoTransform() async { // Future<Matrix4?> getGizmoTransform() async {
return _active?.gizmoTransform; // return _active?.gizmoTransform;
} // }
Future detach() async { // Future detach() async {
if (_attached == null) { // if (_attached == null) {
return; // return;
} // }
await FilamentApp.instance!.setParent(_attached!, null); // await FilamentApp.instance!.setParent(_attached!, null);
await _active?.hide(); // await _active?.hide();
_attached = null; // _attached = null;
} // }
final _initialized = Completer<bool>(); // final _initialized = Completer<bool>();
final _transformController = StreamController<Matrix4>.broadcast(); // final _transformController = StreamController<Matrix4>.broadcast();
Stream<Matrix4> get transformUpdated => _transformController.stream; // Stream<Matrix4> get transformUpdated => _transformController.stream;
final _pickResultController = StreamController<ThermionEntity?>.broadcast(); // final _pickResultController = StreamController<ThermionEntity?>.broadcast();
Stream<ThermionEntity?> get onPickResult => _pickResultController.stream; // Stream<ThermionEntity?> get onPickResult => _pickResultController.stream;
GizmoInputHandler({required this.viewer, required GizmoType initialType}) { // GizmoInputHandler({required this.viewer, required GizmoType initialType}) {
initialize().then((_) { // initialize().then((_) {
setGizmoType(initialType); // setGizmoType(initialType);
}); // });
} // }
GizmoType? getGizmoType() { // GizmoType? getGizmoType() {
return _active?.type; // return _active?.type;
} // }
Future setGizmoType(GizmoType? type) async { // Future setGizmoType(GizmoType? type) async {
if (type == null) { // if (type == null) {
await detach(); // await detach();
_active?.hide(); // _active?.hide();
_active = null; // _active = null;
} else { // } else {
_active?.hide(); // _active?.hide();
_active = _gizmos[type]!; // _active = _gizmos[type]!;
_active!.reveal(); // _active!.reveal();
if (_attached != null) { // if (_attached != null) {
await attach(_attached!); // await attach(_attached!);
} // }
} // }
} // }
Future initialize() async { // Future initialize() async {
if (_initialized.isCompleted) { // if (_initialized.isCompleted) {
throw Exception("Already initialized"); // throw Exception("Already initialized");
} // }
await viewer.initialized; // await viewer.initialized;
_gizmos[GizmoType.translation] = // _gizmos[GizmoType.translation] =
await _Gizmo.forType(viewer, GizmoType.translation); // await _Gizmo.forType(viewer, GizmoType.translation);
_gizmos[GizmoType.rotation] = // _gizmos[GizmoType.rotation] =
await _Gizmo.forType(viewer, GizmoType.rotation); // await _Gizmo.forType(viewer, GizmoType.rotation);
await setGizmoType(GizmoType.translation); // await setGizmoType(GizmoType.translation);
for (final gizmo in _gizmos.values) { // for (final gizmo in _gizmos.values) {
gizmo.transformUpdates.stream.listen((update) { // gizmo.transformUpdates.stream.listen((update) {
_transformController.add(update.transform); // _transformController.add(update.transform);
}); // });
} // }
_initialized.complete(true); // _initialized.complete(true);
} // }
@override // @override
Future dispose() async { // Future dispose() async {
_gizmos[GizmoType.rotation]!.dispose(); // _gizmos[GizmoType.rotation]!.dispose();
_gizmos[GizmoType.translation]!.dispose(); // _gizmos[GizmoType.translation]!.dispose();
_gizmos.clear(); // _gizmos.clear();
} // }
@override // @override
InputAction? getActionForType(InputType gestureType) { // InputAction? getActionForType(InputType gestureType) {
if (gestureType == InputType.LMB_DOWN) { // if (gestureType == InputType.LMB_DOWN) {
return InputAction.PICK; // return InputAction.PICK;
} // }
throw UnimplementedError(); // throw UnimplementedError();
} // }
@override // @override
Future<bool> get initialized => _initialized.future; // Future<bool> get initialized => _initialized.future;
@override // @override
void keyDown(PhysicalKey key) {} // void keyDown(PhysicalKey key) {}
@override // @override
void keyUp(PhysicalKey key) {} // void keyUp(PhysicalKey key) {}
@override // @override
Future<void>? onPointerDown(Vector2 localPosition, bool isMiddle) async { // Future<void>? onPointerDown(Vector2 localPosition, bool isMiddle) async {
if (!_initialized.isCompleted) { // if (!_initialized.isCompleted) {
return; // return;
} // }
if (isMiddle) { // if (isMiddle) {
return; // return;
} // }
await viewer.pick(localPosition.x.toInt(), localPosition.y.toInt(), // await viewer.pick(localPosition.x.toInt(), localPosition.y.toInt(),
(result) async { // (result) async {
if (_active?._gizmo.isNonPickable(result.entity) == true || // if (_active?._gizmo.isNonPickable(result.entity) == true ||
result.entity == FILAMENT_ENTITY_NULL) { // result.entity == FILAMENT_ENTITY_NULL) {
_pickResultController.add(null); // _pickResultController.add(null);
return; // return;
} // }
if (_active?._gizmo.isGizmoEntity(result.entity) != true) { // if (_active?._gizmo.isGizmoEntity(result.entity) != true) {
_pickResultController.add(result.entity); // _pickResultController.add(result.entity);
} // }
}); // });
} // }
@override // @override
Future<void>? onPointerHover(Vector2 localPosition, Vector2 delta) async { // Future<void>? onPointerHover(Vector2 localPosition, Vector2 delta) async {
if (!_initialized.isCompleted) { // if (!_initialized.isCompleted) {
return; // return;
} // }
_active?.checkHover(localPosition.x.floor(), localPosition.y.floor()); // _active?.checkHover(localPosition.x.floor(), localPosition.y.floor());
} // }
@override // @override
Future<void>? onPointerMove( // Future<void>? onPointerMove(
Vector2 localPosition, Vector2 delta, bool isMiddle) async { // Vector2 localPosition, Vector2 delta, bool isMiddle) async {
if (!isMiddle && _active?._active != null) { // if (!isMiddle && _active?._active != null) {
final scaledDelta = Vector2( // final scaledDelta = Vector2(
delta.x, // delta.x,
delta.y, // delta.y,
); // );
_active!._updateTransform(localPosition, scaledDelta); // _active!._updateTransform(localPosition, scaledDelta);
return; // return;
} // }
} // }
@override // @override
Future<void>? onPointerScroll( // Future<void>? onPointerScroll(
Vector2 localPosition, double scrollDelta) async {} // Vector2 localPosition, double scrollDelta) async {}
@override // @override
Future<void>? onPointerUp(bool isMiddle) async {} // Future<void>? onPointerUp(bool isMiddle) async {}
@override // @override
Future<void>? onScaleEnd(int pointerCount, double velocity) {} // Future<void>? onScaleEnd(int pointerCount, double velocity) {}
@override // @override
Future<void>? onScaleStart( // Future<void>? onScaleStart(
Vector2 focalPoint, int pointerCount, Duration? sourceTimestamp) {} // Vector2 focalPoint, int pointerCount, Duration? sourceTimestamp) {}
@override // @override
Future<void>? onScaleUpdate( // Future<void>? onScaleUpdate(
Vector2 focalPoint, // Vector2 focalPoint,
Vector2 focalPointDelta, // Vector2 focalPointDelta,
double horizontalScale, // double horizontalScale,
double verticalScale, // double verticalScale,
double scale, // double scale,
int pointerCount, // int pointerCount,
double rotation, // double rotation,
Duration? sourceTimestamp) {} // Duration? sourceTimestamp) {}
@override // @override
void setActionForType(InputType gestureType, InputAction gestureAction) { // void setActionForType(InputType gestureType, InputAction gestureAction) {
throw UnimplementedError(); // throw UnimplementedError();
} // }
} // }

View File

@@ -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/filament/src/scene.dart';
import 'package:thermion_dart/src/viewer/src/ffi/src/callbacks.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_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_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_render_target.dart';
import 'package:thermion_dart/src/viewer/src/ffi/src/ffi_scene.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); .firstWhere((x) => _swapChains[x]?.contains(view) == true);
final out = Uint8List(viewport.width * viewport.height * 4); 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) { await withBoolCallback((cb) {
Renderer_beginFrameRenderThread(renderer, swapChain.swapChain, 0, cb); Renderer_beginFrameRenderThread(renderer, swapChain.swapChain, 0, cb);
}); });
@@ -647,20 +640,18 @@ class FFIFilamentApp extends FilamentApp<Pointer> {
}); });
await withVoidCallback((cb) { await withVoidCallback((cb) {
Engine_destroyFenceRenderThread(engine, fence, cb); Engine_flushAndWaitRenderThead(engine, cb);
}); });
// await withVoidCallback((cb) {
// Engine_flushAndWaitRenderThead(engine, cb);
// });
return out; return out;
} }
/// ///
/// ///
/// ///
Future setClearColor(double r, double g, double b, double a) async { Future setClearOptions(double r, double g, double b, double a,
Renderer_setClearOptions(renderer, r, g, b, a, 0, true, false); {int clearStencil = 0, bool discard = false, bool clear = true}) async {
Renderer_setClearOptions(
renderer, r, g, b, a, clearStencil, clear, discard);
} }
/// ///
@@ -751,6 +742,43 @@ class FFIFilamentApp extends FilamentApp<Pointer> {
ColorGrading_createRenderThread( ColorGrading_createRenderThread(
engine, TToneMapping.values[mapper.index], cb)); 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 { class FinalizableUint8List implements Finalizable {

View File

@@ -35,6 +35,11 @@ external ffi.Pointer<TMaterial> Material_createGridMaterial(
ffi.Pointer<TEngine> tEngine, 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>)>( @ffi.Native<ffi.Bool Function(ffi.Pointer<TMaterial>, ffi.Pointer<ffi.Char>)>(
isLeaf: true) isLeaf: true)
external bool Material_hasParameter( external bool Material_hasParameter(
@@ -1057,22 +1062,40 @@ external void TextureSampler_destroy(
); );
@ffi.Native< @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) ffi.UnsignedInt)>(symbol: "Gizmo_create", isLeaf: true)
external ffi.Pointer<TGizmo> _Gizmo_create( external ffi.Pointer<TGizmo> _Gizmo_create(
ffi.Pointer<TEngine> tEngine, ffi.Pointer<TEngine> tEngine,
ffi.Pointer<TGltfAssetLoader> assetLoader,
ffi.Pointer<TGltfResourceLoader> tGltfResourceLoader,
ffi.Pointer<TNameComponentManager> tNameComponentManager,
ffi.Pointer<TView> tView, ffi.Pointer<TView> tView,
ffi.Pointer<TMaterial> tMaterial,
int tGizmoType, int tGizmoType,
); );
ffi.Pointer<TGizmo> Gizmo_create( ffi.Pointer<TGizmo> Gizmo_create(
ffi.Pointer<TEngine> tEngine, ffi.Pointer<TEngine> tEngine,
ffi.Pointer<TGltfAssetLoader> assetLoader,
ffi.Pointer<TGltfResourceLoader> tGltfResourceLoader,
ffi.Pointer<TNameComponentManager> tNameComponentManager,
ffi.Pointer<TView> tView, ffi.Pointer<TView> tView,
ffi.Pointer<TMaterial> tMaterial,
TGizmoType tGizmoType, TGizmoType tGizmoType,
) => ) =>
_Gizmo_create( _Gizmo_create(
tEngine, tEngine,
assetLoader,
tGltfResourceLoader,
tNameComponentManager,
tView, tView,
tMaterial,
tGizmoType.value, tGizmoType.value,
); );
@@ -1991,6 +2014,19 @@ external void Material_createImageMaterialRenderThread(
onComplete, 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.Native<
ffi.Void Function( ffi.Void Function(
ffi.Pointer<TEngine>, ffi.Pointer<TEngine>,
@@ -2151,17 +2187,6 @@ external void MaterialProvider_createMaterialInstanceRenderThread(
callback, 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.Native<
ffi.Void Function( ffi.Void Function(
ffi.Pointer<TAnimationManager>, ffi.Pointer<TAnimationManager>,
@@ -2741,6 +2766,52 @@ external void Scene_addFilamentAssetRenderThread(
ffi.Pointer<ffi.NativeFunction<ffi.Void Function()>> callback, 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.Native<
ffi.Pointer<TGltfResourceLoader> Function( ffi.Pointer<TGltfResourceLoader> Function(
ffi.Pointer<TEngine>, ffi.Pointer<ffi.Char>)>(isLeaf: true) ffi.Pointer<TEngine>, ffi.Pointer<ffi.Char>)>(isLeaf: true)
@@ -3824,15 +3895,15 @@ final class Aabb3 extends ffi.Struct {
} }
enum TGizmoType { enum TGizmoType {
TRANSLATION(0), GIZMO_TYPE_TRANSLATION(0),
ROTATION(1); GIZMO_TYPE_ROTATION(1);
final int value; final int value;
const TGizmoType(this.value); const TGizmoType(this.value);
static TGizmoType fromValue(int value) => switch (value) { static TGizmoType fromValue(int value) => switch (value) {
0 => TRANSLATION, 0 => GIZMO_TYPE_TRANSLATION,
1 => ROTATION, 1 => GIZMO_TYPE_ROTATION,
_ => throw ArgumentError("Unknown value for TGizmoType: $value"), _ => throw ArgumentError("Unknown value for TGizmoType: $value"),
}; };
} }

View File

@@ -91,8 +91,7 @@ class ThermionViewerFFI extends ThermionViewer {
View_setDitheringEnabled(view.view, false); View_setDitheringEnabled(view.view, false);
View_setRenderQuality(view.view, TQualityLevel.MEDIUM); View_setRenderQuality(view.view, TQualityLevel.MEDIUM);
await FilamentApp.instance!.setClearOptions(0.0, 0.0, 0.0, 0.0);
await FilamentApp.instance!.setClearColor(1.0, 0.0, 0.0, 1.0);
scene = FFIScene(Engine_createScene(app.engine)); scene = FFIScene(Engine_createScene(app.engine));
await view.setScene(scene); await view.setScene(scene);
final camera = FFICamera( 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); _grid ??= _grid = await GridOverlay.create(app, animationManager);
if (visible) {
await scene.add(_grid!); await scene.add(_grid!);
await view.setLayerVisibility(VisibilityLayers.OVERLAY, true); 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; return asset;
} }
//// final _gizmos = <GizmoType, GizmoAsset>{};
///
///
/// ///
//
@override @override
Future<GizmoAsset> createGizmo(FFIView view, GizmoType gizmoType) async { Future<GizmoAsset> getGizmo(GizmoType gizmoType) async {
throw UnimplementedError(); if (_gizmos[gizmoType] == null) {
// var scene = View_getScene(view.view); _gizmos[gizmoType] =
// final gizmo = await withPointerCallback<TGizmo>((cb) { await FilamentApp.instance!.createGizmo(view, animationManager, gizmoType);
// SceneManager_createGizmoRenderThread(_sceneManager!, view.view, scene, }
// TGizmoType.values[gizmoType.index], cb); return _gizmos[gizmoType]!;
// });
// 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 addToScene(covariant FFIAsset asset) async { Future addToScene(covariant FFIAsset asset) async {

View File

@@ -239,9 +239,10 @@ abstract class ThermionViewer {
{List<MaterialInstance>? materialInstances, bool keepData = false, bool addToScene=true}); {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. /// Register a callback to be invoked when this viewer is disposed.
@@ -261,12 +262,7 @@ abstract class ThermionViewer {
/// ///
/// ///
/// ///
Future showGridOverlay(); Future setGridOverlayVisibility(bool visible);
///
///
///
Future removeGridOverlay();
/// ///
/// ///
@@ -293,6 +289,14 @@ abstract class ThermionViewer {
/// ///
int getCameraCount(); int getCameraCount();
///
/// Adds the asset to the scene, meaning the asset will be rendered/visible.
///
Future addToScene(covariant ThermionAsset asset); 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); Future removeFromScene(covariant ThermionAsset asset);
} }

View File

@@ -36,6 +36,7 @@ set(MATERIAL_SOURCES
"${CMAKE_CURRENT_SOURCE_DIR}/include/material/image.c" "${CMAKE_CURRENT_SOURCE_DIR}/include/material/image.c"
"${CMAKE_CURRENT_SOURCE_DIR}/include/material/grid.c" "${CMAKE_CURRENT_SOURCE_DIR}/include/material/grid.c"
"${CMAKE_CURRENT_SOURCE_DIR}/include/material/unlit.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" "${CMAKE_CURRENT_SOURCE_DIR}/include/material/gizmo.c"
) )

View File

@@ -153,8 +153,8 @@ extern "C"
typedef struct Aabb3 Aabb3; typedef struct Aabb3 Aabb3;
enum TGizmoType { enum TGizmoType {
TRANSLATION, GIZMO_TYPE_TRANSLATION,
ROTATION GIZMO_TYPE_ROTATION
}; };
enum TPrimitiveType { enum TPrimitiveType {

View File

@@ -16,7 +16,15 @@ enum TGizmoPickResultType { AxisX, AxisY, AxisZ, Parent, None };
typedef void (*GizmoPickCallback)(TGizmoPickResultType resultType, float x, float y, float z); 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_pick(TGizmo *tGizmo, uint32_t x, uint32_t y, GizmoPickCallback callback);
EMSCRIPTEN_KEEPALIVE void Gizmo_highlight(TGizmo *tGizmo, TGizmoAxis axis); EMSCRIPTEN_KEEPALIVE void Gizmo_highlight(TGizmo *tGizmo, TGizmoAxis axis);
EMSCRIPTEN_KEEPALIVE void Gizmo_unhighlight(TGizmo *tGizmo); EMSCRIPTEN_KEEPALIVE void Gizmo_unhighlight(TGizmo *tGizmo);

View File

@@ -13,7 +13,7 @@ EMSCRIPTEN_KEEPALIVE TGltfAssetLoader *GltfAssetLoader_create(TEngine *tEngine,
EMSCRIPTEN_KEEPALIVE TFilamentAsset *GltfAssetLoader_load( EMSCRIPTEN_KEEPALIVE TFilamentAsset *GltfAssetLoader_load(
TEngine *tEngine, TEngine *tEngine,
TGltfAssetLoader *tAssetLoader, TGltfAssetLoader *tAssetLoader,
uint8_t *data, const uint8_t *data,
size_t length, size_t length,
uint8_t numInstances uint8_t numInstances
); );

View File

@@ -70,6 +70,7 @@ extern "C"
EMSCRIPTEN_KEEPALIVE TMaterialInstance *Material_createInstance(TMaterial *tMaterial); EMSCRIPTEN_KEEPALIVE TMaterialInstance *Material_createInstance(TMaterial *tMaterial);
EMSCRIPTEN_KEEPALIVE TMaterial *Material_createImageMaterial(TEngine *tEngine); EMSCRIPTEN_KEEPALIVE TMaterial *Material_createImageMaterial(TEngine *tEngine);
EMSCRIPTEN_KEEPALIVE TMaterial *Material_createGridMaterial(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 Material_hasParameter(TMaterial *tMaterial, const char *propertyName);
EMSCRIPTEN_KEEPALIVE bool MaterialInstance_isStencilWriteEnabled(TMaterialInstance *materialInstance); EMSCRIPTEN_KEEPALIVE bool MaterialInstance_isStencilWriteEnabled(TMaterialInstance *materialInstance);
EMSCRIPTEN_KEEPALIVE void MaterialInstance_setStencilWrite(TMaterialInstance *materialInstance, bool enabled); EMSCRIPTEN_KEEPALIVE void MaterialInstance_setStencilWrite(TMaterialInstance *materialInstance, bool enabled);

View File

@@ -27,7 +27,7 @@ extern "C"
TEngine *tEngine, TEngine *tEngine,
TGltfAssetLoader *tAssetLoader, TGltfAssetLoader *tAssetLoader,
TNameComponentManager *tNameComponentManager, TNameComponentManager *tNameComponentManager,
uint8_t *data, const uint8_t *data,
size_t length, size_t length,
size_t numInstances size_t numInstances
); );
@@ -37,7 +37,7 @@ extern "C"
TGltfResourceLoader *tResourceLoader, TGltfResourceLoader *tResourceLoader,
TEngine *tEngine, TEngine *tEngine,
TNameComponentManager *tNameComponentManager, TNameComponentManager *tNameComponentManager,
uint8_t *data, const uint8_t *data,
size_t length, size_t length,
size_t numInstances size_t numInstances
); );

View File

@@ -81,6 +81,7 @@ namespace thermion
EMSCRIPTEN_KEEPALIVE void Material_createInstanceRenderThread(TMaterial *tMaterial, void (*onComplete)(TMaterialInstance *)); EMSCRIPTEN_KEEPALIVE void Material_createInstanceRenderThread(TMaterial *tMaterial, void (*onComplete)(TMaterialInstance *));
EMSCRIPTEN_KEEPALIVE void Material_createImageMaterialRenderThread(TEngine *tEngine, void (*onComplete)(TMaterial *)); 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 ColorGrading_createRenderThread(TEngine *tEngine, TToneMapping toneMapping, void (*callback)(TColorGrading *));
EMSCRIPTEN_KEEPALIVE void View_setColorGradingRenderThread(TView *tView, TColorGrading *tColorGrading, void (*callback)()); EMSCRIPTEN_KEEPALIVE void View_setColorGradingRenderThread(TView *tView, TColorGrading *tColorGrading, void (*callback)());
@@ -116,7 +117,6 @@ namespace thermion
void (*callback)(TSceneAsset *) void (*callback)(TSceneAsset *)
); );
EMSCRIPTEN_KEEPALIVE void MaterialProvider_createMaterialInstanceRenderThread(TMaterialProvider *tMaterialProvider, TMaterialKey *tKey, void (*callback)(TMaterialInstance *)); 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( EMSCRIPTEN_KEEPALIVE void AnimationManager_updateBoneMatricesRenderThread(
TAnimationManager *tAnimationManager, TAnimationManager *tAnimationManager,
@@ -269,6 +269,16 @@ namespace thermion
void (*callback)(TFilamentAsset *) void (*callback)(TFilamentAsset *)
); );
EMSCRIPTEN_KEEPALIVE void Scene_addFilamentAssetRenderThread(TScene* tScene, TFilamentAsset *tAsset, void (*callback)()); 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 *)
);

View File

@@ -37,7 +37,6 @@ namespace thermion
SceneAsset *sceneAsset, SceneAsset *sceneAsset,
Engine *engine, Engine *engine,
View *view, View *view,
Scene *scene,
Material *material) noexcept; Material *material) noexcept;
Gizmo(Gizmo &&other) noexcept; Gizmo(Gizmo &&other) noexcept;

View File

@@ -117,6 +117,9 @@ namespace thermion
for(int i = 0; i < getChildEntityCount(); i++) { for(int i = 0; i < getChildEntityCount(); i++) {
auto entity = getChildEntities()[i]; auto entity = getChildEntities()[i];
auto nameInstance = _ncm->getInstance(entity); auto nameInstance = _ncm->getInstance(entity);
if(!nameInstance.isValid()) {
continue;
}
auto entityName = _ncm->getName(nameInstance); auto entityName = _ncm->getName(nameInstance);
if (strcmp(entityName, name) == 0) { if (strcmp(entityName, name) == 0) {

View File

@@ -3,7 +3,11 @@
#include <filament/Scene.h> #include <filament/Scene.h>
#include "c_api/TGizmo.h" #include "c_api/TGizmo.h"
#include "c_api/TSceneAsset.h"
#include "scene/Gizmo.hpp" #include "scene/Gizmo.hpp"
#include "scene/GltfSceneAsset.hpp"
#include "resources/translation_gizmo_glb.h"
#include "resources/rotation_gizmo_glb.h"
#include "Log.hpp" #include "Log.hpp"
#ifdef __cplusplus #ifdef __cplusplus
@@ -14,6 +18,61 @@ namespace thermion
using namespace filament; using namespace filament;
#endif #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) EMSCRIPTEN_KEEPALIVE void Gizmo_pick(TGizmo *tGizmo, uint32_t x, uint32_t y, GizmoPickCallback callback)
{ {
auto *gizmo = reinterpret_cast<Gizmo *>(tGizmo); auto *gizmo = reinterpret_cast<Gizmo *>(tGizmo);

View File

@@ -56,7 +56,7 @@ EMSCRIPTEN_KEEPALIVE TGltfAssetLoader *GltfAssetLoader_create(TEngine *tEngine,
EMSCRIPTEN_KEEPALIVE TFilamentAsset *GltfAssetLoader_load( EMSCRIPTEN_KEEPALIVE TFilamentAsset *GltfAssetLoader_load(
TEngine *tEngine, TEngine *tEngine,
TGltfAssetLoader *tAssetLoader, TGltfAssetLoader *tAssetLoader,
uint8_t *data, const uint8_t *data,
size_t length, size_t length,
uint8_t numInstances) uint8_t numInstances)
{ {

View File

@@ -11,6 +11,7 @@
#include "MathUtils.hpp" #include "MathUtils.hpp"
#include "material/image.h" #include "material/image.h"
#include "material/grid.h" #include "material/grid.h"
#include "material/unlit_fixed_size.h"
#include "c_api/TMaterialInstance.h" #include "c_api/TMaterialInstance.h"
@@ -45,7 +46,13 @@ namespace thermion
return reinterpret_cast<TMaterial *>(material); 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) { EMSCRIPTEN_KEEPALIVE bool Material_hasParameter(TMaterial *tMaterial, const char *propertyName) {
auto *material = reinterpret_cast<filament::Material *>(tMaterial); auto *material = reinterpret_cast<filament::Material *>(tMaterial);

View File

@@ -70,7 +70,7 @@ extern "C"
TEngine *tEngine, TEngine *tEngine,
TGltfAssetLoader *tAssetLoader, TGltfAssetLoader *tAssetLoader,
TNameComponentManager *tNameComponentManager, TNameComponentManager *tNameComponentManager,
uint8_t *data, const uint8_t *data,
size_t length, size_t length,
size_t numInstances size_t numInstances
) { ) {

View File

@@ -8,6 +8,7 @@
#include "c_api/APIBoundaryTypes.h" #include "c_api/APIBoundaryTypes.h"
#include "c_api/TAnimationManager.h" #include "c_api/TAnimationManager.h"
#include "c_api/TEngine.h" #include "c_api/TEngine.h"
#include "c_api/TGizmo.h"
#include "c_api/TGltfAssetLoader.h" #include "c_api/TGltfAssetLoader.h"
#include "c_api/TGltfResourceLoader.h" #include "c_api/TGltfResourceLoader.h"
#include "c_api/TRenderer.h" #include "c_api/TRenderer.h"
@@ -381,6 +382,17 @@ extern "C"
auto fut = _renderThread->add_task(lambda); 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 *)) EMSCRIPTEN_KEEPALIVE void Material_createInstanceRenderThread(TMaterial *tMaterial, void (*onComplete)(TMaterialInstance *))
{ {
std::packaged_task<void()> lambda( std::packaged_task<void()> lambda(
@@ -1010,4 +1022,23 @@ extern "C"
}); });
auto fut = _renderThread->add_task(lambda); 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);
}
} }