public SkeletalAnimation() { InitializeComponent(); // Use helper class (defined in this sample project) to load the native assimp libraries // IMPORTANT: See commend in the AssimpLoader class for details on how to prepare your project to use assimp library. AssimpLoader.LoadAssimpNativeLibrary(); string fileName = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources\\soldier.X"); LoadFileWithSkinnedAnimation(fileName); var dragAndDropHelper = new DragAndDropHelper(ViewportBorder, ".*"); dragAndDropHelper.FileDropped += (sender, e) => { LoadFileWithSkinnedAnimation(e.FileName); }; // Stop the animation when user leaves this sample. this.Unloaded += delegate(object sender, RoutedEventArgs args) { if (_assimpAnimationController != null) { _assimpAnimationController.StopAnimation(); _assimpAnimationController = null; } }; }
private void LoadFileWithSkinnedAnimation(string fileName) { InfoTextBox.Text = ""; // Create an instance of AssimpWpfImporter var assimpWpfImporter = new AssimpWpfImporter(); Model3D readModel3D; try { readModel3D = assimpWpfImporter.ReadModel3D(fileName, texturesPath: null); // we can also define a textures path if the textures are located in some other directory (this is parameter can be skipped, but is defined here so you will know that you can use it) } catch (Exception ex) { MessageBox.Show(string.Format("Error reading file:\r\n{0}\r\n\r\n{1}", fileName, ex.Message)); return; } _lastLoadedModel3D = readModel3D; ModelSilhueteVisual3D.Children.Clear(); ModelSilhueteVisual3D.Children.Add(readModel3D.CreateModelVisual3D()); // Set camera to show the whole model Camera1.TargetPosition = readModel3D.Bounds.GetCenterPosition(); Camera1.Distance = readModel3D.Bounds.GetDiagonalLength() * 1.2; BoneMarkersVisual3D.Children.Clear(); // Stop current animation if it was running if (_assimpAnimationController != null) { _assimpAnimationController.StopAnimation(); _assimpAnimationController = null; } _assimpScene = assimpWpfImporter.ImportedAssimpScene; if (_assimpScene.AnimationCount == 0) { // No animation in the file AnimationSelectionComboBox.IsEnabled = false; AnimationSelectionComboBox.ItemsSource = new string[] { "(no animation defined)" }; AnimationSelectionComboBox.SelectedIndex = 0; ShowBonesCheckBox.IsEnabled = false; ShowBonesCheckBox.IsChecked = false; AnimationSlider.IsEnabled = false; StartStopAnimationButton.IsEnabled = false; UpdateAnimationUI(0); return; } AnimationSlider.IsEnabled = true; StartStopAnimationButton.IsEnabled = true; try { // Create AssimpAnimationController - it will play the keyframe and skeletal animation _assimpAnimationController = new AssimpAnimationController(assimpWpfImporter); _assimpAnimationController.AutoReverse = false; _assimpAnimationController.AutoRepeat = true; _assimpAnimationController.AfterFrameUpdated += OnAssimpAnimationControllerOnAfterFrameUpdated; SetupAnimationUI(); UpdateBoneMarkersAndUI(); } catch (Exception ex) { MessageBox.Show("Error starting animation:\r\n" + ex.Message); return; } // Setup animation names var animationNames = _assimpScene.Animations.Select(a => a.Name).ToList(); AnimationSelectionComboBox.IsEnabled = true; AnimationSelectionComboBox.ItemsSource = animationNames; int startupIndex = animationNames.IndexOf("Run"); // Start with "Run" animation if it exists if (startupIndex < 0) { startupIndex = 0; } AnimationSelectionComboBox.SelectedIndex = startupIndex; // This will call ChangeAnimationName method if (_assimpAnimationController.HasSkeletalAnimation) { ShowBonesCheckBox.IsEnabled = true; ShowBonesCheckBox.ToolTip = null; // In case ShowBonesCheckBox is checked then set model opacity to 0.8 UpdateModelOpacity(); } else { // No skeletal animation (probably only keyframe animation) ShowBonesCheckBox.IsEnabled = false; ShowBonesCheckBox.IsChecked = false; ShowBonesCheckBox.ToolTip = "This files does not define any skeletal animation."; } //StartAnimation(); }