apunka 2018-04-12 23:15 采纳率: 62.5%
浏览 507
已结题

求Unity下Texure2DArrya应用实例,本网站上有的就不必了已经看完了

求Unity下Texure2DArrya应用实例,本网站上有的就不必了已经看完了(或者没有完整实例)

实际需求是这样:
1生成一个2X2的网格

2在C#脚本里生成Texture2DArray
3使用一个shader 分别应用Texture2DArray 上的不同index的贴图 使四个网格的贴图贴上不同的四张贴图

已解决部分:1生成网格 2在C#脚本里生成Texture2DArray
未解决部分 3 使用一个shader 分别应用Texture2DArray 上的不同index的贴图 使四个网格的贴图贴上不同的四张贴图

下面是已解决部分的代码

 public class T9arr : MonoBehaviour
{

    public Texture2D tex2d;
    public Material material;
    public Texture2DArray tex2dArr;
    public Mesh mesh;
    public MeshFilter meshFilter;

    int texWidth;
    int texHeight;
    int cellCountWidth;
    int cellCountHeight;
    public Texture2D[] tex2ds;

    int rowCount;
    int columCount;



   // [HideInInspector]
    public int MapWidth =2;
   // [HideInInspector]
    public int MapHeight = 2;
   // [HideInInspector]
    public int MapDepth = 1;
    void Start()
    {
        tex2d = (Texture2D)UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/T8/" + "pst1" + ".jpg", typeof(Texture2D));

        string assetPath = AssetDatabase.GetAssetPath(tex2d);
        var tImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;
        if (tImporter != null)
        {
            tImporter.textureType = TextureImporterType.Default;

            tImporter.isReadable = true;

            AssetDatabase.ImportAsset(assetPath);
            AssetDatabase.Refresh();
        }
        rowCount = MapWidth + 1;
        columCount = MapHeight + 1;
        material = GetComponent<MeshRenderer>().material;
        meshFilter = GetComponent<MeshFilter>();



        SetTexure();
        CreatMesh();


    }
    void SetTexure()
    {
        texWidth = (int)tex2d.width / 16;
        texHeight = (int)tex2d.height / 16;

        cellCountWidth = Mathf.FloorToInt(tex2d.width / texWidth);
        cellCountHeight = Mathf.FloorToInt(tex2d.height / texHeight);

        tex2dArr = new Texture2DArray(texWidth, texHeight, cellCountWidth * cellCountHeight, TextureFormat.RGBA32, false);
        tex2ds = new Texture2D[cellCountWidth * cellCountHeight];
        Texture2D temptex2d = new Texture2D(texWidth, texHeight, TextureFormat.RGBA32, false);
        int t = 0;
        for (int i = 0; i < cellCountWidth; i++)
        {
            for (int j = 0; j < cellCountHeight; j++)
            {
                tex2ds[t] = new Texture2D(texWidth, texHeight);
                temptex2d.SetPixels(tex2d.GetPixels(texWidth * i, texHeight * j, texWidth, texHeight));

                temptex2d.Apply();
                tex2dArr.SetPixels(temptex2d.GetPixels(), t);
                tex2ds[t].SetPixels(temptex2d.GetPixels());
                t++;
            }
        }
        tex2dArr.Apply();
        material.SetTexture("_Texture2DArray", tex2dArr);
        // material.SetFloatArray("_esuv", uvMapping);




    }
    Vector3[] BaseVertex;
    List<int> TriangleFullVertexList = new List<int>();
    List<Vector3> MapFullVertexList;
    public static List<Vector3> BasePointPosList =new List<Vector3>() ;
    public List<float> uvMapping = new List<float>();

    void CreatMesh()
    {
        BasePointPosList.Clear();
        Uves.Clear();
        TriangleFullVertexList.Clear();
        BaseVertex = CreateBaseVertex(rowCount, columCount, 1); //创建第一轮基本顶点 无复用
        BasePointPosList.AddRange( CreateBasePoint(MapWidth, MapHeight)); //创建地图点基本中心点
        MapFullVertexList = CreateBaseFullVertexs(MapWidth, MapHeight, BaseVertex, out TriangleFullVertexList);//第二轮全部顶点的创建(有复用)

        mesh = new Mesh();
        mesh.vertices = MapFullVertexList.ToArray();
        mesh.triangles = TriangleFullVertexList.ToArray();
        mesh.uv = Uves.ToArray();
        mesh.RecalculateBounds();
        mesh.RecalculateNormals();
        meshFilter.sharedMesh = mesh;

        new WaitForEndOfFrame();
        Vector2[] vector3s = mesh.uv;

        foreach (Vector2 v2 in vector3s)
        {
            Debug.Log(v2);
        }

    }
    public List<Vector2> Uves = new List<Vector2>();
    private List<Vector3> CreateBaseFullVertexs(int mapWidth, int mapHeight, Vector3[] baseVertex, out List<int> TriangleIntList)
    {

        List<Vector3> tempFullVertexList = new List<Vector3>();
        TriangleIntList = new List<int>();
        int PointCount = MapWidth * MapHeight;
        for (int cellIndex = 0; cellIndex < PointCount; cellIndex++)
        {

            int C0 = cellIndex + Mathf.FloorToInt(cellIndex / MapHeight);
            int C1 = C0 + 1;
            int C2 = cellIndex + Mathf.FloorToInt(cellIndex / MapHeight) + (MapHeight + 1);
            int C3 = C2 + 1;



            tempFullVertexList.Add(baseVertex[C0]);  //0
            tempFullVertexList.Add(baseVertex[C1]);  //1
            tempFullVertexList.Add(baseVertex[C3]);  //2
            tempFullVertexList.Add(baseVertex[C2]);  //3


            TriangleIntList.Add(cellIndex * 4 + 0);//0
            TriangleIntList.Add(cellIndex * 4 + 3);//3
            TriangleIntList.Add(cellIndex * 4 + 2);//2
            TriangleIntList.Add(cellIndex * 4 + 0);//0
            TriangleIntList.Add(cellIndex * 4 + 2);//2
            TriangleIntList.Add(cellIndex * 4 + 1);//1
                                                   //注意,UV 添加必须遵循顶点添加的顺序
                                                   //注意 该部分未解决
            //Uves.Add(new Vector2(0, 1));           //0
            //Uves.Add(new Vector2(1, 1));           //1
            //Uves.Add(new Vector2(2, 3));           //2
            //Uves.Add(new Vector2(3, 3));           //3


        }

        return tempFullVertexList;
    }

    public static Vector3[] CreateBaseVertex(int rowCount, int columCount, int depthCount)
    {
        Vector3[] temaArr = new Vector3[rowCount * columCount];
        int i = 0;
        for (int x = 0; x < rowCount; x++)
        {
            for (int z = 0; z < columCount; z++)
            {
                temaArr[i] = new Vector3(z-0.5f, 0, x-0.5f);
                i++;
            }
        }
        return temaArr;
    }
    public static Vector3[] CreateBasePoint(int mapWidth, int mapHeight)
    {
        Vector3[] temaArr = new Vector3[mapWidth * mapHeight];
        int i = 0;
        for (int x = 0; x < mapWidth; x++)
        {
            for (int z = 0; z < mapHeight; z++)
            {
                temaArr[i] = new Vector3(x + 0.5f, 0, z + 0.5f);

            }
        }       
        return temaArr;
    }
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!
    • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?