EG/plugins/user/morphing_animation/examples.py
2025-12-12 16:16:15 +08:00

283 lines
9.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
Morphing和变形动画插件使用示例
提供各种使用场景的详细示例
"""
from panda3d.core import *
from direct.showbase.ShowBase import ShowBase
from direct.task import Task
# 导入插件模块
from .core import MorphingCore, InterpolationMode
from .advanced import AnimationStateMachine, AnimationCurveEditor, PhysicsAnimationIntegration, AnimationStateType, TransitionType, CurveType
from .optimization import AnimationCompressor, AnimationLODSystem, AnimationCacheManager, CompressionMethod
from .network import AnimationNetworkSync, NetworkMode, SyncMode
def example_basic_morphing():
"""
基础Morphing使用示例
"""
print("=== 基础Morphing使用示例 ===")
# 假设我们有一个world对象和模型
# world = ShowBase()
# model = world.loader.loadModel("path/to/model")
# model.reparentTo(world.render)
# 创建Morphing核心系统
# morphing_core = MorphingCore(world)
# 注册模型
# model_id = morphing_core.register_model(model, "my_model")
# 创建Morph目标示例数据
# vertex_offsets = [(0.1, 0.0, 0.0)] * 100 # 假设有100个顶点
# morphing_core.create_morph_target(model_id, "smile", vertex_offsets)
# 设置Morph目标权重
# morphing_core.set_morph_target_weight(model_id, "smile", 0.5)
print("基础Morphing示例:")
print(" 1. 创建MorphingCore实例")
print(" 2. 注册模型")
print(" 3. 创建Morph目标")
print(" 4. 设置Morph目标权重")
print("=== 基础Morphing使用示例结束 ===\n")
def example_animation_state_machine():
"""
动画状态机使用示例
"""
print("=== 动画状态机使用示例 ===")
# 假设已有morphing_core和model_id
# state_machine = AnimationStateMachine(morphing_core, model_id)
# 添加状态
# state_machine.add_state("idle", {"neutral": 1.0}, duration=0.0, loop=True)
# state_machine.add_state("happy", {"smile": 1.0}, duration=2.0)
# state_machine.add_state("sad", {"frown": 1.0}, duration=2.0)
# 设置状态转换
# state_machine.set_transition("idle", "happy", TransitionType.SMOOTH, transition_time=0.5)
# state_machine.set_transition("happy", "sad", TransitionType.SMOOTH, transition_time=0.5)
# state_machine.set_transition("sad", "idle", TransitionType.SMOOTH, transition_time=0.5)
# 进入初始状态
# state_machine.enter_state("idle")
# 启动更新
# state_machine.start_updating()
print("动画状态机示例:")
print(" 1. 创建AnimationStateMachine实例")
print(" 2. 添加动画状态")
print(" 3. 设置状态转换")
print(" 4. 进入初始状态")
print(" 5. 启动更新")
print("=== 动画状态机使用示例结束 ===\n")
def example_curve_editor():
"""
曲线编辑器使用示例
"""
print("=== 曲线编辑器使用示例 ===")
# 假设已有morphing_core
# curve_editor = AnimationCurveEditor(morphing_core)
# 创建曲线
# control_points = [(0.0, 0.0), (0.5, 1.0), (1.0, 0.0)]
# curve_editor.create_curve("bounce", CurveType.BEZIER, control_points)
# 应用曲线到Morph目标
# curve_editor.apply_curve_to_morph_target("bounce", model_id, "smile", 0.5)
print("曲线编辑器示例:")
print(" 1. 创建AnimationCurveEditor实例")
print(" 2. 创建动画曲线")
print(" 3. 应用曲线到Morph目标")
print("=== 曲线编辑器使用示例结束 ===\n")
def example_physics_integration():
"""
物理集成使用示例
"""
print("=== 物理集成使用示例 ===")
# 假设已有morphing_core
# physics_system = PhysicsAnimationIntegration(morphing_core)
# 添加物理约束
# physics_system.add_constraint(model_id, "spring_smile", "spring", "smile", stiffness=2.0, damping=0.5)
# 启动更新
# physics_system.start_updating()
print("物理集成示例:")
print(" 1. 创建PhysicsAnimationIntegration实例")
print(" 2. 添加物理约束")
print(" 3. 启动更新")
print("=== 物理集成使用示例结束 ===\n")
def example_compression():
"""
动画压缩使用示例
"""
print("=== 动画压缩使用示例 ===")
# 创建压缩器
# compressor = AnimationCompressor()
# 创建测试数据
# morph_data = [(0.0, 0.0, 0.0), (0.1, 0.05, 0.02), (0.2, 0.1, 0.04)]
# 压缩数据
# compressed = compressor.compress_morph_data(morph_data, CompressionMethod.KEYFRAME_REDUCTION)
# 解压缩数据
# if compressed:
# decompressed = compressor.decompress_morph_data(compressed)
print("动画压缩示例:")
print(" 1. 创建AnimationCompressor实例")
print(" 2. 准备Morph数据")
print(" 3. 压缩数据")
print(" 4. 解压缩数据")
print("=== 动画压缩使用示例结束 ===\n")
def example_lod_system():
"""
LOD系统使用示例
"""
print("=== LOD系统使用示例 ===")
# 假设已有world对象
# lod_system = AnimationLODSystem(world)
# 设置相机
# lod_system.set_camera(world.camera)
# 更新LOD级别在渲染循环中
# lod_system.update_lod_levels(models)
print("LOD系统示例:")
print(" 1. 创建AnimationLODSystem实例")
print(" 2. 设置相机")
print(" 3. 更新LOD级别")
print("=== LOD系统使用示例结束 ===\n")
def example_network_synchronization():
"""
网络同步使用示例
"""
print("=== 网络同步使用示例 ===")
# 假设已有world和morphing_core
# network_sync = AnimationNetworkSync(world, morphing_core)
# 设置网络模式
# network_sync.set_network_mode(NetworkMode.SERVER)
# network_sync.set_sync_mode(SyncMode.STATE_SYNC)
# 注册模型进行同步
# network_id = network_sync.register_model_for_sync(model_id)
# 启动同步任务
# network_sync.start_sync_task()
print("网络同步示例:")
print(" 1. 创建AnimationNetworkSync实例")
print(" 2. 设置网络模式")
print(" 3. 注册模型进行同步")
print(" 4. 启动同步任务")
print("=== 网络同步使用示例结束 ===\n")
def example_complete_workflow():
"""
完整工作流程示例
"""
print("=== 完整工作流程示例 ===")
print("完整Morphing动画工作流程:")
print(" 1. 初始化系统")
print(" - 创建MorphingCore")
print(" - 创建高级功能模块")
print(" - 创建优化模块")
print(" - 创建网络模块")
print(" 2. 模型设置")
print(" - 加载模型")
print(" - 注册到MorphingCore")
print(" - 创建Morph目标")
print(" 3. 动画创建")
print(" - 使用状态机管理复杂动画")
print(" - 使用曲线编辑器创建自定义曲线")
print(" - 集成物理效果")
print(" 4. 性能优化")
print(" - 应用动画压缩")
print(" - 设置LOD系统")
print(" - 使用缓存管理")
print(" 5. 网络同步")
print(" - 配置网络同步")
print(" - 注册模型同步")
print(" - 启动同步任务")
print(" 6. 运行时更新")
print(" - 在主循环中更新各模块")
print(" - 处理用户输入")
print(" - 监控性能")
print("=== 完整工作流程示例结束 ===\n")
def example_plugin_usage():
"""
插件使用示例
"""
print("=== 插件使用示例 ===")
print("在主程序中使用Morphing动画插件:")
print(" # 启用插件系统")
print(" world.plugin_manager.enable_plugin_system()")
print("")
print(" # 启用Morphing动画插件")
print(" world.plugin_manager.enable_plugin('morphing_animation')")
print("")
print(" # 获取插件实例")
print(" plugin = world.plugin_manager.get_plugin('morphing_animation')")
print("")
print(" # 获取核心系统")
print(" core_system = plugin.get_core_system()")
print("")
print(" # 使用核心系统功能")
print(" model_id = core_system.register_model(model_node)")
print(" core_system.create_morph_target(model_id, 'target_name', vertex_offsets)")
print(" core_system.set_morph_target_weight(model_id, 'target_name', 0.5)")
print("=== 插件使用示例结束 ===\n")
# 主函数用于测试示例
def main():
"""
主函数 - 运行所有示例
"""
print("Morphing和变形动画插件使用示例")
print("=" * 50)
example_basic_morphing()
example_animation_state_machine()
example_curve_editor()
example_physics_integration()
example_compression()
example_lod_system()
example_network_synchronization()
example_complete_workflow()
example_plugin_usage()
print("所有示例运行完毕")
if __name__ == "__main__":
main()