Mesh Manipulation - Mesh重置中心點

做項目的時候,偶爾會遇到美術(shù)提供的模型的中心點不統(tǒng)一,或者中心點設(shè)置的不符合程序的要求,以前為了解決這個問題,要么是找美術(shù)重新修改Mesh,要不然就是在模型外面再套一層殼,然后通過把模型放在殼下面,設(shè)置localPosition,然后通過操作殼來達(dá)到需求。但是這樣做又會無故增加復(fù)雜度,也很容易出bug。
所以,為了一次性解決這類型的問題,就讓程序充當(dāng)一次美術(shù)吧

using UnityEngine;

namespace LDFW.Model
{

    public class MeshShifter : MonoBehaviour
    {

        public static Mesh targetMesh;
        public static Vector3 center;
        public static Vector3 offset;
        public static string savePath = "Assets/";


        public static Mesh SetMeshCenter(Mesh targetMesh, Vector3 targetReferenceCenter)
        {
            if (targetMesh == null)
            {
                Debug.LogError("Target mesh cannot be null!");
                return null;
            }

            //Debug.Log("Set mesh");
            Mesh newMesh = MeshGenerator.DuplicateMesh(targetMesh);
            
            
            Vector3 diff = Vector3.Scale(targetMesh.bounds.size * 0.5f, targetReferenceCenter * -1) - targetMesh.bounds.center;

            int vertexCount = targetMesh.vertexCount;
            Vector3[] oldVertices = newMesh.vertices;
            Vector3[] newVertices = new Vector3[vertexCount];

            for (int i = 0; i < vertexCount; i++)
            {
                newVertices[i] = oldVertices[i] + diff;
            }
            newMesh.vertices = newVertices;
            oldVertices = null;
            

            newMesh.RecalculateBounds();
            newMesh.RecalculateNormals();

            System.GC.Collect();
            return newMesh;
        }

        public static Mesh ShiftMesh(Mesh targetMesh, Vector3 offsetValue)
        {
            if (targetMesh == null)
            {
                Debug.LogError("Target mesh cannot be null!");
                return null;
            }

            Mesh newMesh = MeshGenerator.DuplicateMesh(targetMesh);


            Vector3 diff = offsetValue;

            int vertexCount = targetMesh.vertexCount;
            Vector3[] oldVertices = newMesh.vertices;
            Vector3[] newVertices = new Vector3[vertexCount];

            for (int i = 0; i < vertexCount; i++)
            {
                newVertices[i] = oldVertices[i] + diff;
            }
            newMesh.vertices = newVertices;
            oldVertices = null;


            newMesh.RecalculateBounds();
            newMesh.RecalculateNormals();

            System.GC.Collect();
            return newMesh;
        }
    }

}

返回的是Mesh類型,然后再寫個保存方法,把Mesh以.asset格式保存到項目里

        #if UNITY_EDITOR
        public static void SaveAssetToFile(Object asset, string path, string extension = "asset")
        {
            AssetDatabase.CreateAsset(asset, path + "." + extension);
        }
        #endif

然后新的Mesh就可以替代原來的了,想怎么改都可以。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 更新:【面試題含答案】http://bbs.9ria.com/thread-288394-1-1.html 高頻問...
    好怕怕閱讀 5,081評論 3 53
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,544評論 19 139
  • 關(guān)于這些技巧這些技巧不可能適用于每個項目。這些是基于我的一些項目經(jīng)驗,項目團(tuán)隊的規(guī)模從3人到20人不等;框架結(jié)構(gòu)的...
    游戲開發(fā)小Y閱讀 1,089評論 0 1
  • 有人認(rèn)真講了一個笑話,你真的就認(rèn)了真 小時候雷雨天,有人一本正經(jīng)地告訴我,雷公專門劈壞蛋,所以一一你千萬不要做壞事...
    晷艏閱讀 1,235評論 2 3
  • 一、好言 生活有一點我不是太喜歡,就是它總讓更懂事的人來承擔(dān)糟糕的感受和結(jié)果 二、背景 剛剛來公司,公司的dubb...
    吳世浩閱讀 1,277評論 0 0

友情鏈接更多精彩內(nèi)容