Пример #1
0
    /// <summary>
    /// Creates the mesh data for the block at the given internal position within the chunk at the given chunk position.
    /// </summary>
    /// <param name="block">The block to create mesh for.</param>
    /// <param name="internalPos">The internal position of the block to create a mesh for.</param>
    /// <param name="chunkPos">The chunk position of the block to create a mesh for.</param>
    /// <returns>Returns the mesh data created for the block.</returns>
    public static MeshData CreateMesh(Block block, Vector3Int internalPos, Vector3Int chunkPos)
    {
        MeshData meshData  = new MeshData();
        bool     bottomVis = CheckFaceVisibility(internalPos, chunkPos, Side.Bottom, out _);
        bool     topVis    = CheckFaceVisibility(internalPos, chunkPos, Side.Top, out _);
        bool     frontVis  = CheckFaceVisibility(internalPos, chunkPos, Side.Front, out _);
        bool     backVis   = CheckFaceVisibility(internalPos, chunkPos, Side.Back, out _);
        bool     leftVis   = CheckFaceVisibility(internalPos, chunkPos, Side.Left, out _);
        bool     rightVis  = CheckFaceVisibility(internalPos, chunkPos, Side.Right, out _);

        // Bottom Face
        if (bottomVis)
        {
            meshData.Merge(new MeshData(bottomFaceVerts, bottomFaceTriangles, UVMap.GetUVs(block.BlockName), defaultUVs));
        }
        // Top Face
        if (topVis)
        {
            meshData.Merge(new MeshData(topFaceVerts, topFaceTriangles, UVMap.GetUVs(block.BlockName), defaultUVs));
        }
        // Front Face
        if (frontVis)
        {
            meshData.Merge(new MeshData(frontFaceVerts, frontFaceTriangles, UVMap.GetUVs(block.BlockName), defaultUVs));
        }
        // Back Face
        if (backVis)
        {
            meshData.Merge(new MeshData(backFaceVerts, backFaceTriangles, UVMap.GetUVs(block.BlockName), defaultUVs));
        }
        // Left Face
        if (leftVis)
        {
            meshData.Merge(new MeshData(leftFaceVerts, leftFaceTriangles, UVMap.GetUVs(block.BlockName), defaultUVs));
        }
        // Right Face
        if (rightVis)
        {
            meshData.Merge(new MeshData(rightFaceVerts, rightFaceTriangles, UVMap.GetUVs(block.BlockName), defaultUVs));
        }
        meshData.OffsetPosition(new Vector3(internalPos.x - 0.5f, internalPos.y - 0.5f, internalPos.z - 0.5f));
        return(meshData);
    }
Пример #2
0
    /// <summary>
    /// Creates the mesh data for the block at the given internal position within the chunk at the given chunk position.
    /// </summary>
    /// <param name="block">The block type to create mesh for.</param>
    /// <param name="internalPos">The internal position of the block to create a mesh for.</param>
    /// <param name="chunkPos">The chunk position of the block to create a mesh for.</param>
    /// <returns>Returns the mesh data created for the block.</returns>
    public static MeshData CreateMesh(Block block, Vector3Int internalPos, Vector3Int chunkPos)
    {
        // TODO: Add support for block rotation so blocks with different faces can be placed in any orientation, or at least additional orientations.
        // TODO: Add lighting data into UV2 for mesh and use it in shader to determine brightness of texture.
        MeshData   meshData  = new MeshData();
        bool       topVis    = CheckFaceVisibility(internalPos, chunkPos, Side.Top, out _);
        bool       bottomVis = CheckFaceVisibility(internalPos, chunkPos, Side.Bottom, out _);
        bool       frontVis  = CheckFaceVisibility(internalPos, chunkPos, Side.Front, out _);
        bool       backVis   = CheckFaceVisibility(internalPos, chunkPos, Side.Back, out _);
        bool       rightVis  = CheckFaceVisibility(internalPos, chunkPos, Side.Right, out _);
        bool       leftVis   = CheckFaceVisibility(internalPos, chunkPos, Side.Left, out _);
        Vector3Int worldPos  = internalPos.InternalPosToWorldPos(chunkPos);
        int        rotation  = Mathf.RoundToInt(GameManager.Instance.CaveWormPositionNoiseGenerator.GetNoise(worldPos.x, worldPos.y, worldPos.z).Remap(-1, 1, 0, 3));
        BlockType  blockType = BlockType.BlockTypes[block.ID];

        // Top Face
        if (topVis)
        {
            string    uvName = ((blockType.UniqueFaces & BlockType.UniqueFacesEnum.Top) == BlockType.UniqueFacesEnum.Top) ? blockType.BlockName + "_" + blockType.TopTextureName : blockType.BlockName;
            Vector2[] uvs    = ((blockType.RandomFaces & BlockType.RandomFacesEnum.Top) == BlockType.RandomFacesEnum.Top) ? UVMap.GetUVs(uvName).RotateUVs(rotation) : UVMap.GetUVs(uvName);
            meshData.Merge(new MeshData(topFaceVerts, topFaceTriangles, uvs, defaultUVs));
        }
        // Bottom Face
        if (bottomVis)
        {
            string    uvName = ((blockType.UniqueFaces & BlockType.UniqueFacesEnum.Bottom) == BlockType.UniqueFacesEnum.Bottom) ?  blockType.BlockName + "_" + blockType.BottomTextureName : blockType.BlockName;
            Vector2[] uvs    = ((blockType.RandomFaces & BlockType.RandomFacesEnum.Bottom) == BlockType.RandomFacesEnum.Bottom) ? UVMap.GetUVs(uvName).RotateUVs(rotation) : UVMap.GetUVs(uvName);
            meshData.Merge(new MeshData(bottomFaceVerts, bottomFaceTriangles, uvs, defaultUVs));
        }
        // Front Face
        if (frontVis)
        {
            if (blockType.BlockName == "Grass" && World.TryGetBlockFromWorldPos(internalPos.InternalPosToWorldPos(chunkPos) + new Vector3Int(0, -1, 1), out Block neighborBlock) == true && BlockType.BlockTypes[neighborBlock.ID].BlockName == "Grass")
            {
                string    uvName = blockType.BlockName;
                Vector2[] uvs    = UVMap.GetUVs(uvName).RotateUVs(rotation);
                meshData.Merge(new MeshData(frontFaceVerts, frontFaceTriangles, uvs, defaultUVs));
            }
            else
            {
                string    uvName = ((blockType.UniqueFaces & BlockType.UniqueFacesEnum.Front) == BlockType.UniqueFacesEnum.Front) ? blockType.BlockName + "_" + blockType.FrontTextureName : blockType.BlockName;
                Vector2[] uvs    = ((blockType.RandomFaces & BlockType.RandomFacesEnum.Front) == BlockType.RandomFacesEnum.Front) ? UVMap.GetUVs(uvName).RotateUVs(rotation) : UVMap.GetUVs(uvName).RotateUVs(3);
                meshData.Merge(new MeshData(frontFaceVerts, frontFaceTriangles, uvs, defaultUVs));
            }
        }
        // Back Face
        if (backVis)
        {
            if (blockType.BlockName == "Grass" && World.TryGetBlockFromWorldPos(internalPos.InternalPosToWorldPos(chunkPos) + new Vector3Int(0, -1, -1), out Block neighborBlock) == true && BlockType.BlockTypes[neighborBlock.ID].BlockName == "Grass")
            {
                string    uvName = blockType.BlockName;
                Vector2[] uvs    = UVMap.GetUVs(uvName).RotateUVs(rotation);
                meshData.Merge(new MeshData(backFaceVerts, backFaceTriangles, uvs, defaultUVs));
            }
            else
            {
                string    uvName = ((blockType.UniqueFaces & BlockType.UniqueFacesEnum.Back) == BlockType.UniqueFacesEnum.Back) ? blockType.BlockName + "_" + blockType.BackTextureName : blockType.BlockName;
                Vector2[] uvs    = ((blockType.RandomFaces & BlockType.RandomFacesEnum.Back) == BlockType.RandomFacesEnum.Back) ? UVMap.GetUVs(uvName).RotateUVs(rotation) : UVMap.GetUVs(uvName).RotateUVs(3);
                meshData.Merge(new MeshData(backFaceVerts, backFaceTriangles, uvs, defaultUVs));
            }
        }
        // Right Face
        if (rightVis)
        {
            if (blockType.BlockName == "Grass" && World.TryGetBlockFromWorldPos(internalPos.InternalPosToWorldPos(chunkPos) + new Vector3Int(1, -1, 0), out Block neighborBlock) == true && BlockType.BlockTypes[neighborBlock.ID].BlockName == "Grass")
            {
                string    uvName = blockType.BlockName;
                Vector2[] uvs    = UVMap.GetUVs(uvName).RotateUVs(rotation);
                meshData.Merge(new MeshData(rightFaceVerts, rightFaceTriangles, uvs, defaultUVs));
            }
            else
            {
                string    uvName = ((blockType.UniqueFaces & BlockType.UniqueFacesEnum.Right) == BlockType.UniqueFacesEnum.Right) ? blockType.BlockName + "_" + blockType.RightTextureName : blockType.BlockName;
                Vector2[] uvs    = ((blockType.RandomFaces & BlockType.RandomFacesEnum.Right) == BlockType.RandomFacesEnum.Right) ? UVMap.GetUVs(uvName).RotateUVs(rotation) : UVMap.GetUVs(uvName).RotateUVs(3);
                meshData.Merge(new MeshData(rightFaceVerts, rightFaceTriangles, uvs, defaultUVs));
            }
        }
        // Left Face
        if (leftVis)
        {
            if (blockType.BlockName == "Grass" && World.TryGetBlockFromWorldPos(internalPos.InternalPosToWorldPos(chunkPos) + new Vector3Int(-1, -1, 0), out Block neighborBlock) == true && BlockType.BlockTypes[neighborBlock.ID].BlockName == "Grass")
            {
                string    uvName = blockType.BlockName;
                Vector2[] uvs    = UVMap.GetUVs(uvName).RotateUVs(rotation);
                meshData.Merge(new MeshData(leftFaceVerts, leftFaceTriangles, uvs, defaultUVs));
            }
            else
            {
                string    uvName = ((blockType.UniqueFaces & BlockType.UniqueFacesEnum.Left) == BlockType.UniqueFacesEnum.Left) ? blockType.BlockName + "_" + blockType.LeftTextureName : blockType.BlockName;
                Vector2[] uvs    = ((blockType.RandomFaces & BlockType.RandomFacesEnum.Left) == BlockType.RandomFacesEnum.Left) ? UVMap.GetUVs(uvName).RotateUVs(rotation) : UVMap.GetUVs(uvName).RotateUVs(3);
                meshData.Merge(new MeshData(leftFaceVerts, leftFaceTriangles, uvs, defaultUVs));
            }
        }
        meshData.OffsetPosition(new Vector3(internalPos.x - 0.5f, internalPos.y - 0.5f, internalPos.z - 0.5f));
        return(meshData);
    }