web : mandate child entity for morph animation functions and implement some missing functions

This commit is contained in:
Nick Fisher
2024-05-17 14:49:40 +08:00
parent a566851efe
commit 3a98bd53de
3 changed files with 88 additions and 56 deletions

View File

@@ -14,12 +14,10 @@ class DartFilamentJSExportViewer {
final AbstractFilamentViewer viewer; final AbstractFilamentViewer viewer;
static void initializeBindings(AbstractFilamentViewer viewer) { static void initializeBindings(AbstractFilamentViewer viewer) {
print("Initializing JS bindings");
var shim = DartFilamentJSExportViewer(viewer); var shim = DartFilamentJSExportViewer(viewer);
var wrapper = createJSInteropWrapper<DartFilamentJSExportViewer>(shim) var wrapper = createJSInteropWrapper<DartFilamentJSExportViewer>(shim)
as DartFilamentJSShim; as DartFilamentJSShim;
globalContext.setProperty("filamentViewer".toJS, wrapper); globalContext.setProperty("filamentViewer".toJS, wrapper);
print("Set global context property");
} }
DartFilamentJSExportViewer(this.viewer); DartFilamentJSExportViewer(this.viewer);
@@ -163,11 +161,12 @@ class DartFilamentJSExportViewer {
@JSExport() @JSExport()
JSPromise<JSArray<JSString>> getMorphTargetNames( JSPromise<JSArray<JSString>> getMorphTargetNames(
FilamentEntity entity, String meshName) => FilamentEntity entity, FilamentEntity childEntity) {
viewer var morphTargetNames = viewer
.getMorphTargetNames(entity, meshName) .getMorphTargetNames(entity, childEntity)
.then((v) => v.map((s) => s.toJS).toList().toJS) .then((v) => v.map((s) => s.toJS).toList().toJS);
.toJS; return morphTargetNames.toJS;
}
@JSExport() @JSExport()
JSPromise<JSArray<JSString>> getAnimationNames(FilamentEntity entity) => JSPromise<JSArray<JSString>> getAnimationNames(FilamentEntity entity) =>
@@ -186,29 +185,52 @@ class DartFilamentJSExportViewer {
@JSExport() @JSExport()
JSPromise setMorphAnimationData( JSPromise setMorphAnimationData(
FilamentEntity entity, FilamentEntity entity,
JSArray<JSArray<JSNumber>> animation, JSArray<JSArray<JSNumber>> animation,
JSArray<JSString> morphTargets, JSArray<JSString> morphTargets,
JSArray<JSString> targetMeshNames) => JSArray<JSString>? targetMeshNames,
viewer double frameLengthInMs) {
try {
var morphTargetsDart = morphTargets.toDart.map((m) => m.toDart).toList();
var animationDataDart = animation.toDart
.map((x) => x.toDart.map((y) => y.toDartDouble).toList())
.toList();
var morphAnimationData = MorphAnimationData(
animationDataDart, morphTargetsDart,
frameLengthInMs: frameLengthInMs);
var targetMeshNamesDart =
targetMeshNames?.toDart.map((x) => x.toDart).toList();
if (animationDataDart.first.length != morphTargetsDart.length) {
throw Exception(
"Length mismatch between morph targets and animation data");
}
var result = viewer
.setMorphAnimationData( .setMorphAnimationData(
entity, entity,
MorphAnimationData( morphAnimationData,
animation.toDart targetMeshNames: targetMeshNamesDart,
.map((x) => x.toDart.map((y) => y.toDartDouble).toList()) )
.toList(), .onError((err, st) {
morphTargets.toDart.map((m) => m.toDart).toList()), print("ERROR SETTING MORPH ANIMATION DATA : $err\n$st");
targetMeshNames: return null;
targetMeshNames.toDart.map((x) => x.toDart).toList(), }).then((r) {
) print("set morph animation data complete");
.toJS; });
return result.toJS;
} catch (err, st) {
print(err);
print(st);
rethrow;
}
}
@JSExport() @JSExport()
JSPromise resetBones(FilamentEntity entity) => viewer.resetBones(entity).toJS; JSPromise resetBones(FilamentEntity entity) => viewer.resetBones(entity).toJS;
@JSExport() @JSExport()
JSPromise addBoneAnimation(FilamentEntity entity, JSObject animation) { JSPromise addBoneAnimation(FilamentEntity entity, JSObject animation) {
throw Exception(); throw UnimplementedError();
} }
// viewer // viewer
// .addBoneAnimation( // .addBoneAnimation(
@@ -508,7 +530,10 @@ class DartFilamentJSExportViewer {
renderableOnly, renderableOnly,
) )
.then((entities) => entities.map((entity) => entity.toJS).toList().toJS) .then((entities) => entities.map((entity) => entity.toJS).toList().toJS)
.toJS; .onError((e, st) async {
print("Error : $e\n$st");
return <JSNumber>[].toJS;
}).toJS;
} }
@JSExport() @JSExport()
@@ -519,7 +544,10 @@ class DartFilamentJSExportViewer {
childName, childName,
) )
.then((entity) => entity.toJS) .then((entity) => entity.toJS)
.toJS; .onError((e, st) async {
print("Error getChildEntity : $e\n$st");
return 0.toJS;
}).toJS;
} }
@JSExport() @JSExport()

View File

@@ -114,7 +114,7 @@ extension type DartFilamentJSShim(JSObject _) implements JSObject {
@JS('getMorphTargetNames') @JS('getMorphTargetNames')
external JSPromise<JSArray<JSString>> getMorphTargetNames( external JSPromise<JSArray<JSString>> getMorphTargetNames(
FilamentEntity entity, String meshName); FilamentEntity entity, FilamentEntity childEntity);
@JS('getAnimationNames') @JS('getAnimationNames')
external JSPromise<JSArray<JSString>> getAnimationNames(FilamentEntity entity); external JSPromise<JSArray<JSString>> getAnimationNames(FilamentEntity entity);
@@ -129,6 +129,7 @@ extension type DartFilamentJSShim(JSObject _) implements JSObject {
JSArray<JSArray<JSNumber>> animation, JSArray<JSArray<JSNumber>> animation,
JSArray<JSString> morphTargets, JSArray<JSString> morphTargets,
JSArray<JSString>? targetMeshNames, JSArray<JSString>? targetMeshNames,
double frameLengthInMs
); );
@JS('resetBones') @JS('resetBones')

View File

@@ -12,8 +12,6 @@ class JsInteropFilamentViewer implements AbstractFilamentViewer {
late final DartFilamentJSShim _jsObject; late final DartFilamentJSShim _jsObject;
JsInteropFilamentViewer(String globalPropertyName) { JsInteropFilamentViewer(String globalPropertyName) {
print(
"Initializing interop viewer with global property $globalPropertyName");
this._jsObject = globalContext.getProperty(globalPropertyName.toJS) this._jsObject = globalContext.getProperty(globalPropertyName.toJS)
as DartFilamentJSShim; as DartFilamentJSShim;
} }
@@ -134,7 +132,9 @@ class JsInteropFilamentViewer implements AbstractFilamentViewer {
@override @override
Future<FilamentEntity> loadGlb(String path, {int numInstances = 1}) async { Future<FilamentEntity> loadGlb(String path, {int numInstances = 1}) async {
return (await _jsObject.loadGlb(path, numInstances).toDart).toDartInt; var entity = (await _jsObject.loadGlb(path, numInstances).toDart).toDartInt;
scene.registerEntity(entity);
return entity;
} }
@override @override
@@ -201,18 +201,17 @@ class JsInteropFilamentViewer implements AbstractFilamentViewer {
@override @override
Future<void> setMorphTargetWeights( Future<void> setMorphTargetWeights(
FilamentEntity entity, List<double> weights) async { FilamentEntity entity, List<double> weights) async {
throw UnimplementedError(); await _jsObject
.setMorphTargetWeights(entity, weights.map((x) => x.toJS).toList().toJS)
// JSArray<JSNumber>.withLength(weights.length) .toDart;
// await _jsObject.setMorphTargetWeights(entity, weights.toJSBox as JSArray<JSNumber>).toDart;
} }
@override @override
Future<List<String>> getMorphTargetNames( Future<List<String>> getMorphTargetNames(
FilamentEntity entity, String meshName) async { FilamentEntity entity, FilamentEntity childEntity) async {
var result = _jsObject.getMorphTargetNames(entity, meshName).toDart; var result =
var dartResult = (await result).toDart; await _jsObject.getMorphTargetNames(entity, childEntity).toDart;
return dartResult.map((r) => r.toDart).toList(); return result.toDart.map((r) => r.toDart).toList();
} }
@override @override
@@ -235,16 +234,23 @@ class JsInteropFilamentViewer implements AbstractFilamentViewer {
Future<void> setMorphAnimationData( Future<void> setMorphAnimationData(
FilamentEntity entity, MorphAnimationData animation, FilamentEntity entity, MorphAnimationData animation,
{List<String>? targetMeshNames}) async { {List<String>? targetMeshNames}) async {
await _jsObject try {
.setMorphAnimationData( var animationDataJs = animation.data
entity, .map((x) => x.map((y) => y.toJS).toList().toJS)
animation.data .toList()
.map((x) => x.map((y) => y.toJS).toList().toJS) .toJS;
.toList() var morphTargetsJs =
.toJS, animation.morphTargets.map((x) => x.toJS).toList().toJS;
animation.morphTargets.map((x) => x.toJS).toList().toJS, var targetMeshNamesJS = targetMeshNames?.map((x) => x.toJS).toList().toJS;
targetMeshNames?.map((x) => x.toJS).toList().toJS) await _jsObject
.toDart; .setMorphAnimationData(entity, animationDataJs, morphTargetsJs,
targetMeshNamesJS, animation.frameLengthInMs)
.toDart;
} catch (err, st) {
print(err);
print(st);
rethrow;
}
} }
@override @override
@@ -266,7 +272,6 @@ class JsInteropFilamentViewer implements AbstractFilamentViewer {
@override @override
Future<void> clearEntities() async { Future<void> clearEntities() async {
print("clear entities on js interop side");
await _jsObject.clearEntities().toDart; await _jsObject.clearEntities().toDart;
} }
@@ -582,14 +587,12 @@ class JsInteropFilamentViewer implements AbstractFilamentViewer {
@override @override
Future<List<FilamentEntity>> getChildEntities( Future<List<FilamentEntity>> getChildEntities(
FilamentEntity parent, bool renderableOnly) async { FilamentEntity parent, bool renderableOnly) async {
throw UnimplementedError(); final children =
// final List<JSObject> jsEntities = await _jsObject await _jsObject.getChildEntities(parent, renderableOnly).toDart;
// .getChildEntities(parent, renderableOnly.toJSBoolean()) return children.toDart
// .toDart; .map((js) => js.toDartInt)
// return jsEntities .cast<FilamentEntity>()
// .map((js) => FilamentEntity._fromJSObject(js)) .toList();
// .toList()
// .toDart;
} }
@override @override