// 生成墙的多边形 private void create_wall_mesh_sub(Mesh mesh, int start, int end, WALL_SIDE side, float height) { int point_num = end - start + 1; // ---------------------------------------------------- // // 墙的截面形状 Vector3[] wall_vertices; wall_vertices = new Vector3[4]; wall_vertices[0] = Vector3.zero; wall_vertices[1] = wall_vertices[0] + Vector3.up * 0.5f; wall_vertices[2] = wall_vertices[1] + Vector3.right * (1.0f); wall_vertices[3] = wall_vertices[2] + Vector3.up * height; // ---------------------------------------------------- // // 顶点(位置坐标,uv) // 一个矩形所需要的顶点索引(两个三角形因此是6个) const int quad_index_num = 6; Vector3[] vertices = new Vector3[point_num * wall_vertices.Length]; Vector2[] uvs = new Vector2[point_num * wall_vertices.Length]; int[] triangles = new int[(point_num - 1) * wall_vertices.Length * quad_index_num]; Section section; Vector2 uv; if (side == WALL_SIDE.LEFT) { for (int i = 0; i < point_num; i++) { section = this.sections[start + i]; for (int j = 0; j < wall_vertices.Length; j++) { vertices[i * wall_vertices.Length + j] = section.positions[0]; vertices[i * wall_vertices.Length + j] += -wall_vertices[j].x * section.right + wall_vertices[j].y * section.up; // uv.x = (float)j / (float)(wall_vertices.Length - 1); uv.y = (float)i * 2.0f + 0.5f; uvs[i * wall_vertices.Length + j] = uv; } } } else { for (int i = 0; i < point_num; i++) { section = this.sections[start + i]; for (int j = 0; j < wall_vertices.Length; j++) { vertices[i * wall_vertices.Length + j] = section.positions[1]; vertices[i * wall_vertices.Length + j] += wall_vertices[j].x * section.right + wall_vertices[j].y * section.up; // uv.x = (float)j / (float)(wall_vertices.Length - 1); uv.y = (float)i * 2.0f + 0.5f; uvs[i * wall_vertices.Length + j] = uv; } } } // ---------------------------------------------------- // // 生成三角形(顶点索引数组) int position_index = 0; int i00, i10, i01, i11; if (side == WALL_SIDE.LEFT) { for (int i = 0; i < point_num - 1; i++) { for (int j = 0; j < wall_vertices.Length - 1; j++) { i00 = (i + 1) * wall_vertices.Length + (j + 1); // 左上. i10 = (i + 1) * wall_vertices.Length + (j + 0); // 右上. i01 = (i + 0) * wall_vertices.Length + (j + 1); // 左下. i11 = (i + 0) * wall_vertices.Length + (j + 0); // 右下. RoadCreator.add_quad_index(triangles, position_index, i00, i10, i01, i11); position_index += 6; } } } else { for (int i = 0; i < point_num - 1; i++) { for (int j = 0; j < wall_vertices.Length - 1; j++) { i00 = (i + 1) * wall_vertices.Length + (j + 0); // 左上. i10 = (i + 1) * wall_vertices.Length + (j + 1); // 右上. i01 = (i + 0) * wall_vertices.Length + (j + 0); // 左下. i11 = (i + 0) * wall_vertices.Length + (j + 1); // 右下. RoadCreator.add_quad_index(triangles, position_index, i00, i10, i01, i11); position_index += 6; } } } // // 由于会出现警告,这里生成uv(适当数值) mesh.vertices = vertices; mesh.uv = uvs; mesh.uv2 = uvs; mesh.triangles = triangles; mesh.Optimize(); mesh.RecalculateNormals(); }
// 壁のポリゴンを作る. private void create_wall_mesh_sub(Mesh mesh, int start, int end, WALL_SIDE side, float height) { int point_num = end - start + 1; // ---------------------------------------------------- // // 壁の断面形状. Vector3[] wall_vertices; wall_vertices = new Vector3[4]; wall_vertices[0] = Vector3.zero; wall_vertices[1] = wall_vertices[0] + Vector3.up * 0.5f; wall_vertices[2] = wall_vertices[1] + Vector3.right * (1.0f); wall_vertices[3] = wall_vertices[2] + Vector3.up * height; // ---------------------------------------------------- // // 頂点(位置座標、UV). // 四角形ひとつに必要な頂点インデックス(三角形ふたつなので6個). const int quad_index_num = 6; Vector3[] vertices = new Vector3[point_num * wall_vertices.Length]; Vector2[] uvs = new Vector2[point_num * wall_vertices.Length]; int[] triangles = new int[(point_num - 1) * wall_vertices.Length * quad_index_num]; Section section; Vector2 uv; if (side == WALL_SIDE.LEFT) { for (int i = 0; i < point_num; i++) { section = this.sections[start + i]; for (int j = 0; j < wall_vertices.Length; j++) { vertices[i * wall_vertices.Length + j] = section.positions[0]; vertices[i * wall_vertices.Length + j] += -wall_vertices[j].x * section.right + wall_vertices[j].y * section.up; // uv.x = (float)j / (float)(wall_vertices.Length - 1); uv.y = (float)i * 2.0f + 0.5f; uvs[i * wall_vertices.Length + j] = uv; } } } else { for (int i = 0; i < point_num; i++) { section = this.sections[start + i]; for (int j = 0; j < wall_vertices.Length; j++) { vertices[i * wall_vertices.Length + j] = section.positions[1]; vertices[i * wall_vertices.Length + j] += wall_vertices[j].x * section.right + wall_vertices[j].y * section.up; // uv.x = (float)j / (float)(wall_vertices.Length - 1); uv.y = (float)i * 2.0f + 0.5f; uvs[i * wall_vertices.Length + j] = uv; } } } // ---------------------------------------------------- // // 三角形(頂点インデックスの配列)を作る. int position_index = 0; int i00, i10, i01, i11; if (side == WALL_SIDE.LEFT) { for (int i = 0; i < point_num - 1; i++) { for (int j = 0; j < wall_vertices.Length - 1; j++) { i00 = (i + 1) * wall_vertices.Length + (j + 1); // 左上. i10 = (i + 1) * wall_vertices.Length + (j + 0); // 右上. i01 = (i + 0) * wall_vertices.Length + (j + 1); // 左下. i11 = (i + 0) * wall_vertices.Length + (j + 0); // 右下. RoadCreator.add_quad_index(triangles, position_index, i00, i10, i01, i11); position_index += 6; } } } else { for (int i = 0; i < point_num - 1; i++) { for (int j = 0; j < wall_vertices.Length - 1; j++) { i00 = (i + 1) * wall_vertices.Length + (j + 0); // 左上. i10 = (i + 1) * wall_vertices.Length + (j + 1); // 右上. i01 = (i + 0) * wall_vertices.Length + (j + 0); // 左下. i11 = (i + 0) * wall_vertices.Length + (j + 1); // 右下. RoadCreator.add_quad_index(triangles, position_index, i00, i10, i01, i11); position_index += 6; } } } // // 警告がでるので、uv も作成しておく(値は適当). mesh.vertices = vertices; mesh.uv = uvs; mesh.uv2 = uvs; mesh.triangles = triangles; mesh.Optimize(); mesh.RecalculateNormals(); }