public void UpdateLightCamera() { Vector3 forward = MathHelper.Forward; Vector3 targetPlaneX = MathHelper.Right; Vector3 targetPlaneY = MathHelper.Up; var world = Matrix.Identity; world *= Matrix.Scaling(new Vector3(LightRadius, LightRadius, LightRadius)); world *= Matrix.Scaling((float)Math.Tan(SpotLightAngle) * (targetPlaneX + targetPlaneY) + forward); Vector3 up = MathHelper.Up; if (Math.Abs(Vector3.Dot(up, SpotDirection)) > 0.999) { up = MathHelper.Right; } world *= Matrix.Invert(Matrix.LookAtRH(Vector3.Zero, -SpotDirection, up));//world *= Matrix.CreateWorld(Vector3.Zero, -SpotDirection, up); world *= Matrix.Translation(LightPosition); Vector3 scale, translation; Quaternion rotation; world.Decompose(out scale, out rotation, out translation); Matrix view = Matrix.Invert(world); Matrix projection = Matrix.PerspectiveFovRH(MathHelper.PiOver2, 1, 0.01f, 1); //TODO: fix field of view? LightCamera.SetViewProjectionMatrix(view, projection); LightCamera.NearClip = 0.01f; LightCamera.FarClip = 1; }
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(); }
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); }