feat: add clearMorphAnimationData function

This commit is contained in:
Nick Fisher
2024-07-23 09:37:08 +08:00
parent d745712650
commit 41bf9ededa
12 changed files with 105 additions and 15 deletions

View File

@@ -368,6 +368,12 @@ external bool set_morph_animation(
double frameLengthInMs,
);
@ffi.Native<ffi.Void Function(ffi.Pointer<ffi.Void>, ffi.Int)>()
external void clear_morph_animation(
ffi.Pointer<ffi.Void> sceneManager,
int entity,
);
@ffi.Native<ffi.Void Function(ffi.Pointer<ffi.Void>, ffi.Int)>()
external void reset_to_rest_pose(
ffi.Pointer<ffi.Void> sceneManager,

View File

@@ -226,6 +226,11 @@ class ThermionViewerJSDartBridge {
.then((v) => v.toJS)
.toJS;
@JSExport()
void clearMorphAnimationData(ThermionEntity entity) {
viewer.clearMorphAnimationData(entity);
}
@JSExport()
JSPromise setMorphAnimationData(
ThermionEntity entity,

View File

@@ -263,6 +263,11 @@ class ThermionViewerJS implements ThermionViewer {
.toDartDouble;
}
@override
Future<void> clearMorphAnimationData(ThermionEntity entity) async {
_shim.clearMorphAnimationData(entity);
}
@override
Future<void> setMorphAnimationData(
ThermionEntity entity, MorphAnimationData animation,

View File

@@ -6,11 +6,10 @@ import 'dart:js_interop';
import 'package:thermion_dart/thermion_dart/thermion_viewer.dart';
///
/// An extension type on [JSObject] that represents a
/// An extension type on [JSObject] that represents a
/// Javascript shim implementation of the [ThermionViewer] interface.
///
///
extension type ThermionViewerJSShim(JSObject _) implements JSObject {
@JS('initialized')
external JSPromise<JSBoolean> get initialized;
@@ -136,6 +135,9 @@ extension type ThermionViewerJSShim(JSObject _) implements JSObject {
external JSPromise<JSNumber> getAnimationDuration(
ThermionEntity entity, int animationIndex);
@JS('clearMorphAnimationData')
external void clearMorphAnimationData(ThermionEntity entity);
@JS('setMorphAnimationData')
external JSPromise setMorphAnimationData(
ThermionEntity entity,
@@ -403,19 +405,16 @@ extension type ThermionViewerJSShim(JSObject _) implements JSObject {
ThermionEntity entity, JSArray<JSNumber> transform);
@JS('setBoneTransform')
external JSPromise setBoneTransform(
ThermionEntity entity, int boneIndex, JSArray<JSNumber> transform, int skinIndex);
external JSPromise setBoneTransform(ThermionEntity entity, int boneIndex,
JSArray<JSNumber> transform, int skinIndex);
@JS('setShadowsEnabled')
external JSPromise setShadowsEnabled(
bool enabled);
external JSPromise setShadowsEnabled(bool enabled);
@JS('setShadowType')
external JSPromise setShadowType(
int shadowType);
external JSPromise setShadowType(int shadowType);
@JS('setSoftShadowOptions')
external JSPromise setSoftShadowOptions(
double penumbraScale, double penumbraRatioScale);
}

View File

@@ -788,6 +788,27 @@ class ThermionViewerWasm implements ThermionViewer {
null);
}
@override
Future clearMorphAnimationData(
ThermionEntity entity) async {
var meshEntities = await getChildEntities(entity, false);
for(final childEntity in meshEntities) {
_module.ccall(
"clear_morph_animation",
"void",
[
"void*".toJS,
"int".toJS,
].toJS,
[
_sceneManager!,
childEntity.toJS,
].toJS,
null);
}
}
@override
Future setMorphAnimationData(
ThermionEntity entity, MorphAnimationData animation,

View File

@@ -269,6 +269,12 @@ abstract class ThermionViewer {
ThermionEntity entity, MorphAnimationData animation,
{List<String>? targetMeshNames});
///
/// Clear all current morph animations for [entity].
///
Future clearMorphAnimationData(
ThermionEntity entity);
///
/// Resets all bones in the given entity to their rest pose.
/// This should be done before every call to addBoneAnimation.

View File

@@ -599,6 +599,18 @@ class ThermionViewerFFI extends ThermionViewer {
return getAnimationDuration(entity, index);
}
Future clearMorphAnimationData(ThermionEntity entity) async {
var meshEntities = await getChildEntities(entity, true);
for(final childEntity in meshEntities) {
clear_morph_animation(
_sceneManager!,
childEntity);
}
}
///
///
///

View File

@@ -586,6 +586,11 @@ class ThermionViewerStub extends ThermionViewer {
throw UnimplementedError();
}
@override
Future clearMorphAnimationData(ThermionEntity entity) {
throw UnimplementedError();
}
@override
Future setMorphAnimationData(
ThermionEntity entity, MorphAnimationData animation,
@@ -726,19 +731,19 @@ class ThermionViewerStub extends ThermionViewer {
// TODO: implement zoomUpdate
throw UnimplementedError();
}
@override
Future setShadowType(ShadowType shadowType) {
// TODO: implement setShadowType
throw UnimplementedError();
}
@override
Future setShadowsEnabled(bool enabled) {
// TODO: implement setShadowsEnabled
throw UnimplementedError();
}
@override
Future setSoftShadowOptions(double penumbraScale, double penumbraRatioScale) {
// TODO: implement setSoftShadowOptions

View File

@@ -87,6 +87,9 @@ namespace thermion_filament
int numFrames,
float frameLengthInMs);
void clearMorphAnimationBuffer(
EntityId entityId);
bool setMorphTargetWeights(EntityId entityId, const float *const weights, int count);
math::mat4f getLocalTransform(EntityId entityId);

View File

@@ -136,6 +136,9 @@ extern "C"
int numMorphTargets,
int numFrames,
float frameLengthInMs);
EMSCRIPTEN_KEEPALIVE void clear_morph_animation(
void *sceneManager,
EntityId entity);
EMSCRIPTEN_KEEPALIVE void reset_to_rest_pose(
void *sceneManager,

View File

@@ -781,6 +781,27 @@ namespace thermion_filament
return true;
}
void SceneManager::clearMorphAnimationBuffer(
EntityId entityId)
{
std::lock_guard lock(_mutex);
auto entity = Entity::import(entityId);
if (entity.isNull())
{
Log("ERROR: invalid entity %d.", entityId);
return;
}
auto animationComponentInstance = _animationComponentManager->getInstance(entity);
auto &animationComponent = _animationComponentManager->elementAt<0>(animationComponentInstance);
auto &morphAnimations = animationComponent.morphAnimations;
morphAnimations.clear();
return;
}
bool SceneManager::setMaterialColor(EntityId entityId, const char *meshName, int materialIndex, const float r, const float g, const float b, const float a)
{

View File

@@ -405,7 +405,7 @@ extern "C"
return ((SceneManager *)sceneManager)->setMorphTargetWeights(asset, weights, numWeights);
}
bool set_morph_animation(
EMSCRIPTEN_KEEPALIVE bool set_morph_animation(
void *sceneManager,
EntityId asset,
const float *const morphData,
@@ -418,6 +418,10 @@ extern "C"
return result;
}
EMSCRIPTEN_KEEPALIVE void clear_morph_animation(void* sceneManager, EntityId asset) {
((SceneManager *)sceneManager)->clearMorphAnimationBuffer(asset);
}
EMSCRIPTEN_KEEPALIVE void reset_to_rest_pose(void *sceneManager, EntityId entityId)
{
((SceneManager *)sceneManager)->resetBones(entityId);