Пример #1
0
    public void Render(BuildingsRender buildingsRender)
    {
        GameObject prefab;
        if (!buildingsRender.PrefabMap.TryGetValue("pillar", out prefab))
        {
            Debug.Log("Cannot find pillar prefab");
            return;
        }
        
        var meshFilter = gameObject.AddComponent<MeshFilter>();
        var meshRenderer = gameObject.AddComponent<MeshRenderer>();
        var meshCollider = gameObject.AddComponent<MeshCollider>();

        Material material;
        if (buildingsRender.MaterialMap.TryGetValue(Building.Type, out material))
        {
            meshRenderer.material = material;
        }
        
        vertices = new List<Vector3>();
        triangles = new List<int>();

        highest = Building.GetHighestPoint(Map.TerrainData) + 0.2f;
        lowest = Building.GetLowestPoint(Map.TerrainData) - 0.2f;

        var buildingWall = new PolygonSoup();

        foreach (var position in Building.Positions)
        {
            var tile = Map.TerrainData.GetTile(position.x, position.y);

            RenderFloor(tile, highest, false);

            var surrounded = true;
            for (var i = 0; i < 3; i++)
            {
                var edgePosition = tile.GetEdgePosition(i);
                if (!Building.HasPosition(edgePosition))
                {
                    var edgePoints = tile.GetEdgeCorners(i);
                    buildingWall.AddPair(edgePoints);

                    surrounded = false;
                    RenderDownwall(tile, highest, highest - 1.0f, i);
                }
            }
            
            RenderFloor(tile, highest - 1.0f, true);

            if (!surrounded)
            {
                RenderPillar(prefab, tile, highest, false);
                RenderPillar(prefab, tile, highest - 1.0f, true, highest - lowest);
            }
        }

        var offsetPoints = buildingWall.Offset(-0.5f, highest);
        RenderPoints(offsetPoints, false);
        offsetPoints = buildingWall.Offset(-1.0f, highest);
        RenderPoints(offsetPoints, true);

        var mesh = new Mesh();
        mesh.vertices = vertices.ToArray();
        mesh.triangles = triangles.ToArray();
        mesh.Optimize();
        mesh.RecalculateNormals();
        
        meshFilter.mesh = mesh;
        meshCollider.sharedMesh = mesh;

        vertices = null;
        triangles = null;
    }
Пример #2
0
    public void Render(BuildingsRender buildingsRender)
    {
        GameObject prefab;

        if (!buildingsRender.PrefabMap.TryGetValue("pillar", out prefab))
        {
            Debug.Log("Cannot find pillar prefab");
            return;
        }

        var meshFilter   = gameObject.AddComponent <MeshFilter>();
        var meshRenderer = gameObject.AddComponent <MeshRenderer>();
        var meshCollider = gameObject.AddComponent <MeshCollider>();

        Material material;

        if (buildingsRender.MaterialMap.TryGetValue(Building.Type, out material))
        {
            meshRenderer.material = material;
        }

        vertices  = new List <Vector3>();
        triangles = new List <int>();

        highest = Building.GetHighestPoint(Map.TerrainData) + 0.2f;
        lowest  = Building.GetLowestPoint(Map.TerrainData) - 0.2f;

        var buildingWall = new PolygonSoup();

        foreach (var position in Building.Positions)
        {
            var tile = Map.TerrainData.GetTile(position.x, position.y);

            RenderFloor(tile, highest, false);

            var surrounded = true;
            for (var i = 0; i < 3; i++)
            {
                var edgePosition = tile.GetEdgePosition(i);
                if (!Building.HasPosition(edgePosition))
                {
                    var edgePoints = tile.GetEdgeCorners(i);
                    buildingWall.AddPair(edgePoints);

                    surrounded = false;
                    RenderDownwall(tile, highest, highest - 1.0f, i);
                }
            }

            RenderFloor(tile, highest - 1.0f, true);

            if (!surrounded)
            {
                RenderPillar(prefab, tile, highest, false);
                RenderPillar(prefab, tile, highest - 1.0f, true, highest - lowest);
            }
        }

        var offsetPoints = buildingWall.Offset(-0.5f, highest);

        RenderPoints(offsetPoints, false);
        offsetPoints = buildingWall.Offset(-1.0f, highest);
        RenderPoints(offsetPoints, true);

        var mesh = new Mesh();

        mesh.vertices  = vertices.ToArray();
        mesh.triangles = triangles.ToArray();
        mesh.Optimize();
        mesh.RecalculateNormals();

        meshFilter.mesh         = mesh;
        meshCollider.sharedMesh = mesh;

        vertices  = null;
        triangles = null;
    }