public Material MaterialForSurface(MapSurface surface) { if (surface is Terrain) { switch (((Terrain)surface).terrainType) { case TerrainType.Landscape: switch (surface.elevation) { case 0: return(continental1Material); case 1: return(continental2Material); default: return(continental3Material); } case TerrainType.Agrarian: switch (((Terrain)surface).type) { case 0: return(field1Material); case 1: return(field2Material); default: return(field3Material); } } } return(continental3Material); }
public bool AreSurfacesNear(MapSurface surf1, MapSurface surf2) { return(surf1.borderRect.Contains(new Vector2(surf2.borderRect.xMin, surf2.borderRect.yMin)) || surf1.borderRect.Contains(new Vector2(surf2.borderRect.xMin, surf2.borderRect.yMax)) || surf1.borderRect.Contains(new Vector2(surf2.borderRect.xMax, surf2.borderRect.yMin)) || surf1.borderRect.Contains(new Vector2(surf2.borderRect.xMax, surf2.borderRect.yMax))); }
GameObject CreateAreaSurface(MapSurface surface, Material material, GameObject parentObject) { if (!surface.IsValid()) { return(new GameObject()); } int[] surfaceIndices = new Triangulator(surface.border).TriangulateOriented(); Vector3[] border3 = new Vector3[surface.border.Count]; for (int i1 = 0; i1 < surface.border.Count; i1++) { border3[i1] = surface.border[i1]; } // Make texture coordinates not depending of area position float chunkSize = 0.01f; float terrainKoef = terrainLayouter.TextureScaleForSurface(surface); float textureSizeKoef = chunkSize * terrainKoef / surface.borderRect.height; Vector2 textureScale = new Vector2(surface.borderRect.height / surface.borderRect.width * textureSizeKoef, textureSizeKoef); Vector2 surfaceOffset = new Vector2(surface.borderRect.xMin - chunkSize * (int)(surface.borderRect.xMin / chunkSize), surface.borderRect.yMin - chunkSize * (int)(surface.borderRect.yMin / chunkSize)); Vector2 textureOffset = new Vector2(surface.borderRect.xMin, surface.borderRect.yMin); //Create Surface GameObject surfaceObject = new GameObject(name, typeof(MeshRenderer), typeof(MeshFilter)); surfaceObject.hideFlags = HideFlags.DontSave | HideFlags.HideInHierarchy; Mesh mesh = new Mesh(); mesh.hideFlags = HideFlags.DontSave; mesh.vertices = border3; mesh.triangles = surfaceIndices; // uv mapping if (material.mainTexture != null) { Vector2[] uv = new Vector2[surface.border.Count]; for (int k = 0; k < uv.Length; k++) { Vector2 coor = surface.border[k]; coor.x /= textureScale.x; coor.y /= textureScale.y; coor += textureOffset; Vector2 normCoor = new Vector2((coor.x - surface.borderRect.xMin) / surface.borderRect.width, (coor.y - surface.borderRect.yMin) / surface.borderRect.height); uv[k] = normCoor; } mesh.uv = uv; } mesh.RecalculateNormals(); mesh.RecalculateBounds(); MeshUtility.Optimize(mesh); MeshFilter meshFilter = surfaceObject.GetComponent <MeshFilter>(); meshFilter.mesh = mesh; surfaceObject.GetComponent <Renderer>().sharedMaterial = material; surfaceObject.transform.SetParent(parentObject.transform, false); surfaceObject.transform.localPosition = new Vector3(0, 0, -surface.elevation * 0.001f); surfaceObject.transform.localRotation = Quaternion.Euler(Vector3.zero); surfaceObject.layer = gameObject.layer; return(surfaceObject); }
GameObject CreateAreaBorder(MapSurface surface, Material material) { if (!surface.IsValid()) { return(new GameObject()); } List <Vector3> frontiersPoints = new List <Vector3>(); int pointsCount = surface.border.Count; for (int i1 = 0; i1 < pointsCount - 1; i1++) { Vector3 p0 = surface.border[i1]; Vector3 p1 = surface.border[i1 + 1]; double v = (p0.x + p1.x) + MapPrecision * (p0.y + p1.y); frontiersPoints.Add(p0); frontiersPoints.Add(p1); } frontiersPoints.Add(surface.border[pointsCount - 1]); frontiersPoints.Add(surface.border[0]); int[] provincesIndices = new int[frontiersPoints.Count]; Vector3[] provincesBorders = new Vector3[frontiersPoints.Count]; for (int j = 0; j < frontiersPoints.Count; j++) { provincesBorders[j] = frontiersPoints[j]; provincesIndices[j] = j; } GameObject borderSurface = new GameObject("borderSurface"); borderSurface.hideFlags = HideFlags.DontSave | HideFlags.HideInHierarchy; borderSurface.transform.SetParent(areaBackgroundObject.transform, false); borderSurface.transform.localPosition = new Vector3(0, 0, -0.05f); borderSurface.transform.localRotation = Quaternion.Euler(Vector3.zero); borderSurface.layer = areaBackgroundObject.layer; Mesh mesh = new Mesh(); mesh.vertices = provincesBorders; mesh.SetIndices(provincesIndices, MeshTopology.Lines, 0); mesh.RecalculateBounds(); mesh.hideFlags = HideFlags.DontSave; MeshFilter mf = borderSurface.AddComponent <MeshFilter>(); mf.sharedMesh = mesh; MeshRenderer mr = borderSurface.AddComponent <MeshRenderer>(); mr.receiveShadows = false; mr.reflectionProbeUsage = UnityEngine.Rendering.ReflectionProbeUsage.Off; mr.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off; mr.sharedMaterial = material; return(borderSurface); }
public float TextureScaleForSurface(MapSurface surface) { if (surface is Terrain) { switch (((Terrain)surface).terrainType) { case TerrainType.Agrarian: return(0.3f); default: return(1.0f); } } return(1); }
public bool IsPointInsideSurface(Vector2 point, MapSurface polygon) { int polygonLength = polygon.border.Count, i = 0; bool inside = false; float pointX = point.x, pointY = point.y; float startX, startY, endX, endY; Vector2 endPoint = polygon.border[polygonLength - 1]; endX = endPoint.x; endY = endPoint.y; while (i < polygonLength) { startX = endX; startY = endY; endPoint = polygon.border[i++]; endX = endPoint.x; endY = endPoint.y; inside ^= (endY > pointY ^ startY > pointY) /* ? pointY inside [startY;endY] segment ? */ && /* if so, test if it is under the segment */ ((pointX - endX) < (pointY - endY) * (startX - endX) / (startY - endY)); } return(inside); }
public void IntersectSurface(MapSurface surface) { IntersectBorder(surface.border); }
public void ClipSurface(MapSurface surface) { ClipBorder(surface.border); }