예제 #1
0
        Renderer GenerateCellSurface(Cell cell, int cacheIndex, Material material, Vector2 textureScale, Vector2 textureOffset, float textureRotation)
        {
            Rect rect = cell.rect2D;

            Vector2[] thePoints  = cell.points;                 // this method is expensive
            int       pointCount = thePoints.Length;

            for (int k = 0; k < pointCount; k++)
            {
                hexPoints [k] = thePoints [k];
            }
            if (cell.isWrapped)
            {
                hexPoints [6] = hexPoints [2] + Misc.Vector3left;
                hexPoints [7] = hexPoints [3] + Misc.Vector3left;
                hexPoints [8] = hexPoints [4] + Misc.Vector3left;
            }
            Renderer   renderer = Drawing.CreateSurface("Cell", hexPoints, cell.isWrapped ? hexIndicesWrapped : hexIndices, material, rect, textureScale, textureOffset, textureRotation, disposalManager);
            GameObject surf     = renderer.gameObject;

            surf.transform.SetParent(gridSurfacesLayer.transform, false);
            surf.transform.localPosition = Misc.Vector3zero;
            surf.layer = gameObject.layer;
            gridSurfaces [cacheIndex] = renderer;
            return(renderer);
        }
예제 #2
0
        GameObject GenerateProvinceRegionSurface(int provinceIndex, int regionIndex, Material material, Vector2 textureScale, Vector2 textureOffset, float textureRotation)
        {
            if (provinceIndex < 0 || provinceIndex >= provinces.Length)
            {
                return(null);
            }
            if (provinces [provinceIndex].regions == null)
            {
                ReadProvincePackedString(provinces [provinceIndex]);
            }
            if (provinces [provinceIndex].regions == null || regionIndex < 0 || regionIndex >= provinces [provinceIndex].regions.Count)
            {
                return(null);
            }

            Province province = provinces [provinceIndex];
            Region   region   = province.regions [regionIndex];

            if (region.points.Length < 3)
            {
                return(null);
            }

            // Triangulate to get the polygon vertex indices
            Poly2Tri.Polygon poly = new Poly2Tri.Polygon(region.points);

            if (_enableEnclaves && regionIndex == province.mainRegionIndex)
            {
                ProvinceSubstractProvinceEnclaves(provinceIndex, region, poly);
            }

            P2T.Triangulate(poly);

            // Prepare surface cache entry and deletes older surface if exists
            int       cacheIndex    = GetCacheIndexForProvinceRegion(provinceIndex, regionIndex);
            string    cacheIndexSTR = cacheIndex.ToString();
            Transform t             = surfacesLayer.transform.Find(cacheIndexSTR);

            if (t != null)
            {
                DestroyImmediate(t.gameObject);                  // Deletes potential residual surface
            }
            // Creates surface mesh
            GameObject surf = Drawing.CreateSurface(cacheIndexSTR, poly, material, region.rect2D, textureScale, textureOffset, textureRotation, disposalManager);

            surf.transform.SetParent(surfacesLayer.transform, false);
            surf.transform.localPosition = Misc.Vector3zero;
            surf.transform.localRotation = Quaternion.Euler(Misc.Vector3zero);
            surf.layer            = gameObject.layer;
            surfaces [cacheIndex] = surf;
            return(surf);
        }
예제 #3
0
        GameObject GenerateCountryRegionSurface(int countryIndex, int regionIndex, Material material, Vector2 textureScale, Vector2 textureOffset, float textureRotation)
        {
            if (countryIndex < 0 || countryIndex >= _countries.Length)
            {
                return(null);
            }
            Country country = _countries [countryIndex];
            Region  region  = country.regions [regionIndex];

            if (region.points.Length < 3)
            {
                return(null);
            }

            Poly2Tri.Polygon poly = new Poly2Tri.Polygon(region.points);
            // Extracts enclaves from main region
            if (_enableEnclaves && regionIndex == country.mainRegionIndex)
            {
                // Remove negative provinces
                if (_showProvinces)
                {
                    CountrySubstractProvinceEnclaves(countryIndex, region, poly);
                }
                else
                {
                    CountrySubstractCountryEnclaves(countryIndex, region, poly);
                }
            }
            P2T.Triangulate(poly);

            // Prepare surface cache entry and deletes older surface if exists
            int    cacheIndex = GetCacheIndexForCountryRegion(countryIndex, regionIndex);
            string id         = cacheIndex.ToString();

            // Creates surface mesh
            GameObject surf = Drawing.CreateSurface(id, poly, material, region.rect2D, textureScale, textureOffset, textureRotation, disposalManager);

            ParentObjectToRegion(id, COUNTRY_SURFACES_ROOT_NAME, surf);
            surfaces [cacheIndex] = surf;

            return(surf);
        }
예제 #4
0
        /// <summary>
        /// Creates an extruded version of a given region
        /// </summary>
        /// <returns>The generate extrude game object.</returns>
        /// <param name="name">Name.</param>
        /// <param name="extrusionAmount">Size of the extrusion.</param>
        /// <param name="region">Region.</param>
        /// <param name="material">Material.</param>
        /// <param name="textureScale">Texture scale.</param>
        /// <param name="textureOffset">Texture offset.</param>
        /// <param name="textureRotation">Texture rotation.</param>
        public GameObject RegionGenerateExtrudeGameObject(string name, Region region, float extrusionAmount, Material topMaterial, Material sideMaterial, Vector2 textureScale, Vector2 textureOffset, float textureRotation, bool useRegionRect = true)
        {
            if (region == null || region.points.Length < 3)
            {
                return(null);
            }

            Rect       rect = useRegionRect ? region.rect2D : new Rect(0.5f, 0.5f, 1f, 1f);
            GameObject go   = new GameObject(name);

            go.transform.SetParent(transform, false);

            Poly2Tri.Polygon poly = new Poly2Tri.Polygon(region.points);
            P2T.Triangulate(poly);

            // Creates surface mesh
            GameObject surf = Drawing.CreateSurface("RegionTop", poly, topMaterial, rect, textureScale, textureOffset, textureRotation, null);

            surf.transform.SetParent(go.transform, false);
            surf.transform.localPosition = new Vector3(0, 0, -extrusionAmount);

            // Create side band
            int pointCount = region.points.Length;

            Vector3[] vertices = new Vector3[pointCount * 2];
            int[]     indices = new int[pointCount * 6];
            int       vi = 0, ii = -1;

            for (int k = 0; k < pointCount; k++, vi += 2)
            {
                vertices[vi]       = region.points[k];
                vertices[vi].z     = -extrusionAmount;
                vertices[vi + 1]   = vertices[vi];
                vertices[vi + 1].z = 0;
                if (k == pointCount - 1)
                {
                    indices[++ii] = vi + 1;
                    indices[++ii] = vi;
                    indices[++ii] = 1;
                    indices[++ii] = vi + 1;
                    indices[++ii] = 1;
                    indices[++ii] = 0;
                }
                else
                {
                    indices[++ii] = vi;
                    indices[++ii] = vi + 1;
                    indices[++ii] = vi + 2;
                    indices[++ii] = vi + 1;
                    indices[++ii] = vi + 3;
                    indices[++ii] = vi + 2;
                }
            }

            GameObject band = new GameObject("RegionBand");

            band.transform.SetParent(go.transform, false);
            Mesh mesh = new Mesh();

            mesh.vertices  = vertices;
            mesh.triangles = indices;
            mesh.RecalculateNormals();
            MeshFilter mf = band.AddComponent <MeshFilter>();

            mf.mesh = mesh;
            MeshRenderer mr = band.AddComponent <MeshRenderer>();

            mr.sharedMaterial = sideMaterial;

            if (region.entity.allowHighlight && (region.entity is Country && _enableCountryHighlight || region.entity is Province && _enableProvinceHighlight))
            {
                ExtrudedRegionInteraction interaction = go.AddComponent <ExtrudedRegionInteraction>();
                interaction.map            = this;
                interaction.region         = region;
                interaction.topMaterial    = topMaterial;
                interaction.sideMaterial   = sideMaterial;
                interaction.highlightColor = region.entity is Country ? _fillColor : _provincesFillColor;
            }
            return(go);
        }
예제 #5
0
        /// <summary>
        /// Highlights the province region specified. Returns the generated highlight surface gameObject.
        /// Internally used by the Map UI and the Editor component, but you can use it as well to temporarily mark a country region.
        /// </summary>
        /// <param name="refreshGeometry">Pass true only if you're sure you want to force refresh the geometry of the highlight (for instance, if the frontiers data has changed). If you're unsure, pass false.</param>
        public GameObject HighlightProvinceRegion(int provinceIndex, int regionIndex, bool refreshGeometry)
        {
            if (!refreshGeometry && _provinceHighlightedIndex == provinceIndex && _provinceRegionHighlightedIndex == regionIndex)
            {
                return(provinceRegionHighlightedObj);
            }
            if (provinceRegionHighlightedObj != null)
            {
                HideProvinceRegionHighlight();
            }
            if (provinceIndex < 0 || provinceIndex >= provinces.Length || provinces [provinceIndex].regions == null || regionIndex < 0 || regionIndex >= provinces [provinceIndex].regions.Count)
            {
                return(null);
            }

            if (OnProvinceHighlight != null)
            {
                bool allowHighlight = true;
                OnProvinceHighlight(provinceIndex, regionIndex, ref allowHighlight);
                if (!allowHighlight)
                {
                    return(null);
                }
            }

            int        cacheIndex = GetCacheIndexForProvinceRegion(provinceIndex, regionIndex);
            GameObject obj;
            bool       existsInCache = surfaces.TryGetValue(cacheIndex, out obj);

            if (refreshGeometry && existsInCache)
            {
                surfaces.Remove(cacheIndex);
                DestroyImmediate(obj);
                existsInCache = false;
            }

            bool doHighlight = true;

            if (_highlightMaxScreenAreaSize < 1f)
            {
                // Check screen area size
                Region region = provinces [provinceIndex].regions [regionIndex];
                doHighlight = CheckScreenAreaSizeOfRegion(region);
            }
            if (doHighlight)
            {
                if (_enableProvinceHighlight && _provinces [provinceIndex].allowHighlight && _countries [_provinces [provinceIndex].countryIndex].allowProvincesHighlight)
                {
                    if (existsInCache)
                    {
                        provinceRegionHighlightedObj = surfaces [cacheIndex];
                        if (provinceRegionHighlightedObj == null)
                        {
                            surfaces.Remove(cacheIndex);
                        }
                        else
                        {
                            if (!provinceRegionHighlightedObj.activeSelf)
                            {
                                provinceRegionHighlightedObj.SetActive(true);
                            }
                            Renderer[] rr = provinceRegionHighlightedObj.GetComponentsInChildren <Renderer> (true);
                            for (int k = 0; k < rr.Length; k++)
                            {
                                if (rr [k].sharedMaterial != hudMatProvince && rr [k].sharedMaterial != outlineMatSimple)
                                {
                                    rr [k].enabled        = true;
                                    rr [k].sharedMaterial = hudMatProvince;
                                }
                            }
                        }
                    }
                    else
                    {
                        provinceRegionHighlightedObj = GenerateProvinceRegionSurface(provinceIndex, regionIndex, hudMatProvince);
                        // Add rest of regions?
                        if (_highlightAllProvinceRegions)
                        {
                            Province province = provinces [provinceIndex];
                            for (int r = 0; r < province.regions.Count; r++)
                            {
                                if (r != regionIndex)
                                {
                                    Region otherRegion = province.regions [r];
                                    // Triangulate to get the polygon vertex indices
                                    Poly2Tri.Polygon poly = new Poly2Tri.Polygon(otherRegion.points);
                                    P2T.Triangulate(poly);
                                    GameObject otherSurf = Drawing.CreateSurface(provinceRegionHighlightedObj.name, poly, hudMatProvince, otherRegion.rect2D, Misc.Vector2zero, Misc.Vector2zero, 0, disposalManager);

                                    otherSurf.transform.SetParent(provinceRegionHighlightedObj.transform, false);
                                    otherSurf.transform.localPosition = Misc.Vector3zero;
                                    otherSurf.transform.localRotation = Quaternion.Euler(Misc.Vector3zero);
                                    otherSurf.layer = gameObject.layer;
                                }
                            }
                        }
                    }
                }
                else
                {
                    provinceRegionHighlightedObj = null;
                }
            }

            _provinceHighlighted            = provinces [provinceIndex];
            _provinceHighlightedIndex       = provinceIndex;
            _provinceRegionHighlighted      = _provinceHighlighted.regions [regionIndex];
            _provinceRegionHighlightedIndex = regionIndex;
            return(provinceRegionHighlightedObj);
        }