本文整理汇总了C#中Mesh.UnlockVertexBuffer方法的典型用法代码示例。如果您正苦于以下问题:C# Mesh.UnlockVertexBuffer方法的具体用法?C# Mesh.UnlockVertexBuffer怎么用?C# Mesh.UnlockVertexBuffer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Mesh
的用法示例。
在下文中一共展示了Mesh.UnlockVertexBuffer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateMesh
protected override BulletMesh CreateMesh(Device device)
{
int totalTriangles = this.indices.Length /3;
int totalVerts = this.vertices.Length;
Mesh m = new Mesh(device, totalTriangles, totalVerts, MeshFlags.Use32Bit | MeshFlags.SystemMemory, VertexFormat.Position | VertexFormat.Normal);
SlimDX.DataStream data = m.LockVertexBuffer(LockFlags.None);
for (int i = 0; i < this.vertices.Length; i++)
{
data.Write(this.vertices[i].X);
data.Write(this.vertices[i].Y);
data.Write(this.vertices[i].Z);
data.Write(0.0f);
data.Write(0.0f);
data.Write(0.0f);
//data.Write(this.texcoords[i]);
}
m.UnlockVertexBuffer();
data = m.LockIndexBuffer(LockFlags.None);
for (int i = 0; i < this.indices.Length; i++)
{
data.Write(this.indices[i]);
}
m.UnlockIndexBuffer();
m.ComputeNormals();
return null;// new BulletMesh(m);
}
示例2: render
public override void render(float elapsedTime)
{
Device d3dDevice = GuiController.Instance.D3dDevice;
time += elapsedTime;
if (time > 1f)
{
Mesh d3dMesh = new Mesh(boxMesh.NumberTriangles, boxMesh.NumberVertices, MeshFlags.Managed, TgcSceneLoader.VertexColorVertexElements, d3dDevice);
TgcSceneLoader.VertexColorVertex[] origVert = (TgcSceneLoader.VertexColorVertex[])boxMesh.D3dMesh.LockVertexBuffer(
typeof(TgcSceneLoader.VertexColorVertex), LockFlags.ReadOnly, boxMesh.D3dMesh.NumberVertices);
boxMesh.D3dMesh.UnlockVertexBuffer();
TgcSceneLoader.VertexColorVertex[] newVert = (TgcSceneLoader.VertexColorVertex[])d3dMesh.LockVertexBuffer(
typeof(TgcSceneLoader.VertexColorVertex), LockFlags.None, d3dMesh.NumberVertices);
for (int i = 0; i < origVert.Length; i++)
{
newVert[i] = origVert[i];
}
//d3dMesh.SetVertexBufferData(newVert, LockFlags.None);
d3dMesh.UnlockVertexBuffer();
boxMesh.changeD3dMesh(d3dMesh);
time = 0;
}
boxMesh.render();
}
示例3: 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();
}
示例4: CreateShape
Mesh CreateShape(CollisionShape shape)
{
uint[] indices;
BulletSharp.Math.Vector3[] vertices = CreateShape(shape, out indices);
int vertexCount = vertices.Length / 2;
int indexCount = (indices != null) ? indices.Length : vertexCount;
bool index32 = indexCount > 65536;
Mesh mesh = new Mesh(device, indexCount / 3, vertexCount,
MeshFlags.SystemMemory | (index32 ? MeshFlags.Use32Bit : 0), VertexFormat.Position | VertexFormat.Normal);
DataStream vertexBuffer = mesh.LockVertexBuffer(LockFlags.Discard);
vertexBuffer.WriteRange(vertices);
mesh.UnlockVertexBuffer();
DataStream indexBuffer = mesh.LockIndexBuffer(LockFlags.Discard);
if (index32)
{
if (indices == null)
{
indices = new uint[indexCount];
uint i = 0;
while (i < indexCount)
{
indices[i] = i;
i++;
}
}
indexBuffer.WriteRange(indices);
}
else
{
ushort[] indices_s;
if (indices == null)
{
indices_s = new ushort[indexCount];
ushort i = 0;
while (i < indexCount)
{
indices_s[i] = i;
i++;
}
}
else
{
indices_s = CompactIndexBuffer(indices);
}
indexBuffer.WriteRange(indices_s);
}
mesh.UnlockIndexBuffer();
shapes.Add(shape, mesh);
return mesh;
}
示例5: CreateCylinderMesh
//.........这里部分代码省略.........
{
Position = vertexPos,
Texture = i < 6 ? startTextures[i] : default(Vector2),
Normal = i < 6 ? GetFlatNormal(vertexPos) : GetSideNormal(vertexPos, height, rtop, rbottom)
}));
var indices = new List<Int16>
{
0, 1, 2,
3, 4, 5,
6, 7, 8,
8, 9, 6
};
var attributes = new List<Int32> {0, 1, 2, 2};
short prevtop = 2;
short prevbot = 4;
short prevtopside = 9;
short prevbotside = 8;
for(short i = 1; i < thetaDiv - 1; i++)
{
var vertcount = (short)(10 + 4 * i);
// Top surface:
Vector3 topPoint = topPoints[i + 1];
vertices.Add(new CustomVertex
{
Position = topPoint,
Texture = GetFlatTextureCoordinates(topPoint, rtop),
Normal = GetFlatNormal(topPoint)
});
indices.Add(0);
indices.Add(prevtop);
indices.Add(vertcount);
attributes.Add(0);
// Bottom surface:
Vector3 bottomPoint = bottomPoints[i + 1];
vertices.Add(new CustomVertex
{
Position = bottomPoint,
Texture = GetFlatTextureCoordinates(bottomPoint, rbottom),
Normal = GetFlatNormal(bottomPoint)
});
indices.Add(3); //center
indices.Add((short)(vertcount + 1)); //latest
indices.Add(prevbot); //previous
attributes.Add(1);
// Side surface:
vertices.Add(new CustomVertex
{
Position = bottomPoint,
Normal = GetSideNormal(bottomPoint, height, rtop, rbottom)
});
vertices.Add(new CustomVertex
{
Position = topPoint,
Normal = GetSideNormal(bottomPoint, height, rtop, rbottom)
});
indices.Add(prevtopside);
indices.Add(prevbotside);
indices.Add((short)(vertcount + 2));
indices.Add((short)(vertcount + 2));
indices.Add((short)(vertcount + 3));
indices.Add(prevtopside);
attributes.Add(2);
attributes.Add(2);
prevtop = vertcount;
prevbot = (short)(vertcount + 1);
prevbotside = (short)(vertcount + 2);
prevtopside = (short)(vertcount + 3);
}
//top
indices.Add(0);
indices.Add(thetaDiv - 1);
indices.Add(1);
attributes.Add(0);
indices.Add(3); //center
indices.Add(5); //latest
indices.Add(thetaDiv - 1);
attributes.Add(1);
indices.Add(prevtopside);
indices.Add(prevbotside);
indices.Add(7);
indices.Add(7);
indices.Add(6);
indices.Add(prevtop);
attributes.Add(2);
attributes.Add(2);
var mesh = new Mesh(device, indices.Count / 3, vertices.Count, MeshFlags.Managed, CustomVertex.TheVertexFormat);
const LockFlags lockFlags = LockFlags.None;
using(DataStream vertexStream = mesh.LockVertexBuffer(lockFlags),
indexStream = mesh.LockIndexBuffer(lockFlags),
attributeStream = mesh.LockAttributeBuffer(lockFlags))
{
vertices.ForEach(vertexStream.Write);
indices.ForEach(indexStream.Write);
attributes.ForEach(attributeStream.Write);
}
mesh.UnlockVertexBuffer();
mesh.UnlockIndexBuffer();
mesh.UnlockAttributeBuffer();
return mesh;
}
示例6: 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;
}
示例7: ColorReceptiveField
public static void ColorReceptiveField(Mesh d3dMesh, AnimatTools.Framework.Vec3d vFieldPoint, AnimatTools.DataObjects.Gain gnGain)
{
CustomVertex.PositionNormalColored vVertex;
GraphicsStream buffer = d3dMesh.LockVertexBuffer(LockFlags.None);
float fltBaseVal = (float) gnGain.CalculateGain(0);
float fltDist, fltGain=0;
for(int i=0; i<d3dMesh.NumberVertices; i++)
{
buffer.Position = i * d3dMesh.NumberBytesPerVertex;
vVertex = (CustomVertex.PositionNormalColored)buffer.Read(typeof(CustomVertex.PositionNormalColored));
//dwc changes
fltDist = (float) Distance(vFieldPoint, vVertex.X, vVertex.Y, vVertex.Z) * Util.Environment.DistanceUnitValue;
fltGain = (float) gnGain.CalculateGain(fltDist);
fltGain = 255 - (255*(fltGain/fltBaseVal));
if(fltGain < 0) fltGain = 0;
if(fltGain>255) fltGain = 255;
vVertex.Color = Color.FromArgb(255, (int) fltGain, (int) fltGain).ToArgb();
buffer.Position = i * d3dMesh.NumberBytesPerVertex;
buffer.Write(new CustomVertex.PositionNormalColored(vVertex.X, vVertex.Y, vVertex.Z, vVertex.Nx, vVertex.Ny, vVertex.Nz, vVertex.Color));
}
d3dMesh.UnlockVertexBuffer();
}
示例8: UpdateMesh
//this method gets called, when Update() was called in evaluate,
//or a graphics device asks for its mesh, here you can alter the data of the mesh
protected override void UpdateMesh(Mesh mesh)
{
//do something with the mesh data
var vertices = mesh.LockVertexBuffer(LockFlags.None);
for (int i = 0; i < mesh.VertexCount; i++) {
//get the vertex content
var pos = vertices.Read<Vector3>();
var norm = vertices.Read<Vector3>();
pos.X = (pos.X + FRandomizeIn[i].X) % 2 - 0;
pos.Y = (pos.Y + FRandomizeIn[i].Y) % 2 - 1;
pos.Z = (pos.Z + FRandomizeIn[i].Z) % 2 - 0;
//to write the data move the stream position back!
vertices.Position -= mesh.BytesPerVertex;
vertices.Write(pos);
vertices.Write(norm);
}
mesh.UnlockVertexBuffer();
}
示例9: fillVertexBuffer
protected override void fillVertexBuffer(Mesh mesh)
{
List<KeyValuePair<byte, float>> blendData = new List<KeyValuePair<byte, float>>();
byte[] blendIndices = new byte[MaxBlendIndexCount];
float[] blendWeights = new float[MaxBlendIndexCount];
byte blendIndex;
float blendWeight, blendWeightSum;
bool displayMaxNumInfluenceWarning = true;
// get weight input element
Document.Input weightInput = null;
foreach (Document.Input input in skin.vertexWeights.inputs)
if (input.semantic == "WEIGHT")
weightInput = input;
DataStream ds = mesh.LockVertexBuffer(LockFlags.None);
try
{
// Read out all blend weights and indices to build an offset table
uint[] offsets = new uint[skin.vertexWeights.count];
uint offset = 0;
for (int i = 0; i < offsets.Length; i++)
{
offsets[i] = offset;
offset += 2 * skin.vertexWeights.vcount[i];
}
for (int vertexIndex = 0; vertexIndex < VertexCount; vertexIndex++)
{
ds.WriteRange(VertexArray, vertexIndex * VertexStride, VertexStride);
int v = InvVertexMapping[vertexIndex];
offset = offsets[v];
// read in all blend indices, sort them by weight,
// select up to MaxBlendIndexCount and renormalize.
blendData.Clear();
for (int j = 0; j < skin.vertexWeights.vcount[v]; j++)
{
blendIndex = (byte) skin.vertexWeights.v[offset + 2 * j];
blendWeight = COLLADAUtil.GetSourceElement(
model.Doc,
weightInput,
skin.vertexWeights.v[offset + 2 * j + 1])[0];
// ignore blend indices with values greater than MaxSkinningMatrixCount
//if (blendIndex < MaxBlendIndexCount)
blendData.Add(new KeyValuePair<byte, float>(blendIndex, blendWeight));
}
blendData.Sort(
delegate(KeyValuePair<byte, float> firstPair, KeyValuePair<byte, float> secondPair)
{
return firstPair.Value.CompareTo(secondPair.Value);
});
blendWeightSum = 0;
for (int j = 0; j < MaxBlendIndexCount; j++)
{
if (j < blendData.Count)
{
blendIndices[j] = blendData[blendData.Count - 1 - j].Key;
blendWeights[j] = blendData[blendData.Count - 1 - j].Value;
blendWeightSum += blendData[blendData.Count - 1 - j].Value;
}
else
{
blendIndices[j] = 0;
blendWeights[j] = 0;
}
}
// There are only up to four influences allowed.
if (displayMaxNumInfluenceWarning && skin.vertexWeights.vcount[v] > 4)
{
COLLADAUtil.Log(COLLADALogType.Warning, "There are only up to four influences allowed. Skinning might be incorrect.");
displayMaxNumInfluenceWarning = false;
}
// renormalize
if (blendWeightSum > 0)
{
for (int j = 0; j < 4; j++)
{
blendWeights[j] = blendWeights[j] / blendWeightSum;
}
}
ds.WriteRange(blendIndices);
ds.WriteRange(blendWeights);
}
}
catch (Exception e)
{
COLLADAUtil.Log(COLLADALogType.Error, e.Message + e.StackTrace);
}
finally
{
mesh.UnlockVertexBuffer();
}
//.........这里部分代码省略.........
示例10: UpdateMesh
//this method gets called, when Update() was called in evaluate,
//or a graphics device asks for its mesh, here you can alter the data of the mesh
void UpdateMesh(MeshData meshData, Mesh mesh)
{
FLogger.Log(LogType.Debug, "Updating Mesh...");
//do something with the mesh data
var vertices = mesh.LockVertexBuffer(LockFlags.None);
for (int i = 0; i < mesh.VertexCount; i++)
{
//get the vertex content
var pos = vertices.Read<Vector3>();
var norm = vertices.Read<Vector3>();
pos.X = (pos.X + meshData.Randomness.X) % 2 - 0;
pos.Y = (pos.Y + meshData.Randomness.Y) % 2 - 1;
pos.Z = (pos.Z + meshData.Randomness.Z) % 2 - 0;
//to write the data move the stream position back!
vertices.Position -= mesh.BytesPerVertex;
vertices.Write(pos);
vertices.Write(norm);
}
mesh.UnlockVertexBuffer();
}
示例11: FillVertexBuffer
private void FillVertexBuffer(Mesh animationMesh, List<odfVertex> vertexList, float[][] vertexWeights, int selectedBoneIdx)
{
using (DataStream vertexStream = animationMesh.LockVertexBuffer(LockFlags.None))
{
Color4 col = new Color4(1f, 1f, 1f);
for (int j = 0; j < vertexList.Count; j++)
{
odfVertex vertex = vertexList[j];
#if !DONT_MIRROR
Vector3 position = new Vector3(vertex.Position.X, vertex.Position.Y, -vertex.Position.Z);
Vector3 normal = new Vector3(vertex.Normal.X, vertex.Normal.Y, -vertex.Normal.Z);
#else
Vector3 position = vertex.Position;
Vector3 normal = vertex.Normal;
#endif
float[] boneWeights = vertexWeights[j];
vertexStream.Write(position.X);
vertexStream.Write(position.Y);
vertexStream.Write(position.Z);
vertexStream.Write(boneWeights[0]);
vertexStream.Write(boneWeights[1]);
vertexStream.Write(boneWeights[2]);
vertexStream.Write(vertex.BoneIndices[0]);
vertexStream.Write(vertex.BoneIndices[1]);
vertexStream.Write(vertex.BoneIndices[2]);
vertexStream.Write(vertex.BoneIndices[3]);
vertexStream.Write(normal.X);
vertexStream.Write(normal.Y);
vertexStream.Write(normal.Z);
if (selectedBoneIdx >= 0)
{
col.Red = 0f; col.Green = 0f; col.Blue = 0f;
byte[] boneIndices = vertex.BoneIndices;
for (int k = 0; k < boneIndices.Length; k++)
{
if (boneIndices[k] == 0xFF)
{
continue;
}
byte boneIdx = boneIndices[k];
if (boneIdx == selectedBoneIdx)
{
/* switch (cols)
{
case WeightsColourPreset.Greyscale:
col.r = col.g = col.b = boneWeights[k];
break;
case WeightsColourPreset.Metal:
col.r = boneWeights[k] > 0.666f ? 1f : boneWeights[k] * 1.5f;
col.g = boneWeights[k] * boneWeights[k] * boneWeights[k];
break;
case WeightsColourPreset.Rainbow:*/
if (boneWeights[k] > 0.75f)
{
col.Red = 1f;
col.Green = (1f - boneWeights[k]) * 2f;
col.Blue = 0f;
}
else if (boneWeights[k] > 0.5f)
{
col.Red = 1f;
col.Green = (1f - boneWeights[k]) * 2f;
col.Blue = 0f;
}
else if (boneWeights[k] > 0.25f)
{
col.Red = (boneWeights[k] - 0.25f) * 4f;
col.Green = 1f;
col.Blue = 0f;
}
else
{
col.Green = boneWeights[k] * 4f;
col.Blue = 1f - boneWeights[k] * 4f;
}
/* break;
}*/
break;
}
}
}
vertexStream.Write(col.ToArgb());
vertexStream.Write(vertex.UV[0]);
vertexStream.Write(vertex.UV[1]);
}
animationMesh.UnlockVertexBuffer();
}
}
示例12: CreateTriangleMeshShape
Mesh CreateTriangleMeshShape(TriangleMeshShape shape)
{
StridingMeshInterface meshInterface = shape.MeshInterface;
BulletSharp.DataStream verts, indices;
int numVerts, numFaces;
PhyScalarType vertsType, indicesType;
int vertexStride, indexStride;
meshInterface.GetLockedReadOnlyVertexIndexData(out verts, out numVerts, out vertsType, out vertexStride,
out indices, out indexStride, out numFaces, out indicesType);
bool index32 = numVerts > 65536;
Mesh mesh = new Mesh(device, numFaces, numVerts,
MeshFlags.SystemMemory | (index32 ? MeshFlags.Use32Bit : 0), VertexFormat.Position | VertexFormat.Normal);
SlimDX.DataStream data = mesh.LockVertexBuffer(LockFlags.None);
while (verts.Position < verts.Length)
{
Vector3 v = verts.Read<Vector3>();
data.Write(v);
verts.Position += vertexStride - 12;
// Normals will be calculated later
data.Position += 12;
}
mesh.UnlockVertexBuffer();
data = mesh.LockIndexBuffer(LockFlags.None);
while (indices.Position < indices.Length)
{
int index = indices.Read<int>();
if (index32)
data.Write(index);
else
data.Write((short)index);
}
mesh.UnlockVertexBuffer();
mesh.ComputeNormals();
shapes.Add(shape, mesh);
return mesh;
}
示例13: SaveMeshVertices
public static void SaveMeshVertices(ref AnimatTools.Interfaces.StdXml oXml, string strName, Mesh myMesh, bool bSaveIndices)
{
CustomVertex.PositionNormalTextured[] cvVerts = new CustomVertex.PositionNormalTextured[myMesh.NumberVertices];
oXml.AddChildElement(strName);
oXml.IntoElem(); //Into the mesh
GraphicsStream buffer = myMesh.LockVertexBuffer(LockFlags.ReadOnly);
AnimatTools.Framework.Vec3d vVertex = new AnimatTools.Framework.Vec3d(null);
for(int i=0; i<myMesh.NumberVertices; i++)
{
buffer.Position = i * myMesh.NumberBytesPerVertex;
cvVerts[i] = (CustomVertex.PositionNormalTextured)buffer.Read(typeof(CustomVertex.PositionNormalTextured));
//System.Diagnostics.Debug.WriteLine("v " + cvVerts[i].X + " " + cvVerts[i].Y + " " + cvVerts[i].Z);
vVertex.X = cvVerts[i].X;// - this.AbsoluteLocation.X;
vVertex.Y = cvVerts[i].Y;// - this.AbsoluteLocation.Y;
vVertex.Z = cvVerts[i].Z;// - this.AbsoluteLocation.Z;
Util.SaveVector(ref oXml, "Vector", vVertex);
//Debug.WriteLine("data.Write(new CustomVertex.PositionTextured(" + vVertex.X + "f, " + vVertex.Y + "f, " + vVertex.Z + "f, 0f, 0f));");
//if(i%3 == 0) Debug.WriteLine("");
//Debug.WriteLine("V: " + i + " (" + cvVerts[i].X + ", " + cvVerts[i].Y + ", " + cvVerts[i].Z + ", " + cvVerts[i].Nx + ", " + cvVerts[i].Ny + ", " + cvVerts[i].Nz + ")");
}
myMesh.UnlockVertexBuffer();
if(bSaveIndices)
{
using (IndexBuffer ib = myMesh.IndexBuffer)
{
GraphicsStream gs = ib.Lock(0, myMesh.NumberFaces*3*2, LockFlags.ReadOnly);
string strIndex = "";
short iIndex;
for(int i=0; i<myMesh.NumberFaces*3; i++)
{
gs.Position = i * 2;
iIndex = (short) gs.Read(typeof(short));
strIndex += iIndex.ToString();
if(i<gs.Length-1) strIndex += ",";
}
oXml.AddChildElement("IndexBuffer", strIndex);
ib.Unlock();
}
}
oXml.OutOfElem(); //Out of the mesh
}
示例14: CreateStaticPlaneShape
Mesh CreateStaticPlaneShape(StaticPlaneShape shape)
{
// Load shader
if (planeShader == null)
{
Assembly assembly = Assembly.GetExecutingAssembly();
Stream shaderStream = assembly.GetManifestResourceStream("DemoFramework.checker_shader.fx");
planeShader = Effect.FromStream(device, shaderStream, ShaderFlags.None);
}
Vector3[] vertices = new Vector3[4 * 2];
Mesh mesh = new Mesh(device, 2, 4, MeshFlags.SystemMemory, VertexFormat.Position | VertexFormat.Normal);
Vector3 planeOrigin = shape.PlaneNormal * shape.PlaneConstant;
Vector3 vec0, vec1;
PlaneSpace1(shape.PlaneNormal, out vec0, out vec1);
float size = 1000;
Vector3[] verts = new Vector3[4]
{
planeOrigin + vec0*size,
planeOrigin - vec0*size,
planeOrigin + vec1*size,
planeOrigin - vec1*size
};
SlimDX.DataStream vertexBuffer = mesh.LockVertexBuffer(LockFlags.Discard);
vertexBuffer.Write(verts[0]);
vertexBuffer.Position += 12;
vertexBuffer.Write(verts[1]);
vertexBuffer.Position += 12;
vertexBuffer.Write(verts[2]);
vertexBuffer.Position += 12;
vertexBuffer.Write(verts[3]);
vertexBuffer.Position += 12;
mesh.UnlockVertexBuffer();
SlimDX.DataStream indexBuffer = mesh.LockIndexBuffer(LockFlags.Discard);
indexBuffer.Write((short)1);
indexBuffer.Write((short)2);
indexBuffer.Write((short)0);
indexBuffer.Write((short)1);
indexBuffer.Write((short)3);
indexBuffer.Write((short)0);
mesh.UnlockIndexBuffer();
mesh.ComputeNormals();
complexShapes.Add(shape, mesh);
return mesh;
}
示例15: CreateConvexHullShape
Mesh CreateConvexHullShape(ConvexHullShape shape)
{
ShapeHull hull = new ShapeHull(shape);
hull.BuildHull(shape.Margin);
UIntArray hullIndices = hull.Indices;
Vector3Array points = hull.Vertices;
int vertexCount = hull.NumIndices;
int faceCount = hull.NumTriangles;
bool index32 = vertexCount > 65536;
Mesh mesh = new Mesh(device, faceCount, vertexCount,
MeshFlags.SystemMemory | (index32 ? MeshFlags.Use32Bit : 0), VertexFormat.Position | VertexFormat.Normal);
SlimDX.DataStream indices = mesh.LockIndexBuffer(LockFlags.Discard);
int i;
if (index32)
{
for (i = 0; i < vertexCount; i++)
indices.Write(i);
}
else
{
for (i = 0; i < vertexCount; i++)
indices.Write((short)i);
}
mesh.UnlockIndexBuffer();
SlimDX.DataStream verts = mesh.LockVertexBuffer(LockFlags.Discard);
Vector3 scale = Vector3.Multiply(shape.LocalScaling, 1.0f + shape.Margin);
for (i = 0; i < vertexCount; i += 3)
{
verts.Write(Vector3.Modulate(points[(int)hullIndices[i]], scale));
verts.Position += 12;
verts.Write(Vector3.Modulate(points[(int)hullIndices[i+1]], scale));
verts.Position += 12;
verts.Write(Vector3.Modulate(points[(int)hullIndices[i+2]], scale));
verts.Position += 12;
}
mesh.UnlockVertexBuffer();
mesh.ComputeNormals();
shapes.Add(shape, mesh);
return mesh;
}