Exemplo n.º 1
0
        public void Play(string name = "")
        {
            if (string.IsNullOrEmpty(name))
            {
                name = aiScene.Animations[0].Name;
            }

            Stop();

            var replaceCamera = true;

            aniThread = new Thread(() => {
                viewport3D.Dispatcher.Invoke(() =>
                {
                    assimpAnimation = new AssimpAnimation(aiScene, name);
                });

                while (true)
                {
                    var ani = aiScene.Animations.SingleOrDefault(m => m.Name == name);

                    if (ani == null)
                    {
                        break;
                    }

                    for (float tick = 0; tick < ani.DurationInTicks; tick++)
                    {
                        try
                        {
                            viewport3D.Dispatcher.Invoke(() =>
                            {
                                var transform = viewport3D.Children[2].Transform as Transform3DGroup;
                                var model     = viewport3D.Children[2] as ModelVisual3D;
                                var aniModel  = assimpAnimation.GetCache(tick);
                                model.Content = aniModel.Clone();

                                if (replaceCamera)
                                {
                                    viewport3D.Children[2].Transform = GetTransform(assimpAnimation.Bounds, new AxisAngleRotation3D());

                                    LightCamera.Lookat(new Point3D(0, 0, 0), 64);

                                    replaceCamera = false;
                                }
                            });
                        }
                        catch (Exception ex)
                        {
                        }

                        Thread.Sleep((int)1000 / (int)ani.TicksPerSecond);
                    }
                }
            });

            aniThread.Start();
        }
Exemplo n.º 2
0
        private void RenderMesh(Rotation3D rotation3D)
        {
            MeshGroup.Children.Clear();

            if (!aiScene.HasMeshes)
            {
                return;
            }

            var material = new DiffuseMaterial(new SolidColorBrush(Colors.SandyBrown));

            material.AmbientColor = Colors.SaddleBrown;

            foreach (var aiMesh in aiScene.Meshes)
            {
                var geoModel = new GeometryModel3D();
                geoModel.Material = material;
                //geoModel.BackMaterial = material;
                var mesh = new MeshGeometry3D();

                aiMesh.Vertices.ForEach(m =>
                {
                    mesh.Positions.Add(new Point3D(m.X, m.Y, m.Z));
                });

                aiMesh.Normals.ForEach(m =>
                {
                    mesh.Normals.Add(new System.Windows.Media.Media3D.Vector3D(m.X, m.Y, m.Z));
                });

                aiMesh.Faces.ForEach(m =>
                {
                    mesh.TriangleIndices.Add(m.Indices[0]);
                    mesh.TriangleIndices.Add(m.Indices[1]);
                    mesh.TriangleIndices.Add(m.Indices[2]);

                    if (m.IndexCount == 4)
                    {
                        mesh.TriangleIndices.Add(m.Indices[2]);
                        mesh.TriangleIndices.Add(m.Indices[3]);
                        mesh.TriangleIndices.Add(m.Indices[0]);
                    }
                });

                geoModel.Geometry = mesh;
                MeshGroup.Children.Add(geoModel);
            }

            var model = new ModelVisual3D();

            model.Content = MeshGroup;
            viewport3D.Children.Add(model);
            viewport3D.Children[2].Transform = GetTransform(MeshGroup.Bounds, rotation3D);

            LightCamera.Lookat(new Point3D(0, 0, 0), 64);
        }