DrawTextureHexClass.cs
上传用户:lslight
上传日期:2022-01-10
资源大小:14248k
文件大小:6k
- ////////////////////////////////////////////////////////////////////////
- // ■■■■ ■■■■■ ■■■■ ■ ■ //
- // ■ ■ ■ ■ ■ //
- // ■ ■ ■ ■■■ ■ ■ //
- // ■ ■ ■ ■ ■ ■ //
- // ■■■■ ■ ■■■■ ■■■■ //
- // Copyright (c) 三峡大学水利与环境学院 肖泽云. All rights reserved. //
- ////////////////////////////////////////////////////////////////////////
- using System;
- using System.Collections.Generic;
- using System.Text;
- using Microsoft.DirectX;
- using Microsoft.DirectX.Direct3D;
- namespace 贴图
- {
- class DrawTextureHexClass
- {
- public static Mesh DrawTextureHexObject(Device device, Vector3[] pointVectors, string texturePath, out Texture meshtexture)//定义网格
- {
- //定义模型贴图并导入贴图
- Texture meshTexture = TextureLoader.FromFile(device, texturePath);
- Mesh meshObject;
- //定义顶点
- CustomVertex.PositionTextured[] arrayVertices = new CustomVertex.PositionTextured[36];
- //定义索引
- Int16[] arrayIndices = new Int16[36];
- //定义属性
- AttributeRange attributeRange = new AttributeRange();
- //实例化网格对象
- meshObject = new Mesh(arrayIndices.Length / 3, arrayVertices.Length, MeshFlags.SystemMemory, CustomVertex.PositionTextured.Format, device);
- //设置顶点坐标值及贴图坐标,顶部三角形
- arrayVertices[0].Position = pointVectors[0];
- arrayVertices[0].Tu = 0; arrayVertices[0].Tv = 1;
- arrayVertices[1].Position = pointVectors[1];
- arrayVertices[1].Tu = 0; arrayVertices[1].Tv = 0;
- arrayVertices[2].Position = pointVectors[3];
- arrayVertices[2].Tu = 1; arrayVertices[2].Tv = 1;
- arrayVertices[3] = arrayVertices[1];
- arrayVertices[4].Position = pointVectors[2];
- arrayVertices[4].Tu = 1; arrayVertices[4].Tv = 0;
- arrayVertices[5] = arrayVertices[2];
- //周围三角形
- arrayVertices[6].Position = pointVectors[0];
- arrayVertices[6].Tu = 0; arrayVertices[6].Tv = 0;
- arrayVertices[7].Position = pointVectors[7];
- arrayVertices[7].Tu = 1; arrayVertices[7].Tv = 1;
- arrayVertices[8].Position = pointVectors[4];
- arrayVertices[8].Tu = 0; arrayVertices[8].Tv = 1;
- arrayVertices[9] = arrayVertices[6];
- arrayVertices[10].Position = pointVectors[3];
- arrayVertices[10].Tu = 1; arrayVertices[10].Tv = 0;
- arrayVertices[11] = arrayVertices[7];
- arrayVertices[12].Position = pointVectors[3];
- arrayVertices[12].Tu = 0; arrayVertices[12].Tv = 0;
- arrayVertices[13].Position = pointVectors[6];
- arrayVertices[13].Tu = 1; arrayVertices[13].Tv = 1;
- arrayVertices[14].Position = pointVectors[7];
- arrayVertices[14].Tu = 0; arrayVertices[14].Tv = 1;
- arrayVertices[15] = arrayVertices[12];
- arrayVertices[16].Position = pointVectors[2];
- arrayVertices[16].Tu = 1; arrayVertices[16].Tv = 0;
- arrayVertices[17] = arrayVertices[13];
- arrayVertices[18].Position = pointVectors[2];
- arrayVertices[18].Tu = 0; arrayVertices[18].Tv = 0;
- arrayVertices[19].Position = pointVectors[5];
- arrayVertices[19].Tu = 1; arrayVertices[19].Tv = 1;
- arrayVertices[20].Position = pointVectors[6];
- arrayVertices[20].Tu = 0; arrayVertices[20].Tv = 1;
- arrayVertices[21] = arrayVertices[18];
- arrayVertices[22].Position = pointVectors[1];
- arrayVertices[22].Tu = 1; arrayVertices[22].Tv = 0;
- arrayVertices[23] = arrayVertices[19];
- arrayVertices[24].Position = pointVectors[1];
- arrayVertices[24].Tu = 0; arrayVertices[24].Tv = 0;
- arrayVertices[25].Position = pointVectors[4];
- arrayVertices[25].Tu = 1; arrayVertices[25].Tv = 1;
- arrayVertices[26].Position = pointVectors[5];
- arrayVertices[26].Tu = 0; arrayVertices[26].Tv = 1;
- arrayVertices[27] = arrayVertices[24];
- arrayVertices[28].Position = pointVectors[0];
- arrayVertices[28].Tu = 1; arrayVertices[28].Tv = 0;
- arrayVertices[29] = arrayVertices[25];
- //底部三角形
- arrayVertices[30].Position = pointVectors[4];
- arrayVertices[30].Tu = 0; arrayVertices[30].Tv = 0;
- arrayVertices[31].Position = pointVectors[6];
- arrayVertices[31].Tu = 1; arrayVertices[31].Tv = 1;
- arrayVertices[32].Position = pointVectors[5];
- arrayVertices[32].Tu = 0; arrayVertices[32].Tv = 1;
- arrayVertices[33] = arrayVertices[30];
- arrayVertices[34].Position = pointVectors[7];
- arrayVertices[34].Tu = 1; arrayVertices[34].Tv = 0;
- arrayVertices[35] = arrayVertices[31];
- //设置索引,顶部三角形
- for (Int16 i = 0; i < arrayIndices.Length; i++)
- {
- arrayIndices[i] = i;
- }
- //设置属性
- attributeRange.AttributeId = 0;
- attributeRange.FaceStart = 0;
- attributeRange.FaceCount = arrayIndices.Length / 3;
- attributeRange.VertexStart = 0;
- attributeRange.VertexCount = arrayVertices.Length;
- //设置网格对象的索引缓冲和顶点缓冲数据
- meshObject.VertexBuffer.SetData(arrayVertices, 0, LockFlags.None);
- meshObject.IndexBuffer.SetData(arrayIndices, 0, LockFlags.None);
- meshObject.SetAttributeTable(new AttributeRange[] { attributeRange });
- meshtexture = meshTexture;
- return meshObject;
- }
- }
- }