本文整理汇总了C#中Mesh.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# Mesh.Dispose方法的具体用法?C# Mesh.Dispose怎么用?C# Mesh.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Mesh
的用法示例。
在下文中一共展示了Mesh.Dispose方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Release
public void Release(Mesh mesh)
{
if (refcount[mesh]-- == 1)
{
float[] hash = hashes[mesh];
hashes.Remove(mesh);
cache.Remove(hash);
refcount.Remove(mesh);
mesh.Dispose();
nreleased++;
}
}
示例2: Create
/// <summary>
/// Creates a new mesh
/// </summary>
/// <param name="device">The device used to create the mesh</param>
/// <param name="filename">the file to load</param>
public void Create(Device device, string filename)
{
worldPosition = new WorldPosition();
GraphicsStream adjacencyBuffer;
ExtendedMaterial[] Mat;
this.device = device;
if (device != null) {
device.DeviceLost += new System.EventHandler(this.InvalidateDeviceObjects);
device.Disposing += new System.EventHandler(this.InvalidateDeviceObjects);
device.DeviceReset += new System.EventHandler(this.RestoreDeviceObjects);
}
filename = MediaUtilities.FindFile(filename);
// Load the mesh
systemMemoryMesh = Mesh.FromFile(filename, MeshFlags.SystemMemory, device, out adjacencyBuffer, out Mat);
Mesh tempMesh = null;
string errorString;
tempMesh = Mesh.Clean(CleanType.Optimization, systemMemoryMesh, adjacencyBuffer, adjacencyBuffer, out errorString);
if (tempMesh != systemMemoryMesh) {
systemMemoryMesh.Dispose();
systemMemoryMesh = tempMesh;
}
// Optimize the mesh for performance
MeshFlags flags = MeshFlags.OptimizeCompact | MeshFlags.OptimizeAttributeSort | MeshFlags.OptimizeVertexCache;
systemMemoryMesh.OptimizeInPlace(flags, adjacencyBuffer);
adjacencyBuffer.Close();
adjacencyBuffer = null;
// Setup bounding volumes
VertexBuffer vb = systemMemoryMesh.VertexBuffer;
GraphicsStream vertexData = vb.Lock(0, 0, LockFlags.ReadOnly);
boundingSphere.Radius = Geometry.ComputeBoundingSphere(vertexData,systemMemoryMesh.NumberVertices,systemMemoryMesh.VertexFormat, out boundingSphere.CenterPoint);
vb.Unlock();
vb.Dispose();
textures = new Texture[Mat.Length];
materials = new Direct3D.Material[Mat.Length];
for (int i=0; i<Mat.Length; i++) {
materials[i] = Mat[i].Material3D;
// Set the ambient color for the material (D3DX does not do this)
materials[i].Ambient = materials[i].Diffuse;
if (Mat[i].TextureFilename != null) {
// Create the texture
textures[i] = TextureLoader.FromFile(device, MediaUtilities.FindFile(Mat[i].TextureFilename));
}
}
RestoreDeviceObjects(device, null);
}
示例3: SafeRelease
/// <summary>
/// Releases an unmanaged resources of a mesh
/// </summary>
/// <param name="mesh"></param>
protected void SafeRelease(ref Mesh mesh)
{
if(mesh != null)
{
mesh.Dispose();
mesh = null;
}
}
示例4: RenderSoftBodyTextured
public void RenderSoftBodyTextured(SoftBody softBody)
{
if (!(softBody.UserObject is Array))
return;
object[] userObjArr = softBody.UserObject as object[];
FloatArray vertexBuffer = userObjArr[0] as FloatArray;
IntArray indexBuffer = userObjArr[1] as IntArray;
int vertexCount = (vertexBuffer.Count / 8);
if (vertexCount > 0)
{
int faceCount = indexBuffer.Count / 2;
bool index32 = vertexCount > 65536;
Mesh mesh = new Mesh(device, faceCount, vertexCount,
MeshFlags.SystemMemory | (index32 ? MeshFlags.Use32Bit : 0),
VertexFormat.Position | VertexFormat.Normal | VertexFormat.Texture1);
DataStream indices = mesh.LockIndexBuffer(LockFlags.Discard);
if (index32)
{
foreach (int i in indexBuffer)
indices.Write(i);
}
else
{
foreach (int i in indexBuffer)
indices.Write((short)i);
}
mesh.UnlockIndexBuffer();
DataStream verts = mesh.LockVertexBuffer(LockFlags.Discard);
foreach (float f in vertexBuffer)
verts.Write(f);
mesh.UnlockVertexBuffer();
mesh.ComputeNormals();
mesh.DrawSubset(0);
mesh.Dispose();
}
}
示例5: MeshClass
/// <summary>
/// For creating shape objects
/// </summary>
/// <param name="type">the name of the object you wish to create</param>
public MeshClass(MeshType type = MeshType.Cube)
{
if (type == MeshType.Cube)
{
ObjectMesh = Mesh.CreateBox(Engine.GameEngine.LocalDevice.ThisDevice, 1f, 1f, 1f);
ObjectMesh.ComputeNormals();
ObjectMesh.Optimize(MeshOptimizeFlags.Compact);
ApplyColor(Color.White);
}
else if (type == MeshType.Sphere)
{
ObjectMesh = Mesh.CreateSphere(Engine.GameEngine.LocalDevice.ThisDevice, .1f, 10, 10);
ObjectMesh.ComputeNormals();
ObjectMesh.Optimize(MeshOptimizeFlags.Compact);
ApplyColor(Color.White);
}
else if (type == MeshType.Teapot)
{
ObjectMesh = Mesh.CreateTeapot(Engine.GameEngine.LocalDevice.ThisDevice);
ObjectMesh.ComputeNormals();
ObjectMesh.OptimizeInPlace(MeshOptimizeFlags.Compact);
ApplyColor(Color.White);
}
else if (type == MeshType.Triangle)
{
VertexPositionColor[] ShapeVertices = new VertexPositionColor[] {
new VertexPositionColor() { Color = Color.White.ToArgb(), Position = new Vector3(-1f, 0f, 1f) },
new VertexPositionColor() { Color = Color.White.ToArgb(), Position = new Vector3(1f, 0f, 1f) },
new VertexPositionColor() { Color = Color.White.ToArgb(), Position = new Vector3(-1f, 0f, -1f) },
new VertexPositionColor() { Color = Color.White.ToArgb(), Position = new Vector3(1f, 0f, -1f) },
new VertexPositionColor() { Color = Color.White.ToArgb(), Position = new Vector3(0f, 1f, 0f) },
};
var ShapeIndices = new short[] {
0, 2, 1, // base
1, 2, 3,
0, 1, 4, // sides
1, 3, 4,
3, 2, 4,
2, 0, 4,
};
ObjectMesh = new Mesh(Engine.GameEngine.LocalDevice.ThisDevice, ShapeIndices.Length, ShapeVertices.Length, MeshFlags.Managed, VertexPositionColor.Format);
ObjectMesh.LockVertexBuffer(LockFlags.None).WriteRange<VertexPositionColor>(ShapeVertices);
ObjectMesh.UnlockVertexBuffer();
ObjectMesh.LockIndexBuffer(LockFlags.None).WriteRange<short>(ShapeIndices);
ObjectMesh.UnlockIndexBuffer();
Mesh other = ObjectMesh.Clone(Engine.GameEngine.LocalDevice.ThisDevice, MeshFlags.Managed, ObjectMesh.VertexFormat | VertexFormat.Normal | VertexFormat.Texture2);
ObjectMesh.Dispose();
ObjectMesh = null;
//other.ComputeNormals();
ObjectMesh = other.Clone(Engine.GameEngine.LocalDevice.ThisDevice, MeshFlags.Managed, other.VertexFormat);
other.Dispose();
ObjectMesh.Optimize(MeshOptimizeFlags.Compact);
ApplyColor(Color.White);
}
ObjectPosition = Vector3.Zero;
ObjectRotate = Vector3.Zero;
ObjectScale = new Vector3(1, 1, 1);
_world = Matrix.Translation(ObjectPosition);
IsShapeObject = true;
}
示例6: BasicSetup
//.........这里部分代码省略.........
subsets[i].data.gformat=desc.Format;
} else subsets[i].data.gWidth=-1;
stream=BSAArchive.GetNormalTexture(texFileName);
if(stream!=null) {
subsets[i].normalMap=TextureLoader.FromStream(device, stream);
SurfaceDescription desc=subsets[i].normalMap.GetLevelDescription(0);
subsets[i].data.nWidth=desc.Width;
subsets[i].data.nHeight=desc.Height;
subsets[i].data.nformat=desc.Format;
} else subsets[i].data.nWidth=-1;
} else {
subsets[i].colorMap=null;
subsets[i].data.cWidth=-1;
subsets[i].data.gWidth=-1;
subsets[i].data.nWidth=-1;
}
//Get vertex information
VertexElement[] decl=new VertexElement[7];
decl[0]=new VertexElement(0, 0, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Position, 0);
decl[1]=new VertexElement(0, 12, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Tangent, 0);
decl[2]=new VertexElement(0, 24, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.BiNormal, 0);
decl[3]=new VertexElement(0, 36, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Normal, 0);
decl[4]=new VertexElement(0, 48, DeclarationType.Float2, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 0);
decl[5]=new VertexElement(0, 56, DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.Color, 0);
decl[6]=VertexElement.VertexDeclarationEnd;
subsets[i].vDecl=new VertexDeclaration(device, decl);
if(subsets[i].info.containsTangentBinormal) {
Mesh m=new Mesh(subsets[i].info.iCount/3, subsets[i].info.vCount, MeshFlags.SystemMemory, decl, device);
subsets[i].vBuffer=new VertexBuffer(typeof(ConvertedVertex), subsets[i].info.vCount, device, Usage.WriteOnly, VertexFormats.None, Pool.Managed);
//ConvertedVertex[] cv=(ConvertedVertex[])subsets[i].vBuffer.Lock(0, LockFlags.None);
ConvertedVertex[] cv=(ConvertedVertex[])m.LockVertexBuffer(typeof(ConvertedVertex), LockFlags.None, subsets[i].info.vCount);
fixed(ConvertedVertex* cvPtr = cv) {
GetVerticies(i, cvPtr);
}
m.UnlockVertexBuffer();
//subsets[i].vBuffer.Unlock();
//Indicies
subsets[i].iBuffer=new IndexBuffer(typeof(ushort), subsets[i].info.iCount, device, Usage.WriteOnly, Pool.Managed);
//ushort[] indicies=(ushort[])subsets[i].iBuffer.Lock(0, LockFlags.None);
ushort[] indicies=(ushort[])m.LockIndexBuffer(typeof(ushort), LockFlags.None, subsets[i].info.iCount);
fixed(ushort* iPtr = indicies) {
GetIndicies(i, iPtr);
}
//subsets[i].iBuffer.Unlock();
m.UnlockIndexBuffer();
subsets[i].numTris=subsets[i].info.iCount/3;
int[] adj=new int[subsets[i].info.iCount];
m.GenerateAdjacency(0.01f, adj);
m.ComputeTangent(0, 0, 0, 1, adj);
//m.ComputeTangentFrame(TangentOptions.CalculateNormals|TangentOptions.GenerateInPlace);
cv=(ConvertedVertex[])m.LockVertexBuffer(typeof(ConvertedVertex), LockFlags.ReadOnly, subsets[i].info.vCount);
ConvertedVertex[] cv2=(ConvertedVertex[])subsets[i].vBuffer.Lock(0, LockFlags.None);
for(int j=0;j<cv.Length;j++) {
cv2[j]=cv[j];
}
m.UnlockVertexBuffer();
subsets[i].vBuffer.Unlock();
indicies=(ushort[])m.LockIndexBuffer(typeof(ushort), LockFlags.None, subsets[i].info.iCount);
ushort[] indicies2=(ushort[])subsets[i].iBuffer.Lock(0, LockFlags.None);
for(int j=0;j<indicies.Length;j++) {
indicies2[j]=indicies[j];
}
m.UnlockIndexBuffer();
subsets[i].iBuffer.Unlock();
m.Dispose();
} else {
subsets[i].vBuffer=new VertexBuffer(typeof(ConvertedVertex), subsets[i].info.vCount, device, Usage.WriteOnly, VertexFormats.None, Pool.Managed);
ConvertedVertex[] cv=(ConvertedVertex[])subsets[i].vBuffer.Lock(0, LockFlags.None);
fixed(ConvertedVertex* cvPtr = cv) {
GetVerticies(i, cvPtr);
}
subsets[i].vBuffer.Unlock();
//Indicies
subsets[i].iBuffer=new IndexBuffer(typeof(ushort), subsets[i].info.iCount, device, Usage.WriteOnly, Pool.Managed);
ushort[] indicies=(ushort[])subsets[i].iBuffer.Lock(0, LockFlags.None);
fixed(ushort* iPtr = indicies) {
GetIndicies(i, iPtr);
}
subsets[i].iBuffer.Unlock();
subsets[i].numTris=subsets[i].info.iCount/3;
}
}
int alphaCount=0;
for(int i=0;i<Subsets;i++) if(subsets[i].info.hasalpha) alphaCount++;
AlphaIndicies=new int[alphaCount];
alphaCount=0;
for(int i=0;i<Subsets;i++) if(subsets[i].info.hasalpha) AlphaIndicies[alphaCount++]=i;
}
示例7: CanUpsample
// try new mesh size (this way is rather ugly)
private bool CanUpsample()
{
int newWidth = heightMap.Width*2-1;
int newHeight = heightMap.Height*2-1;
int newVerticesLength = newWidth * newHeight;
int newIndicesLength = (newWidth-1) * (newHeight-1) * 6;
Mesh newMesh = null;
try
{
newMesh = new Mesh(newIndicesLength / 3, newVerticesLength,
0, CustomVertex.PositionNormalTextured.Format, device);
}
catch
{
return false;
}
newMesh.Dispose();
return true;
}
示例8: LoadMesh
public void LoadMesh(string Meshname)
{
string sModel;
List<ExtendedMaterial> mtrlBuffer = new List<ExtendedMaterial>();
string sTemp;
sModel = OuterSpace.meshdir + Meshname;
if (File.Exists(sModel))
{
ExtendedMaterial[] mtrlArray = mtrlBuffer.ToArray();
moMesh = Mesh.FromFile(sModel, MeshFlags.Managed, OuterSpace.device, out adj, out mtrlArray);
mtrlBuffer.InsertRange(0, mtrlArray);
if ((moMesh.VertexFormat & VertexFormats.Normal) != VertexFormats.Normal)
{
Mesh tempMesh = moMesh.Clone(moMesh.Options.Value, moMesh.VertexFormat |
VertexFormats.Normal, OuterSpace.device);
tempMesh.ComputeNormals();
moMesh.Dispose();
moMesh = tempMesh;
}
// now initialize our materials and textures
mlMeshMaterials = mtrlBuffer.Count;
materialsList.Clear();
texturesList.Clear();
// Now load our textures and materials
for (int x = 0; x < mtrlBuffer.Count; x++)
{
Material mtrl = new Material();// materialsList[x];
mtrl = mtrlBuffer[x].Material3D;
mtrl.Ambient = mtrl.Diffuse; // because directx sometimes doesnt do this for you
materialsList.Insert(x, mtrl);
if (mtrlBuffer[x].TextureFilename != String.Empty)
{
sTemp = OuterSpace.meshdir + mtrlBuffer[x].TextureFilename;
texturesList.Insert(x, TextureLoader.FromFile(OuterSpace.device, sTemp));
}
}
havemesh = true;
}
else
havemesh = false;
}
示例9: ConvertPosOnlyVertexToPosNormCol
//Converts the positiononly vertices generated from the Mesh.?? creation functions to be positionnomralcolored instead.
public static void ConvertPosOnlyVertexToPosNormCol(Microsoft.DirectX.Direct3D.Device d3dDevice, ref Mesh meshOriginal)
{
Mesh pTempMesh = meshOriginal.Clone(meshOriginal.Options.Value, meshOriginal.VertexFormat | VertexFormats.Normal | VertexFormats.Diffuse, d3dDevice);
pTempMesh.ComputeNormals();
meshOriginal.Dispose();
meshOriginal = pTempMesh;
}
示例10: MeshClass
/// <summary>
/// For creating shape objects
/// </summary>
/// <param name="type">the name of the object you wish to create</param>
public MeshClass(MeshType type)
{
if (type == MeshType.Cube)
{
objectMesh = Mesh.CreateBox(DeviceManager.LocalDevice, 1f, 1f, 1f);
objectMesh.ComputeNormals();
objectMesh.Optimize(MeshOptimizeFlags.Compact);
ApplyColor(Color.White);
}
else if (type == MeshType.Triangle)
{
var ShapeVertices = new CustomVertex.VertexPositionColor[] {
new CustomVertex.VertexPositionColor() { Color = Color.White.ToArgb(), Position = new Vector3(-1f, 0f, 1f) },
new CustomVertex.VertexPositionColor() { Color = Color.White.ToArgb(), Position = new Vector3(1f, 0f, 1f) },
new CustomVertex.VertexPositionColor() { Color = Color.White.ToArgb(), Position = new Vector3(-1f, 0f, -1f) },
new CustomVertex.VertexPositionColor() { Color = Color.White.ToArgb(), Position = new Vector3(1f, 0f, -1f) },
new CustomVertex.VertexPositionColor() { Color = Color.White.ToArgb(), Position = new Vector3(0f, 1f, 0f) },
};
var ShapeIndices = new short[] {
0, 2, 1, // base
1, 2, 3,
0, 1, 4, // sides
1, 3, 4,
3, 2, 4,
2, 0, 4,
};
objectMesh = new Mesh(DeviceManager.LocalDevice, ShapeIndices.Length, ShapeVertices.Length, MeshFlags.Managed, VertexFormat.Position | VertexFormat.Diffuse);
objectMesh.LockVertexBuffer(LockFlags.None).WriteRange<CustomVertex.VertexPositionColor>(ShapeVertices);
objectMesh.UnlockVertexBuffer();
objectMesh.LockIndexBuffer(LockFlags.None).WriteRange<short>(ShapeIndices);
objectMesh.UnlockIndexBuffer();
Mesh other = objectMesh.Clone(DeviceManager.LocalDevice, MeshFlags.Managed, objectMesh.VertexFormat | VertexFormat.Normal | VertexFormat.Texture2);
objectMesh.Dispose();
objectMesh = null;
other.ComputeNormals();
objectMesh = other.Clone(DeviceManager.LocalDevice, MeshFlags.Managed, other.VertexFormat);
other.Dispose();
objectMesh.Optimize(MeshOptimizeFlags.Compact);
}
ObjectPosition = Vector3.Zero;
ObjectRotate = Vector3.Zero;
ObjectScale = new Vector3(1, 1, 1);
world = Matrix.Translation(ObjectPosition);
Name = type.ToString();
IsShapeObject = true;
}