最近在做一个地球的东西,记录一下如何生成圆形
public float EarthRadius = 50;
public GameObject a, pra;
private int ZoomLevel=3, interval = 10;
private void Ins()
{
for (int i = 0; i < 90; i++)
{
if (i % interval == 0)
{
for (int x = 0; x < 360; x++)
{
if (x % interval == 0)
{
GameObject h = Instantiate(a, pra.transform);
h.SetActive(true);
h.transform.position = GetSphericalCoordinates(i, x);
}
}
}
}
for (int i = 0; i > -90; i--)
{
if (i % -interval == 0)
{
for (int x = 0; x > -360; x--)
{
if (x % -interval == 0)
{
GameObject h = Instantiate(a, pra.transform);
h.SetActive(true);
h.transform.position = GetSphericalCoordinates(i, x);
}
}
}
}
}
private Vector3 GetSphericalCoordinates(double longitude, double latitude)
{
latitude = latitude * Mathf.PI / 180D;
longitude = longitude * Mathf.PI / 180D;
double x = EarthRadius * Mathf.Cos((float)latitude) * Mathf.Sin((float)longitude);
double y = EarthRadius * Mathf.Sin((float)latitude);
double z = -EarthRadius * Mathf.Cos((float)latitude) * Mathf.Cos((float)longitude);
return new Vector3((float)x, (float)y, (float)z);
}
private Vector2 CalculateLontitudeAndLatitude(Vector3 pos)
{
int s = 1;
int q = 1;
if (pos.x < 0.0f)
{
s = -1;
}
if (pos.y > 0.0f)
{
q = -1;
}
float latitudeAngle = Mathf.Asin(pos.y / EarthRadius);
float latitude = latitudeAngle * 180.0f / Mathf.PI;
float longtitudeAngle = Mathf.Acos(pos.z / (-1.0f * EarthRadius * Mathf.Cos(latitudeAngle)));
float longtitude = longtitudeAngle * 180.0f / Mathf.PI;
return new Vector2(s * longtitude, q * latitude);
}
private float Sinh(float x)
{
float ax = (Mathf.Exp(x) - Mathf.Exp(-1.0f * x)) / 2;
return ax;
}
private Vector2 CalculateTileIndex(double longtitude, double latitude)
{
int x = (int)(Mathf.Pow(2, ZoomLevel - 1) * (longtitude / 180 + 1));
int y = (int)(Mathf.Pow(2, ZoomLevel - 1) * (1 - Mathf.Log((Mathf.Tan(Mathf.PI * (float)latitude / 180) + 1 / Mathf.Cos(Mathf.PI * (float)latitude / 180)), 2.7182818f) / Mathf.PI));
return new Vector2(x, y);
}
private Vector2 CalculateLongAndLa(int xtile, int ytile)
{
float n = Mathf.Pow(2, ZoomLevel);
float longtitude = xtile / n * 360.0f - 180.0f;
float latitude = Mathf.Atan(Sinh(Mathf.PI * (1 - 2 * ytile / n))) * 180.0f / Mathf.PI;
return new Vector2(longtitude, latitude);
}
private Vector2 LongAndLaToPixel(float longtitude, float latitude)
{
float pixelX = (longtitude + 180 / 360) * Mathf.Pow(2, ZoomLevel) * 256 % 256;
float pixelY = (1 - Mathf.Log(Mathf.Tan(latitude * Mathf.PI / 180) + 1 / (Mathf.Cos(latitude * Mathf.PI / 180)), 2.7182818f) / (2 * Mathf.PI)) * Mathf.Pow(2, ZoomLevel) * 256 % 256;
return new Vector2(pixelX, pixelY);
}
private Vector2 PixelToLongAndLa(float tileX, float tileY, float pixelX, float pixelY)
{
float longtitude = (tileX + pixelX / 256) / (Mathf.Pow(2, ZoomLevel)) * 360 - 180.0f;
float latitude = (Mathf.Asin(Sinh(Mathf.PI - 2 * Mathf.PI * (tileY + pixelY / 256) / (Mathf.Pow(2, ZoomLevel))))) * 180 / Mathf.PI;
return new Vector2(longtitude, latitude);
}
|