public void UpdateWalls(WALL_SIDE transparentSide, WALL_SIDE toDeleteSide) { this.UpdateWalls( new WALL_SIDE[] { transparentSide }, new WALL_SIDE[] { toDeleteSide } ); }
public void AddDoor(WALL_SIDE toAddDoor) { wall[(int)toAddDoor][1] = RoomCreator.Instance.doorWall; wall[(int)toAddDoor][2] = wall[(int)toAddDoor][0]; InitWalls(18); }
// 生成墙的多边形 private void create_wall_mesh(GameObject game_object, int start, int end, WALL_SIDE side) { game_object.AddComponent <MeshFilter>(); game_object.AddComponent <MeshRenderer>(); game_object.AddComponent <MeshCollider>(); MeshFilter mesh_filter = game_object.GetComponent <MeshFilter>(); MeshCollider mesh_collider = game_object.GetComponent <MeshCollider>(); Mesh mesh = mesh_filter.mesh; MeshRenderer render = game_object.GetComponent <MeshRenderer>(); // mesh.Clear(); mesh.name = "wall mesh"; this.create_wall_mesh_sub(mesh, start, end, side, 1.0f); //render.material = this.material; /*if(side == WALL_SIDE.LEFT) { * * render.material.color = Color.green; * * } else { * * render.material.color = Color.blue; * }*/ render.material = this.wall_material; // 碰撞网格 Mesh coli_mesh = new Mesh(); coli_mesh.name = "wall mesh(coli)"; this.create_wall_mesh_sub(coli_mesh, start, end, side, RoadCreator.WallHeight); mesh_collider.sharedMesh = coli_mesh; mesh_collider.enabled = true; // }
private void create_wall_mesh(GameObject game_object, WALL_SIDE side) { game_object.AddComponent <MeshFilter>(); game_object.AddComponent <MeshRenderer>(); game_object.AddComponent <MeshCollider>(); MeshFilter mesh_filter = game_object.GetComponent <MeshFilter>(); MeshCollider mesh_collider = game_object.GetComponent <MeshCollider>(); Mesh mesh = mesh_filter.mesh; MeshRenderer render = game_object.GetComponent <MeshRenderer>(); // mesh.Clear(); mesh.name = "WallMesh"; Vector3[] vertices = new Vector3[position_num * 2]; Vector2[] uvs = new Vector2[position_num * 2]; int[] triangles = new int[(position_num - 1) * 2 * 3]; float height = 0.1f; if (side == WALL_SIDE.LEFT) { for (int i = 0; i < position_num; i++) { vertices[i * 2 + 0] = this.sections[i].positions[0]; vertices[i * 2 + 1] = this.sections[i].positions[0] + Vector3.up * height; uvs[i * 2 + 0] = new Vector2(0.0f, (float)i / (float)(this.position_num - 1)); uvs[i * 2 + 1] = new Vector2(1.0f, (float)i / (float)(this.position_num - 1)); } } else { for (int i = 0; i < position_num; i++) { vertices[i * 2 + 0] = this.sections[i].positions[1]; vertices[i * 2 + 1] = this.sections[i].positions[1] + Vector3.up * height; uvs[i * 2 + 0] = new Vector2(0.0f, (float)i / (float)(this.position_num - 1)); uvs[i * 2 + 1] = new Vector2(1.0f, (float)i / (float)(this.position_num - 1)); } } int position_index = 0; if (side == WALL_SIDE.LEFT) { for (int i = 0; i < this.position_num - 1; i++) { triangles[position_index++] = i * 2 + 0; triangles[position_index++] = i * 2 + 1; triangles[position_index++] = (i + 1) * 2 + 1; triangles[position_index++] = (i + 1) * 2 + 1; triangles[position_index++] = (i + 1) * 2 + 0; triangles[position_index++] = i * 2 + 0; } } else { for (int i = 0; i < this.position_num - 1; i++) { triangles[position_index++] = i * 2 + 1; triangles[position_index++] = i * 2 + 0; triangles[position_index++] = (i + 1) * 2 + 0; triangles[position_index++] = (i + 1) * 2 + 0; triangles[position_index++] = (i + 1) * 2 + 1; triangles[position_index++] = i * 2 + 1; } } // // 因为会出现警告信息,所以这里也提前把uv生成(值没有特定要求) mesh.vertices = vertices; mesh.uv = uvs; mesh.uv2 = uvs; mesh.triangles = triangles; mesh.Optimize(); mesh.RecalculateNormals(); render.material = this.material; if (side == WALL_SIDE.LEFT) { render.material.color = Color.green; } else { render.material.color = Color.blue; } mesh_collider.sharedMesh = mesh; mesh_collider.enabled = true; // }
public void InitWalls(WALL_SIDE toHide, WALL_SIDE toDestroy) { this.InitWalls(new WALL_SIDE[] { toHide }, new WALL_SIDE[] { toDestroy }); }
// 生成墙的多边形 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(); }