Exemple #1
0
        private IslandGO constructIslandGO(CartographicIsland island, Vector3 pos)
        {
            int rngSeed = island.getName().GetHashCode() + 200;

            RNG = new System.Random(rngSeed);
            GameObject islandGO          = new GameObject(island.getName());
            IslandGO   islandGOComponent = islandGO.AddComponent <IslandGO>();

            islandGOComponent.setIslandStructure(island);
            island.setIslandGO(islandGO);

            #region create countries
            List <List <TnetMesh> > tmeshList   = island.getPackageMeshes();
            List <List <VFace> >    islandCells = island.getPackageCells();
            List <Package>          packageList = island.getPackages();

            float maximumBuildingBoundSize = 0;
            int   counter = 0;
            foreach (List <TnetMesh> tmesh in tmeshList)
            {
                Package pckg = packageList[counter];

                GameObject region = new GameObject(pckg.getName());

                Region regionComponent = region.AddComponent <Region>();
                regionComponent.setParentIsland(islandGOComponent);
                region.transform.SetParent(islandGO.transform);
                islandGOComponent.addRegion(regionComponent);

                #region RegionArea
                GameObject regionArea = new GameObject("Region area");
                regionArea.transform.SetParent(region.transform);
                MeshFilter   mFilter = regionArea.AddComponent <MeshFilter>();
                MeshRenderer mRender = regionArea.AddComponent <MeshRenderer>();
                mRender.sharedMaterial = combinedHoloMaterial;

                regionComponent.setRegionArea(regionArea);
                regionComponent.setPackage(pckg);
                #endregion

                List <VFace>      packageCells      = islandCells[counter];
                CombineInstance[] combineCellMeshes = new CombineInstance[tmesh.Count];
                int cc = 0;
                #region Combine package cell meshes
                foreach (TnetMesh tm in tmesh)
                {
                    Mesh m = Helperfunctions.convertTriangleNETMesh(tm);
                    combineCellMeshes[cc].mesh = m;
                    cc++;
                }
                mFilter.mesh = new Mesh();
                mFilter.mesh.CombineMeshes(combineCellMeshes, true, false);

                float rndU = (float)RNG.NextDouble();
                float rndV = (float)RNG.NextDouble() * 0.4f;

                Vector2 rndUV = new Vector2(rndU, rndV);
                setUVsToSingularCoord(rndUV, mFilter);
                #endregion

                cc = 0;
                #region Create CUs
                foreach (CompilationUnit cu in pckg.getCompilationUnits())
                {
                    float x           = (float)packageCells[cc].generator.X;
                    float y           = (float)packageCells[cc].generator.Z;
                    float z           = (float)packageCells[cc].generator.Y;
                    int   heightLevel = Helperfunctions.mapLOCtoLevel(cu.getLoc());

                    GameObject building;
                    if (cu.implementsServiceComponent())
                    {
                        building = GameObject.Instantiate(SIPrefabs[heightLevel], region.transform);
                        building.AddComponent <ServiceLayerGO>();
                    }
                    else if (cu.declaresService())
                    {
                        building = GameObject.Instantiate(SDPrefabs[heightLevel], region.transform);
                        building.AddComponent <ServiceLayerGO>();
                    }
                    else
                    {
                        building = GameObject.Instantiate(CUPrefabs[heightLevel], region.transform);
                    }

                    building.name = cu.getName();
                    Vector3 randomRotation = new Vector3(0f, UnityEngine.Random.Range(-180, 180), 0f);
                    building.transform.localEulerAngles = randomRotation;
                    Building buildingComponent = building.AddComponent <Building>();
                    buildingComponent.setCU(cu);
                    cu.setGameObject(building);
                    building.transform.position   = new Vector3(x, y, z);
                    building.transform.localScale = new Vector3(GlobalVar.cuScale, GlobalVar.cuScale, GlobalVar.cuScale);
                    regionComponent.addBuilding(buildingComponent);
                    //////////////////////////
                    #region BuildingCollider
                    building.layer = LayerMask.NameToLayer("InteractionSystemLayer");
                    CapsuleCollider capsuleCol = building.AddComponent <CapsuleCollider>();
                    capsuleCol.isTrigger = true;
                    #endregion
                    float currentBuildingExtent = capsuleCol.bounds.size.magnitude;
                    if (currentBuildingExtent > maximumBuildingBoundSize)
                    {
                        maximumBuildingBoundSize = currentBuildingExtent;
                    }
                    //////////////////////////
                    cc++;
                }
                #endregion

                counter++;
            }


            #endregion

            #region Combine CU meshes

            /*
             * GameObject combinedCUs = new GameObject("Combined CUs");
             * MeshFilter mFilterCU = combinedCUs.AddComponent<MeshFilter>();
             * MeshRenderer mRenderCU = combinedCUs.AddComponent<MeshRenderer>();
             * mRenderCU.material = defaultMaterial;
             * mFilterCU.mesh = new Mesh();
             * mFilterCU.mesh.CombineMeshes(combineCuMeshes, true, true);
             * combinedCUs.transform.SetParent(islandGO.transform);
             */
            #endregion

            #region create coastline
            GameObject coastline = new GameObject("Coastline");
            islandGOComponent.setCoast(coastline);
            coastline.transform.SetParent(islandGO.transform);
            MeshFilter   coastMFilter = coastline.AddComponent <MeshFilter>();
            MeshRenderer coastMRender = coastline.AddComponent <MeshRenderer>();
            coastMRender.sharedMaterial = combinedHoloMaterial;
            List <TnetMesh>   tmeshCoastList       = island.getCoastlineMeshes();
            CombineInstance[] combineCoastInstance = new CombineInstance[tmeshCoastList.Count];
            counter = 0;
            foreach (TnetMesh tmesh in tmeshCoastList)
            {
                Mesh m = Helperfunctions.convertTriangleNETMesh(tmesh);
                combineCoastInstance[counter].mesh = m;
                counter++;
            }
            coastMFilter.mesh = new Mesh();
            coastMFilter.mesh.CombineMeshes(combineCoastInstance, true, false);

            setUVsToSingularCoord(new Vector2(0f, 0.7f), coastMFilter);

            #endregion

            #region init docks

            //get graph vertex associated with the island
            GraphVertex vert = island.getDependencyVertex();
            if (vert != null)
            {
                //Relative dock position
                Vector3 dockDirection = new Vector3(UnityEngine.Random.value, 0, UnityEngine.Random.value);
                dockDirection.Normalize();
                dockDirection *= island.getRadius();

                //Import Dock
                Vector3 dockPosition = island.getWeightedCenter() + dockDirection;
                dockPosition.y -= Mathf.Abs(GlobalVar.islandHeightProfile[GlobalVar.islandHeightProfile.Length - 1]) * GlobalVar.islandAboveOcean;
                GameObject importD = Instantiate(importDockPrefab, dockPosition, Quaternion.identity);
                importD.layer = LayerMask.NameToLayer("InteractionSystemLayer");
                importD.name  = island.getName() + " import dock";
                importD.transform.localScale = new Vector3(1, 1, 1);
                importD.transform.SetParent(islandGO.transform);
                islandGOComponent.setImportDock(importD);
                //setUVsToSingularCoord(new Vector2(0.7f, 0.1f), importD.GetComponent<MeshFilter>());

                //Export Dock
                GameObject exportD = Instantiate(exportDockPrefab, dockPosition, Quaternion.identity);
                exportD.layer = LayerMask.NameToLayer("InteractionSystemLayer");
                exportD.name  = island.getName() + " export dock";
                exportD.transform.localScale = new Vector3(1, 1, 1);
                exportD.transform.SetParent(islandGO.transform);
                islandGOComponent.setExportDock(exportD);
                //setUVsToSingularCoord(new Vector2(0.1f, 0.1f), exportD.GetComponent<MeshFilter>());
            }
            #endregion

            islandGO.transform.position = pos;
            islandGO.transform.SetParent(VisualizationContainer.transform);

            #region rise Islands above ocean
            float   newIslandHeight = Mathf.Abs(GlobalVar.islandHeightProfile[GlobalVar.islandHeightProfile.Length - 1]) * GlobalVar.islandAboveOcean;
            Vector3 newIslandPos    = islandGO.transform.localPosition;
            newIslandPos.y = newIslandHeight;
            islandGO.transform.localPosition = newIslandPos;
            #endregion

            #region Create colliders

            #region CountryCollider
            List <Region> regions = islandGOComponent.getRegions();
            foreach (Region region in regions)
            {
                GameObject countryGO = region.gameObject;
                countryGO.layer = LayerMask.NameToLayer("InteractionSystemLayer");
                MeshCollider cColliderCountry = countryGO.AddComponent <MeshCollider>();
                MeshFilter   mFilter          = region.getRegionArea().GetComponent <MeshFilter>();

                cColliderCountry.sharedMesh = mFilter.sharedMesh;
                cColliderCountry.convex     = true;
                cColliderCountry.isTrigger  = true;
            }
            #endregion

            #region IslandCollider
            islandGO.layer = LayerMask.NameToLayer("InteractionSystemLayer");
            CapsuleCollider cColliderIsland = islandGO.AddComponent <CapsuleCollider>();
            float           b = island.getRadius();
            cColliderIsland.radius = b;
            float newColliderHeight = islandGOComponent.getCoast().GetComponent <MeshFilter>().sharedMesh.bounds.size.y;
            cColliderIsland.height = newColliderHeight;
            Vector3 newCenter = island.getWeightedCenter();
            newCenter.y               = -islandGOComponent.getCoast().GetComponent <MeshFilter>().sharedMesh.bounds.size.y + (newColliderHeight * 0.5f);
            cColliderIsland.center    = newCenter;
            cColliderIsland.isTrigger = true;
            #endregion

            #endregion

            return(islandGOComponent);
        }
        private CartographicIsland constructIslandFromBundle(Bundle b)
        {
            int rngSeed = b.getName().GetHashCode() + 200;

            RNG = new System.Random(rngSeed);

            #region VoronoiPlane
            TriangleNet.Configuration conf     = new TriangleNet.Configuration();
            List <Vertex>             vertices = Helperfunctions.createPointsOnPlane(GlobalVar.voronoiCellScalefactor, GlobalVar.voronoiCellScalefactor, 50, 50, 1.0f, RNG);
            BoundedVoronoi            voronoi  = Helperfunctions.createRelaxedVoronoi(vertices, 1);
            #endregion

            #region initFirstCell
            VFace firstCell = Helperfunctions.closestCell(0, 0, voronoi);
            Dictionary <int, VFace> startingCandidates = new Dictionary <int, VFace>();
            startingCandidates.Add(firstCell.ID, firstCell);
            #endregion

            List <Package>     packages = b.getPackages();
            CartographicIsland island   = new CartographicIsland(b);
            island.setVoronoi(voronoi);
            #region sort package list
            packages.Sort((x, y) => x.getCompilationUnits().Count.CompareTo(y.getCompilationUnits().Count));
            packages.Reverse();
            #endregion
            //Compute maximal compilation unit count in bundle
            float maxCUCountInIsland = 0;
            foreach (Package package in packages)
            {
                long cuCount = package.getCuCount();
                if (cuCount > maxCUCountInIsland)
                {
                    maxCUCountInIsland = cuCount;
                }
            }
            #region construct regions
            foreach (Package package in packages)
            {
                float cohesionMult = (float)package.getCuCount() / maxCUCountInIsland;
                cohesionMult *= maxCohesion;
                cohesionMult  = Mathf.Max(minCohesion, cohesionMult);
                Dictionary <int, VFace> newCandidates = constructRegionFromPackage(package, island, startingCandidates, cohesionMult);
                updateAndFuseCandidates(startingCandidates, newCandidates);
            }
            #endregion


            #region Shape island coast
            //Advance startingCandidates X cells outwards and ajdust the height of all vertices
            shapeCoastArea(startingCandidates, GlobalVar.islandHeightProfile);
            #endregion

            #region WeightedCenter & set coast
            List <VFace> coastlineList  = new List <VFace>();
            Vector3      weightedCenter = Vector3.zero;
            foreach (KeyValuePair <int, VFace> kvp in startingCandidates)
            {
                coastlineList.Add(kvp.Value);
                float   x       = (float)kvp.Value.generator.X;
                float   z       = (float)kvp.Value.generator.Y;
                Vector3 tilePos = new Vector3(x, 0, z);
                weightedCenter += tilePos;
            }
            weightedCenter /= startingCandidates.Count;
            island.setWeightedCenter(weightedCenter);
            island.setCoastlineCells(coastlineList);
            #endregion

            #region conservative Radius
            List <float> radii = new List <float>();
            foreach (KeyValuePair <int, VFace> kvp in startingCandidates)
            {
                float x      = (float)kvp.Value.generator.X - island.getWeightedCenter().x;
                float z      = (float)kvp.Value.generator.Y - island.getWeightedCenter().z;
                float radius = Mathf.Sqrt(x * x + z * z);
                radii.Add(radius);
            }
            float maxRadius = Helperfunctions.computeMax(radii);
            island.setRadius(maxRadius);
            #endregion

            #region TnetMeshesConstruction
            foreach (List <VFace> cellMap in island.getPackageCells())
            {
                island.addPackageMesh(constructTnetMeshFromCellmap(cellMap));
            }

            island.setCoastlineMesh(constructTnetMeshFromCellmap(coastlineList));
            #endregion

            #region link dependency vertex

            //Find graph vertex associated with the island
            BidirectionalGraph <GraphVertex, GraphEdge> depGraph = b.getParentProject().getDependencyGraph();
            List <GraphVertex> allVertices = depGraph.Vertices.ToList();
            GraphVertex        vert        = allVertices.Find(v => string.Equals(v.getName(), b.getName()));
            if (vert != null)
            {
                //Link GraphVertex-Island
                vert.setIsland(island);
                island.setDependencyVertex(vert);
            }

            #endregion

            return(island);
        }