示例#1
0
    public static void CombineForAll(GameObject currentChunk, bool remove = false)
    {
        Vector3 pos = currentChunk.transform.position;

        IChunk chunk = currentChunk.GetComponent <IChunk>();

        currentChunk.transform.position = Vector3.zero;
        currentChunk.SetActive(false);

        if (uvs == null)
        {
            uvs = SetUVs.GetStandardUVs();
        }

        Block[]           blocks  = chunk.GetBlocks();
        CombineInstance[] combine = new CombineInstance[blocks.Length];

        currentChunk.GetComponents <MeshCollider>().ToList().ForEach(GameObject.Destroy);

        for (int i = 0; i < blocks.Length; i++)
        {
            combine[i].mesh      = blocks[i].Mesh;
            combine[i].transform = Matrix4x4.TRS(blocks[i].Position, Quaternion.identity, Vector3.one);
        }

        MeshFilter refMesh = currentChunk.GetComponent <MeshFilter>();


        refMesh.mesh = new Mesh();
        refMesh.mesh.CombineMeshes(combine, true);

        List <Vector2> newMeshUVs = new List <Vector2>();

        for (int i = 0; i < blocks.Length; i++)
        {
            //add new UVs based on individual block settings
            UVSetter suv      = blocks[i].UVSetter;
            float    tilePerc = 1 / UVSetter.pixelSize;
            float    umin     = tilePerc * suv.TileX;
            float    umax     = tilePerc * (suv.TileX + 1);
            float    vmin     = tilePerc * suv.TileY;
            float    vmax     = tilePerc * (suv.TileY + 1);

            for (int j = 0; j < 24; j++)
            {
                float x = Mathf.Approximately(uvs[j].x, 0f) ? umin : umax;
                float y = Mathf.Approximately(uvs[j].y, 0f) ? vmin : vmax;

                newMeshUVs.Add(new Vector2(x, y));
            }
        }

        refMesh.mesh.uv = newMeshUVs.ToArray();

        refMesh.mesh.RecalculateBounds();
        refMesh.mesh.RecalculateNormals();

        currentChunk.transform.position = pos;
        currentChunk.AddComponent <MeshCollider>();
        currentChunk.gameObject.SetActive(true);
    }
示例#2
0
    public static void Combine(Block blockToAdd, GameObject currentChunk)
    {
        Vector3 pos = currentChunk.transform.position;

        currentChunk.transform.position = Vector3.zero;

        MeshFilter meshFilter = currentChunk.GetComponent <MeshFilter>();

        CombineInstance[] combine = new CombineInstance[2];


        currentChunk.GetComponents <MeshCollider>().ToList().ForEach(GameObject.Destroy);

        Vector2[] oldMeshUVs = currentChunk.GetComponent <MeshFilter>().mesh.uv;


        combine[0].mesh      = meshFilter.sharedMesh;
        combine[0].transform = meshFilter.transform.localToWorldMatrix;
        meshFilter.gameObject.SetActive(false);

        combine[1].mesh = blockToAdd.Mesh; // TODO: Hier möglicherweise zu mesh wechseln
        Matrix4x4 m = Matrix4x4.TRS(blockToAdd.Position, Quaternion.identity, Vector3.one);

        combine[1].transform = m;


        MeshFilter refMesh = currentChunk.GetComponent <MeshFilter>();

        refMesh.mesh = new Mesh();

        refMesh.mesh.CombineMeshes(combine, true);

        //make new UV array
        Vector2[] newMeshUVs = new Vector2[oldMeshUVs.Length + 24];

//        //copy over all UVs
        for (int j = 0; j < oldMeshUVs.Length; j++)
        {
            newMeshUVs[j] = oldMeshUVs[j];
        }

        UVSetter suv      = blockToAdd.UVSetter;
        float    tilePerc = 1 / UVSetter.pixelSize;
        float    umin     = tilePerc * suv.TileX;
        float    umax     = tilePerc * (suv.TileX + 1);
        float    vmin     = tilePerc * suv.TileY;
        float    vmax     = tilePerc * (suv.TileY + 1);

        if (uvs == null)
        {
            uvs = SetUVs.GetStandardUVs();
        }

        int k = 0;

        for (int j = newMeshUVs.Length - 24; j < newMeshUVs.Length; j++)
        {
            float x = Mathf.Approximately(uvs[k].x, 0f) ? umin : umax;
            float y = Mathf.Approximately(uvs[k].y, 0f) ? vmin : vmax;

            newMeshUVs[j] = new Vector2(x, y);
            k++;
        }

        refMesh.mesh.uv = newMeshUVs;

        refMesh.mesh.RecalculateBounds();
        refMesh.mesh.RecalculateNormals();

        currentChunk.transform.position = pos;
        currentChunk.AddComponent <MeshCollider>();
        currentChunk.gameObject.SetActive(true);
    }