public void AddCubeSurface(MeshTool.BlockSurface surface, int normalIndex, BlockTypeFunBase blockFun) { int startVericesNum = vertices.Count; //三角形 for (int t = 0; t < 6; t++) { triangles.Add(startVericesNum + indices[t]); } //顶点 for (int p = 0; p < 4; p++) { vertices.Add(surface.pos + MeshTool.VertexOffset[normalIndex, p]); normals.Add(MeshTool.NormalDefine[normalIndex]); tangents.Add(new Vector4(surface.raytraceAo[0], surface.raytraceAo[1], surface.raytraceAo[2], surface.raytraceAo[3])); colors.Add(blockFun.getFaceColor(surface.type, (Block.BlockFaceIndex)normalIndex)); } //uv Rect rect = blockFun.getTextureUV(surface.type, (Block.BlockFaceIndex)normalIndex); uv.Add(new Vector2(rect.xMax, rect.yMin)); uv.Add(new Vector2(rect.xMin, rect.yMin)); uv.Add(new Vector2(rect.xMin, rect.yMax)); uv.Add(new Vector2(rect.xMax, rect.yMax)); //uv2 uv2.Add(new Vector2(0, 0)); uv2.Add(new Vector2(0, 1)); uv2.Add(new Vector2(1, 1)); uv2.Add(new Vector2(1, 0)); }
private void AddSubSurface(MeshTool.BlockSurface surface, int normalIndex, BlockTypeFunBase blockFun, Rect rect, float depth) { int startVericesNum = vertices.Count; //三角形 for (int t = 0; t < 6; t++) { triangles.Add(startVericesNum + indices[t]); } //顶点 Vector3[] vs = GetSubSurfaceVertex(normalIndex, rect, depth); for (int p = 0; p < 4; p++) { Vector3 pos = surface.pos + vs[p]; vertices.Add(pos); normals.Add(MeshTool.NormalDefine[normalIndex]); //tangents.Add(new Vector4(surface.raytraceAo[0], surface.raytraceAo[1], surface.raytraceAo[2], surface.raytraceAo[3])); Color color = blockFun.getFaceColor(surface.type, (Block.BlockFaceIndex)normalIndex); //color *= ((vs[p] - Vector3.one * 0.5f).magnitude); //color.a = surface.raytraceAo[p]; color.a = (surface.extendAo[p] * 0.5f + 0.5f) * surface.raytraceAo[p]; colors.Add(color); } //uv Rect uvRect = blockFun.getTextureUV(surface.type, (Block.BlockFaceIndex)normalIndex); uvRect = GetSubRect(uvRect, rect); uv.Add(new Vector2(uvRect.xMax, uvRect.yMin)); uv.Add(new Vector2(uvRect.xMin, uvRect.yMin)); uv.Add(new Vector2(uvRect.xMin, uvRect.yMax)); uv.Add(new Vector2(uvRect.xMax, uvRect.yMax)); //uv2 uv2.Add(new Vector2(rect.xMin, rect.yMin)); uv2.Add(new Vector2(rect.xMax, rect.yMin)); uv2.Add(new Vector2(rect.xMax, rect.yMax)); uv2.Add(new Vector2(rect.xMin, rect.yMax)); }
public static Mesh createMesh(List <BlockSurface> surfacePoints, int normalIndex, Texture2D aoTexture, BlockTypeFunBase blockFun) { if (surfacePoints.Count > 0) { Vector3[] vertices = new Vector3[surfacePoints.Count * 4]; for (int i = 0; i < surfacePoints.Count; i++) { for (int p = 0; p < 4; p++) { vertices[i * 4 + p] = surfacePoints[i].pos + VertexOffset[normalIndex, p]; } } int[] triangles = new int[surfacePoints.Count * 6]; for (int i = 0; i < surfacePoints.Count; i++) { triangles[i * 6 + 0] = i * 4 + 0; triangles[i * 6 + 1] = i * 4 + 1; triangles[i * 6 + 2] = i * 4 + 2; triangles[i * 6 + 3] = i * 4 + 0; triangles[i * 6 + 4] = i * 4 + 2; triangles[i * 6 + 5] = i * 4 + 3; } Vector3[] normals = new Vector3[vertices.Length]; for (int i = 0; i < normals.Length; i++) { normals[i] = NormalDefine[normalIndex]; } Vector4[] tangents = new Vector4[surfacePoints.Count * 4]; if (aoTexture) { Color[] aoColors = aoTexture.GetPixels(); for (int i = 0; i < surfacePoints.Count; i++) { for (int v = 0; v < 4; v++) { tangents[i * 4 + v] = new Vector4(aoColors[i * 4 + 0].a, aoColors[i * 4 + 1].a, aoColors[i * 4 + 2].a, aoColors[i * 4 + 3].a); } } } Vector2[] uv = new Vector2[surfacePoints.Count * 4]; for (int i = 0; i < surfacePoints.Count; i++) { Rect rect = blockFun.getTextureUV(surfacePoints[i].type, (Block.BlockFaceIndex)normalIndex); uv[i * 4 + 0] = new Vector2(rect.xMax, rect.yMin); uv[i * 4 + 1] = new Vector2(rect.xMin, rect.yMin); uv[i * 4 + 2] = new Vector2(rect.xMin, rect.yMax); uv[i * 4 + 3] = new Vector2(rect.xMax, rect.yMax); } Vector2[] uv2 = new Vector2[surfacePoints.Count * 4]; for (int i = 0; i < surfacePoints.Count; i++) { uv2[i * 4 + 0] = new Vector2(0, 0); uv2[i * 4 + 1] = new Vector2(0, 1); uv2[i * 4 + 2] = new Vector2(1, 1); uv2[i * 4 + 3] = new Vector2(1, 0); } Color[] colors = new Color[surfacePoints.Count * 4]; for (int i = 0; i < surfacePoints.Count; i++) { for (int v = 0; v < 4; v++) { colors[i * 4 + v] = blockFun.getFaceColor(surfacePoints[i].type, (Block.BlockFaceIndex)normalIndex); } } Mesh mesh = new Mesh(); mesh.vertices = vertices; mesh.triangles = triangles; mesh.normals = normals; mesh.tangents = tangents; mesh.uv = uv; mesh.uv2 = uv2; mesh.colors = colors; return(mesh); } return(null); }