public void CreateWallMesh(int[,] map)
    {
        CalculateMeshOutLines();

        List <Vector3> wallVertices  = new List <Vector3>();
        List <int>     wallTriangles = new List <int>();
        Mesh           wallMesh      = new Mesh();


        foreach (List <int> outline in outlines)
        {
            for (int i = 0; i < outline.Count - 1; i++)
            {
                int startIndex = wallVertices.Count;
                wallVertices.Add(vertices[outline[i]]);                               // TopLeft
                wallVertices.Add(vertices[outline[i + 1]]);                           // TopRight
                wallVertices.Add(vertices[outline[i]] - Vector3.up * wallHeight);     // BottomLeft
                wallVertices.Add(vertices[outline[i + 1]] - Vector3.up * wallHeight); // BottomRight

                wallTriangles.Add(startIndex + 0);
                wallTriangles.Add(startIndex + 2);
                wallTriangles.Add(startIndex + 3);

                wallTriangles.Add(startIndex + 3);
                wallTriangles.Add(startIndex + 1);
                wallTriangles.Add(startIndex + 0);
            }
        }
        wallMesh.vertices  = wallVertices.ToArray();
        wallMesh.triangles = wallTriangles.ToArray();
        walls.mesh         = wallMesh;

        MeshCollider[] m = walls.GetComponents <MeshCollider>();
        for (int i = 0; i < m.Length; i++)
        {
            Destroy(m[i]);
        }

        MeshCollider wallCollider = walls.gameObject.AddComponent <MeshCollider>();

        wallCollider.sharedMesh = wallMesh;
        Vector2[] wuvs = new Vector2[wallMesh.vertices.Length];
        for (int i = 0; i < wallMesh.vertices.Length; i++)
        {
            float x = wallMesh.vertices[i].x;

            if (i + 1 < wallMesh.vertices.Length && x == wallMesh.vertices[i + 1].x && i > 1 && wallMesh.vertices[i - 1].x == x)
            {
                //Fix bug texture
                x = wallMesh.vertices[i].z;
            }
            wuvs[i]     = new Vector2(x, wallMesh.vertices[i].y);
            wallMesh.uv = wuvs;
        }
    }
示例#2
0
 /// <summary>
 /// Deletes mesh colliders in the wall game object.
 /// </summary>
 public void DestroyWallMeshColliders()
 {
     meshFilter = GetComponent <MeshFilter>();
     MeshCollider[] colliders = meshFilter.GetComponents <MeshCollider>();
     for (int i = 0; i < colliders.Length; i++)
     {
         DestroyImmediate(colliders[i]);
     }
     meshColliders = null;
     Debug.Log("Destroyed " + colliders.Length + " colliders");
 }
示例#3
0
    private void SaveMesh(MeshFilter meshFilter, bool saveAsAsset)
    {
        if (IsPrefab(meshFilter))
        {
            Debug.LogWarning("Modifying prefabs directly is not allowed, create an instance in the scene instead!");
            return;
        }

        string savedMeshName = meshFilter.sharedMesh.name;

        while (savedMeshName.EndsWith("(Clone)"))
        {
            savedMeshName = savedMeshName.Substring(0, savedMeshName.Length - 7);
        }

        string savePath = EditorUtility.SaveFilePanelInProject("Save As", savedMeshName, saveAsAsset ? "asset" : "obj", string.Empty);

        if (string.IsNullOrEmpty(savePath))
        {
            return;
        }

        Mesh originalMesh = meshFilter.sharedMesh;
        Mesh savedMesh    = saveAsAsset ? SaveMeshAsAsset(meshFilter, savePath) : SaveMeshAsOBJ(meshFilter, savePath);

        if (meshFilter.sharedMesh != savedMesh)
        {
            Undo.RecordObject(meshFilter, UNDO_SAVE_MODEL_AS);
            meshFilter.sharedMesh = savedMesh;
        }

        MeshCollider[] meshColliders = meshFilter.GetComponents <MeshCollider>();

        foreach (MeshCollider meshCollider in meshColliders)
        {
            if (!IsNull(meshCollider) && meshCollider.sharedMesh == originalMesh && meshCollider.sharedMesh != savedMesh)
            {
                Undo.RecordObject(meshCollider, UNDO_SAVE_MODEL_AS);
                meshCollider.sharedMesh = savedMesh;
            }
        }
    }
        private void GenerateMorphController()
        {
            if (!_meshFilter.sharedMesh)
            {
                MorphDebug.LogError("MeshFilter组件丢失了Mesh数据!", _meshFilter.gameObject);
                return;
            }
            if (!_meshRenderer)
            {
                MorphDebug.LogError("GameObject丢失了组件MeshRenderer!", _meshFilter.gameObject);
                return;
            }

            string path = EditorUtility.SaveFilePanel("Save Morph Mesh", Application.dataPath, _meshFilter.sharedMesh.name + "(Morph)", "asset");

            if (path.Length != 0)
            {
                Collider[] cols = _meshFilter.GetComponents <Collider>();
                for (int i = 0; i < cols.Length; i++)
                {
                    cols[i].enabled = false;
                }

                string subPath = path.Substring(0, path.IndexOf("Asset"));
                path = path.Replace(subPath, "");
                Mesh mesh = Instantiate(_meshFilter.sharedMesh);
                AssetDatabase.CreateAsset(mesh, path);
                AssetDatabase.SaveAssets();

                Mesh meshAsset = AssetDatabase.LoadAssetAtPath(path, typeof(Mesh)) as Mesh;

                //生成蒙皮网格组件,并创建根骨骼
                if (!_skinnedMeshRenderer)
                {
                    _skinnedMeshRenderer = _meshFilter.transform.gameObject.AddComponent <SkinnedMeshRenderer>();
                }
                _skinnedMeshRenderer.sharedMesh      = meshAsset;
                _skinnedMeshRenderer.rootBone        = _meshFilter.transform;
                _skinnedMeshRenderer.sharedMaterials = _meshRenderer.sharedMaterials;
                _skinnedMeshRenderer.enabled         = true;

                GameObject boneRoot = new GameObject("BoneRoot");
                boneRoot.hideFlags = HideFlags.HideInHierarchy;
                MorphBone mb = boneRoot.AddComponent <MorphBone>();
                mb.hideFlags = HideFlags.HideInInspector;
                Transform[] bones     = new Transform[1];
                Matrix4x4[] bindposes = new Matrix4x4[1];
                bones[0] = boneRoot.transform;
                bones[0].SetParent(_skinnedMeshRenderer.rootBone);
                bones[0].localPosition     = Vector3.zero;
                bones[0].localRotation     = Quaternion.identity;
                bindposes[0]               = bones[0].worldToLocalMatrix * _skinnedMeshRenderer.transform.localToWorldMatrix;
                _skinnedMeshRenderer.bones = bones;
                _skinnedMeshRenderer.sharedMesh.bindposes = bindposes;

                //生成网格碰撞器
                if (!_meshCollider)
                {
                    _meshCollider = _meshFilter.transform.gameObject.AddComponent <MeshCollider>();
                }
                _meshCollider.sharedMesh = meshAsset;
                _meshCollider.enabled    = true;

                //生成变形动画数据组件
                if (!_morphAnimationData)
                {
                    _morphAnimationData = _meshFilter.transform.gameObject.AddComponent <MorphAnimationData>();
                }
                _morphAnimationData.Identity  = true;
                _morphAnimationData.hideFlags = HideFlags.HideInInspector;
                _morphAnimationData.Vertexs.Clear();
                _morphAnimationData.Triangles.Clear();
                //处理顶点
                List <int> repetitionVertices = new List <int>();
                for (int i = 0; i < meshAsset.vertices.Length; i++)
                {
                    EditorUtility.DisplayProgressBar("Please wait", "Dispose vertices(" + i + "/" + meshAsset.vertices.Length + ")......", 1.0f / meshAsset.vertices.Length * i);

                    if (repetitionVertices.Contains(i))
                    {
                        continue;
                    }

                    List <int> verticesGroup = new List <int>();
                    verticesGroup.Add(i);

                    for (int j = i + 1; j < meshAsset.vertices.Length; j++)
                    {
                        if (meshAsset.vertices[i] == meshAsset.vertices[j])
                        {
                            verticesGroup.Add(j);
                            repetitionVertices.Add(j);
                        }
                    }
                    _morphAnimationData.Vertexs.Add(new MorphVertex(_meshFilter.transform.localToWorldMatrix.MultiplyPoint3x4(meshAsset.vertices[i]), verticesGroup));
                }
                //处理三角面
                List <int> allTriangles = new List <int>(meshAsset.triangles);
                for (int i = 0; (i + 2) < allTriangles.Count; i += 3)
                {
                    EditorUtility.DisplayProgressBar("Please wait", "Dispose triangles(" + i + "/" + allTriangles.Count + ")......", 1.0f / allTriangles.Count * i);

                    int           mv1 = _morphAnimationData.GetVertexIndexByIndex(allTriangles[i]);
                    int           mv2 = _morphAnimationData.GetVertexIndexByIndex(allTriangles[i + 1]);
                    int           mv3 = _morphAnimationData.GetVertexIndexByIndex(allTriangles[i + 2]);
                    MorphTriangle mt  = new MorphTriangle(mv1, mv2, mv3);
                    _morphAnimationData.Triangles.Add(mt);
                }
                _morphAnimationData.BindposesPosition.Add(boneRoot.transform.localPosition);
                _morphAnimationData.BindposesRotation.Add(boneRoot.transform.localRotation);
                _morphAnimationData.BindposesScale.Add(boneRoot.transform.localScale);

                EditorUtility.ClearProgressBar();

                DestroyImmediate(_meshFilter);
                DestroyImmediate(_meshRenderer);

                _skinnedMeshRenderer.transform.parent = null;
            }
        }