/// <summary> /// Crea un mesh sin texturas, solo con VertexColors /// </summary> /// <param name="meshData"></param> private TgcKeyFrameMesh crearMeshSoloColor(TgcKeyFrameMeshData meshData) { //Crear Mesh Mesh mesh = new Mesh(meshData.coordinatesIndices.Length / 3, meshData.coordinatesIndices.Length, MeshFlags.Managed, VertexColorVertexElements, 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++) { VertexColorVertex v = new VertexColorVertex(); //vertices int coordIdx = meshData.coordinatesIndices[j] * 3; v.Position = new Vector3( meshData.verticesCoordinates[coordIdx], meshData.verticesCoordinates[coordIdx + 1], meshData.verticesCoordinates[coordIdx + 2] ); //color int colorIdx = meshData.colorIndices[j]; v.Color = meshData.verticesColors[colorIdx]; data.Write(v); } vb.Unlock(); } //Cargar indexBuffer en forma plana using (IndexBuffer ib = mesh.IndexBuffer) { short[] indices = new short[meshData.coordinatesIndices.Length]; for (int i = 0; i < indices.Length; i++) { indices[i] = (short)i; } ib.SetData(indices, 0, LockFlags.None); } //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 = null; originalData.textureCoordinates = null; //Crear mesh de TGC TgcKeyFrameMesh tgcMesh = new TgcKeyFrameMesh(mesh, meshData.name, TgcKeyFrameMesh.MeshRenderType.VERTEX_COLOR, originalData); return(tgcMesh); }
/// <summary> /// Crea un mesh sin texturas, solo con VertexColors /// </summary> /// <param name="meshData"></param> private TgcKeyFrameMesh crearMeshSoloColor(TgcKeyFrameMeshData meshData) { //Crear Mesh Mesh mesh = new Mesh(meshData.coordinatesIndices.Length / 3, meshData.coordinatesIndices.Length, MeshFlags.Managed, VertexColorVertexElements, 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++) { VertexColorVertex v = new VertexColorVertex(); //vertices int coordIdx = meshData.coordinatesIndices[j] * 3; v.Position = new Vector3( meshData.verticesCoordinates[coordIdx], meshData.verticesCoordinates[coordIdx + 1], meshData.verticesCoordinates[coordIdx + 2] ); //color int colorIdx = meshData.colorIndices[j]; v.Color = meshData.verticesColors[colorIdx]; data.Write(v); } vb.Unlock(); } //Cargar indexBuffer en forma plana using (IndexBuffer ib = mesh.IndexBuffer) { short[] indices = new short[meshData.coordinatesIndices.Length]; for (int i = 0; i < indices.Length; i++) { indices[i] = (short)i; } ib.SetData(indices, 0, LockFlags.None); } //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 = null; originalData.textureCoordinates = null; //Crear mesh de TGC TgcKeyFrameMesh tgcMesh = new TgcKeyFrameMesh(mesh, meshData.name, TgcKeyFrameMesh.MeshRenderType.VERTEX_COLOR, originalData); return tgcMesh; }
/// <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; }
/// <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); }