private void Deactivate(IslandGO islandGO)
        {
            List <Region> regions = islandGO.getRegions();

            for (int c = 0; c < regions.Count; c++)
            {
                List <Building> building = regions[c].getBuildings();
                for (int b = 0; b < building.Count; b++)
                {
                    building[b].gameObject.SetActive(false);
                }
                regions[c].gameObject.SetActive(false);
            }
            islandGO.getExportDock().SetActive(false);
            islandGO.getImportDock().SetActive(false);
            islandGO.getCoast().SetActive(false);
        }
Esempio n. 2
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 void bakeIslandMesh(IslandGO islandGOComponent)
        {
            GameObject             islandGO      = islandGOComponent.gameObject;
            List <CombineInstance> currentCiList = new List <CombineInstance>();

            foreach (Region region in islandGOComponent.getRegions())
            {
                foreach (Building b in region.getBuildings())
                {
                    CombineInstance ci       = new CombineInstance();
                    CompilationUnit cu       = b.getCU();
                    long            loc      = cu.getLoc();
                    int             modelIdx = Helperfunctions.mapLOCtoLevel(loc);

                    if (cu.implementsServiceComponent())
                    {
                        ci.mesh = SILod2Prefabs[modelIdx].GetComponent <MeshFilter>().sharedMesh;
                    }
                    else if (cu.declaresService())
                    {
                        ci.mesh = SDLod2Prefabs[modelIdx].GetComponent <MeshFilter>().sharedMesh;
                    }
                    else
                    {
                        ci.mesh = CULod2Prefabs[modelIdx].GetComponent <MeshFilter>().sharedMesh;
                    }

                    ci.subMeshIndex = 0;
                    ci.transform    = islandGO.transform.worldToLocalMatrix * b.gameObject.transform.localToWorldMatrix;
                    currentCiList.Add(ci);
                }

                CombineInstance ciCountry = new CombineInstance();
                ciCountry.mesh         = region.getRegionArea().GetComponent <MeshFilter>().sharedMesh;
                ciCountry.subMeshIndex = 0;
                ciCountry.transform    = Matrix4x4.identity;
                currentCiList.Add(ciCountry);
            }

            #region Add coast to FinalCombineList
            CombineInstance ciCoast = new CombineInstance();
            ciCoast.mesh         = islandGOComponent.getCoast().GetComponent <MeshFilter>().sharedMesh;
            ciCoast.subMeshIndex = 0;
            ciCoast.transform    = Matrix4x4.identity;
            currentCiList.Add(ciCoast);
            #endregion

            #region Add docks to FinalCombineList
            GameObject expDock = islandGOComponent.getExportDock();
            GameObject impDock = islandGOComponent.getImportDock();

            CombineInstance eDockCI = new CombineInstance();
            eDockCI.mesh         = eDockLod1Prefab.GetComponent <MeshFilter>().sharedMesh;
            eDockCI.subMeshIndex = 0;
            eDockCI.transform    = islandGOComponent.transform.worldToLocalMatrix * expDock.transform.localToWorldMatrix;

            CombineInstance iDockCI = new CombineInstance();
            iDockCI.mesh         = iDockLod1Prefab.GetComponent <MeshFilter>().sharedMesh;
            iDockCI.subMeshIndex = 0;
            iDockCI.transform    = islandGOComponent.transform.worldToLocalMatrix * impDock.transform.localToWorldMatrix;

            currentCiList.Add(eDockCI);
            currentCiList.Add(iDockCI);
            #endregion

            #region Replace Island Mesh and Materials with baked one
            MeshRenderer bakedMR = islandGO.AddComponent <MeshRenderer>();
            bakedMR.sharedMaterial = holomaterial;
            MeshFilter bakedMF = islandGO.AddComponent <MeshFilter>();
            bakedMF.mesh = new Mesh();
            bakedMF.sharedMesh.CombineMeshes(currentCiList.ToArray(), true, true);

            #endregion
        }