Beispiel #1
0
    private static void CreateShadowCasters(Level level, LevelBuildData buildData)
    {
        EnvironmentObject    environment = CreateEnvironment("Shadow Caster", EnvironmentObjectComponent.MeshFilter | EnvironmentObjectComponent.MeshRenderer);
        LevelEnvironmentData data        = new LevelEnvironmentData();

        data.Triangles.Add(0, new List <int>());

        foreach (var pair in buildData.Tiles)
        {
            if (!pair.Value.CastsShadows)
            {
                continue;
            }

            Vector2     worldPosition = Utility.AxialToWorldPosition(pair.Key);
            byte        bitmask       = Utility.GetBitmask(pair.Key, level.TilePositions);
            List <Line> lines         = pair.Value.Brush.GetCollisionCoordinates(bitmask);

            foreach (Line line in lines)
            {
                AddLineToMeshData(data, line, worldPosition);
            }
        }

        Mesh mesh = data.BuildMesh("Shadow Caster Mesh");

        environment.Filter.mesh = mesh;
        environment.Renderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.ShadowsOnly;
        environment.Renderer.material          = new Material(Shader.Find("Standard"));

        level.AddEnvironment(environment.GameObject);
    }
Beispiel #2
0
    public static void Build(LevelEnvironmentData data, TileData tile, Vector3 worldPosition, byte bitmask)
    {
        foreach (Line edge in tile.Brush.GetEdgeCoordinates(bitmask))
        {
            int vertexCount = data.Vertices.Count;

            // Vertices.
            Vector3 scaledA = Utility.ScaleToHexagonalSize(edge.A) + worldPosition;
            Vector3 scaledB = Utility.ScaleToHexagonalSize(edge.B) + worldPosition;

            data.Vertices.AddRange(new Vector3[4]
            {
                scaledA,
                scaledB,
                scaledB + new Vector3(0, 0, EdgeDepth),
                scaledA + new Vector3(0, 0, EdgeDepth),
            });

            // Triangles.
            data.SetTriangles(tile.EdgeMaterial, new int[6]
            {
                vertexCount + 0, vertexCount + 1, vertexCount + 2,
                vertexCount + 2, vertexCount + 3, vertexCount + 0,
            });

            // UVs.
            data.UVs.AddRange(new List <Vector2>()
            {
                new Vector2(0, 1),
                new Vector2(1, 1),
                new Vector2(1, 0),
                new Vector2(0, 0),
            });
        }
    }
Beispiel #3
0
    private static void AddLineToMeshData(LevelEnvironmentData data, Line line, Vector3 offset)
    {
        int verticeCount = data.Vertices.Count;

        data.Vertices.AddRange(new Vector3[4]
        {
            offset + Utility.ScaleToHexagonalSize((Vector3)line.A),
            offset + Utility.ScaleToHexagonalSize((Vector3)line.A + CeilingOffset),
            offset + Utility.ScaleToHexagonalSize((Vector3)line.B + CeilingOffset),
            offset + Utility.ScaleToHexagonalSize((Vector3)line.B),
        });

        data.Triangles[0].AddRange(new int[6]
        {
            verticeCount + 2, verticeCount + 1, verticeCount + 0,
            verticeCount + 0, verticeCount + 3, verticeCount + 2,
        });
    }
Beispiel #4
0
    private static void CreateEnvironment(Level level, LevelBuildData buildData)
    {
        EnvironmentObject    environment = CreateEnvironment("Environment", EnvironmentObjectComponent.MeshFilter | EnvironmentObjectComponent.MeshRenderer);
        LevelEnvironmentData data        = new LevelEnvironmentData();

        foreach (var pair in buildData.Tiles)
        {
            Vector2 worldPosition = Utility.AxialToWorldPosition(pair.Key);
            byte    bitmask       = Utility.GetBitmask(pair.Key, level.TilePositions);

            AddTileToLevel(data, pair.Value, worldPosition, bitmask);
        }

        Mesh mesh = data.BuildMesh("Environment Mesh");

        environment.Filter.mesh        = mesh;
        environment.Renderer.materials = data.Materials.ToArray();

        level.AddEnvironment(environment.GameObject);
    }
Beispiel #5
0
    private static void AddTileToLevel(LevelEnvironmentData data, TileData tile, Vector3 worldPosition, byte bitmask)
    {
        int verticeCount = data.Vertices.Count;

        TileMeshData meshData = tile.Brush.GetMeshData(bitmask);

        // Vertices.
        foreach (Vector3 vertex in meshData.Vertices)
        {
            data.Vertices.Add(Utility.ScaleToHexagonalSize(vertex) + worldPosition);
        }

        // Triangles.
        int[][] rawTriangles = meshData.Triangles;
        for (int i = 0; i < rawTriangles.Length; i++)
        {
            int[] trianglesToSet = new int[rawTriangles[i].Length];

            // Offset the triangles
            for (int w = 0; w < rawTriangles[i].Length; w++)
            {
                trianglesToSet[w] = rawTriangles[i][w] + verticeCount;
            }

            data.SetTriangles(tile.BrushMaterials[i], trianglesToSet);
        }

        // UVs.
        foreach (Vector2 uv in meshData.UVs)
        {
            data.UVs.Add(uv);
        }

        // Edges.
        if (!AxialDirection.ContainsDirection(bitmask, AxialDirection.DownLeft) || !AxialDirection.ContainsDirection(bitmask, AxialDirection.DownRight))
        {
            EdgeBuilder.Build(data, tile, worldPosition, bitmask);
        }
    }