In my app I need teh user to be able to download already prepared 360 photos, cut into 6 pieces.
After that, the cubemap must be made, and loaded as a skybox, so that the user could explore 360 degrees photos on his mobile device.
I've been stuck here for almost a week, but nothing worked for different reasons, finally I've ended up with something that doesn't throw exceptions, but still it sets an emty gray skybox in the end.
**Option 1: HttpWebRespone**
public Material second;
private Cubemap c;
public void SetSkybox()
{
c = new Cubemap (1024, TextureFormat.RGB24, false);
PhotoRequest("front", CubemapFace.PositiveZ);
PhotoRequest ("back", CubemapFace.NegativeZ);
PhotoRequest ("top", CubemapFace.PositiveY);
PhotoRequest ("bottom", CubemapFace.NegativeY);
PhotoRequest ("right", CubemapFace.PositiveX);
PhotoRequest ("left", CubemapFace.NegativeX);
c.Apply ();
Material mat = new Material (second);
mat.SetTexture ("_Cube", c);
RenderSettings.skybox = mat;
}
private void PhotoRequest(string filename, CubemapFace cmf)
{
ServicePointManager.ServerCertificateValidationCallback = SkyboxAuthentication.Validator; // I know, it's bad
string url = {path to the webfolder with the photos};
string completeURL = url + filename;
var request = (HttpWebRequest)WebRequest.Create (completeURL);
var response = (HttpWebResponse)request.GetResponse ();
Stream dataStream = response.GetResponseStream ();
BinaryReader reader = new BinaryReader (dataStream);
byte[] responseFromServer = ReadAllBytes(reader);
Texture2D tex = new Texture2D (1920, 1920);
tex.LoadImage (responseFromServer);
Color[] colors = tex.GetPixels ();
c.SetPixels (colors, cmf);
reader.Close ();
response.Close ();
}
byte[] ReadAllBytes(BinaryReader rdr)
{
const int bufferSize = 1024;
using (var ms = new MemoryStream()) {
byte[] buffer = new byte[bufferSize];
int count;
while ((count = rdr.Read (buffer, 0, buffer.Length)) != 0) {
ms.Write (buffer, 0, count);
}
return ms.ToArray ();
}
}
**Option 2: WWW**
bool front = false;
bool back = false;
bool top = false;
bool bottom = false;
bool right = false;
bool left = false;
public void SecondSkybox()
{
c = new Cubemap (1024, TextureFormat.RGB24, false);
StartCoroutine(PhotoRequest ("front", CubemapFace.PositiveZ));
StartCoroutine(PhotoRequest ("back", CubemapFace.NegativeZ));
StartCoroutine(PhotoRequest ("top", CubemapFace.PositiveY));
StartCoroutine(PhotoRequest ("bottom", CubemapFace.NegativeY));
StartCoroutine(PhotoRequest ("right", CubemapFace.PositiveX));
StartCoroutine(PhotoRequest ("left", CubemapFace.NegativeX));
StartCoroutine (DoLast ());
c.Apply ();
Material mat = new Material (second);
mat.SetTexture ("_Cube", c);
RenderSettings.skybox = mat;
}
private IEnumerator PhotoRequest(string filename, CubemapFace cmf)
{
switch (filename) {
case "front":
front = true;
break;
case "back":
back = true;
break;
case "top":
top = true;
break;
case "bottom":
bottom = true;
break;
case "right":
right = true;
break;
case "left":
left = true;
break;
}
string url = {path to the webfolder with the photos};
WWW www = new WWW (url + filename);
yield return www;
Debug.Log (www.responseHeaders["STATUS"].ToString());
Color[] colors = www.texture.GetPixels();
c.SetPixels (colors, cmf);
switch (filename) {
case "front":
front = false;
break;
case "back":
back = false;
break;
case "top":
top = false;
break;
case "bottom":
bottom = false;
break;
case "right":
right = false;
break;
case "left":
left = false;
break;
}
}
private IEnumerator DoLast()
{
while (front || back || top || bottom || right || left) {
yield return new WaitForSeconds (0.1f);
}
}
↧