Esempio n. 1
0
        private void ConvertTerrain2Mesh(Terrain terrain)
        {
            var meshes = MeshUtil.ConvertTerrain2Mesh(terrain, _indexFormat, _splitCount, _lod);

            var rootGo = new GameObject($"{terrain.name}_{_lod}");

            rootGo.CopyTransform(terrain.gameObject);

            var tileGridNum = (int)Mathf.Pow(2, _splitCount);
            var tileSize    = terrain.terrainData.size.x / tileGridNum;

            for (int i = 0; i < meshes.Count; i++)
            {
                var mesh = meshes[i];

                // 从下往上,再从左往右
                var widthIndex  = i / tileGridNum;
                var heightIndex = i % tileGridNum;

                var go = new GameObject($"{terrain.name}_{_lod}_mesh_{widthIndex}_{heightIndex}");
                go.transform.SetParent(rootGo.transform);
                go.transform.localPosition = new Vector3(widthIndex * tileSize, 0, heightIndex * tileSize);
                var filer = go.AddComponent <MeshFilter>();
                filer.mesh = mesh;
                var renderer = go.AddComponent <MeshRenderer>();
                // renderer.sharedMaterial = AssetDatabase.GetBuiltinExtraResource<Material>("Default-Terrain-Standard.mat");
                renderer.sharedMaterial = AssetDatabase.GetBuiltinExtraResource <Material>("Default-Material.mat");
                InitMeshRenderer(terrain, renderer, tileGridNum, widthIndex, heightIndex);
            }
        }