예제 #1
0
    public static GameObject getGameObject(MyMaterial m)
    {
        Block block = new Block(null, new Vector(0, 0, 0), m);

        BlockFace[]      blockfaces = new BlockFace[4];
        ChunkMeshCreator cmc        = new ChunkMeshCreator(null);

        for (int _d = 0; _d < 6; _d++)
        {
            Direction direction = (Direction)_d;
            blockfaces[0] = block.getBlockFace(direction);
            blockfaces[1] = block.getBlockFace(direction);
            blockfaces[2] = block.getBlockFace(direction);
            blockfaces[3] = block.getBlockFace(direction);
            cmc.creatFace(m.getMaterial(direction), blockfaces, 0, 0, scale: 0.4f);
        }

        GameObject rootObject = new GameObject("CubeItemEntity");

        foreach (Material material in cmc.materialVertices.Keys)
        {
            Mesh mesh = new Mesh();
            mesh.vertices  = cmc.materialVertices[material].ToArray();
            mesh.triangles = cmc.materialTriangles[material].ToArray();
            mesh.uv        = cmc.materialUVs[material].ToArray();
            mesh.RecalculateBounds();
            mesh.RecalculateNormals();
            mesh.MarkDynamic();
            mesh.Optimize();
            GameObject   o            = new GameObject(material + "");
            MeshFilter   meshFilter   = o.AddComponent <MeshFilter>();
            MeshRenderer meshRenderer = o.AddComponent <MeshRenderer>();
            meshFilter.sharedMesh       = mesh;
            meshRenderer.sharedMaterial = material;
            o.transform.SetParent(rootObject.transform);
        }

        MeshCollider meshCollider = rootObject.AddComponent <MeshCollider>();

        meshCollider.convex = true;
        Mesh colliderMesh = new Mesh();

        colliderMesh.vertices  = cmc.colliderVertices.ToArray();
        colliderMesh.triangles = cmc.colliderTriangles.ToArray();
        colliderMesh.RecalculateBounds();
        colliderMesh.RecalculateNormals();
        colliderMesh.MarkDynamic();
        colliderMesh.Optimize();
        meshCollider.sharedMesh = colliderMesh;
        rootObject.tag          = "Item";
        return(rootObject);
    }
예제 #2
0
    private void prepareMesh()
    {
        chunk.update = false;
        if (chunk.hasBlock == false)
        {
            return;
        }
        materialVertices.Clear();
        materialTriangles.Clear();
        materialUVs.Clear();
        for (int _d = 0; _d < 6; _d++)
        {
            Direction direction = (Direction)_d;
            for (int i1 = 0; i1 < GameManager.chunkSize; i1++)
            {
                blockRendered = new bool[GameManager.chunkSize, GameManager.chunkSize, GameManager.chunkSize];
                for (int i2 = 0; i2 < GameManager.chunkSize; i2++)
                {
                    for (int i3 = 0; i3 < GameManager.chunkSize; i3++)
                    {
                        int x = 0;
                        int y = 0;
                        int z = 0;
                        switch (direction)
                        {
                        case Direction.UP:
                        case Direction.DOWN: {
                            x = i2;
                            y = i1;
                            z = i3;
                            break;
                        }

                        case Direction.NORTH:
                        case Direction.SOUTH: {
                            x = i3;
                            y = i2;
                            z = i1;
                            break;
                        }

                        case Direction.EAST:
                        case Direction.WEST: {
                            x = i1;
                            y = i3;
                            z = i2;
                            break;
                        }
                        }
                        Block block = chunk.getBlock(x, y, z);
                        if (shouldRenderBlock(block, direction))
                        {
                            Block       originalBlock = block;
                            MyMaterial  m             = block.getType();
                            Material    mat           = m.getMaterial(direction);
                            BlockFace[] blockFaces    = new BlockFace[4];
                            BlockFace   blockFace     = originalBlock.getBlockFace(direction);
                            blockFaces[2] = blockFace;
                            blockFaces[3] = blockFace;
                            int belowIncrease = 0;
                            int nextIncrease  = 0;
                            while (getBelowBlock(block, direction) != null)
                            {
                                Block lastBlock = block;
                                block = getBelowBlock(block, direction);
                                if (block.getType() != m || shouldRenderBlock(block, direction) == false)
                                {
                                    block = lastBlock;
                                    break;
                                }
                                setRendered(block);
                                belowIncrease++;
                            }
                            BlockFace bf = block.getBlockFace(direction);
                            blockFaces[0] = bf;
                            blockFaces[1] = bf;
                            block         = originalBlock;
                            bool exit = false;
                            while (getNextBlock(block, direction) != null && exit == false)
                            {
                                Block oriBlock = getNextBlock(block, direction);
                                block = getNextBlock(block, direction);
                                if (block.getType() != m || shouldRenderBlock(block, direction) == false)
                                {
                                    break;
                                }
                                for (int bi = 1; bi <= belowIncrease; bi++)
                                {
                                    if (getBelowBlock(block, direction) != null)
                                    {
                                        block = getBelowBlock(block, direction);
                                        if (block.getType() != m || shouldRenderBlock(block, direction) == false)
                                        {
                                            exit = true;
                                            break;
                                        }
                                    }
                                    else
                                    {
                                        exit = true;
                                        break;
                                    }
                                }
                                if (exit == false)
                                {
                                    block = oriBlock;
                                    setRendered(block);
                                    for (int bi = 1; bi <= belowIncrease; bi++)
                                    {
                                        block = getBelowBlock(block, direction);
                                        setRendered(block);
                                    }
                                    nextIncrease++;
                                    blockFaces[2] = oriBlock.getBlockFace(direction);
                                    blockFaces[1] = block.getBlockFace(direction);
                                    block         = oriBlock;
                                }
                            }
                            creatFace(mat, blockFaces, nextIncrease, belowIncrease);
                        }
                    }
                }
            }
        }
    }