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

View File

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

View File

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