void Update() { if (Run1) { Run1 = false; if (Print) { Debug.Log("[originalpos][Lon][Lat][Rad] : " + mark.transform.position + " " + CoordinatesProjector.CartesianToLon(mark.transform.position) + " " + CoordinatesProjector.CartesianToLat(mark.transform.position) + " " + CoordinatesProjector.CartesianToRadius(mark.transform.position)); } } if (Run2) { Run2 = false; Vector3 pos = CoordinatesProjector.InverseMercatorProjector( LnLat.x * Mathf.Deg2Rad, LnLat.y * Mathf.Deg2Rad, Rad); mark.transform.position = pos; if (Print) { Debug.Log("[pos] : " + pos); } } if (Flush) { Debug.Log("+---------------------+"); } }
Vector3 FindCloudPosition(Texture2D cloudmap) { int it = 0; bool isInMap = false; Vector2 texPos = Vector2.zero; while (!isInMap) { texPos = new Vector2( Random.Range(0, cloudmap.width), Random.Range(0, cloudmap.height)); if (cloudmap.GetPixel((int)texPos.x, (int)texPos.y) == Color.white) { isInMap = true; } it++; if (it > 50) { isInMap = true; } } return(CoordinatesProjector.InverseMercatorProjector( (cloudmap.width / texPos.x) * 360f * Mathf.Deg2Rad, (cloudmap.height / texPos.y) * 180f * Mathf.Deg2Rad, Size)); }
void CreateMesh() { float lnStep = datasSelf.Longitude / datasSelf.VerticesXAmount; float latStep = datasSelf.Latitude / datasSelf.VerticesYAmount; vertices = new Vector3[(datasSelf.VerticesXAmount + 1) * (datasSelf.VerticesYAmount + 1)]; uvs = new Vector2[vertices.Length]; for (int x = 0, i = 0; x <= datasSelf.VerticesXAmount; x++) { for (int y = 0; y <= datasSelf.VerticesYAmount; y++, i++) { vertices[i] = CoordinatesProjector.InverseMercatorProjector( (datasSelf.LongitudeBegin + (x * lnStep)) * Mathf.Deg2Rad, (datasSelf.LatitudeBegin + (y * latStep)) * Mathf.Deg2Rad, datasSelf.Radius); uvs[i] = new Vector2( (float)x / (float)datasSelf.VerticesXAmount, (float)y / (float)datasSelf.VerticesYAmount); } } filter.sharedMesh.Clear(); filter.sharedMesh.vertices = vertices; filter.sharedMesh.uv = uvs; filter.sharedMesh.triangles = CalculateTriangles(); }
/// <summary> /// Create the face's mesh. /// MUST be called after a InitTerrainFace. /// </summary> public void ConstructMesh() { // TODO put this in planettest.cs // we make sure resolution becomes pair // so a 3x3 stays a 3x3 visually but become a 4x4 if (resolution % 2 != 0) { resolution += 1; } Vector3[] vertices = new Vector3[resolution * resolution]; Vector2[] uvs = new Vector2[vertices.Length]; int[] triangles = new int[(resolution - 1) * (resolution) * 6]; int triIndex = 0; float ln = 0f; float lat = 0f; float lnOffset = 360f / (float)((resolution - 1) * 4); float latOffset = 180f / (float)((resolution - 1) * 4); for (int y = 0; y < resolution; y++) { for (int x = 0; x < resolution; x++) { int i = x + y * resolution; Vector2 percent = new Vector2(x, y) / (resolution - 1); Vector3 pointOnUnitCube = localUp + (percent.x - .5f) * 2 * axisA + (percent.y - .5f) * 2 * axisB; Vector3 pointOnUnitSphere = pointOnUnitCube.normalized; ln = CoordinatesProjector.CartesianToLon(pointOnUnitSphere); lat = CoordinatesProjector.CartesianToLat(pointOnUnitSphere); vertices[i] = pointOnUnitSphere; uvs[i] = percent; if (x != resolution - 1 && y != resolution - 1) { triangles[triIndex] = i; triangles[triIndex + 1] = i + resolution + 1; triangles[triIndex + 2] = i + resolution; triangles[triIndex + 3] = i; triangles[triIndex + 4] = i + 1; triangles[triIndex + 5] = i + resolution + 1; triIndex += 6; } } } mesh.Clear(); mesh.vertices = vertices; mesh.uv = uvs; mesh.triangles = triangles; mesh.RecalculateNormals(); }
public void Generate(NodeMeshData data) { datas = data; trans = transform; var Filter = gameObject.AddComponent <MeshFilter>(); Filter.mesh = new Mesh(); rend = gameObject.AddComponent <MeshRenderer>(); var vertices = new Vector3[((int)datas.MeshSize.x + 1) * ((int)datas.MeshSize.y + 1)]; var triangles = new int[(int)datas.MeshSize.x * (int)datas.MeshSize.y * 6]; var uv = new Vector2[vertices.Length]; var normal = new Vector3[vertices.Length]; float longitude; float latitude; for (int i = 0, y = 0; y <= (int)datas.MeshSize.y; y++) { for (int x = 0; x <= (int)datas.MeshSize.x; x++, i++) { if (datas.level == 0) { longitude = CoordinatesProjector.GetLongitude(x, (int)data.MeshSize.x) * (Mathf.PI / 180.0f); latitude = CoordinatesProjector.GetLatitude(y, (int)data.MeshSize.y) * (Mathf.PI / 180.0f); } longitude = CoordinatesProjector.GetLongitudeFromPositions(x, (int)datas.MeshSize.x, datas.BeginCoordinates.x, datas.LengthCoordinates.x) * (Mathf.PI / 180.0f); latitude = CoordinatesProjector.GetLatitudeFromPositions(y, (int)datas.MeshSize.y, datas.BeginCoordinates.y, datas.LengthCoordinates.y) * (Mathf.PI / 180.0f); vertices[i] = CoordinatesProjector.InverseMercatorProjector(longitude, latitude, 1f);//new Vector3(x,0, y); uv[i] = new Vector2((float)x / (int)datas.MeshSize.x, (float)y / (int)datas.MeshSize.y); normal[i] = vertices[i] - transform.position; } } Filter.mesh.vertices = vertices; Filter.mesh.uv = uv; Filter.mesh.normals = normal; for (int ti = 0, vi = 0, y = 0; y < (int)datas.MeshSize.y; y++, vi++) { for (int x = 0; x < (int)datas.MeshSize.x; x++, ti += 6, vi++) { triangles[ti] = vi; triangles[ti + 3] = triangles[ti + 2] = vi + 1; triangles[ti + 4] = triangles[ti + 1] = vi + (int)datas.MeshSize.x + 1; triangles[ti + 5] = vi + (int)datas.MeshSize.x + 2; } } Filter.mesh.triangles = triangles; MeshCenter = vertices[vertices.Length / 2]; }
Vector3 GetSpherifiedPositionFromXY(int x, int y) { Vector3 pos = GetVerticePosition(GetPercentage(x, y)); float ln = CoordinatesProjector.CartesianToLon(pos); float lat = CoordinatesProjector.CartesianToLat(pos); float intensity = BaseElevation + (GetGrayScale(ln + 180f, lat + 90f) * MeanElevation); Vector3 newpos = CoordinatesProjector.InverseMercatorProjector( ln * Mathf.Deg2Rad, lat * Mathf.Deg2Rad, intensity); return(newpos); }
/// <summary> /// Sample noise's pixels to compute every vertices elevation and vertex's color color. /// </summary> /// <param name="noise">The texture representing the planet.</param> /// <param name="baseElevation">The minimum radius the planet can be.</param> /// <param name="meanElevation">The maximum elevation the planet can have (base + mean).</param> /// <param name="grad">The color gradient the texture is based on.</param> public void ElevateMesh(Texture2D noise, float baseElevation, float meanElevation, Gradient grad = null) { if (grad == null) { return; } _grad = grad; BaseElevation = baseElevation; MeanElevation = meanElevation; tex = noise; vertices = mesh.vertices; Vector3[] normals = new Vector3[vertices.Length]; Color[] colors = new Color[normals.Length]; float ln = 0f; float lat = 0f; float intensity = 0f; for (int x = 0, i = 0; x < resolution; x++) { for (int y = 0; y < resolution; y++, i++) { ln = CoordinatesProjector.CartesianToLon(vertices[i].normalized); lat = CoordinatesProjector.CartesianToLat(vertices[i].normalized); var invertedln = Mathf.Abs(ln - 360f) + 90f; intensity = baseElevation + (GetGrayScale(invertedln, lat) * meanElevation);// ln, la are in [-180;180] - [-90;90] interval here vertices[i] = CoordinatesProjector.InverseMercatorProjector( ln * Mathf.Deg2Rad, lat * Mathf.Deg2Rad, intensity); if (x < resolution && y == 0) { } colors[i] = GetColor(invertedln, lat); // ln, la are in [-180;180] - [-90;90] interval here } } mesh.vertices = vertices; mesh.colors = colors; mesh.RecalculateNormals(); mesh.RecalculateTangents(); mesh.RecalculateBounds(); mesh.normals = CalculateNormals(); }
/// <summary> /// Get a position using a 3d position and x and y offsets. /// </summary> /// <param name="pos">The origin position.</param> /// <param name="XOffset">the magnitude of the longitude offset.</param> /// <param name="YOffset">the magnitude of the latitude offset.</param> /// <returns>The 3D position of the offseted point.</returns> Vector3 GetPosition(Vector3 pos, int XOffset, int YOffset) { Vector2 posLnLat = CoordinatesProjector.GetLnLatFromPosition(pos); float lnOffset = 360f / ((float)resolution * 4f); float latOffset = 180f / ((float)resolution * 4f) * 2f; Vector2 newpos = new Vector2( posLnLat.x + (lnOffset * (XOffset / 2f)), posLnLat.y + (latOffset * (YOffset / 2f))); float MapLn = Mathf.Abs(newpos.x - 360f) + 90f; float elevation = BaseElevation + (GetGrayScale(MapLn, newpos.y) * MeanElevation); Vector3 finalpos = CoordinatesProjector.InverseMercatorProjector( (newpos.x) * Mathf.Deg2Rad, (newpos.y) * Mathf.Deg2Rad, elevation); return(finalpos); }
void CustomOffset() { Debug.Log("CustomOffset"); Vector3 pos = filter.sharedMesh.vertices[(X * Resolution) + Y]; Vector2 posLnLat;// = CoordinatesProjector.GetLnLatFromPosition(pos); //posLnLat = new Vector2(posLnLat.x, posLnLat.y); posLnLat = Vector2.zero; Vector2 newpos = new Vector2( posLnLat.x + (lnlatoffset.x), posLnLat.y + (lnlatoffset.y)); Vector3 finalpos = CoordinatesProjector.InverseMercatorProjector( (newpos.x) * Mathf.Deg2Rad, (newpos.y) * Mathf.Deg2Rad, .5f); Points[1].transform.position = finalpos; }
Vector3 getpos(Vector3 pos, int XOffset, int YOffset) { Vector2 posLnLat = CoordinatesProjector.GetLnLatFromPosition(pos); //posLnLat = new Vector2(posLnLat.x, posLnLat.y); float lnOffset = (360f / ((float)Resolution * 4f) * Distance) / 2f; float latOffset = 180f / ((float)Resolution * 4f) * Distance; Vector2 newpos = new Vector2( posLnLat.x + (lnOffset * XOffset), posLnLat.y + (latOffset * YOffset)); float elevation = BaseElevation + (GetGrayScale(newpos.x, newpos.y) * MeanElevation); Vector3 finalpos = CoordinatesProjector.InverseMercatorProjector( (newpos.x) * Mathf.Deg2Rad, (newpos.y) * Mathf.Deg2Rad, elevation); Debug.Log("getpos[posLnLat][lnOffset][latOffset][newpos][elevation][finalpos] : " + posLnLat.ToString() + " | " + lnOffset + " | " + latOffset + " | " + newpos.ToString() + " | " + elevation + " | " + finalpos.ToString()); return(finalpos); }
void PlaceClouds(Texture2D cloudmap) { // cloud's points of interest List <Vector2> POIs = new List <Vector2>(); for (int i = 0; i < CloudAmount / PerCloud; i++) { POIs.Add(FindPOILonLat(cloudmap)); } for (int i = 0; i < CloudAmount; i++) { // particle pos relative to it's POI Vector2 particlePositionFromPOI = new Vector2( Random.Range(-CloudSize.x, CloudSize.x), Random.Range(-CloudSize.y, CloudSize.y)); Vector2 particlePositionLonLat = new Vector2( Mathf.Clamp( (POIs[i / PerCloud].x + particlePositionFromPOI.x), 0, cloudmap.width), Mathf.Clamp( (POIs[i / PerCloud].y + particlePositionFromPOI.y), 0, cloudmap.height)); Vector2 particlePosition = new Vector2( ((particlePositionLonLat.x / cloudmap.width) * 360f) - 180f, ((particlePositionLonLat.y / cloudmap.height) * 180f) - 90f); particles[i].position = CoordinatesProjector.InverseMercatorProjector( particlePosition.x * Mathf.Deg2Rad, particlePosition.y * Mathf.Deg2Rad, Size); } }