public void UnloadLevel()
 {
     ModelView.Children.Remove(ModelViewer);
     ModelViewer.Children.Clear();
     ImportedModels = new Dictionary <string, Model3D>();
     Models         = new Dictionary <dynamic, ModelVisual3D>();
     Positions      = new Dictionary <dynamic, Vector3D>();
     ModelViewer    = new SortingVisual3D();
     ModelViewer.SortingFrequency = 0.5;
     ModelView.Children.Add(ModelViewer);
     ModelView.Camera.LookAt(new Point3D(0, 0, 0), 50, 1000);
     CameraTarget = new Vector3D(0, 0, 0);
 }
示例#2
0
 public void UnloadLevel()
 {
     ModelView.Children.Remove(ModelViewer);
     ModelViewer.Children.Clear();
     //ImportedModels = new Dictionary<string, Model3D>();
     Models      = new Dictionary <string, List <ModelVisual3D> >();
     Positions   = new Dictionary <string, List <Vector3D> >();
     ModelViewer = new SortingVisual3D();
     ModelViewer.SortingFrequency = 0.5;
     ModelView.Children.Add(ModelViewer);
     AddKey("TmpChildrenObjs");
     AddKey("SelectedRail");
     AddKey("TmpAreaChildrenObjs");
     AddKey("C0EditingListObjs");
     AddKey("SelectionLayer");
     ModelView.Camera.LookAt(new Point3D(0, 0, 0), 50, 1000);
     CameraTarget = new Vector3D(0, 0, 0);
 }
        /// <summary>
        /// Gets the thumbnail image
        /// </summary>
        /// <param name="width">The width of the image that should be returned.</param>
        /// <returns>
        /// The image for the thumbnail
        /// </returns>
        protected override Bitmap GetThumbnailImage(uint width)
        {
            //  Attempt to open the stream with a reader
            var pmx = PMXLoaderScript.Import(SelectedItemPath);

            MeshCreationInfo creation_info = CreateMeshCreationInfoSingle(pmx);

            var diffuseMat = MaterialHelper.CreateMaterial(Colors.Gray);

            var models = new Model3DGroup();

            for (int i = 0, i_max = creation_info.value.Length; i < i_max; ++i)
            {
                int[] indices = creation_info.value[i].plane_indices.Select(x => (int)creation_info.reassign_dictionary[x])
                                .ToArray();
                var mesh = new MeshGeometry3D
                {
                    Positions = new Point3DCollection(pmx.vertex_list.vertex.Select(x => x.pos)),

                    TextureCoordinates = new PointCollection(pmx.vertex_list.vertex.Select(x => new System.Windows.Point(x.uv.X, x.uv.Y)))
                };



                indices.ToList()
                .ForEach(x => mesh.TriangleIndices.Add(x));
                var textureIndex = pmx.material_list.material[creation_info.value[i].material_index].usually_texture_index;
                var texturePath  = pmx.texture_list.texture_file.ElementAtOrDefault((int)textureIndex);

                var material = diffuseMat;
                if (!string.IsNullOrWhiteSpace(texturePath))
                {
                    texturePath = Path.Combine(Path.GetDirectoryName(SelectedItemPath), texturePath);
                    //Log($"Texture found: {texturePath}");

                    if (!string.IsNullOrWhiteSpace(texturePath) && File.Exists(texturePath))
                    {
                        //  dds and tga
                        if (new string[] { ".dds", ".tga" }.Any(x => x.Equals(Path.GetExtension(texturePath))))
                        {
                            var bitmap = PFimToBitmap(texturePath);
                            material = MaterialHelper.CreateImageMaterial(Bitmap2BitmapImage(bitmap), 1);
                        }
                        else
                        {
                            material = MaterialHelper.CreateImageMaterial(BitmapImageFromFile(texturePath), 1);
                        }
                    }
                }

                models.Children.Add(new GeometryModel3D(mesh, material));
            }

            var sorting = new SortingVisual3D()
            {
                Content = models
            };

            var view = new HelixViewport3D();

            view.Children.Add(sorting);
            view.Camera.Position      = new Point3D(0, 15, -30);
            view.Camera.LookDirection = new Vector3D(0, -5, 30);

            view.Background = System.Windows.Media.Brushes.Transparent;

            view.Children.Add(new SunLight()
            {
                Altitude = 260
            });

            view.Children.Add(new DefaultLights());

            try
            {
                var bitmap = view.Viewport.RenderBitmap(width, width, new SolidColorBrush(Colors.Transparent));
                view.Children.Clear();
                view    = null;
                sorting = null;
                models  = null;
                GC.Collect();
                return(BitmapFromSource(bitmap));
            }
            catch (Exception exception)
            {
                view.Children.Clear();
                view    = null;
                sorting = null;
                models  = null;
                GC.Collect();
                LogError("An exception occurred Rendering bitmap.", exception);
                //MessageBox.Show(exception.Message);
                //MessageBox.Show(exception.StackTrace);
                return(null);
            }
        }