예제 #1
0
        public void UpdateTexture(TileLight[] lights)
        {
            for (int x = 0; x < chunkSize.x; x++)
            {
                for (int y = 0; y < chunkSize.y; y++)
                {
                    int2 lightPosition = TileUtil.TileToWorldTile(new int2(x, y), chunkPosition, chunkSize);

                    int index = TileUtil.To1DIndex(lightPosition, mapSize);

                    int sunLight = lights[index].GetSunLight();

                    int torchRedLight   = lights[index].GetRedLight();
                    int torchGreenLight = lights[index].GetGreenLight();
                    int torchBlueLight  = lights[index].GetBlueLight();

                    byte redIntensity   = (byte)Mathf.Max(torchRedLight, sunLight);
                    byte greenIntensity = (byte)Mathf.Max(torchGreenLight, sunLight);
                    byte blueIntensity  = (byte)Mathf.Max(torchBlueLight, sunLight);

                    Color32 color = new Color32(redIntensity, greenIntensity, blueIntensity, 255);
                    texture.SetPixel(x, y, color);
                }
            }

            texture.Apply();
        }
예제 #2
0
        public void Init(int2 position, TileManager manager, int2 chunkSize, int2 mapSize, Material tileMaterial, Material lightMaterial)
        {
            tileManager   = manager;
            chunkPosition = position;
            mesh.Init(chunkSize, mapSize, chunkPosition, tileMaterial);
            lightTexture.Init(chunkSize, mapSize, chunkPosition, lightMaterial);

            int2    lightTextureTilePosition = TileUtil.TileToWorldTile(int2.zero, chunkPosition, chunkSize);
            Vector3 lightTexturePosition     = new Vector3(lightTextureTilePosition.x, lightTextureTilePosition.y);

            lightTexturePosition.z         -= 10;
            lightTexture.transform.position = lightTexturePosition;

            meshDirty  = true;
            lightDirty = true;
        }
        void GenerateMesh(int[] tiles, float[] waterDensities)
        {
            vertices.Clear();
            indices.Clear();
            uvs.Clear();
            colors.Clear();
            paths.Clear();;
            visited.Clear();

            int numQuads = 0;

            for (int x = 0; x < chunkSize.x; x++)
            {
                for (int y = 0; y < chunkSize.y;)
                {
                    int2 tilePosition = TileUtil.TileToWorldTile(new int2(x, y), chunkPosition, chunkSize);
                    int  index        = TileUtil.To1DIndex(tilePosition, mapSize);
                    int  tile         = tiles[index];

                    if (tile == 0)
                    {
                        y++;
                        continue;
                    }

                    if (visited.Contains(tilePosition))
                    {
                        y++;
                        continue;
                    }

                    visited.Add(tilePosition);

                    int height;
                    for (height = 1; height + y < chunkSize.y; height++)
                    {
                        int2 nextPosition = tilePosition + TileUtil.Up * height;

                        if (!TileUtil.BoundaryCheck(nextPosition, chunkPosition, chunkSize))
                        {
                            break;
                        }

                        int nextIndex = TileUtil.To1DIndex(nextPosition, mapSize);

                        int nextTile = tiles[nextIndex];

                        if (nextTile != tile)
                        {
                            break;
                        }

                        if (!TileManager.tileInformations[tile].isSolid)
                        {
                            break;
                        }

                        if (visited.Contains(nextPosition))
                        {
                            break;
                        }

                        visited.Add(nextPosition);
                    }

                    bool done = false;
                    int  width;
                    for (width = 1; width + x < chunkSize.x; width++)
                    {
                        for (int dy = 0; dy < height; dy++)
                        {
                            int2 nextPosition = tilePosition + TileUtil.Up * dy + TileUtil.Right * width;

                            if (!TileUtil.BoundaryCheck(nextPosition, chunkPosition, chunkSize))
                            {
                                done = true;
                                break;
                            }

                            int nextIndex = TileUtil.To1DIndex(nextPosition, mapSize);

                            int nextTile = tiles[nextIndex];

                            if (nextTile != tile || visited.Contains(nextPosition))
                            {
                                done = true;
                                break;
                            }

                            if (!TileManager.tileInformations[tile].isSolid)
                            {
                                done = true;
                                break;
                            }
                        }

                        if (done)
                        {
                            break;
                        }

                        for (int dy = 0; dy < height; dy++)
                        {
                            int2 nextPosition =
                                tilePosition +
                                TileUtil.Up * dy +
                                TileUtil.Right * width;
                            visited.Add(nextPosition);
                        }
                    }

                    float2 scale = new float2(width, height);

                    List <Vector2> points = new List <Vector2>();
                    for (int i = 0; i < 4; i++)
                    {
                        Vector2 vertex = tileVertices[i] * scale + tilePosition;
                        vertices.Add(vertex);

                        Vector2 uv2 = tileVertices[i] * scale;
                        Vector4 uv4 = new Vector4(uv2.x, uv2.y, scale.x, scale.y);
                        uvs.Add(uv4);

                        Color32 color = TileManager.tileInformations[tile].color;
                        if (TileManager.tileInformations[tile].isSolid)
                        {
                            colors.Add(color);
                        }
                        else
                        {
                            float density = Mathf.Clamp(waterDensities[index], 0.0f, 1.0f);

                            int2 upPosition = tilePosition + TileUtil.Up;
                            if (TileUtil.BoundaryCheck(upPosition, mapSize))
                            {
                                int upTile = tiles[TileUtil.To1DIndex(upPosition, mapSize)];
                                if (!TileManager.tileInformations[upTile].isSolid && upTile != 0)
                                {
                                    density = 1.0f;
                                }
                            }

                            int2 downPosition = tilePosition + TileUtil.Down;
                            if (TileUtil.BoundaryCheck(downPosition, mapSize))
                            {
                                int downTile = tiles[TileUtil.To1DIndex(downPosition, mapSize)];
                                if (!TileManager.tileInformations[downTile].isSolid && downTile == 0)
                                {
                                    density = 1.0f;
                                }
                            }

                            color.a = (byte)(byte.MaxValue * density);
                            colors.Add(color);
                        }


                        // Not Optimized Collider Generation
                        Vector2 point = colliderPoints[i] * scale + tilePosition;
                        points.Add(point);
                    }
                    paths.Add(points);

                    for (int i = 0; i < 6; i++)
                    {
                        indices.Add(tileIndices[i] + numQuads * 4);
                    }

                    y += height;
                    numQuads++;
                }
            }
        }