本文整理汇总了C#中Mesh.UnlockAttributeBuffer方法的典型用法代码示例。如果您正苦于以下问题:C# Mesh.UnlockAttributeBuffer方法的具体用法?C# Mesh.UnlockAttributeBuffer怎么用?C# Mesh.UnlockAttributeBuffer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Mesh
的用法示例。
在下文中一共展示了Mesh.UnlockAttributeBuffer方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: fillAttributeBuffer
protected virtual void fillAttributeBuffer(Mesh mesh, ref int attribId)
{
List<int> attribList = new List<int>();
int faceStart = 0;
int faceEnd = 0;
foreach (Document.Primitive primitive in Primitives)
{
faceEnd = faceStart + primitive.count;
for (int i = faceStart; i < faceEnd; i++)
attribList.Add(attribId);
attribId++;
faceStart = faceEnd;
}
DataStream ds = mesh.LockAttributeBuffer(LockFlags.None);
ds.WriteRange(attribList.ToArray());
mesh.UnlockAttributeBuffer();
}
示例2: 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;
}
示例3: fromTgcMesh
//.........这里部分代码省略.........
//Crear nuevo VertexBuffer
using (VertexBuffer vb = d3dMesh.VertexBuffer)
{
//Iterar sobre triangulos
GraphicsStream data = vb.Lock(0, 0, LockFlags.None);
for (int i = 0; i < triCount; i++)
{
//Vertices originales
TgcSceneLoader.DiffuseMapVertex vOrig1 = origVertexBuffer[i * 3];
TgcSceneLoader.DiffuseMapVertex vOrig2 = origVertexBuffer[i * 3 + 1];
TgcSceneLoader.DiffuseMapVertex vOrig3 = origVertexBuffer[i * 3 + 2];
//Nuevo vertice 1
BumpMappingVertex v1 = new BumpMappingVertex();
v1.Position = vOrig1.Position;
v1.Color = vOrig1.Color;
v1.Tu = vOrig1.Tu;
v1.Tv = vOrig1.Tv;
v1.Normal = normals[i * 3];
//Nuevo vertice 2
BumpMappingVertex v2 = new BumpMappingVertex();
v2.Position = vOrig2.Position;
v2.Color = vOrig2.Color;
v2.Tu = vOrig2.Tu;
v2.Tv = vOrig2.Tv;
v2.Normal = normals[i * 3 + 1];
//Nuevo vertice 3
BumpMappingVertex v3 = new BumpMappingVertex();
v3.Position = vOrig3.Position;
v3.Color = vOrig3.Color;
v3.Tu = vOrig3.Tu;
v3.Tv = vOrig3.Tv;
v3.Normal = normals[i * 3 + 2];
//Calcular tangente y binormal para todo el triangulo y cargarlas en cada vertice
Vector3 tangent;
Vector3 binormal;
TgcMeshBumpMapping.computeTangentBinormal(v1, v2, v3, out tangent, out binormal);
v1.Tangent = tangent;
v1.Binormal = binormal;
v2.Tangent = tangent;
v2.Binormal = binormal;
v3.Tangent = tangent;
v3.Binormal = binormal;
//Cargar VertexBuffer
data.Write(v1);
data.Write(v2);
data.Write(v3);
}
vb.Unlock();
}
//Cargar IndexBuffer en forma plana
using (IndexBuffer ib = d3dMesh.IndexBuffer)
{
short[] indices = new short[origVertexBuffer.Length];
for (int i = 0; i < indices.Length; i++)
{
indices[i] = (short)i;
}
ib.SetData(indices, 0, LockFlags.None);
}
//Clonar texturas y materials
TgcTexture[] diffuseMaps = new TgcTexture[mesh.DiffuseMaps.Length];
Material[] materials = new Material[mesh.Materials.Length];
for (int i = 0; i < mesh.DiffuseMaps.Length; i++)
{
diffuseMaps[i] = mesh.DiffuseMaps[i].clone();
materials[i] = TgcD3dDevice.DEFAULT_MATERIAL;
}
//Cargar attributeBuffer
if (diffuseMaps.Length > 1)
{
int[] origAttributeBuffer = mesh.D3dMesh.LockAttributeBufferArray(LockFlags.None);
int[] newAttributeBuffer = d3dMesh.LockAttributeBufferArray(LockFlags.None);
Array.Copy(origAttributeBuffer, newAttributeBuffer, origAttributeBuffer.Length);
mesh.D3dMesh.UnlockAttributeBuffer();
d3dMesh.UnlockAttributeBuffer(newAttributeBuffer);
}
//Crear mesh de BumpMapping Mesh
TgcMeshBumpMapping bumpMesh = new TgcMeshBumpMapping(d3dMesh, mesh.Name, mesh.RenderType);
bumpMesh.diffuseMaps = diffuseMaps;
bumpMesh.materials = materials;
bumpMesh.normalMaps = normalMaps;
bumpMesh.layer = mesh.Layer;
bumpMesh.alphaBlendEnable = mesh.AlphaBlendEnable;
bumpMesh.UserProperties = mesh.UserProperties;
bumpMesh.boundingBox = mesh.BoundingBox.clone();
bumpMesh.enabled = true;
return bumpMesh;
}
示例4: d3d_DxRestore
/// <summary>
/// Restore the mesh when the DirectX device is restored
/// </summary>
void d3d_DxRestore(Direct3d d3d, Device dx)
{
// If the direct3d device wasn't lost in the first place, don't restore it.
// This happens the first timeMs around.
if (mMesh != null)
return;
// Restore mesh
mMesh = new Mesh(mNumFaces, mNumVertices, mFlags, mVertexFormat, dx);
Debug.Assert(mMesh.NumberBytesPerVertex == mNumBytesPerVertex);
// Copy pathIndex buffer
GraphicsStream stream = mMesh.LockIndexBuffer(LockFlags.Discard);
stream.Write(mIndexBufferCopy);
mMesh.UnlockIndexBuffer();
// Copy vertex buffer
stream = mMesh.LockVertexBuffer(LockFlags.Discard);
stream.Write(mVertexBufferCopy);
mMesh.UnlockVertexBuffer();
// Copy attribute buffer
int[] attributeBuffer = mMesh.LockAttributeBufferArray(LockFlags.Discard);
mAttributeBufferCopy.CopyTo(attributeBuffer, 0);
mMesh.UnlockAttributeBuffer(attributeBuffer);
mIndexBufferCopy = null;
mVertexBufferCopy = null;
mAttributeBufferCopy = null;
}
示例5: mergeTwoMeshes
//.........这里部分代码省略.........
foreach (Material m in mesh1.Materials)
{
materials[mIdx++] = m;
}
foreach (Material m in mesh2.Materials)
{
materials[mIdx++] = m;
}
//Texturas del mesh1
textures = new TgcTexture[mesh1.DiffuseMaps.Length + mesh2.DiffuseMaps.Length];
int tIdx = 0;
foreach (TgcTexture t in mesh1.DiffuseMaps)
{
textures[tIdx++] = t.clone();
}
//Texturas del mesh2
foreach (TgcTexture t in mesh2.DiffuseMaps)
{
textures[tIdx++] = t.clone();
}
//Cargar el AttributeBuffer con la suma de ambos mesh
int attIdx = 0;
int textureId = 0;
int[] attributeBuffer = mesh.LockAttributeBufferArray(LockFlags.None);
//AttributeBuffer del mesh 1
if (mesh1.DiffuseMaps.Length > 1)
{
//Copiar el AttributeBuffer del mesh1 tal cual al mesh unificado
int[] attributeBuffer1 = mesh1.D3dMesh.LockAttributeBufferArray(LockFlags.ReadOnly);
Array.Copy(attributeBuffer1, attributeBuffer, attributeBuffer1.Length);
mesh1.D3dMesh.UnlockAttributeBuffer(attributeBuffer1);
}
else
{
//Hay una sola textura, llenar el AttributeBuffer para que apunte solo a esa textura
for (int i = 0; i < mesh1.NumberTriangles; i++)
{
attributeBuffer[i] = textureId;
}
}
attIdx += mesh1.NumberTriangles;
textureId += mesh1.DiffuseMaps.Length;
//AttributeBuffer del mesh 2
if (mesh2.DiffuseMaps.Length > 1)
{
//Copiar el AttributeBuffer del mesh2 al mesh unificado pero sumando el offset de texturas del primero
int[] attributeBuffer2 = mesh2.D3dMesh.LockAttributeBufferArray(LockFlags.ReadOnly);
int[] attributeBuffer2Offset = new int[attributeBuffer2.Length];
for (int i = 0; i < attributeBuffer2.Length; i++)
{
attributeBuffer2Offset[i] = attributeBuffer2[i] + textureId;
}
mesh2.D3dMesh.UnlockAttributeBuffer(attributeBuffer2);
Array.Copy(attributeBuffer2Offset, 0, attributeBuffer, attIdx, attributeBuffer2Offset.Length);
attributeBuffer2Offset = null;
}
else
{
//Hay una sola textura, llenar el AttributeBuffer para que apunte solo a esa textura
for (int i = 0; i < mesh2.NumberTriangles; i++)
{
attributeBuffer[attIdx++] = textureId;
示例6: crearMeshDiffuseMap
//.........这里部分代码省略.........
//normal
if (meshData.verticesNormals != null)
{
v.Normal = new Vector3(
meshData.verticesNormals[coordIdx],
meshData.verticesNormals[coordIdx + 1],
meshData.verticesNormals[coordIdx + 2]
);
}
else
{
v.Normal = new Vector3(0, 0, 0);
}
//tangent
if (meshData.verticesTangents != null)
{
v.Tangent = new Vector3(
meshData.verticesTangents[coordIdx],
meshData.verticesTangents[coordIdx + 1],
meshData.verticesTangents[coordIdx + 2]
);
}
else
{
v.Tangent = new Vector3(0, 0, 0);
}
//binormal
if (meshData.verticesBinormals != null)
{
v.Binormal = new Vector3(
meshData.verticesBinormals[coordIdx],
meshData.verticesBinormals[coordIdx + 1],
meshData.verticesBinormals[coordIdx + 2]
);
}
else
{
v.Binormal = new Vector3(0, 0, 0);
}
//BlendWeights y BlendIndices
TgcSkeletalVertexWeight vWeight = verticesWeights[meshData.coordinatesIndices[j]];
vWeight.createVector4WeightsAndIndices(out v.BlendWeights, out v.BlendIndices);
data.Write(v);
}
vb.Unlock();
}
//Cargar IndexBuffer en forma plana
using (IndexBuffer ib = mesh.IndexBuffer)
{
short[] indices = new short[meshData.coordinatesIndices.Length];
for (int j = 0; j < indices.Length; j++)
{
indices[j] = (short)j;
}
ib.SetData(indices, 0, LockFlags.None);
}
//Configurar Material y Textura para un solo SubSet
TgcSkeletalLoaderMaterialAux matAux = materialsArray[meshData.materialId];
Material[] meshMaterials;
TgcTexture[] meshTextures;
if (matAux.subMaterials == null)
{
meshMaterials = new Material[] { matAux.materialId };
meshTextures = new TgcTexture[] { matAux.texture };
}
//Configurar Material y Textura para varios SubSet
else
{
//Cargar attributeBuffer con los id de las texturas de cada triángulo
int[] attributeBuffer = mesh.LockAttributeBufferArray(LockFlags.None);
Array.Copy(meshData.materialsIds, attributeBuffer, attributeBuffer.Length);
mesh.UnlockAttributeBuffer(attributeBuffer);
//Cargar array de Materials y Texturas
meshMaterials = new Material[matAux.subMaterials.Length];
meshTextures = new TgcTexture[matAux.subMaterials.Length];
for (int m = 0; m < matAux.subMaterials.Length; m++)
{
meshMaterials[m] = matAux.subMaterials[m].materialId;
meshTextures[m] = matAux.subMaterials[m].texture;
}
}
//Crear mesh de TGC
TgcSkeletalMesh tgcMesh = meshFactory.createNewMesh(mesh, meshData.name, TgcSkeletalMesh.MeshRenderType.DIFFUSE_MAP, bones);
tgcMesh.Materials = meshMaterials;
tgcMesh.DiffuseMaps = meshTextures;
return tgcMesh;
}
示例7: crearMeshDiffuseMap
/// <summary>
/// Crea un mesh con uno o varios DiffuseMap
/// </summary>
/// <returns></returns>
private TgcKeyFrameMesh crearMeshDiffuseMap(TgcKeyFrameLoaderMaterialAux[] materialsArray, TgcKeyFrameMeshData meshData)
{
//Crear Mesh
Mesh mesh = new Mesh(meshData.coordinatesIndices.Length / 3, meshData.coordinatesIndices.Length, MeshFlags.Managed, DiffuseMapVertexElements, device);
//Cargar VertexBuffer
using (VertexBuffer vb = mesh.VertexBuffer)
{
GraphicsStream data = vb.Lock(0, 0, LockFlags.None);
for (int j = 0; j < meshData.coordinatesIndices.Length; j++)
{
DiffuseMapVertex v = new DiffuseMapVertex();
//vertices
int coordIdx = meshData.coordinatesIndices[j] * 3;
v.Position = new Vector3(
meshData.verticesCoordinates[coordIdx],
meshData.verticesCoordinates[coordIdx + 1],
meshData.verticesCoordinates[coordIdx + 2]
);
//texture coordinates diffuseMap
int texCoordIdx = meshData.texCoordinatesIndices[j] * 2;
v.Tu = meshData.textureCoordinates[texCoordIdx];
v.Tv = meshData.textureCoordinates[texCoordIdx + 1];
//color
int colorIdx = meshData.colorIndices[j];
v.Color = meshData.verticesColors[colorIdx];
data.Write(v);
}
vb.Unlock();
}
//Cargar IndexBuffer
using (IndexBuffer ib = mesh.IndexBuffer)
{
short[] indices = new short[meshData.coordinatesIndices.Length];
for (int j = 0; j < indices.Length; j++)
{
indices[j] = (short)j;
}
ib.SetData(indices, 0, LockFlags.None);
}
//Configurar Material y Textura para un solo SubSet
TgcKeyFrameLoaderMaterialAux matAux = materialsArray[meshData.materialId];
Material[] meshMaterials;
TgcTexture[] meshTextures;
if (matAux.subMaterials == null)
{
meshMaterials = new Material[] { matAux.materialId };
meshTextures = new TgcTexture[] { matAux.texture };
}
//Configurar Material y Textura para varios SubSet
else
{
//Cargar attributeBuffer con los id de las texturas de cada triángulo
int[] attributeBuffer = mesh.LockAttributeBufferArray(LockFlags.None);
Array.Copy(meshData.materialsIds, attributeBuffer, attributeBuffer.Length);
mesh.UnlockAttributeBuffer(attributeBuffer);
//Cargar array de Materials y Texturas
meshMaterials = new Material[matAux.subMaterials.Length];
meshTextures = new TgcTexture[matAux.subMaterials.Length];
for (int m = 0; m < matAux.subMaterials.Length; m++)
{
meshMaterials[m] = matAux.subMaterials[m].materialId;
meshTextures[m] = matAux.subMaterials[m].texture;
}
}
//Cargar datos que originales que tienen que mantenerse
TgcKeyFrameMesh.OriginalData originalData = new TgcKeyFrameMesh.OriginalData();
originalData.coordinatesIndices = meshData.coordinatesIndices;
originalData.colorIndices = meshData.colorIndices;
originalData.verticesColors = meshData.verticesColors;
originalData.texCoordinatesIndices = meshData.texCoordinatesIndices;
originalData.textureCoordinates = meshData.textureCoordinates;
//Crear mesh de TGC
TgcKeyFrameMesh tgcMesh = new TgcKeyFrameMesh(mesh, meshData.name, TgcKeyFrameMesh.MeshRenderType.DIFFUSE_MAP, originalData);
tgcMesh.Materials = meshMaterials;
tgcMesh.DiffuseMaps = meshTextures;
return tgcMesh;
}
示例8: BuildGridGeometry
void BuildGridGeometry()
{
Vector3[] globalverts, gridverts;
int[] globalindices, gridindices, gridindices2;
float dx = 10.0f;
float dz = 10.0f;
// Generate global grid
GenTriGrid(inputImageHeight, inputImageWidth, dx, dz, new Vector3(0.0f, -1000f, 0f), out globalverts, out globalindices);
// Number of sub-grids
int nGridsY = GetGoodAverage(inputImageHeight);
int nGridsX = GetGoodAverage(inputImageWidth);
// Subgrid size
int GridW = inputImageWidth / nGridsX;
int GridD = inputImageHeight / nGridsY;
int gridNumVerts = (GridW+1) * (GridD+1);
int gridNumTris = (GridD ) * (GridW ) * 2;
// Generate subgrid indices
GenTriGrid(GridD+1, GridW+1, dx, dz, new Vector3(0.0f, -5000f, 0f), out gridverts, out gridindices);
GenTriGrid(GridD, GridW, dx, dz, new Vector3(0.0f, -5000f, 0f), out gridverts, out gridindices2);
// Define some often used variables
bool overflowX = false, overflowY = false;
float w = (GridW*nGridsX) * dx;
float d = (GridD * nGridsY) * dz;
Vector3 normal = new Vector3(0f, 1f, 0f);
Mesh mesh;
VertexPositionNormalTextured[] vertexData = new VertexPositionNormalTextured[gridNumVerts];
int subgridX, subgridY, globalIndex, gridIndex;
// foreach subgrid
for (int gridX = 0; gridX < nGridsX; gridX++)
{
for (int gridY = 0; gridY < nGridsY; gridY++)
{
overflowY = false;
overflowX = false;
mesh = new Mesh(Renderer.Instance.device, gridNumTris, gridNumVerts, MeshFlags.Use32Bit, VertexPositionNormalTextured.Format);
// Check for overflow
if ((gridX+1) * (GridW + 1) > inputImageWidth)
overflowX = true;
else if ((gridY+1) * (GridD + 1) > inputImageHeight)
overflowY = true;
if (overflowY || overflowX)
{
}
else
{
for (int subD = 0; subD < GridD + 1; ++subD)
{
for (int subW = 0; subW < GridW + 1; ++subW)
{
subgridX = gridX * GridW + subW;
subgridY = gridY * GridD + subD;
globalIndex = (subgridY * inputImageHeight) + subgridX;
gridIndex = (subD * (GridD + 1)) + subW;
vertexData[gridIndex].Position = globalverts[globalIndex];
//vertexData[gridIndex].Y += GetHeightFromImage(subgridY, subgridX) * scale;
vertexData[gridIndex].Position.Y += SampleHeightMap3x3(subgridY, subgridX) * scale;
vertexData[gridIndex].Normal = normal;
vertexData[gridIndex].TextureCoordinate = new Vector2((vertexData[gridIndex].Position.X + (0.5f * w)) / w, (vertexData[gridIndex].Position.Z - (0.5f * d)) / -d);
}
}
DataStream gs = mesh.LockVertexBuffer(LockFlags.None);
gs.WriteRange<VertexPositionNormalTextured>(vertexData);
gs.Seek(0, SeekOrigin.Begin);
// Todo: Fix AABB and frustrum culling
Vector3 min, max;
//Geometry.ComputeBoundingBox(gs, gridNumVerts, VertexPositionNormalTextured.Format, out min, out max);
mesh.UnlockVertexBuffer();
//int[] meshIndices = new int[gridNumTris * 3];
DataStream ds = mesh.LockAttributeBuffer(LockFlags.None);
for (int i = 0; i < gridNumTris; ++i)
{
ds.Write<int>(0);
}
mesh.UnlockAttributeBuffer();
//meshIndices = ;
gs = mesh.LockIndexBuffer(LockFlags.None);
gs.WriteRange<int>(gridindices);
mesh.UnlockIndexBuffer();
//gs = mesh.LockAttributeBuffer(LockFlags.None);
//gs.Write(meshAttr);
mesh.ComputeNormals();
int[] adj = new int[mesh.FaceCount * 3];
mesh.GenerateAdjacency(float.Epsilon);
//mesh.OptimizeInPlace(MeshFlags.OptimizeAttributeSort | MeshFlags.OptimizeVertexCache | MeshFlags.OptimizeCompact, adj);
Mesh newmesh = mesh.Clone(Renderer.Instance.device, MeshFlags.Managed, VertexPosTexNormalTanBitan.Elements);
//.........这里部分代码省略.........
示例9: crearMeshDiffuseMapLightmap
/// <summary>
/// Crea un mesh con uno o varios DiffuseMap y un Lightmap
/// </summary>
/// <returns></returns>
private TgcMesh crearMeshDiffuseMapLightmap(TgcSceneData sceneData, string mediaPath, TgcSceneLoaderMaterialAux[] materialsArray, TgcMeshData meshData)
{
//Crear Mesh
Mesh mesh = new Mesh(meshData.coordinatesIndices.Length / 3, meshData.coordinatesIndices.Length, MeshFlags.Managed, DiffuseMapAndLightmapVertexElements, device);
//Cargar vertexBuffer
using (VertexBuffer vb = mesh.VertexBuffer)
{
GraphicsStream data = vb.Lock(0, 0, LockFlags.None);
for (int j = 0; j < meshData.coordinatesIndices.Length; j++)
{
DiffuseMapAndLightmapVertex v = new DiffuseMapAndLightmapVertex();
//vertices
int coordIdx = meshData.coordinatesIndices[j] * 3;
v.Position = new Vector3(
meshData.verticesCoordinates[coordIdx],
meshData.verticesCoordinates[coordIdx + 1],
meshData.verticesCoordinates[coordIdx + 2]
);
//normals
//puede haber una normal compartida para cada vertice del mesh
if (meshData.verticesNormals.Length == meshData.verticesCoordinates.Length)
{
v.Normal = new Vector3(
meshData.verticesNormals[coordIdx],
meshData.verticesNormals[coordIdx + 1],
meshData.verticesNormals[coordIdx + 2]
);
}
//o una normal propia por cada vertice de cada triangulo (version mejorada del exporter)
else
{
int normalIdx = j * 3;
v.Normal = new Vector3(
meshData.verticesNormals[normalIdx],
meshData.verticesNormals[normalIdx + 1],
meshData.verticesNormals[normalIdx + 2]
);
}
//texture coordinates diffuseMap
int texCoordIdx = meshData.texCoordinatesIndices[j] * 2;
v.Tu0 = meshData.textureCoordinates[texCoordIdx];
v.Tv0 = meshData.textureCoordinates[texCoordIdx + 1];
//texture coordinates LightMap
int texCoordIdxLM = meshData.texCoordinatesIndicesLightMap[j] * 2;
v.Tu1 = meshData.textureCoordinatesLightMap[texCoordIdxLM];
v.Tv1 = meshData.textureCoordinatesLightMap[texCoordIdxLM + 1];
//color
int colorIdx = meshData.colorIndices[j];
v.Color = meshData.verticesColors[colorIdx];
data.Write(v);
}
vb.Unlock();
}
//Cargar IndexBuffer
using (IndexBuffer ib = mesh.IndexBuffer)
{
short[] indices = new short[meshData.coordinatesIndices.Length];
for (int j = 0; j < indices.Length; j++)
{
indices[j] = (short)j;
}
ib.SetData(indices, 0, LockFlags.None);
}
//Configurar Material y Textura para un solo SubSet
TgcSceneLoaderMaterialAux matAux = materialsArray[meshData.materialId];
Material[] meshMaterials;
TgcTexture[] meshTextures;
if (matAux.subMaterials == null)
{
meshMaterials = new Material[] { matAux.materialId };
meshTextures = new TgcTexture[] { matAux.texture };
}
//Configurar Material y Textura para varios SubSet
else
{
//Cargar attributeBuffer con los id de las texturas de cada triángulo
int[] attributeBuffer = mesh.LockAttributeBufferArray(LockFlags.None);
Array.Copy(meshData.materialsIds, attributeBuffer, attributeBuffer.Length);
mesh.UnlockAttributeBuffer(attributeBuffer);
//Cargar array de Materials y Texturas
meshMaterials = new Material[matAux.subMaterials.Length];
meshTextures = new TgcTexture[matAux.subMaterials.Length];
for (int m = 0; m < matAux.subMaterials.Length; m++)
{
//.........这里部分代码省略.........
示例10: 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;
}
示例11: 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;
}