Ejemplo n.º 1
0
    void Start()
    {
        if (heightmapGameObject != null)
        {
            heightmap = UnityEngineHelper.GetInterface <IHeightmap>(heightmapGameObject);
        }

        if (heightmap == null)
        {
            Debug.LogError("MockAllotment needs a reference to a game object containing at least one component that implements IHeightmap");
            return;
        }

        if (!heightmap.Finished())
        {
            Debug.LogError("MockAllotment script can only execute after the components that implements IHeightmap (Configure execution order that in Edit > Project Settings > Script Execution Order)");
            return;
        }

        if (allotmentBuilderGameObject != null)
        {
            allotmentBuilders = UnityEngineHelper.GetInterfaces <IAllotmentBuilder>(allotmentBuilderGameObject);
        }
        if (allotmentBuilders == null)
        {
            Debug.LogError("MockAllotment needs a reference to a game object containing at least one component that implements IAllotmentBuilder");
            return;
        }

        Generate();
    }
Ejemplo n.º 2
0
    void Start()
    {
        if (roadNetwork == null)
        {
            Debug.LogError("RoadNetworkMesh needs a reference to a RoadNetwork");
            return;
        }

        if (!roadNetwork.Finished)
        {
            Debug.LogError("RoadNetworkMesh script can only execute after RoadNetwork (Configure execution order that in Edit > Project Settings > Script Execution Order)");
            return;
        }

        IHeightmap heightmap = null;

        if (heightmapGameObject != null)
        {
            heightmap = UnityEngineHelper.GetInterface <IHeightmap>(heightmapGameObject);
        }

        if (heightmap == null)
        {
            Debug.LogError("RoadNetworkMesh needs a reference to a game object containing at least one component that implements IHeightmap");
            return;
        }

        if (!heightmap.Finished())
        {
            Debug.LogError("RoadNetworkMesh script can only execute after the components that implements IHeightmap (Configure execution order that in Edit > Project Settings > Script Execution Order)");
            return;
        }

        var geometry = RoadNetworkGeometryBuilder.Build(
            1.0f,
            Config.highwaySegmentWidth,
            Config.streetSegmentWidth,
            lengthStep,
            roadNetwork.Segments,
            roadNetwork.Mask
            );

        GameObject     roadGO   = new GameObject("Road");
        List <Vector3> vertices = new List <Vector3>();

        geometry.GetSegmentPositions().ForEach((p) =>
        {
            vertices.Add(new Vector3(p.x, heightmap.GetHeight(p.x, p.y) + zOffset, p.y));
        });
        Mesh mesh = new Mesh();

        mesh.vertices  = vertices.ToArray();
        mesh.triangles = geometry.GetSegmentIndices().ToArray();
        mesh.uv        = geometry.GetSegmentUvs().ToArray();
        mesh.RecalculateNormals();
        GameObject segmentsGO = new GameObject("Segments");

        segmentsGO.AddComponent <MeshFilter>().mesh = mesh;
        var meshRenderer = segmentsGO.AddComponent <MeshRenderer>();

        meshRenderer.material          = roadSegmentsMaterial;
        meshRenderer.shadowCastingMode = ShadowCastingMode.Off;
        segmentsGO.transform.parent    = roadGO.transform;
        vertices = new List <Vector3>();
        geometry.GetCrossingPositions().ForEach((p) =>
        {
            vertices.Add(new Vector3(p.x, heightmap.GetHeight(p.x, p.y) + zOffset, p.y));
        });
        mesh           = new Mesh();
        mesh.vertices  = vertices.ToArray();
        mesh.triangles = geometry.GetCrossingIndices().ToArray();
        mesh.uv        = geometry.GetCrossingUvs().ToArray();
        mesh.RecalculateNormals();
        GameObject crossingsGO = new GameObject("Crossings");

        crossingsGO.AddComponent <MeshFilter>().mesh = mesh;
        meshRenderer                   = crossingsGO.AddComponent <MeshRenderer>();
        meshRenderer.material          = roadCrossingsMaterial;
        meshRenderer.shadowCastingMode = ShadowCastingMode.Off;
        crossingsGO.transform.parent   = roadGO.transform;
        roadGO.transform.parent        = transform;
    }
Ejemplo n.º 3
0
    void Start()
    {
        if (roadNetwork == null)
        {
            Debug.LogError("RandomSettlementsSpawner needs a reference to a RoadNetwork");
            return;
        }

        if (!roadNetwork.Finished)
        {
            Debug.LogError("RandomSettlementsSpawner script can only execute after RoadNetwork (Configure execution order that in Edit > Project Settings > Script Execution Order)");
            return;
        }

        if (heightmapGameObject != null)
        {
            heightmap = UnityEngineHelper.GetInterface <IHeightmap>(heightmapGameObject);
        }

        if (heightmap == null)
        {
            Debug.LogError("RandomSettlementsSpawner needs a reference to a game object containing at least one component that implements IHeightmap");
            return;
        }

        if (!heightmap.Finished())
        {
            Debug.LogError("RandomSettlementsSpawner script can only execute after the components that implements IHeightmap (Configure execution order that in Edit > Project Settings > Script Execution Order)");
            return;
        }

        List <IAllotmentBuilder> allotmentBuilders = null;

        if (allotmentBuilderGameObject != null)
        {
            allotmentBuilders = UnityEngineHelper.GetInterfaces <IAllotmentBuilder>(allotmentBuilderGameObject);
        }
        if (allotmentBuilders == null)
        {
            Debug.LogError("RoadDensitySettlementSpawner needs a reference to a game object containing at least one component that implements IAllotmentBuilder");
            return;
        }

        Context           context = new Context(roadNetwork.Quadtree);
        HashSet <Segment> visited = new HashSet <Segment>();

        foreach (var segment in roadNetwork.Segments)
        {
            RoadNetworkTraversal.PreOrder(segment, ref context, -1, SegmentVisitor, roadNetwork.Mask, ref visited);
        }
        GameObject allotmentsGO = new GameObject("Allotments");

        allotmentsGO.transform.parent = transform;
        foreach (var allotment in context.allotments)
        {
            foreach (var allotmentBuilder in allotmentBuilders)
            {
                GameObject allotmentGO = allotmentBuilder.Build(allotment, heightmap);
                allotmentGO.transform.parent = allotmentsGO.transform;
            }
        }
        Debug.Log(context.allotments.Count + " allotments spawned");
    }
    void Start()
    {
        if (roadNetwork == null)
        {
            Debug.LogError("RoadDensityBasedSettlementSpawner needs a reference to a RoadNetwork");
            return;
        }

        if (!roadNetwork.Finished)
        {
            Debug.LogError("RoadDensityBasedSettlementSpawner script can only execute after RoadNetwork (Configure execution order that in Edit > Project Settings > Script Execution Order)");
            return;
        }

        if (roadDensityMap == null)
        {
            Debug.LogError("RoadDensityBasedSettlementSpawner needs a reference to a RoadDensityMap");
            return;
        }

        if (!roadDensityMap.Finished())
        {
            Debug.LogError("RoadDensityBasedSettlementSpawner script can only execute after RoadDensityMap (Configure execution order that in Edit > Project Settings > Script Execution Order)");
            return;
        }

        if (heightmapGameObject != null)
        {
            heightmap = UnityEngineHelper.GetInterface <IHeightmap>(heightmapGameObject);
        }

        if (heightmap == null)
        {
            Debug.LogError("RoadDensityBasedSettlementSpawner needs a reference to a game object containing at least one component that implements IHeightmap");
            return;
        }

        if (!heightmap.Finished())
        {
            Debug.LogError("RoadDensityBasedSettlementSpawner script can only execute after the components that implements IHeightmap (Configure execution order that in Edit > Project Settings > Script Execution Order)");
            return;
        }

        List <IAllotmentBuilder> allotmentBuilders = null;

        if (allotmentBuilderGameObject != null)
        {
            allotmentBuilders = UnityEngineHelper.GetInterfaces <IAllotmentBuilder>(allotmentBuilderGameObject);
        }
        if (allotmentBuilders == null)
        {
            Debug.LogError("RoadDensityBasedSettlementSpawner needs a reference to a game object containing at least one component that implements IAllotmentBuilder");
            return;
        }

        {
            float minThreshold = -float.MaxValue;
            foreach (var densityTier in densityTiers)
            {
                if (densityTier.threshold < minThreshold)
                {
                    Debug.LogError("Density tier has a threshold smaller than it's predecessor");
                    return;
                }
                minThreshold = densityTier.threshold;
            }
        }

        minAllotmentWidth = Allotment.GetWidth(Config.allotmentMinHalfDiagonal, Config.allotmentMinAspect);

        allotments = new List <Tuple <float, Allotment> >();
        HashSet <Segment> visited = new HashSet <Segment>();

        foreach (var segment in roadNetwork.Segments)
        {
            RoadNetworkTraversal.PreOrder(segment, SegmentVisitor, roadNetwork.Mask, ref visited);
        }
        if (maxNumAllotments > 0 && allotments.Count > maxNumAllotments)
        {
            allotments.Sort((a, b) => b.Item1.CompareTo(a.Item1));
            allotments = allotments.GetRange(0, maxNumAllotments);
        }
        GameObject allotmentsGO = new GameObject("Allotments");

        allotmentsGO.transform.parent = transform;
        {
            foreach (var allotment in allotments)
            {
                foreach (var allotmentBuilder in allotmentBuilders)
                {
                    GameObject allotmentGO = allotmentBuilder.Build(allotment.Item2, heightmap);
                    allotmentGO.transform.parent = allotmentsGO.transform;
                }
            }
        }
        Debug.Log(allotments.Count + " allotments spawned");
    }