Beispiel #1
0
    public RaycastResult?AlaphaRaycast(Vector3 position, Vector3 direction, float distance = 20)
    {
        Vector3 increase = Vector3.Normalize(direction) * 0.05f;

        Point3 last = new Point3();
        Point3 result;

        int count = (int)(distance / 0.05f);

        for (int i = 0; i < count; i++)
        {
            result = new Point3(ToCell(position.x), ToCell(position.y), ToCell(position.z));
            if (!result.Equals(last))
            {
                int value = terrain.GetCellValue(result.X, result.Y, result.Z);
                if (value != BlockTerrain.NULL_BLOCK_VALUE && BlockTerrain.GetContent(value) != 0)
                {
                    return(new RaycastResult
                    {
                        Position = result,
                        LastPosition = last,
                        BlockValue = value,
                        Distance = i * 0.05f
                    });
                }
                last = result;
            }
            position += increase;
        }

        return(null);
    }
Beispiel #2
0
    public void GenerateTerrain(int x, int y, int z, int value, int face, BlockTerrain.Chunk chunk, ref CellFace data)
    {
        int?color = GetColor(BlockTerrain.GetData(value));

        data.TextureSlot = color.HasValue ? BlocksData.paintedTextures[TextureSlot] : TextureSlot;
        data.Color       = BlocksData.ColorFromInt(color);
    }
Beispiel #3
0
    public unsafe void SaveChunk(BlockTerrain.Chunk chunk)
    {
        if (!chunk.isEdited)
        {
            return;
        }
        lock (locker)
        {
            Point2 p = new Point2(chunk.chunkx, chunk.chunky);
            long   value;
            if (chunkOffsets.TryGetValue(p, out value))
            {
                stream.Seek(value, SeekOrigin.Begin);
                WriteChunkHeader(stream, chunk.chunkx, chunk.chunky);

                fixed(byte *bptr = &buffer[0])
                {
                    int *iptr = (int *)bptr;

                    for (int x = 0; x < 16; x++)
                    {
                        for (int y = 0; y < 16; y++)
                        {
                            int index = BlockTerrain.GetCellIndex(15 - x, 0, y);
                            int h     = 0;
                            while (h < 128)
                            {
                                *iptr = chunk.GetCellValue(index);
                                iptr++;
                                h++;
                                index++;
                            }
                        }
                    }
                }

                stream.Write(buffer, 0, 131072);

                fixed(byte *bptr = &buffer[0])
                {
                    int *iptr = (int *)bptr;

                    for (int x = 0; x < 16; x++)
                    {
                        int index = BlockTerrain.GetShiftIndex(15 - x, 0);
                        int h     = 0;
                        while (h < 16)
                        {
                            *iptr = chunk.GetShiftValue(index);
                            iptr++;
                            h++;
                            index++;
                        }
                    }
                }

                stream.Write(buffer, 0, 1024);
            }
        }
    }
Beispiel #4
0
    //	public void SaveChunkEntries ()
    //	{
    //		Point2[] entries = new Point2[chunkOffsets.Count];
    //		foreach (KeyValuePair<Point2, long> p in chunkOffsets) {
    //			entries [(int)((p.Value - 786444L) / 132112L)] = p.Key;
    //		}
    //
    //		stream.Seek (0, SeekOrigin.Begin);
    //		for (int i = 0; i < entries.Length; i++) {
    //			WriteChunkEntry (stream, -entries [i].X, entries [i].Y, i);
    //		}
    //	}

    public unsafe void ReadChunk(int chunkx, int chunky, BlockTerrain.Chunk chunk)
    {
        lock (locker)
        {
            Point2 p = new Point2(chunkx, chunky);
            long   value;
            if (chunkOffsets.TryGetValue(p, out value))
            {
                stream.Seek(value, SeekOrigin.Begin);
                ReadChunkHeader(stream);

                stream.Read(buffer, 0, 131072);

                fixed(byte *bptr = &buffer[0])
                {
                    int *iptr = (int *)bptr;

                    for (int x = 0; x < 16; x++)
                    {
                        for (int y = 0; y < 16; y++)
                        {
                            int index = BlockTerrain.GetCellIndex(15 - x, 0, y);
                            int h     = 0;
                            while (h < 128)
                            {
                                chunk.SetCellValue(index, *iptr);
                                iptr++;
                                h++;
                                index++;
                            }
                        }
                    }
                }

                stream.Read(buffer, 0, 1024);

                fixed(byte *bptr = &buffer[0])
                {
                    int *iptr = (int *)bptr;

                    for (int x = 0; x < 16; x++)
                    {
                        int index = BlockTerrain.GetShiftIndex(15 - x, 0);
                        int h     = 0;
                        while (h < 16)
                        {
                            chunk.SetShiftValue(index, *iptr);
                            iptr++;
                            h++;
                            index++;
                        }
                    }
                }
            }
        }
    }
Beispiel #5
0
    public static int GetBlockColorInt(Block block, int value)
    {
        IPaintableBlock paintable = block as IPaintableBlock;

        if (paintable == null)
        {
            return(0);
        }
        return(paintable.GetColor(BlockTerrain.GetData(value)) ?? 0);
    }
Beispiel #6
0
    public void Load()
    {
        Debug.Log("loading terrain: " + BlockTerrain.terrainSize);
        Terrain = new BlockTerrain();
        LoadTerrain(WorldManager.ChunkDat);
        Camera.main.transform.position = WorldManager.Project.PlayerPosition;

        Point2 p = CurrentChunk();

        Terrain.chunkStats.SetOffset(p.X + 2000, p.Y + 2000);
    }
Beispiel #7
0
    public void GenerateTerrain(int x, int y, int z, int value, BlockTerrain.Chunk chunk, MeshGenerator g)
    {
        int?color = GetColor(BlockTerrain.GetData(value));

        if (color.HasValue)
        {
            g.Terrain.Mesh(x, y, z, paintedBlockMeshes[GetVariant(BlockTerrain.GetData(value))], BlocksData.DEFAULT_COLORS[color.Value]);
        }
        else
        {
            g.Terrain.Mesh(x, y, z, blockMeshes[GetVariant(BlockTerrain.GetData(value))], Color.white);
        }
    }
Beispiel #8
0
    public void GenerateTerrain(int x, int y, int z, int value, BlockTerrain.Chunk chunk, MeshGenerator g)
    {
        int d = BlockTerrain.GetData(value);

        if (furnitureManager.isTransparent[GetDesignIndex(d)])
        {
            g.AlphaTest.Mesh(x, y, z, furnitureManager.GetFurniture(GetDesignIndex(d), GetRotation(d)));
        }
        else
        {
            g.Terrain.Mesh(x, y, z, furnitureManager.GetFurniture(GetDesignIndex(d), GetRotation(d)));
        }
    }
Beispiel #9
0
    public void GenerateTerrain(int x, int y, int z, int value, BlockTerrain.Chunk chunk, MeshGenerator g)
    {
        int data = BlockTerrain.GetData(value);
        int?i    = GetColor(data);
        GreedyTerrainMesh terrainMesh = useAlphaTest ? g.AlphaTest : g.Terrain;

        if (i.HasValue)
        {
            terrainMesh.Mesh(x, y, z, paintedBlockMeshes[GetVariant(data)], BlocksData.DEFAULT_COLORS[i.Value]);
        }
        else
        {
            terrainMesh.Mesh(x, y, z, blockMeshes[GetVariant(data)], unpaintedColor);
        }
    }
Beispiel #10
0
 public override void OnSetToCurrent()
 {
     Console.AssignCommand("setData", delegate(string[] args)
     {
         if (terrainRaycast.LookingAt.HasValue)
         {
             Point3 p = terrainRaycast.LookingAt.Value.Position;
             terrainManager.ChangeCell(p.X, p.Y, p.Z, BlockTerrain.ReplaceData(terrainManager.Terrain.GetCellValue(p.X, p.Y, p.Z), int.Parse(args[0])));
         }
     });
     Console.AssignCommand("setContent", delegate(string[] args)
     {
         placeBlockValue = int.Parse(args[0]);
     });
 }
Beispiel #11
0
    public void GenerateTerrain(int x, int y, int z, int value, BlockTerrain.Chunk chunk, MeshGenerator g)
    {
        switch (GetFace(BlockTerrain.GetData(value)))
        {
        case 1:
            g.AlphaTest.TwoSidedQuad(
                new Vector3(x, y, z),
                new Vector3(x, y + 1.0f, z),
                new Vector3(x, y + 1.0f, z + 1.0f),
                new Vector3(x, y, z + 1.0f),
                TextureSlot, map.Lookup(chunk.GetShiftValue(x, z))
                );
            break;

        case 0:
            g.AlphaTest.TwoSidedQuad(
                new Vector3(x, y, z + 1.0f),
                new Vector3(x, y + 1.0f, z + 1.0f),
                new Vector3(x + 1.0f, y + 1.0f, z + 1.0f),
                new Vector3(x + 1.0f, y, z + 1.0f),
                TextureSlot, map.Lookup(chunk.GetShiftValue(x, z))
                );
            break;

        case 3:
            g.AlphaTest.TwoSidedQuad(
                new Vector3(x + 1.0f, y, z + 1.0f),
                new Vector3(x + 1.0f, y + 1.0f, z + 1.0f),
                new Vector3(x + 1.0f, y + 1.0f, z),
                new Vector3(x + 1.0f, y, z),
                TextureSlot, map.Lookup(chunk.GetShiftValue(x, z))
                );
            break;

        case 2:
            g.AlphaTest.TwoSidedQuad(
                new Vector3(x + 1.0f, y, z),
                new Vector3(x + 1.0f, y + 1.0f, z),
                new Vector3(x, y + 1.0f, z),
                new Vector3(x, y, z),
                TextureSlot, map.Lookup(chunk.GetShiftValue(x, z))
                );
            break;

        default:
            throw new UnityException("undefined face: " + GetFace(BlockTerrain.GetData(value)));
        }
    }
Beispiel #12
0
    public void GenerateTerrain(int x, int y, int z, int value, BlockTerrain.Chunk chunk, MeshGenerator g)
    {
        int   textureSlot = GetIsSmall(BlockTerrain.GetData(value)) ? 84 : 85;
        Color color       = GrassBlock.map.Lookup(chunk.GetShiftValue(x, z));

        g.AlphaTest.TwoSidedQuad(new Vector3(x, y, z),
                                 new Vector3(x + 1.0f, y, z + 1.0f),
                                 new Vector3(x + 1.0f, y + 1.0f, z + 1.0f),
                                 new Vector3(x, y + 1.0f, z),
                                 textureSlot, color);

        g.AlphaTest.TwoSidedQuad(new Vector3(x, y, z + 1.0f),
                                 new Vector3(x + 1.0f, y, z),
                                 new Vector3(x + 1.0f, y + 1.0f, z),
                                 new Vector3(x, y + 1.0f, z + 1.0f),
                                 textureSlot, color);
    }
Beispiel #13
0
    float CalculateNeighborHeight(int value)
    {
        int num = BlockTerrain.GetContent(value);

        if (IsTheSameFluid(num))
        {
            int data = BlockTerrain.GetData(value);
            if (GetIsTop(data))
            {
                return(GetLevelHeight(GetLevel(data)));
            }
            return(1f);
        }
        if (num == 0)
        {
            return(0.01f);
        }
        return(0f);
    }
Beispiel #14
0
    public void ChangeCell(int x, int y, int z, int newValue)
    {
        int index = Terrain.GetChunkIndex(x >> 4, z >> 4);

        BlockTerrain.Chunk        c     = Terrain.GetChunk(index);
        BlockTerrain.ChunkStatics state = Terrain.chunkStats.Get(index);
        if (c != null)
        {
            int cx = x & 15;
            int cz = z & 15;

            int content = c.GetCellContent(cx, y, cz);
            if (content != 0)
            {
                state.state = 3;
            }
            content = BlockTerrain.GetContent(newValue);
            if (content != 0)
            {
                state.state = 3;
            }
            c.SetCellValue(cx, y, cz, newValue);
            c.isEdited = true;
            if (cx == 0 && c.XminusOne != null)
            {
                QuqueChunkUpdate(c.XminusOne.index, 3);
            }
            else if (cx == 15 && c.XplusOne != null)
            {
                QuqueChunkUpdate(c.XplusOne.index, 3);
            }
            if (cz == 0 && c.YminusOne != null)
            {
                QuqueChunkUpdate(c.YminusOne.index, 3);
            }
            else if (cz == 15 && c.YplusOne != null)
            {
                QuqueChunkUpdate(c.YplusOne.index, 3);
            }
            QuqueChunkUpdate(c.index);
        }
    }
Beispiel #15
0
    public void Load(ProjectData project)
    {
        XElement designs = project.GetSubsystem("FurnitureBlockBehavior").GetValues("FurnitureDesigns");

        List <Furniture> fs      = new List <Furniture>();
        List <int>       isTrans = new List <int>();

        foreach (XElement elem in designs.Elements("Values"))
        {
            fs.Add(LoadFurniture(elem));
        }
        int count = 0;

        foreach (Furniture f in fs)
        {
            int[] data = f.Data;
            count = Mathf.Max(count, f.Index);

            for (int i = 0; i < data.Length; i++)
            {
                if (BlockTerrain.GetContent(data[i]) == 15)
                {
                    isTrans.Add(f.Index);
                    break;
                }
            }
        }

        isTransparent = new bool[count + 1];
        foreach (int i in isTrans)
        {
            isTransparent[i] = true;
        }

        foreach (Furniture f in fs)
        {
            if (f.TerrainUseCount > 0)
            {
                LoadMash(f);
            }
        }
    }
Beispiel #16
0
    void Export(string exportPath)
    {
        BlockTerrain terrain = FindObjectOfType <TerrainManager>().Terrain;

        int[] chunks = FindObjectOfType <ChunkRenderer>().RenderingChunks;

        List <Mesh>      ms = new List <Mesh>();
        List <Matrix4x4> ts = new List <Matrix4x4>();

        for (int i = 0; i < chunks.Length; i++)
        {
            ChunkInstance instance = terrain.chunkInstances[chunks[i]];

            ms.Add(instance.Meshes[0]);
            ts.Add(instance.transform);
            ms.Add(instance.Meshes[1]);
            ts.Add(instance.transform);
        }

        Export(exportPath, ms.ToArray(), ts.ToArray());
    }
Beispiel #17
0
    public void GenerateNormalBlocks(BlockTerrain.Chunk chunk)
    {
        bool[]         isTransparent = BlocksData.IsTransparent;
        INormalBlock[] normalBlocks  = BlocksData.NormalBlocks;

        for (int x = 0; x < 16; x++)
        {
            for (int z = 0; z < 16; z++)
            {
                for (int y = 0; y < 128; y++)
                {
                    int value   = chunk.GetCellValue(x, y, z);
                    int content = BlockTerrain.GetContent(value);
                    if (isTransparent[content])
                    {
                        normalBlocks[content].GenerateTerrain(x, y, z, value, chunk, this);
                    }
                }
            }
        }
    }
Beispiel #18
0
    private void Start()
    {
        Terrain = GetComponent <TerrainManager>().Terrain;

        string blockTextureName = WorldManager.Project.GameInfo.BlockTextureName;

        if (!string.IsNullOrEmpty(blockTextureName))
        {
            string path = System.IO.Path.Combine(WorldManager.CurrentEmbeddedContent, blockTextureName);
            defautTexture.LoadImage(System.IO.File.ReadAllBytes(path));
        }

        try
        {
            //Texture2D tex = new Texture2D (TEXTURE_SIZE, TEXTURE_SIZE, TextureFormat.ARGB32, true, true);
            //tex.filterMode = FilterMode.Point;
            //tex.LoadImage(System.IO.File.ReadAllBytes(path));

#if GREEDY_MESHING
            Texture2D main, alphaTest;
            TextureProcessing.ProcessTexture2(defautTexture, out main, out alphaTest);
            terrain.mainTexture    = main;
            alaphaTest.mainTexture = alphaTest;
            terrain.shader         = TerrainShader;
#else
            Texture2D tex;
            TextureProcessing.ProcessTexture(defautTexture, out tex);
            terrain.mainTexture    = tex;
            alaphaTest.mainTexture = tex;
            terrain.shader         = VertexColorShader;
#endif
        }
        catch (System.Exception e)
        {
            Debug.LogException(e);
        }
    }
Beispiel #19
0
    void OnGUI()
    {
        if (showing)
        {
            int w = Screen.width, h = Screen.height;

            GUIStyle style = new GUIStyle();

            Rect rect = new Rect(0, 0, w, h * 9 / 100);
            style.alignment        = TextAnchor.UpperLeft;
            style.fontSize         = h * 3 / 100;
            style.normal.textColor = new Color(1.0f, 1.0f, 1.0f, 1.0f);
            float         msec = deltaTime * 1000.0f;
            float         fps  = 1.0f / deltaTime;
            string        text = string.Format("{0:0.0} ms ({1:0.} fps)\ncurrent chunk: {2}\n按ESC键暂停\n按P键截图", msec, fps, TerrainManager.CurrentChunk());
            RaycastResult?r    = GetComponent <TerrainRaycast>().LookingAt;
            if (r.HasValue)
            {
                //text += string.Format ("\nlooking at {0}", BlocksData.GetBlock(BlockTerrain.GetContent(r.Value.BlockValue)).Name);
                text += string.Format("\nlooking at {0}", BlocksData.Blocks[BlockTerrain.GetContent(r.Value.BlockValue)].ToString(r.Value.BlockValue));
            }
            GUI.Label(rect, text, style);
        }
    }
Beispiel #20
0
 public BlockSelectionPacker(BlockTerrain terrain)
 {
     this.terrain = terrain;
     root         = new XElement("Items");
 }
Beispiel #21
0
 public static int GetFace(int value)
 {
     return(BlockTerrain.GetData(value) >> 2 & 7);
 }
Beispiel #22
0
 public ChunkStatics Statics(BlockTerrain terrain)
 {
     return(terrain.chunkStats.Get(index));
 }
Beispiel #23
0
 public void GenerateTerrain(int x, int y, int z, int value, BlockTerrain.Chunk chunk, MeshGenerator g)
 {
     g.AlphaTest.Mesh(x, y, z, meshes[BlockTerrain.GetData(value)], Color.white);
 }
Beispiel #24
0
    public void GenerateFluidTerrain(int x, int y, int z, int value, BlockTerrain.Chunk chunk, GreedyTerrainMesh terrainMesh, Color topColor, Color sideColor)
    {
        float height1, height2, height3, height4;

        int data = BlockTerrain.GetData(value);

        if (GetIsTop(data))
        {
            int   cellValueFast  = chunk.GetCellValue(x - 1, y, z - 1);
            int   cellValueFast2 = chunk.GetCellValue(x, y, z - 1);
            int   cellValueFast3 = chunk.GetCellValue(x + 1, y, z - 1);
            int   cellValueFast4 = chunk.GetCellValue(x - 1, y, z);
            int   cellValueFast5 = chunk.GetCellValue(x + 1, y, z);
            int   cellValueFast6 = chunk.GetCellValue(x - 1, y, z + 1);
            int   cellValueFast7 = chunk.GetCellValue(x, y, z + 1);
            int   cellValueFast8 = chunk.GetCellValue(x + 1, y, z + 1);
            float h           = CalculateNeighborHeight(cellValueFast);
            float num         = CalculateNeighborHeight(cellValueFast2);
            float h2          = CalculateNeighborHeight(cellValueFast3);
            float num2        = CalculateNeighborHeight(cellValueFast4);
            float num3        = CalculateNeighborHeight(cellValueFast5);
            float h3          = CalculateNeighborHeight(cellValueFast6);
            float num4        = CalculateNeighborHeight(cellValueFast7);
            float h4          = CalculateNeighborHeight(cellValueFast8);
            float levelHeight = GetLevelHeight(GetLevel(data));
            height1 = CalculateFluidVertexHeight(h, num, num2, levelHeight);
            height2 = CalculateFluidVertexHeight(num, h2, levelHeight, num3);
            height3 = CalculateFluidVertexHeight(levelHeight, num3, num4, h4);
            height4 = CalculateFluidVertexHeight(num2, levelHeight, h3, num4);
        }
        else
        {
            height1 = 1f;
            height2 = 1f;
            height3 = 1f;
            height4 = 1f;
        }

        Vector3 v000 = new Vector3(x, y, z);
        Vector3 v001 = new Vector3(x, y, z + 1f);
        Vector3 v010 = new Vector3(x, y + height1, z);
        Vector3 v011 = new Vector3(x, y + height4, z + 1f);
        Vector3 v100 = new Vector3(x + 1.0f, y, z);
        Vector3 v101 = new Vector3(x + 1.0f, y, z + 1f);
        Vector3 v110 = new Vector3(x + 1.0f, y + height2, z);
        Vector3 v111 = new Vector3(x + 1.0f, y + height3, z + 1f);

        int v = chunk.GetCellValue(x - 1, y, z);

        if (v != BlockTerrain.NULL_BLOCK_VALUE && BlockTerrain.GetContent(v) != Index)
        {
            terrainMesh.NormalQuad(v001, v011, v010, v000, TextureSlot, sideColor);
        }

        v = chunk.GetCellValue(x, y - 1, z);
        if (v != BlockTerrain.NULL_BLOCK_VALUE && BlockTerrain.GetContent(v) != Index)
        {
            terrainMesh.NormalQuad(v000, v100, v101, v001, TextureSlot, sideColor);
        }

        v = chunk.GetCellValue(x, y, z - 1);
        if (v != BlockTerrain.NULL_BLOCK_VALUE && BlockTerrain.GetContent(v) != Index)
        {
            terrainMesh.NormalQuad(v000, v010, v110, v100, TextureSlot, sideColor);
        }

        v = chunk.GetCellValue(x + 1, y, z);
        if (v != BlockTerrain.NULL_BLOCK_VALUE && BlockTerrain.GetContent(v) != Index)
        {
            terrainMesh.NormalQuad(v100, v110, v111, v101, TextureSlot, sideColor);
        }

        v = chunk.GetCellValue(x, y + 1, z);
        if (v != BlockTerrain.NULL_BLOCK_VALUE && BlockTerrain.GetContent(v) != Index)
        {
            terrainMesh.NormalQuad(v111, v110, v010, v011, TextureSlot, topColor);
        }

        v = chunk.GetCellValue(x, y, z + 1);
        if (v != BlockTerrain.NULL_BLOCK_VALUE && BlockTerrain.GetContent(v) != Index)
        {
            terrainMesh.NormalQuad(v101, v111, v011, v001, TextureSlot, sideColor);
        }
    }
Beispiel #25
0
 void Start()
 {
     terrain = GetComponent <TerrainManager>().Terrain;
 }
Beispiel #26
0
    public void GenerateCubeBlocks(BlockTerrain.Chunk chunk)
    {
        bool[] isTransparent              = BlocksData.IsTransparent;
        IStandardCubeBlock[] cubeBlocks   = BlocksData.StandardCubeBlocks;
        INormalBlock[]       normalBlocks = BlocksData.NormalBlocks;

        for (int x = 0; x < 16; x++)
        {
            for (int z = 0; z < 16; z++)
            {
                for (int y = 0; y < 128; y++)
                {
                    int value   = chunk.GetCellValue(x, y, z);
                    int content = BlockTerrain.GetContent(value);
                    if (isTransparent[content])
                    {
                        normalBlocks[content].GenerateTerrain(x, y, z, value, chunk, this);
                    }
                    else
                    {
                        Vector3 v000 = new Vector3(x, y, z);
                        Vector3 v001 = new Vector3(x, y, z + 1.0f);
                        Vector3 v010 = new Vector3(x, y + 1.0f, z);
                        Vector3 v011 = new Vector3(x, y + 1.0f, z + 1.0f);
                        Vector3 v100 = new Vector3(x + 1.0f, y, z);
                        Vector3 v101 = new Vector3(x + 1.0f, y, z + 1.0f);
                        Vector3 v110 = new Vector3(x + 1.0f, y + 1.0f, z);
                        Vector3 v111 = new Vector3(x + 1.0f, y + 1.0f, z + 1.0f);

                        var cellFace = new CellFace();
                        var block    = cubeBlocks[content];
                        int neighbor = chunk.GetCellContent(x - 1, y, z);
                        if (neighbor != BlockTerrain.NULL_BLOCK_CONTENT && BlocksData.IsTransparent[neighbor])
                        {
                            block.GenerateTerrain(x, y, z, value, CellFace.BACK, chunk, ref cellFace);
                            Terrain.NormalQuad(v000, v001, v011, v010, cellFace.TextureSlot, cellFace.Color);
                        }

                        neighbor = chunk.GetCellContent(x, y - 1, z);
                        if (neighbor != BlockTerrain.NULL_BLOCK_CONTENT && BlocksData.IsTransparent[neighbor])
                        {
                            block.GenerateTerrain(x, y, z, value, CellFace.BOTTOM, chunk, ref cellFace);
                            Terrain.NormalQuad(v001, v000, v100, v101, cellFace.TextureSlot, cellFace.Color);
                        }

                        neighbor = chunk.GetCellContent(x, y, z - 1);
                        if (neighbor != BlockTerrain.NULL_BLOCK_CONTENT && BlocksData.IsTransparent[neighbor])
                        {
                            block.GenerateTerrain(x, y, z, value, CellFace.LEFT, chunk, ref cellFace);
                            Terrain.NormalQuad(v100, v000, v010, v110, cellFace.TextureSlot, cellFace.Color);
                        }

                        neighbor = chunk.GetCellContent(x + 1, y, z);
                        if (neighbor != BlockTerrain.NULL_BLOCK_CONTENT && BlocksData.IsTransparent[neighbor])
                        {
                            block.GenerateTerrain(x, y, z, value, CellFace.FRONT, chunk, ref cellFace);
                            Terrain.NormalQuad(v101, v100, v110, v111, cellFace.TextureSlot, cellFace.Color);
                        }

                        neighbor = chunk.GetCellContent(x, y + 1, z);
                        if (neighbor != BlockTerrain.NULL_BLOCK_CONTENT && BlocksData.IsTransparent[neighbor])
                        {
                            block.GenerateTerrain(x, y, z, value, CellFace.TOP, chunk, ref cellFace);
                            Terrain.NormalQuad(v011, v111, v110, v010, cellFace.TextureSlot, cellFace.Color);
                        }

                        neighbor = chunk.GetCellContent(x, y, z + 1);
                        if (neighbor != BlockTerrain.NULL_BLOCK_CONTENT && BlocksData.IsTransparent[neighbor])
                        {
                            block.GenerateTerrain(x, y, z, value, CellFace.RIGHT, chunk, ref cellFace);
                            Terrain.NormalQuad(v001, v101, v111, v011, cellFace.TextureSlot, cellFace.Color);
                        }
                    }
                }
            }
        }
    }
Beispiel #27
0
 public string ToString(int value)
 {
     return(string.Format("{0}: {1}, {2}", Name, Index, BlockTerrain.GetData(value)));
 }
Beispiel #28
0
 public Color Lookup(int value)
 {
     return(Lookup(BlockTerrain.GetTemperature(value), BlockTerrain.GetHumidity(value)));
 }
Beispiel #29
0
    public void GenerateFurnitureMeshAlphaTest(FurnitureManager.Furniture furniture, out MeshData mesh)
    {
        Block[] blocks = BlocksData.Blocks;

        int       res         = furniture.Resolution;
        Matrix4x4 matrix      = Matrix4x4.Scale(Vector3.one / res);
        float     uvBlockSize = 0.0625f / res;

        int[] mask = new int[res * res];
        int   u, v, n, w, h, j, i, l, k;

        int[] off;
        int[] x;

        for (int d = 0; d < 3; d++)
        {
            off = new int[] { 0, 0, 0 };
            x   = new int[] { 0, 0, 0 };
            u   = (d + 1) % 3;
            v   = (d + 2) % 3;

            off[d] = 1;

            for (x[d] = -1; x[d] < res;)
            {
                //Debug.LogFormat("x[d]: {0}", x[d]);
                mask = new int[res * res];
                for (n = 0; n < mask.Length; n++)
                {
                    mask[n] = -1;
                }

                n = 0;
                for (x[v] = 0; x[v] < res; x[v]++)
                {
                    for (x[u] = 0; x[u] < res; x[u]++)
                    {
                        int va = furniture.GetCellValue(x[0], x[1], x[2]);
                        int vb = furniture.GetCellValue(x[0] + off[0], x[1] + off[1], x[2] + off[2]);
                        int ia = BlockTerrain.GetContent(va);
                        int ib = BlockTerrain.GetContent(vb);
                        //Debug.LogFormat("{0} and {1}: {2}, {3}", new Point3(x[0], x[1], x[2]), new Point3(x[0] + off[0], x[1] + off[1], x[2] + off[2]), a.Name, b.Name);
                        if (ia == 0 && ib != 0)
                        {
                            Block b = blocks[ib];
                            mask[n]  = b.TextureSlot;
                            mask[n] |= BlocksData.GetBlockColorInt(b, vb) << 8;
                            mask[n] |= 4096;
                        }
                        else if (ia != 0 && ib == 0)
                        {
                            Block a = blocks[ia];
                            mask[n]  = a.TextureSlot;
                            mask[n] |= BlocksData.GetBlockColorInt(a, va) << 8;
                        }
                        n++;
                    }
                }

                ++x[d];
                n = 0;
                for (j = 0; j < res; j++)
                {
                    for (i = 0; i < res;)
                    {
                        if (mask[n] != -1)
                        {
                            for (w = 1; i + w < res && mask[w + n] == mask[n]; w++)
                            {
                            }
                            for (h = 1; h + j < res; h++)
                            {
                                for (k = 0; k < w; k++)
                                {
                                    if (mask[n + k + h * res] != mask[n])
                                    {
                                        goto Done;
                                    }
                                }
                            }
Done:
                            //Debug.LogFormat("quard: {0}, {1}, {2}, {3}; {4}", j, i, h, w, x[d]);

                            x[u] = i;
                            x[v] = j;
                            int[] du = new int[] { 0, 0, 0 };
                            int[] dv = new int[] { 0, 0, 0 };
                            du[u] = w;
                            dv[v] = h;

                            int textureSlot = mask[n] & 255;
                            if (mask[n] >> 12 == 0)
                            {
                                Terrain.FurnitureQuad(
                                    new Vector3(x[0], x[1], x[2]),
                                    new Vector3(x[0] + du[0], x[1] + du[1], x[2] + du[2]),
                                    new Vector3(x[0] + du[0] + dv[0], x[1] + du[1] + dv[1], x[2] + du[2] + dv[2]),
                                    new Vector3(x[0] + dv[0], x[1] + dv[1], x[2] + dv[2]),
                                    x[u], x[v], w, h, uvBlockSize,
                                    textureSlot,
                                    BlocksData.DEFAULT_COLORS[(mask[n] >> 8) & 15]
                                    );
                            }
                            else
                            {
                                Terrain.FurnitureQuad(
                                    new Vector3(x[0], x[1], x[2]),
                                    new Vector3(x[0] + dv[0], x[1] + dv[1], x[2] + dv[2]),
                                    new Vector3(x[0] + du[0] + dv[0], x[1] + du[1] + dv[1], x[2] + du[2] + dv[2]),
                                    new Vector3(x[0] + du[0], x[1] + du[1], x[2] + du[2]),
                                    x[v], x[u], h, w, uvBlockSize,
                                    textureSlot,
                                    BlocksData.DEFAULT_COLORS[(mask[n] >> 8) & 15]
                                    );
                            }

                            for (l = 0; l < h; l++)
                            {
                                for (k = 0; k < w; k++)
                                {
                                    mask[n + k + l * res] = -1;
                                }
                            }
                            i += w;
                            n += w;
                        }
                        else
                        {
                            i++;
                            n++;
                        }
                    }
                }
            }
        }

        //Debug.LogFormat("{0}, {1}, {2}", vertices.Count, colors.Count, uvs.Count);

        Terrain.PushToMesh(out mesh);
        MeshData.Transform(mesh, matrix);
    }
Beispiel #30
0
    public void GenerateChunkMesh(BlockTerrain.Chunk chunk)
    {
        bool[] isTrans = BlocksData.IsTransparent;
        IStandardCubeBlock[] blocks = BlocksData.StandardCubeBlocks;

        CellFace[] mask;
        int        u, v, n, w, h, j, i, l, k;

        int[] off;
        int[] x;
        int[] dim = new int[] { 16, 128, 16 };

        for (int d = 0; d < 3; d++)
        {
            off = new int[] { 0, 0, 0 };
            x   = new int[] { 0, 0, 0 };
            u   = (d + 1) % 3;
            v   = (d + 2) % 3;

            off[d] = 1;

            //face = d;

            mask = new CellFace[dim[u] * dim[v]];
            for (x[d] = -1; x[d] < dim[d];)
            {
                //Debug.LogFormat("x[d]: {0}", x[d]);
                for (n = 0; n < mask.Length; n++)
                {
                    mask[n].TextureSlot = -1;
                }

                n = 0;
                for (x[v] = 0; x[v] < dim[v]; x[v]++)
                {
                    for (x[u] = 0; x[u] < dim[u]; x[u]++)
                    {
                        int va = chunk.GetCellValue(x[0], x[1], x[2]);
                        int vb = chunk.GetCellValue(x[0] + off[0], x[1] + off[1], x[2] + off[2]);
                        if (va == BlockTerrain.NULL_BLOCK_VALUE || vb == BlockTerrain.NULL_BLOCK_VALUE)
                        {
                            break;
                        }
                        int ca = BlockTerrain.GetContent(va);
                        int cb = BlockTerrain.GetContent(vb);
                        //Debug.LogFormat("{0} and {1}: {2}, {3}", new Point3(x[0], x[1], x[2]), new Point3(x[0] + off[0], x[1] + off[1], x[2] + off[2]), a.Name, b.Name);
                        if (isTrans[ca])
                        {
                            if (!isTrans[cb] && (((x[0] + off[0])) & 16) == 0)
                            {
                                mask[n].IsOpposite = true;
                                blocks[cb].GenerateTerrain(x[0] + off[0], x[1] + off[1], x[2] + off[2], vb, CellFace.opposite[d], chunk, ref mask[n]);
                            }
                        }
                        else
                        {
                            if (isTrans[cb])
                            {
                                mask[n].IsOpposite = false;
                                blocks[ca].GenerateTerrain(x[0], x[1], x[2], va, d, chunk, ref mask[n]);
                            }
                        }
                        n++;
                    }
                }

                ++x[d];
                n = 0;
                for (j = 0; j < dim[v]; j++)
                {
                    for (i = 0; i < dim[u];)
                    {
                        if (mask[n].TextureSlot != -1)
                        {
                            for (w = 1; i + w < dim[u] && mask[w + n] == mask[n]; w++)
                            {
                            }
                            for (h = 1; h + j < dim[v]; h++)
                            {
                                for (k = 0; k < w; k++)
                                {
                                    if (mask[n + k + h * dim[u]] != mask[n])
                                    {
                                        goto Done;
                                    }
                                }
                            }
Done:
                            //Debug.LogFormat("quard: {0}, {1}, {2}, {3}; {4}", j, i, h, w, x[d]);

                            x[u] = i;
                            x[v] = j;
                            int[] du = new int[] { 0, 0, 0 };
                            int[] dv = new int[] { 0, 0, 0 };
                            du[u] = w;
                            dv[v] = h;

                            int textureSlot = mask[n].TextureSlot;
                            if (!mask[n].IsOpposite)
                            {
                                Terrain.Quad(
                                    new Vector3(x[0], x[1], x[2]),
                                    new Vector3(x[0] + du[0], x[1] + du[1], x[2] + du[2]),
                                    new Vector3(x[0] + du[0] + dv[0], x[1] + du[1] + dv[1], x[2] + du[2] + dv[2]),
                                    new Vector3(x[0] + dv[0], x[1] + dv[1], x[2] + dv[2]),
                                    textureSlot,
                                    mask[n].Color
                                    );
                            }
                            else
                            {
                                Terrain.Quad(
                                    new Vector3(x[0], x[1], x[2]),
                                    new Vector3(x[0] + dv[0], x[1] + dv[1], x[2] + dv[2]),
                                    new Vector3(x[0] + du[0] + dv[0], x[1] + du[1] + dv[1], x[2] + du[2] + dv[2]),
                                    new Vector3(x[0] + du[0], x[1] + du[1], x[2] + du[2]),
                                    textureSlot,
                                    mask[n].Color
                                    );
                            }

                            for (l = 0; l < h; l++)
                            {
                                for (k = 0; k < w; k++)
                                {
                                    mask[n + k + l * dim[u]].TextureSlot = -1;
                                }
                            }
                            i += w;
                            n += w;
                        }
                        else
                        {
                            i++;
                            n++;
                        }
                    }
                }
            }
        }
    }