Skip to content

Instantly share code, notes, and snippets.

@edom18
Last active June 16, 2019 23:55
Show Gist options
  • Save edom18/60ec503a4a4d24c71ee476eae3bc2c96 to your computer and use it in GitHub Desktop.
Save edom18/60ec503a4a4d24c71ee476eae3bc2c96 to your computer and use it in GitHub Desktop.
直交座標から極座標への変換メモ ref: https://qiita.com/edo_m18/items/5d61f19512fb05f48501
private void Convert(Color[] colors, int width, int height)
{
int halfWidth = width / 2;
int halfHeight = height / 2;
Texture2D tex = new Texture2D(width, height);
Color[] newColors = new Color[colors.Length];
GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad);
quad.name = "PolarCoord";
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int dx = x - halfWidth;
int dy = halfHeight - y;
int X = (int)Mathf.Floor(Mathf.Atan2(dy, dx) * (width / 360f) * (180f / Mathf.PI));
int Y = (int)Mathf.Floor(Mathf.Sqrt(dx * dx + dy * dy));
int idx = (Y * width) + X;
Color color = colors[idx];
int newIdx = (y * width) + x;
newColors[newIdx] = color;
}
}
tex.SetPixels(newColors);
tex.Apply();
Renderer ren = quad.GetComponent<Renderer>();
ren.material.mainTexture = tex;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment