Beispiel #1
0
    private void SetBlockUVCoordinates(BlockType blockType, Vector2 topIndex, Vector2 sideIndex, Vector2 bottomIndex)
    {
        BlockUVCoordinates blockUVCoord = new BlockUVCoordinates(topIndex, sideIndex, bottomIndex);

        MyBlockUvCoordinates.Add((int)(blockType), blockUVCoord);
    }
    public bool GetChunkByXZ(int chunkX, int chunkZ, out GameObject chunk)
    {
        chunk = null;

        if (chunkX >= Chunk_X_Max || chunkZ >= Chunk_Z_Max)
        {
            return(false);
        }

        BytePoint[] positions;
        byte[]      materialIds;
        bool[][]    isVisible;
        int         visiblePlaneCount;

        if (mapChunkIO.GetChunkDataByXZ(chunkX, chunkZ, out positions, out materialIds, out isVisible, out visiblePlaneCount))
        {
            chunk       = new GameObject();
            chunk.layer = 9;

            Mesh mesh = new Mesh
            {
                name = "Combined"
            };
            int       cubeCount    = positions.Length;
            Vector3[] verts        = new Vector3[visiblePlaneCount * 4];
            int[]     tris         = new int[visiblePlaneCount * 6];
            Vector2[] uv           = new Vector2[visiblePlaneCount * 4];
            Vector3[] normals      = new Vector3[visiblePlaneCount * 4];
            int       planeWritten = 0;
            for (int i = 0; i < cubeCount; i++)
            {
                float x = positions[i].X;
                float y = positions[i].Y;
                float z = positions[i].Z;

                //(0, 0, 0)
                tempVector3s[0].x = x;
                tempVector3s[0].y = y;
                tempVector3s[0].z = z;

                //(1, 0, 0)
                tempVector3s[1].x = x + 1;
                tempVector3s[1].y = y;
                tempVector3s[1].z = z;

                //(0, 0, 1)
                tempVector3s[2].x = x;
                tempVector3s[2].y = y;
                tempVector3s[2].z = z + 1;

                //(1, 0, 1)
                tempVector3s[3].x = x + 1;
                tempVector3s[3].y = y;
                tempVector3s[3].z = z + 1;

                //(0, 1, 0)
                tempVector3s[4].x = x;
                tempVector3s[4].y = y + 1;
                tempVector3s[4].z = z;

                //(1, 1, 0)
                tempVector3s[5].x = x + 1;
                tempVector3s[5].y = y + 1;
                tempVector3s[5].z = z;

                //(0, 1, 1)
                tempVector3s[6].x = x;
                tempVector3s[6].y = y + 1;
                tempVector3s[6].z = z + 1;

                //(1, 1, 1)
                tempVector3s[7].x = x + 1;
                tempVector3s[7].y = y + 1;
                tempVector3s[7].z = z + 1;

                //mat
                int curMaterialId = materialIds[i];
                //Debug.Log(curMaterialId);
                BlockUVCoordinates uvCoordinates = m_BlockUVCoordinates[curMaterialId];

                for (int j = 0; j < 6; j++)
                {
                    if (isVisible[i][j])
                    {
                        for (int k = planeWritten * 4; k < planeWritten * 4 + 4; k++)
                        {
                            verts[k] = CopyVector3(tempVector3s[visibleToVerts[j][k - planeWritten * 4]]);
                        }
                        for (int k = planeWritten * 6; k < planeWritten * 6 + 6; k++)
                        {
                            tris[k] = triSorts[k - planeWritten * 6] + planeWritten * 4;
                        }

                        Rect materialRect;
                        switch (j)
                        {
                        case 0:
                        case 1:
                        case 4:
                        case 5:
                            materialRect = uvCoordinates.BlockFaceUvCoordinates[(int)BlockFace.Side];
                            break;

                        case 2:
                            materialRect = uvCoordinates.BlockFaceUvCoordinates[(int)BlockFace.Bottom];
                            break;

                        case 3:
                            materialRect = uvCoordinates.BlockFaceUvCoordinates[(int)BlockFace.Top];
                            break;

                        default:
                            materialRect = uvCoordinates.BlockFaceUvCoordinates[(int)BlockFace.Side];
                            break;
                        }
                        uv[planeWritten * 4 + 0] = new Vector2(materialRect.xMin, materialRect.yMin);
                        uv[planeWritten * 4 + 1] = new Vector2(materialRect.xMin, materialRect.yMax);
                        uv[planeWritten * 4 + 2] = new Vector2(materialRect.xMax, materialRect.yMax);
                        uv[planeWritten * 4 + 3] = new Vector2(materialRect.xMax, materialRect.yMin);

                        //for (int k = planeWritten * 4; k < planeWritten * 4 + 4; k++)
                        //{
                        //    switch (j)
                        //    {
                        //        case 0:
                        //            normals[k] = Vector3.left;
                        //            break;
                        //        case 1:
                        //            normals[k] = Vector3.right;
                        //            break;
                        //        case 2:
                        //            normals[k] = Vector3.down;
                        //            break;
                        //        case 3:
                        //            normals[k] = Vector3.up;
                        //            break;
                        //        case 4:
                        //            normals[k] = Vector3.back;
                        //            break;
                        //        case 5:
                        //            normals[k] = Vector3.forward;
                        //            break;
                        //    }
                        //}
                        planeWritten++;
                    }
                }
            }
            //Debug.Log("vertices length: " + verts.Length);
            //Debug.Log("triangles length: " + tris.Length);
            //Debug.Log("uv length: " + uv.Length);
            //Debug.Log("planeWritten: " + planeWritten);
            mesh.vertices  = verts;
            mesh.triangles = tris;
            mesh.uv        = uv;
            mesh.RecalculateNormals();
            //mesh.RecalculateTangents();
            //mesh.RecalculateBounds();
            //CalculateMeshTangents(mesh);

            MeshFilter thisMeshFilter = chunk.AddComponent <MeshFilter>();
            thisMeshFilter.mesh = mesh;

            MeshRenderer thisMeshRenderer = chunk.AddComponent <MeshRenderer>();
            thisMeshRenderer.sharedMaterial = materialPrefab;

            MeshCollider thisMeshCollider = chunk.AddComponent <MeshCollider>();
            thisMeshCollider.sharedMesh = mesh;
        }
        else
        {
            return(false);
        }

        chunk.name = "Chunk(" + chunkX + ',' + ' ' + chunkZ + ')';
        chunk.transform.position = new Vector3(chunkX * ChunkSize, 0, chunkZ * ChunkSize);
        chunk.layer = 9;
        return(true);
    }