本文整理汇总了C#中Mesh.Optimize方法的典型用法代码示例。如果您正苦于以下问题:C# Mesh.Optimize方法的具体用法?C# Mesh.Optimize怎么用?C# Mesh.Optimize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Mesh
的用法示例。
在下文中一共展示了Mesh.Optimize方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Terrain
/// <summary>
/// Constructor for randomly generating terrian
/// </summary>
public Terrain()
{
//FileStream fs = new FileStream("heightdata.raw", FileMode.Open, FileAccess.Read);
//BinaryReader r = new BinaryReader(fs);
rand = new Random();
width = rand.Next(2, 100);//64;//rand.Next(2, 50);
tall = rand.Next(2, 100);//64;//rand.Next(2, 50);
world = Matrix.Identity;
Height = new int[width, tall];
var vertices = new CustomVertex.VertexPositionColor[width * tall];
var indicies = new short[(width - 1) * (tall - 1) * 3];
for (int i = 0; i < width; i++)
{
for (int j = 0; j < tall; j++)
{
//height[width - 1 - j, tall - 1 - i] = (int)(r.ReadByte() / 50);
Height[i, j] = rand.Next(0, 3);
}
}
for (int i = 0; i < width; i++)
{
for (int j = 0; j < tall; j++)
{
vertices[i + j * width].Position = new Vector3(i, Height[i, j], j);
vertices[i + j * width].Color = Color.White.ToArgb();
}
}
for (int i = 0; i < width - 1; i++)
{
for (int j = 0; j < tall - 1; j++)
{
indicies[(i + j * (width - 1)) * 3] = (short)((i + 1) + (j + 1) * width);
indicies[(i + j * (width - 1)) * 3 + 1] = (short)((i + 1) + j * width);
indicies[(i + j * (width - 1)) * 3 + 2] = (short)(i + j * width);
}
}
mesh = new Mesh(DeviceManager.LocalDevice, indicies.Length, vertices.Length,
MeshFlags.Managed, CustomVertex.VertexPositionColor.Format);
mesh.LockVertexBuffer(LockFlags.Discard).WriteRange<CustomVertex.VertexPositionColor>(vertices);
mesh.UnlockVertexBuffer();
mesh.LockIndexBuffer(LockFlags.Discard).WriteRange<short>(indicies);
mesh.UnlockIndexBuffer();
mesh.Optimize(MeshOptimizeFlags.AttributeSort | MeshOptimizeFlags.Compact);
//r.Dispose();
//fs.Dispose();
}
示例2: 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;
}
示例3: 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;
}
示例4: CreateStemMesh
public static Mesh CreateStemMesh(int seed)
{
Random.seed = seed;
int sides = 7;
int quads = sides * 2;
float height = 0.25f;
float bottomDiameter = 0.125f;
float topDiameter = 0.2f;
Vector3[] vertices = new Vector3[quads * 3 + 1];
int lidOffset = quads * 2;
int centerIndex = vertices.Length-1;
float sideRotation = Random.Range(0.0f, 2.0f * Mathf.PI);
for (int q = 0; q < quads; ++q) {
float x = Mathf.Cos(sideRotation + q * 2.0f * Mathf.PI / quads);
float z = Mathf.Sin(sideRotation + q * 2.0f * Mathf.PI / quads);
Vector3 bottom = new Vector3(x * bottomDiameter, 0.0f, z * bottomDiameter);
Vector3 top = new Vector3(x * topDiameter, height, z * topDiameter);
float rand = q % 2 == 0 ? Random.Range(0.8f, 0.9f) : Random.Range(1.1f, 1.4f);
top = bottom + (top - bottom) * rand;
if ((q % 2) == 1)
top += new Vector3(x, 0.0f, z) * topDiameter * 0.1f * rand;
vertices[q * 2] = bottom;
vertices[q * 2 + 1] = top;
vertices[lidOffset + q] = top; // For the 'lid'
}
vertices[centerIndex] = new Vector3(0.0f, height * 0.5f, 0.0f);
int[] indices = new int[quads * 6 + quads * 3]; // quads * 6 indices for the sides and quads * 3 for the top
for (int s = 0; s < sides-1; ++s) {
AddStemSideIndices(s * 4, s * 4 + 1, s * 4 + 2, s * 4 + 3, s * 4 + 4, s * 4 + 5,
lidOffset + s * 2, lidOffset + s * 2 + 1, lidOffset + s * 2 + 2, centerIndex,
indices, s * 18);
}
int lastSide = (sides - 1) * 4;
int lastLid = lidOffset + (sides - 1) * 2;
AddStemSideIndices(lastSide, lastSide + 1, lastSide + 2, lastSide + 3, 0, 1,
lastLid, lastLid + 1, lidOffset, centerIndex,
indices, (sides - 1) * 18);
Mesh mesh = new Mesh();
mesh.name = "PalmStem_" + seed;
mesh.vertices = vertices;
mesh.triangles = indices;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
mesh.Optimize();
return mesh;
}
示例5: CreateLeafMesh2
//.........这里部分代码省略.........
int leafletDetail = 1; // 1 or above
int vertsPrLeaflet = leafletDetail + 2;
int vertexCountPrSide = leaflets * vertsPrLeaflet;
Vector3[] verts = new Vector3[vertexCountPrSide * 2];
Debug.Log("||vertices|| " + verts.Length);
// Build the first side of the leaf
verts[0] = Vector3.zero;
for (int l = 0; l < leaflets; ++l) {
// Vertex on 'spine'
float spineDelta = (l + 0.3f) / (float)leaflets;
verts[1 + l * vertsPrLeaflet] = new Vector3(0.0f, 0.0f, spineDelta * length);
// Outer vertex
float outerDelta = (l + 1.0f) / (float)leaflets;
Vector2 outerPos = outerBend.GetPosition(outerAngle * outerDelta);
verts[1 + l * vertsPrLeaflet + 1] = new Vector3(outerPos.x, -0.4f * outerPos.x, outerPos.y);
// Inner vertex if not last leaflet
if (l < leaflets-1) {
float innerDelta = (l + 0.9f) / (float)leaflets;
Vector2 innerPos = innerBend.GetPosition(innerAngle * innerDelta);
verts[1 + l * vertsPrLeaflet + 2] = new Vector3(innerPos.x, -0.4f * innerPos.x, innerPos.y);
}
}
// Hard set last vertex to length away from origin to avoit precision errors
verts[vertexCountPrSide-1] = new Vector3(0.0f, 0.0f, length);
// Build the second side of the leaf
for (int v = 0; v < vertexCountPrSide; ++v) {
Vector3 vert = verts[v];
vert.x *= -1.0f;
verts[v + vertexCountPrSide] = vert;
}
string vertsStr = "";
foreach (Vector3 v in verts)
vertsStr += v.ToString("0.000");
Debug.Log(vertsStr);
// Index the first side of the leaf
int trisPrSide = leaflets * leafletDetail + (leaflets - 1) * 2;
int indicesPrSide = trisPrSide * 3;
int[] indices = new int[indicesPrSide * 2];
Debug.Log("||indices|| " + indices.Length);
int prevSpineIndex = 1 - vertsPrLeaflet;
int i = 0;
while (i < indicesPrSide - 1) {
int spineIndex = prevSpineIndex + vertsPrLeaflet;
indices[i] = spineIndex-1;
++i;
indices[i] = spineIndex+1;
++i;
indices[i] = spineIndex;
++i;
for (int l = 0; l < leafletDetail && i < indices.Length-1; ++l) {
indices[i] = spineIndex;
++i;
indices[i] = spineIndex + l + 1;
++i;
indices[i] = spineIndex + l + 2;
++i;
}
if (i < indices.Length-1) {
int nextSpineIndex = spineIndex + vertsPrLeaflet;
indices[i] = spineIndex;
++i;
indices[i] = nextSpineIndex - 1;
++i;
indices[i] = nextSpineIndex;
++i;
}
prevSpineIndex = spineIndex;
}
// Index the second side of the leaf
for (i = 0; i < indicesPrSide; ++i)
indices[indicesPrSide + i] = indices[i] + vertexCountPrSide;
string trisStr = "";
for (i = 0; i < indices.Length / 3; ++i)
trisStr += "[" + indices[i*3] + ", " + indices[i*3+1] + ", " + indices[i*3+2] + "], ";
Debug.Log(trisStr);
Mesh mesh = new Mesh();
mesh.name = "PalmLeaf_" + seed;
mesh.vertices = verts;
mesh.triangles = indices;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
mesh.Optimize();
return mesh;
}
示例6: CreateLeafMesh
public static Mesh CreateLeafMesh(int seed, float length)
{
Random.seed = seed;
float outerAngle = 90.0f;
Vector2 outerP0 = Vector2.zero;
Vector2 outerP1 = new Vector2(0.0f, length);
BenderRodriguez outerBend = new BenderRodriguez(outerP0, outerP1, outerAngle);
int quads = 7;
// TODO Add a 'spine'
Vector3[] verts = new Vector3[(quads+1) * 2];
for (int q = 0; q <= quads; ++q) {
float delta = q / (float)quads;
Vector2 outerPos = outerBend.GetPosition(outerAngle * delta * delta);
verts[2*q ] = outerPos;
outerPos.x *= -1.0f;
verts[2*q+1] = outerPos;
}
int[] indices = new int[quads * 6];
for (int q = 0; q < quads; ++q) {
indices[6*q ] = 2*q;
indices[6*q+1] = 2*q+2;
indices[6*q+2] = 2*q+1;
indices[6*q+3] = 2*q+1;
indices[6*q+4] = 2*q+2;
indices[6*q+5] = 2*q+3;
}
Mesh mesh = new Mesh();
mesh.name = "PalmLeaf_" + seed;
mesh.vertices = verts;
mesh.triangles = indices;
mesh.RecalculateNormals();
mesh.RecalculateBounds();
mesh.Optimize();
return mesh;
}