📄 dxmutmesh.cs
字号:
//--------------------------------------------------------------------------------------
// File: DXMUTMesh.cs
//
// Support code for loading DirectX .X files.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
using System;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace Microsoft.Samples.DirectX.UtilityToolkit
{
/// <summary>Class for loading and rendering file-based meshes</summary>
public sealed class FrameworkMesh : IDisposable
{
#region Instance Data
private string meshFileName;
private Mesh systemMemoryMesh = null; // System Memory mesh, lives through a resize
private Mesh localMemoryMesh = null; // Local mesh, rebuilt on resize
private Material[] meshMaterials = null; // Materials for the mesh
private BaseTexture[] meshTextures = null; // Textures for the mesh
private bool isUsingMeshMaterials = true; // Should the mesh be rendered with the materials
/// <summary>Returns the system memory mesh</summary>
public Mesh SystemMesh { get { return systemMemoryMesh; } }
/// <summary>Returns the local memory mesh</summary>
public Mesh LocalMesh { get { return localMemoryMesh; } }
/// <summary>Should the mesh be rendered with materials</summary>
public bool IsUsingMaterials{ get { return isUsingMeshMaterials; } set { isUsingMeshMaterials = value; } }
/// <summary>Number of materials in mesh</summary>
public int NumberMaterials { get { return meshMaterials.Length; } }
/// <summary>Gets a texture from the mesh</summary>
public BaseTexture GetTexture(int index) { return meshTextures[index]; }
/// <summary>Gets a material from the mesh</summary>
public Material GetMaterial(int index) { return meshMaterials[index]; }
#endregion
#region Creation
/// <summary>Create a new mesh using this file</summary>
public FrameworkMesh(Device device, string name)
{
meshFileName = name;
Create(device, meshFileName);
}
/// <summary>Create a new mesh</summary>
public FrameworkMesh() : this(null, "FrameworkMeshFile_Mesh") {}
/// <summary>Create the mesh data</summary>
public void Create(Device device, string name)
{
// Hook the device events
System.Diagnostics.Debug.Assert(device != null, "Device should not be null.");
device.DeviceLost += new EventHandler(OnLostDevice);
device.DeviceReset += new EventHandler(OnResetDevice);
device.Disposing += new EventHandler(OnDeviceDisposing);
GraphicsStream adjacency; // Adjacency information
ExtendedMaterial[] materials; // Mesh material information
// First try to find the filename
string path = string.Empty;
try
{
path = Utility.FindMediaFile(name);
}
catch(MediaNotFoundException)
{
// The media was not found, maybe a full path was passed in?
if (System.IO.File.Exists(name))
{
path = name;
}
else
{
// No idea what this is trying to find
throw new MediaNotFoundException();
}
}
// Now load the mesh
systemMemoryMesh = Mesh.FromFile(path, MeshFlags.SystemMemory, device, out adjacency,
out materials);
using (adjacency)
{
// Optimize the mesh for performance
systemMemoryMesh.OptimizeInPlace(MeshFlags.OptimizeVertexCache | MeshFlags.OptimizeCompact |
MeshFlags.OptimizeAttributeSort, adjacency);
// Find the folder of where the mesh file is located
string folder = Utility.AppendDirectorySeparator(new System.IO.FileInfo(path).DirectoryName);
// Create the materials
CreateMaterials(folder, device, adjacency, materials);
}
// Finally call reset
OnResetDevice(device, EventArgs.Empty);
}
// TODO: Create with XOF
/// <summary>Create the materials for the mesh</summary>
public void CreateMaterials(string folder, Device device, GraphicsStream adjacency, ExtendedMaterial[] materials)
{
// Does the mesh have materials?
if ((materials != null) && (materials.Length > 0))
{
// Allocate the arrays for the materials
meshMaterials = new Material[materials.Length];
meshTextures = new BaseTexture[materials.Length];
// Copy each material and create it's texture
for(int i = 0; i < materials.Length; i++)
{
// Copy the material first
meshMaterials[i] = materials[i].Material3D;
// Is there a texture for this material?
if ((materials[i].TextureFilename == null) || (materials[i].TextureFilename.Length == 0) )
continue; // No, just continue now
ImageInformation info = new ImageInformation();
string textureFile = folder + materials[i].TextureFilename;
try
{
// First look for the texture in the same folder as the input folder
info = TextureLoader.ImageInformationFromFile(textureFile);
}
catch
{
try
{
// Couldn't find it, look in the media folder
textureFile = Utility.FindMediaFile(materials[i].TextureFilename);
info = TextureLoader.ImageInformationFromFile(textureFile);
}
catch (MediaNotFoundException)
{
// Couldn't find it anywhere, skip it
continue;
}
}
switch (info.ResourceType)
{
case ResourceType.Textures:
meshTextures[i] = TextureLoader.FromFile(device, textureFile);
break;
case ResourceType.CubeTexture:
meshTextures[i] = TextureLoader.FromCubeFile(device, textureFile);
break;
case ResourceType.VolumeTexture:
meshTextures[i] = TextureLoader.FromVolumeFile(device, textureFile);
break;
}
}
}
}
#endregion
#region Class Methods
/// <summary>Updates the mesh to a new vertex format</summary>
public void SetVertexFormat(Device device, VertexFormats format)
{
Mesh tempSystemMesh = null;
Mesh tempLocalMesh = null;
VertexFormats oldFormat = VertexFormats.None;
using(systemMemoryMesh)
{
using (localMemoryMesh)
{
// Clone the meshes
if (systemMemoryMesh != null)
{
oldFormat = systemMemoryMesh.VertexFormat;
tempSystemMesh = systemMemoryMesh.Clone(systemMemoryMesh.Options.Value,
format, device);
}
if (localMemoryMesh != null)
{
tempLocalMesh = localMemoryMesh.Clone(localMemoryMesh.Options.Value,
format, device);
}
}
}
// Store the new meshes
systemMemoryMesh = tempSystemMesh;
localMemoryMesh = tempLocalMesh;
// Compute normals if they are being requested and the old mesh didn't have them
if ( ((oldFormat & VertexFormats.Normal) == 0) && (format != 0) )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -