/// <summary> /// Update thumbnail to represent contained model. /// </summary> private void UpdateThumbnailTexture(ref Thumbnails thumb) { // Get dimensions int thumbWidth = thumbBackgroundTexture.Width; int thumbHeight = thumbBackgroundTexture.Height; // Get model if (thumb.model == null) { // Load model thumb.model = host.ModelManager.GetModelData(thumb.key); // Load texture for each submesh. for (int sm = 0; sm < thumb.model.SubMeshes.Length; sm++) { // Load textures thumb.model.SubMeshes[sm].TextureKey = host.TextureManager.LoadTexture( thumb.model.DFMesh.SubMeshes[sm].TextureArchive, thumb.model.DFMesh.SubMeshes[sm].TextureRecord, TextureManager.TextureCreateFlags.MipMaps | TextureManager.TextureCreateFlags.PowerOfTwo); } // Centre model Vector3 Min = thumb.model.BoundingBox.Min; Vector3 Max = thumb.model.BoundingBox.Max; float transX = (float)(Min.X + ((Max.X - Min.X) / 2)); float transY = (float)(Min.Y + ((Max.Y - Min.Y) / 2)); float transZ = (float)(Min.Z + ((Max.Z - Min.Z) / 2)); Matrix matrix = Matrix.CreateTranslation(-transX, -transY, -transZ); // Rotate model matrix *= Matrix.CreateRotationY(MathHelper.ToRadians(45)); matrix *= Matrix.CreateRotationX(MathHelper.ToRadians(10)); // Scale model Vector3 size = new Vector3(Max.X - Min.X, Max.Y - Min.Y, Max.Z - Min.Z); float scale = 400.0f / (float)((size.X + size.Y + size.Z) / 3); matrix *= Matrix.CreateScale(scale); // Apply matrix to model thumb.model = host.ModelManager.TransformModelData(ref thumb.model, matrix); // Store matrix thumb.matrix = matrix; } // Create projection matrix float aspectRatio = (float)thumb.rect.Width / (float)thumb.rect.Height; projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspectRatio, nearPlaneDistance, farPlaneDistance); // Set world matrix effect.World = Matrix.CreateRotationY(MathHelper.ToRadians(thumb.rotation)); // Create texture to use as render target RenderTarget2D renderTarget; renderTarget = new RenderTarget2D( host.GraphicsDevice, thumbWidth, thumbHeight, false, SurfaceFormat.Color, DepthFormat.Depth16); host.GraphicsDevice.SetRenderTarget(renderTarget); // Render thumbnail components host.GraphicsDevice.Clear(thumbViewBackgroundColor); host.Core.SpriteBatch.Begin(); host.Core.SpriteBatch.Draw(thumbBackgroundTexture, new Rectangle(0, 0, thumbWidth, thumbHeight), Color.White); host.Core.SpriteBatch.End(); DrawSingleModel(ref thumb.model); // Restore default render target host.GraphicsDevice.SetRenderTarget(null); // Store updated values thumb.texture = TextureManager.CloneRenderTarget2DToTexture2D(host.GraphicsDevice, renderTarget); }