分別修改Cube每個面的貼圖UV(Unity3D開發之十八),cubeunity3d

來源:互聯網
上載者:User

分別修改Cube每個面的貼圖UV(Unity3D開發之十八),cubeunity3d

猴子原創,歡迎轉載。轉載請註明: 轉載自Cocos2Der-CSDN,謝謝!
原文地址: http://blog.csdn.net/cocos2der/article/details/46611169

今天項目中需要修改一個Cube中每個面的貼圖UV,也就是貼圖中有多個矩形貼圖,需要程式從貼圖中讀取一部分賦值給Cube每個面。

這裡已經有人實現了。

CustomUVS.cs

using UnityEngine;using System.Collections;[ExecuteInEditMode]public class CustomUVS : MonoBehaviour {    public Vector2 topPoint;    public Vector2 bottomPoint;    public Vector2 leftPoint;    public Vector2 rightPoint;    public Vector2 frontPoint;    public Vector2 backPoint;    private Mesh m_mesh;    public enum CubeFaceType    {        Top,        Bottom,        Left,        Right,        Front,        Back    };    // Use this for initialization    void Start () {        MeshFilter meshFilter = GetComponent<MeshFilter>();        if (meshFilter == null) {            Debug.LogError("Script needs MeshFilter component");            return;        }#if UNITY_EDITOR        Mesh meshCopy = Mesh.Instantiate(meshFilter.sharedMesh) as Mesh;    // Make a deep copy        meshCopy.name = "Cube";        m_mesh = meshFilter.mesh = meshCopy;                                // Assign the copy to the meshes#else        m_mesh = meshFilter.mesh;#endif        if (m_mesh == null || m_mesh.uv.Length != 24) {            Debug.LogError("Script needs to be attached to built-in cube");            return;        }        UpdateMeshUVS();    }    // Update is called once per frame    void Update ()     {#if UNITY_EDITOR        UpdateMeshUVS();#endif    }    void UpdateMeshUVS()    {        Vector2[] uvs = m_mesh.uv;        // Front        SetFaceTexture(CubeFaceType.Front, uvs);        // Top        SetFaceTexture(CubeFaceType.Top, uvs);        // Back        SetFaceTexture(CubeFaceType.Back, uvs);        // Bottom        SetFaceTexture(CubeFaceType.Bottom, uvs);        // Left        SetFaceTexture(CubeFaceType.Left, uvs);          // Right                SetFaceTexture(CubeFaceType.Right, uvs);           m_mesh.uv = uvs;    }    Vector2[] GetUVS(float originX, float originY)    {        Vector2[] uvs = new Vector2[4];        uvs[0] = new Vector2(originX / 3.0f, originY / 3.0f);        uvs[1] = new Vector2((originX + 1) / 3.0f, originY / 3.0f);        uvs[2] = new Vector2(originX / 3.0f, (originY + 1) / 3.0f);        uvs[3] = new Vector2((originX + 1) / 3.0f, (originY + 1) / 3.0f);        return uvs;    }    void SetFaceTexture(CubeFaceType faceType, Vector2[] uvs)    {        if (faceType == CubeFaceType.Front) {            Vector2[] newUVS = GetUVS(frontPoint.x, frontPoint.y);            uvs[0]  = newUVS[0];             uvs[1]  = newUVS[1];            uvs[2]  = newUVS[2];            uvs[3]  = newUVS[3];        }else if (faceType == CubeFaceType.Back) {            Vector2[] newUVS = GetUVS(backPoint.x, backPoint.y);            uvs[10] = newUVS[0];             uvs[11] = newUVS[1];             uvs[6]  = newUVS[2];             uvs[7]  = newUVS[3];         }else if (faceType == CubeFaceType.Top) {            Vector2[] newUVS = GetUVS(topPoint.x, topPoint.y);            uvs[8] = newUVS[0];             uvs[9] = newUVS[1];             uvs[4]  = newUVS[2];             uvs[5]  = newUVS[3];         }else if (faceType == CubeFaceType.Bottom) {            Vector2[] newUVS = GetUVS(bottomPoint.x, bottomPoint.y);            uvs[12] = newUVS[0];             uvs[14] = newUVS[1];             uvs[15]  = newUVS[2];             uvs[13]  = newUVS[3];         }else if (faceType == CubeFaceType.Left) {            Vector2[] newUVS = GetUVS(leftPoint.x, leftPoint.y);            uvs[16] = newUVS[0];             uvs[18] = newUVS[1];             uvs[19]  = newUVS[2];             uvs[17]  = newUVS[3];         }else if (faceType == CubeFaceType.Right) {            Vector2[] newUVS = GetUVS(rightPoint.x, rightPoint.y);            uvs[20] = newUVS[0];             uvs[22] = newUVS[1];             uvs[23]  = newUVS[2];             uvs[21]  = newUVS[3];         }    }}

注意:由於圖片是3x3的,所以截取地區中按3等分截取。

實際效果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.