本文整理汇总了C#中Mesh.OptimizeInPlace方法的典型用法代码示例。如果您正苦于以下问题:C# Mesh.OptimizeInPlace方法的具体用法?C# Mesh.OptimizeInPlace怎么用?C# Mesh.OptimizeInPlace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Mesh
的用法示例。
在下文中一共展示了Mesh.OptimizeInPlace方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateMesh
private Mesh CreateMesh(aiMesh aiMesh, out Vector3 min, out Vector3 max)
{
var numFaces = (int) aiMesh.mNumFaces;
var numVertices = (int) aiMesh.mNumVertices;
var dxMesh = new Mesh(numFaces, numVertices, MeshFlags.Managed | MeshFlags.Use32Bit,
CustomVertex.PositionNormalTextured.Format, device);
var aiPositions = aiMesh.mVertices;
var aiNormals = aiMesh.mNormals;
var aiTextureCoordsAll = aiMesh.mTextureCoords;
var aiTextureCoords = (aiTextureCoordsAll!=null) ? aiTextureCoordsAll[0] : null;
var dxVertices = new CustomVertex.PositionNormalTextured[numVertices];
for (int i = 0; i < numVertices; ++i) {
dxVertices[i].Position = aiPositions[i].ToVector3();
if (aiNormals != null) {
dxVertices[i].Normal = aiNormals[i].ToVector3();
}
if (aiTextureCoords != null) {
var uv = aiTextureCoords[i];
dxVertices[i].Tu = uv.x;
dxVertices[i].Tv = uv.y;
}
}
dxMesh.VertexBuffer.SetData(dxVertices, 0, LockFlags.None);
var aiFaces = aiMesh.mFaces;
var dxIndices = new uint[numFaces * 3];
for (int i = 0; i < numFaces; ++i) {
var aiFace = aiFaces[i];
var aiIndices = aiFace.mIndices;
for (int j = 0; j < 3; ++j) {
dxIndices[i * 3 + j] = aiIndices[j];
}
}
dxMesh.IndexBuffer.SetData(dxIndices, 0, LockFlags.None);
var dxAttributes = dxMesh.LockAttributeBufferArray(LockFlags.None);
// TODO: Set face material index for attributes
dxMesh.UnlockAttributeBuffer(dxAttributes);
var adjacency = new int[numFaces * 3];
dxMesh.GenerateAdjacency(0.0f, adjacency);
dxMesh.OptimizeInPlace(MeshFlags.OptimizeAttributeSort, adjacency);
Geometry.ComputeBoundingBox(dxVertices, CustomVertex.PositionNormalTextured.StrideSize, out min, out max);
return dxMesh;
}
示例2: LoadContent
private void LoadContent()
{
try
{
PresentParameters presentParams = new PresentParameters();
presentParams.BackBufferHeight = pictureBox.Height;
presentParams.BackBufferWidth = pictureBox.Width;
presentParams.BackBufferFormat = Format.A8R8G8B8;
presentParams.DeviceWindowHandle = pictureBox.Handle;
_direct3D = new Direct3D();
_graphigsDevice = new Device(_direct3D, 0, DeviceType.Hardware, pictureBox.Handle, CreateFlags.HardwareVertexProcessing, presentParams);
_camera.Reset(pictureBox.Width, pictureBox.Height);
_graphigsDevice.SetRenderState(RenderState.AlphaBlendEnable, true);
_graphigsDevice.SetRenderState(RenderState.SourceBlend, Blend.SourceAlpha);
_graphigsDevice.SetRenderState(RenderState.DestinationBlend, Blend.BothInverseSourceAlpha);
_graphigsDevice.SetRenderState(RenderState.Lighting, true);
_graphigsDevice.SetRenderState(RenderState.CullMode, Cull.None);
_graphigsDevice.SetRenderState(RenderState.FillMode, FillMode.Solid);
_graphigsDevice.SetRenderState(RenderState.NormalizeNormals, true);
_graphigsDevice.SetRenderState(RenderState.PointSize, 4f);
Vector3 direction = new Vector3(-1f, 0f, -1f);
direction.Normalize();
Light light = new Light();
light.Type = LightType.Directional;
light.Direction = direction;
light.Diffuse = Color.White;
light.Ambient = Color.White;
_graphigsDevice.SetLight(0, light);
_graphigsDevice.EnableLight(0, true);
direction = new Vector3(1f, 0f, 1f);
direction.Normalize();
light = new Light();
light.Type = LightType.Directional;
light.Direction = direction;
light.Diffuse = Color.White;
light.Ambient = Color.White;
_graphigsDevice.SetLight(1, light);
_graphigsDevice.EnableLight(1, true);
Material material = new Material();
material.Diffuse = Color.White;
material.Ambient = Color.Gray;
_graphigsDevice.Material = material;
_line = new Line(_graphigsDevice);
_sky = Mesh.FromFile(_graphigsDevice, @"Content\Meshes\skysphere.x", MeshFlags.Managed);
_sky.OptimizeInPlace(MeshOptimizeFlags.VertexCache | MeshOptimizeFlags.Compact | MeshOptimizeFlags.AttributeSort);
_skyTexture = Texture.FromFile(_graphigsDevice, @"Content\Textures\sky.png", Usage.None, Pool.Managed);
_colorMod = Effect.FromFile(_graphigsDevice, @"Content\Effects\Color.fx", ShaderFlags.None);
if (File.Exists(_project.MeshFilePath))
{
_mesh = LoadMesh(ref _project, true);
}
_box = Mesh.FromFile(_graphigsDevice, @"Content\Meshes\box.x", MeshFlags.Managed);
_box.OptimizeInPlace(MeshOptimizeFlags.VertexCache | MeshOptimizeFlags.Compact | MeshOptimizeFlags.AttributeSort);
_sphere = Mesh.FromFile(_graphigsDevice, @"Content\Meshes\sphere.x", MeshFlags.Managed);
_sphere.OptimizeInPlace(MeshOptimizeFlags.VertexCache | MeshOptimizeFlags.Compact | MeshOptimizeFlags.AttributeSort);
_capsuleEnd = Mesh.FromFile(_graphigsDevice, @"Content\Meshes\capsule_end.x", MeshFlags.Managed);
_capsuleEnd.OptimizeInPlace(MeshOptimizeFlags.VertexCache | MeshOptimizeFlags.Compact | MeshOptimizeFlags.AttributeSort);
_capsuleCylinder = Mesh.FromFile(_graphigsDevice, @"Content\Meshes\capsule_cylinder.x", MeshFlags.Managed);
_capsuleCylinder.OptimizeInPlace(MeshOptimizeFlags.VertexCache | MeshOptimizeFlags.Compact | MeshOptimizeFlags.AttributeSort);
_initialized = true;
}
catch (Exception e)
{
_initialized = false;
MessageBox.Show(this, e.Message);
}
}
示例3: Create3D9Mesh
public virtual Mesh Create3D9Mesh(Device graphicsDevice, ref int attribId)
{
var options = MeshFlags.Use32Bit;
if (graphicsDevice is DeviceEx)
options |= MeshFlags.Dynamic;
else
options |= MeshFlags.Managed;
Mesh mesh = new Mesh(graphicsDevice, FaceCount, VertexCount, options, VertexElements.ToArray());
fillVertexBuffer(mesh);
fillIndexBuffer(mesh);
fillAttributeBuffer(mesh, ref attribId);
mesh.OptimizeInPlace(MeshOptimizeFlags.AttributeSort);
return mesh;
}
示例4: OnCreateDevice
/// <summary>Called when the device has been created</summary>
public static void OnCreateDevice(Device device)
{
// Store the device
DirectionWidget.device = device;
// Read the effect file
string path = "UI\\DXUTShared.fx";
// If this fails, there should be debug output as to
// why the .fx file failed to compile (assuming you have dbmon running).
// If you do not, you can turn on unmanaged debugging for this project.
effect = Effect.FromFile(device, path, null, null, ShaderFlags.NotCloneable, null);
// Load the mesh with D3DX and get back a Mesh. For this
// sample we'll ignore the X file's embedded materials since we know
// exactly the model we're loading. See the mesh samples such as
// "OptimizedMesh" for a more generic mesh loading example.
path = "UI\\arrow.x";
mesh = Mesh.FromFile(path, MeshFlags.Managed, device);
// Optimize the mesh for this graphics card's vertex cache
// so when rendering the mesh's triangle list the vertices will
// cache hit more often so it won't have to re-execute the vertex shader
// on those vertices so it will improve perf.
int[] adj = new int[mesh.NumberFaces * 3];
mesh.GenerateAdjacency(1e-6f, adj);
mesh.OptimizeInPlace(MeshFlags.OptimizeVertexCache, adj);
}
示例5: Create
/// <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);
}
示例6: LoadMesh
// loads the heightMap into the vertex and index buffer
private void LoadMesh()
{
if (mesh != null)
mesh.Dispose();
mesh = new Mesh(heightMap.IndicesCount / 3, heightMap.VerticesCount,
0, CustomVertex.PositionNormalTextured.Format, device);
mesh.VertexBuffer.SetData(heightMap.Vertices, 0, LockFlags.None);
mesh.IndexBuffer.SetData(heightMap.Indices, 0, LockFlags.None);
int[] adjacency = new int[mesh.NumberFaces * 3];
mesh.GenerateAdjacency(0.01F, adjacency);
mesh.OptimizeInPlace(MeshFlags.OptimizeVertexCache, adjacency);
}
示例7: 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)
{
string strPath = null;
GraphicsStream adjacencyBuffer;
ExtendedMaterial[] Mat;
if (device != null) {
device.DeviceLost += new System.EventHandler(this.InvalidateDeviceObjects);
device.Disposing += new System.EventHandler(this.InvalidateDeviceObjects);
device.DeviceReset += new System.EventHandler(this.RestoreDeviceObjects);
}
// Find the path for the file, and convert it to ANSI (for the D3DX API)
strPath = DXUtil.FindMediaFile(null, filename);
// Load the mesh
systemMemoryMesh = Mesh.FromFile(strPath, MeshFlags.SystemMemory, device, out adjacencyBuffer, out Mat);
// Optimize the mesh for performance
systemMemoryMesh.OptimizeInPlace(MeshFlags.OptimizeCompact | MeshFlags.OptimizeAttributeSort | MeshFlags.OptimizeVertexCache, adjacencyBuffer);
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
string texturefilename = DXUtil.FindMediaFile(null, Mat[i].TextureFilename);
textures[i] = TextureLoader.FromFile(device, texturefilename);
}
}
adjacencyBuffer.Close();
adjacencyBuffer = null;
}
示例8: LoadMesh
// 메쉬를 불러오는 함수
private void LoadMesh(string filenameWithPath, ref Mesh mesh, ref Material[] meshmaterials, ref Texture[] meshtextures)
{
ExtendedMaterial[] materialarray;
mesh = Mesh.FromFile(filenameWithPath, MeshFlags.Managed, m_device, out materialarray);
if ((materialarray != null) && (materialarray.Length > 0))
{
meshmaterials = new Material[materialarray.Length];
meshtextures = new Texture[materialarray.Length];
for (int i = 0; i < materialarray.Length; i++)
{
meshmaterials[i] = materialarray[i].Material3D;
meshmaterials[i].Ambient = meshmaterials[i].Diffuse;
if ((materialarray[i].TextureFilename != null) && (materialarray[i].TextureFilename != string.Empty))
{
meshtextures[i] = TextureLoader.FromFile(m_device, Class.Renderer.FOLDER_PATH + materialarray[i].TextureFilename);
}
}
}
mesh = mesh.Clone(mesh.Options.Value, CustomVertex.PositionNormalTextured.Format, m_device);
mesh.ComputeNormals();
int[] adjacency = new int[mesh.NumberFaces * 3];
mesh.GenerateAdjacency(0.01F, adjacency);
mesh.OptimizeInPlace(MeshFlags.OptimizeVertexCache, adjacency);
}
示例9: createMovementGizmo
//.........这里部分代码省略.........
indices[i * eachILength + 11] = (short)(i * eachVLength + 5);
indices[i * eachILength + 12] = (short)(i * eachVLength + 0);
indices[i * eachILength + 13] = (short)(i * eachVLength + 5);
indices[i * eachILength + 14] = (short)(i * eachVLength + 6);
indices[i * eachILength + 15] = (short)(i * eachVLength + 0);
indices[i * eachILength + 16] = (short)(i * eachVLength + 6);
indices[i * eachILength + 17] = (short)(i * eachVLength + 7);
indices[i * eachILength + 18] = (short)(i * eachVLength + 0);
indices[i * eachILength + 19] = (short)(i * eachVLength + 7);
indices[i * eachILength + 20] = (short)(i * eachVLength + 8);
indices[i * eachILength + 21] = (short)(i * eachVLength + 0);
indices[i * eachILength + 22] = (short)(i * eachVLength + 8);
indices[i * eachILength + 23] = (short)(i * eachVLength + 1);
}
for (int i = 3; i < 6; i++)
{
// Shaft
indices[i * eachILength + 0] = (short)((i - 3) * eachVLength + 9);
indices[i * eachILength + 1] = (short)((i - 3) * eachVLength + 13);
indices[i * eachILength + 2] = (short)((i - 3) * eachVLength + 10);
indices[i * eachILength + 3] = (short)((i - 3) * eachVLength + 10);
indices[i * eachILength + 4] = (short)((i - 3) * eachVLength + 13);
indices[i * eachILength + 5] = (short)((i - 3) * eachVLength + 14);
indices[i * eachILength + 6] = (short)((i - 3) * eachVLength + 10);
indices[i * eachILength + 7] = (short)((i - 3) * eachVLength + 14);
indices[i * eachILength + 8] = (short)((i - 3) * eachVLength + 11);
indices[i * eachILength + 9] = (short)((i - 3) * eachVLength + 11);
indices[i * eachILength + 10] = (short)((i - 3) * eachVLength + 14);
indices[i * eachILength + 11] = (short)((i - 3) * eachVLength + 15);
indices[i * eachILength + 12] = (short)((i - 3) * eachVLength + 11);
indices[i * eachILength + 13] = (short)((i - 3) * eachVLength + 15);
indices[i * eachILength + 14] = (short)((i - 3) * eachVLength + 12);
indices[i * eachILength + 15] = (short)((i - 3) * eachVLength + 12);
indices[i * eachILength + 16] = (short)((i - 3) * eachVLength + 15);
indices[i * eachILength + 17] = (short)((i - 3) * eachVLength + 16);
indices[i * eachILength + 18] = (short)((i - 3) * eachVLength + 12);
indices[i * eachILength + 19] = (short)((i - 3) * eachVLength + 16);
indices[i * eachILength + 20] = (short)((i - 3) * eachVLength + 13);
indices[i * eachILength + 21] = (short)((i - 3) * eachVLength + 12);
indices[i * eachILength + 22] = (short)((i - 3) * eachVLength + 13);
indices[i * eachILength + 23] = (short)((i - 3) * eachVLength + 9);
}
// Squares
indices[6 * eachILength + 0] = (short)(3 * eachVLength + 0);
indices[6 * eachILength + 1] = (short)(3 * eachVLength + 1);
indices[6 * eachILength + 2] = (short)(3 * eachVLength + 4);
indices[6 * eachILength + 3] = (short)(3 * eachVLength + 0);
indices[6 * eachILength + 4] = (short)(3 * eachVLength + 2);
indices[6 * eachILength + 5] = (short)(3 * eachVLength + 4);
indices[6 * eachILength + 6] = (short)(3 * eachVLength + 0);
indices[6 * eachILength + 7] = (short)(3 * eachVLength + 2);
indices[6 * eachILength + 8] = (short)(3 * eachVLength + 6);
indices[6 * eachILength + 9] = (short)(3 * eachVLength + 0);
indices[6 * eachILength + 10] = (short)(3 * eachVLength + 3);
indices[6 * eachILength + 11] = (short)(3 * eachVLength + 6);
indices[6 * eachILength + 12] = (short)(3 * eachVLength + 0);
indices[6 * eachILength + 13] = (short)(3 * eachVLength + 3);
indices[6 * eachILength + 14] = (short)(3 * eachVLength + 5);
indices[6 * eachILength + 15] = (short)(3 * eachVLength + 0);
indices[6 * eachILength + 16] = (short)(3 * eachVLength + 1);
indices[6 * eachILength + 17] = (short)(3 * eachVLength + 5);
m.SetIndexBufferData(indices, LockFlags.None);
#endregion
int[] attr = m.LockAttributeBufferArray(LockFlags.Discard);
int step = eachILength / 3; // 1 face = 3 indices
for (int i = 0; i < step; i++)
{
attr[step * 0 + i] = 0;
attr[step * 1 + i] = 1;
attr[step * 2 + i] = 2;
attr[step * 3 + i] = 3;
attr[step * 4 + i] = 4;
attr[step * 5 + i] = 5;
}
for (int i = step * 6; i < attr.Length; i++)
{
attr[i] = 6 + (i - step * 6) / 2;
}
m.UnlockAttributeBuffer(attr);
int[] adj = new int[m.NumberFaces * 3];
m.GenerateAdjacency(0.001f, adj);
m.OptimizeInPlace(MeshFlags.OptimizeVertexCache, adj);
this.gizmo = m;
}
示例10: GenerateMesh
/// <summary>
/// Generates a mesh from the parsed
/// information of the file
/// </summary>
private void GenerateMesh(Device d3dDevice)
{
if(m_alNormals.Count == 0)
throw new System.Exception("No normals were found for this mesh.");
if(m_intNumFaces == 0)
throw new System.Exception("No faces were found for this mesh.");
if(m_intNumVerts == 0)
throw new System.Exception("No vertices were found for this mesh.");
//create a mesh with Poisiton, Normal, and Texture info. Even if it doesn't contain TextCoords
m_d3dMesh = new Mesh(m_intNumFaces, m_intNumVerts, MeshFlags.Managed, CustomVertex.PositionNormalTextured.Format, d3dDevice);
//index array
short[] aryIndices = new short[m_alVertFormat.Count];
CustomVertex.PositionNormalTextured[] aryVerts = new CustomVertex.PositionNormalTextured[m_intNumVerts];
Vector3 v, t, n;
CustomVertex.PositionNormalTextured cvVert;
//loop through each face and apply the format
for(int i=0; i<m_intNumFaces * 3; i++)
{
//parse the vertex information
string[] aryVertInfo = (string[])m_alVertFormat[i];
//first one is vertex index
short index = short.Parse(aryVertInfo[0]);
//OBJ format starts at 1 not 0
index--;
//set the index arry
aryIndices[i] = index;
v = (Vector3)m_alVertices[index];
t = new Vector3(0,0,0);
//parse the texture coords
if( aryVertInfo.Length == 3)
{
index = short.Parse(aryVertInfo[1]);
index--;
//set the texture coordinate
t = (Vector3)m_alTextCoords[index];
index = short.Parse(aryVertInfo[2]);
index--;
//set the normal
n = (Vector3)m_alNormals[index];
}
else
{
index = short.Parse(aryVertInfo[1]);
index--;
//set the normal
n = (Vector3)m_alNormals[index];
}
cvVert = aryVerts[aryIndices[i]];
cvVert.Position = v;
cvVert.Normal = n;
cvVert.Tu = t.X;
cvVert.Tv = t.Y;
aryVerts[aryIndices[i]] = cvVert;
}//end for loop
m_d3dMesh.VertexBuffer.SetData(aryVerts,0, LockFlags.None);
m_d3dMesh.IndexBuffer.SetData(aryIndices,0,LockFlags.None);
AttributeRange ar = new AttributeRange();
ar.AttributeId = 0;
ar.FaceCount = m_intNumFaces;
ar.FaceStart = 0;
ar.VertexCount = m_intNumVerts;
ar.VertexStart = 0;
m_d3dMesh.SetAttributeTable(new AttributeRange[] {ar});
int[] adj = new int[m_intNumFaces * 3];
m_d3dMesh.GenerateAdjacency(0.01f, adj);
m_d3dMesh.OptimizeInPlace(MeshFlags.OptimizeVertexCache | MeshFlags.OptimizeIgnoreVerts, adj);
m_d3dMesh.ComputeNormals();
m_bLoaded = true;
}