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");
    }
Ejemplo n.º 4
0
 protected UnityEngineHelper GetUnityEngineHelper()
 {
     return(UnityEngineHelper.GetInstance());
 }
    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");
    }
Ejemplo n.º 6
0
    void Start()
    {
        IMap map = null;

        if (mapGameObject != null)
        {
            map = UnityEngineHelper.GetInterface <IMap>(mapGameObject);
        }

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

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

        int width            = Mathf.CeilToInt(map.GetWidth() * samplingScale),
            height           = Mathf.CeilToInt(map.GetHeight() * samplingScale);
        Texture2D mapTexture = new Texture2D(width, height, TextureFormat.RGB24, true);

        Color[]          pixels = new Color[width * height];
        ColorGradient    gradient = new ColorGradient(colors);
        int              y = (invertY) ? height - 1 : 0;
        Func <int, bool> yCompare, xCompare;
        Func <int, int>  yMove, xMove;

        if (invertY)
        {
            yCompare = (a) => a >= 0;
            yMove    = (a) => a - 1;
        }
        else
        {
            yCompare = (a) => a < height;
            yMove    = (a) => a + 1;
        }
        if (invertX)
        {
            xCompare = (a) => a >= 0;
            xMove    = (a) => a - 1;
        }
        else
        {
            xCompare = (a) => a < width;
            xMove    = (a) => a + 1;
        }
        for (int p = 0; yCompare(y); y = yMove(y))
        {
            float wY = y / samplingScale + map.GetMinY();
            int   x  = (invertX) ? width - 1 : 0;
            for (; xCompare(x); x = xMove(x), p++)
            {
                float wX    = x / samplingScale + map.GetMinX();
                Color pixel = new Color();
                gradient.GetColorAtValue(map.GetNormalizedValue(wX, wY), ref pixel);
                pixels[p] = pixel;
            }
        }
        mapTexture.SetPixels(pixels);
        mapTexture.Apply(true);
        GameObject mapGO = new GameObject("Map");
        Mesh       mesh  = new Mesh();

        mesh.vertices = new Vector3[]
        {
            new Vector3(map.GetMinX(), map.GetMaxY(), z),
            new Vector3(map.GetMaxX(), map.GetMaxY(), z),
            new Vector3(map.GetMaxX(), map.GetMinY(), z),
            new Vector3(map.GetMinX(), map.GetMinY(), z)
        };
        mesh.triangles = new int[]
        {
            0, 1, 2,
            0, 2, 3
        };
        mesh.uv = new Vector2[]
        {
            new Vector2(1, 1),
            new Vector2(0, 1),
            new Vector2(0, 0),
            new Vector2(1, 0)
        };
        mesh.normals = new Vector3[]
        {
            Vector3.back,
            Vector3.back,
            Vector3.back,
            Vector3.back
        };
        mapGO.AddComponent <MeshFilter>().mesh = mesh;
        Material material = new Material(Shader.Find("Unlit/Texture"));

        material.SetTexture("_MainTex", mapTexture);
        mapGO.AddComponent <MeshRenderer>().material = material;
        mapGO.transform.parent        = transform;
        mapGO.transform.localRotation = Quaternion.identity;
    }