Ejemplo n.º 1
0
    public IEnumerator Create(MeshController mc)
    {
        yield return(null);

        //Mesh, 변수 생성 및 초기화
        mc.mesh = new Mesh();
        mc.root.AddComponent <MeshFilter>().mesh = mc.mesh;
        mc.mesh.Clear();
        var renderer = mc.root.AddComponent <MeshRenderer>();

        var points = new List <Vector3>();
        var minY   = float.MaxValue;
        var maxY   = float.MinValue;

        //모든 정점 좌표를 받아온다. (천장, 바닥, 벽면)
        //층이 다른 건물이 있을 경우 하나 더 만든다.
        List <Vector3> secondPoints;

        points = mc.GetPoints(out secondPoints);

        List <int> triangles = new List <int>();

        //정점 좌표를 근거로 3개씩 묶어 면을 만든다.
        triangles = CreateCeilTriangle(points);
        Vector3[] vertices = points.ToArray();

        //건물 높이 값 계산 (가장 높은 값과 가장 낮은 값을 구해 높이를 구한다.
        foreach (var pos in points)
        {
            minY = Mathf.Min(pos.y, minY);
            maxY = Mathf.Max(pos.y, maxY);
        }

        var height = Mathf.Abs(maxY - minY);

        mc.root.transform.parent        = DataManager.Instance.complexParent;
        mc.root.transform.localPosition = new Vector3(0, mc.data.meta.height, 0);
        mc.root.name = mc.data.meta.name;


        if (triangles.Count > 0)
        {
            mc.mesh.vertices  = vertices;
            mc.mesh.triangles = triangles.ToArray();
            mc.mesh.uv        = new Vector2[vertices.Length];
            SetUV(mc);
            mc.CreatePoints();
            renderer.sharedMaterial = DataManager.Instance.CreateMaterial();
            renderer.sharedMaterial.SetTextureScale("_BaseMap", new Vector2(1, Mathf.Floor(height / 3f)));
            renderer.sharedMaterial.SetTextureOffset("_BaseMap", new Vector2(0, 0));
            //mesh.uv = uv.ToArray();

            mc.mesh.Optimize();
            mc.mesh.RecalculateNormals();
        }
        StartCoroutine(CreateSecondMesh(mc.data, secondPoints));
    }
Ejemplo n.º 2
0
    public void SetUV(MeshController mc)
    {
        List <Vector3> secondPoints;
        var            points    = mc.GetPoints(out secondPoints);
        var            positions = new Dictionary <int, Vector3>();

        for (int i = 0; i < points.Count; i++)
        {
            positions.Add(i, points[i]);
            //포지션 6개 단위로 uv 매핑
            if (positions.Count == 6)
            {
                var uv    = mc.mesh.uv;
                var state = GetWallState(positions, mc.GetFoward());
                var pos   = positions.ToArray();
                if (pos.Length == 0)
                {
                    positions.Clear();
                    continue;
                }
                switch (state)
                {
                case Defines.WallState.FRONT:
                    uv[pos[0].Key] = new Vector2(0.5f, 0);                                      // 1 (1, 0)
                    uv[pos[1].Key] = new Vector2(0, 0);                                         // 0 (0, 0)
                    uv[pos[2].Key] = new Vector2(0, 0.5f);                                      // 3 (0, 1)
                    uv[pos[3].Key] = new Vector2(0, 0.5f);                                      // 3 (0, 1)
                    uv[pos[4].Key] = new Vector2(0.5f, 0.5f);                                   // 2 (1, 1)
                    uv[pos[5].Key] = new Vector2(0.5f, 0);                                      // 1 (1, 0)
                    break;

                case Defines.WallState.WALL:
                    uv[pos[0].Key] = new Vector2(0.75f, 0);                                     // 1 (1, 0)
                    uv[pos[1].Key] = new Vector2(0.5f, 0);                                      // 0 (0, 0)
                    uv[pos[2].Key] = new Vector2(0.5f, 0.5f);                                   // 3 (0, 1)
                    uv[pos[3].Key] = new Vector2(0.5f, 0.5f);                                   // 3 (0, 1)
                    uv[pos[4].Key] = new Vector2(0.75f, 0.5f);                                  // 2 (1, 1)
                    uv[pos[5].Key] = new Vector2(0.75f, 0);                                     // 1 (1, 0)
                    break;

                case Defines.WallState.CEIL:
                    uv[pos[0].Key] = new Vector2(0.75f, 0);                                     // 0 (0, 0)
                    uv[pos[1].Key] = new Vector2(1f, 0);                                        // 1 (1, 0)
                    uv[pos[2].Key] = new Vector2(1f, 0.5f);                                     // 2 (1, 1)
                    uv[pos[3].Key] = new Vector2(0.75f, 0);                                     // 3 (0, 1)
                    uv[pos[4].Key] = new Vector2(0.75f, 0.5f);                                  // 0 (0, 0)
                    uv[pos[5].Key] = new Vector2(1f, 0.5f);                                     // 2 (1, 1)
                    break;
                }
                positions.Clear();
                mc.mesh.uv = uv.ToArray();
            }
        }
    }