public static Texture2D GetSphericalTexture2D(this Cubemap cubemap) { int w = cubemap.width * 2; int h = cubemap.height * 2; Texture2D texture2D = new Texture2D(w, h, cubemap.format, false); Color backColor = cubemap.GetColorInDirection(-Vector3.forward); for (int j = 0; j < h; j++) { for (int i = 0; i < w; i++) { float x = (float)i / (float)w; float y = (float)j / (float)h; float d = Vector2.Distance(Vector2.one * 0.5f, new Vector2(x, y)); if (d <= 0.5f) { float r = 2 * Mathf.Sqrt(-4 * x * x + 4 * x - 1 - 4 * y * y + 4 * y); Vector3 dir = new Vector3( r * (2 * x - 1), r * (2 * y - 1), -8 * x * x + 8 * x - 8 * y * y + 8 * y - 3 ); texture2D.SetPixel(i, j, cubemap.GetColorInDirection(dir)); } else { texture2D.SetPixel(i, j, backColor); } } } texture2D.Apply(); return(texture2D); }
public static Texture2D GetCylindricalTexture2D(this Cubemap cubemap) { int w = cubemap.width * 4; int h = cubemap.height * 2; Texture2D texture2D = new Texture2D(w, h, cubemap.format, false); for (int j = 0; j < h; j++) { for (int i = 0; i < w; i++) { float a = (((float)i / (float)w) - 0.25f) * Mathf.PI * 2; float x = -Mathf.Cos(a); float y = Mathf.Sin(a); Vector3 dir = new Vector3(x, 0, y); float b = ((float)j / (float)h); if (b > 0.5f) { dir = Vector3.Slerp(dir, Vector3.up, (b - 0.5f) * 2); } else { dir = Vector3.Slerp(dir, -Vector3.up, 1 - b * 2); } texture2D.SetPixel(i, j, cubemap.GetColorInDirection(dir)); } } texture2D.Apply(); return(texture2D); }