构建低模风格模型

构建低模风格模型_第1张图片

上图就是一低模风格的地图模型截图,看上去也很不错对吧

那么怎样把一个模型转化为低模风格的呢?

主要是把顶点数减少,同时将顶点的贴图坐标也做相应改变,可参考以下代码

using UnityEngine;
using System.Collections;
using UnityEditor;
public class FlatShading : MonoBehaviour
{

    private Mesh mesh;
    void Start()
    {
        var meshFilters = GetComponentsInChildren();
        string name = this.gameObject.name;
        AssetDatabase.CreateFolder("Assets", name);

        foreach (var meshFilter in meshFilters)
        {
            mesh = meshFilter.mesh;
            Vector3[] oldVerts = mesh.vertices;
            Vector2[] oldUv = mesh.uv;
            int[] triangles = mesh.triangles;

            Vector3[] verts = new Vector3[triangles.Length];
            Vector2[] uv = new Vector2[triangles.Length];
            
            for (int i = 0; i < triangles.Length; i++)
            {
                verts[i] = oldVerts[triangles[i]];
                uv[i] = oldUv[triangles[i]];
                triangles[i] = i;
            }

            mesh.vertices = verts;
            mesh.triangles = triangles;
            mesh.uv = uv;
            mesh.RecalculateBounds();
            mesh.RecalculateNormals();

            //save file  
            string assetName = "Assets/" + name + "/" + mesh.name + ".asset";
            AssetDatabase.CreateAsset(meshFilter.sharedMesh, assetName);
            AssetDatabase.SaveAssets();
        }

        string prefabName = "Assets/" + name + ".prefab";
        PrefabUtility.CreatePrefab(prefabName, this.gameObject);
        AssetDatabase.SaveAssets();
    }
}

这段代码可以生成一个新的低模风格的预设,并保存在根目录

效果截图:

构建低模风格模型_第2张图片

你可能感兴趣的:(构建低模风格模型)