0

I have cubes that are dynamically instantiated with a "Sprite Renderer" component.

With the code below, I load the texture into the sprite of the cubes from an url.

As long as there is no cubemap in the scene, everything is clearly visible. However, when the cubemap is loaded, all the sprites become invisible and only the cubes are visible. What is causing this issue?

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class urlSkybox : MonoBehaviour
{
    //public string url = "http://smupromp.altervista.org/f1.png?fbclid=IwAR2IcpdveA4vXP1_o0b9PdQ1-mxsxO6kkqdyhSQoxY48Dpv_uOhpNfYlRhg";
    public int CubemapResolution = 2048;

    private int cont=0;
    private int contRoom = 0;

    List<string> urlRoom = new List<string>();

    private Texture2D source;

    /// <summary>
    /// These are the faces of a cube
    /// </summary>
    private Vector3[][] faces =
    {
        new Vector3[] {
            new Vector3(1.0f, 1.0f, -1.0f),
            new Vector3(1.0f, 1.0f, 1.0f),
            new Vector3(1.0f, -1.0f, -1.0f),
            new Vector3(1.0f, -1.0f, 1.0f)
        },
        new Vector3[] {
            new Vector3(-1.0f, 1.0f, 1.0f),
            new Vector3(-1.0f, 1.0f, -1.0f),
            new Vector3(-1.0f, -1.0f, 1.0f),
            new Vector3(-1.0f, -1.0f, -1.0f)
        },
        new Vector3[] {
            new Vector3(-1.0f, 1.0f, 1.0f),
            new Vector3(1.0f, 1.0f, 1.0f),
            new Vector3(-1.0f, 1.0f, -1.0f),
            new Vector3(1.0f, 1.0f, -1.0f)
        },
        new Vector3[] {
            new Vector3(-1.0f, -1.0f, -1.0f),
            new Vector3(1.0f, -1.0f, -1.0f),
            new Vector3(-1.0f, -1.0f, 1.0f),
            new Vector3(1.0f, -1.0f, 1.0f)
        },
        new Vector3[] {
            new Vector3(-1.0f, 1.0f, -1.0f),
            new Vector3(1.0f, 1.0f, -1.0f),
            new Vector3(-1.0f, -1.0f, -1.0f),
            new Vector3(1.0f, -1.0f, -1.0f)
        },
        new Vector3[] {
            new Vector3(1.0f, 1.0f, 1.0f),
            new Vector3(-1.0f, 1.0f, 1.0f),
            new Vector3(1.0f, -1.0f, 1.0f),
            new Vector3(-1.0f, -1.0f, 1.0f)
        }
    };

    void Start()
    {
        string tmp = string.Empty;
        do
        {
            Debug.Log("Attesa...");
        } while (!System.IO.File.Exists("config.php"));
        tmp = File.ReadAllText("config.php");
        string[] lines = tmp.Split('\n', '\r');
        foreach (string line in lines)
        {
            if (!string.IsNullOrEmpty(line.Trim()))
            {
                string[] d = line.Split(' ');
                if (d[0][0] == '@') //Qundi se trova il link della 360
                {
                    urlRoom.Add("http://smupromp.altervista.org/uploads/"+d[1]);
                    Debug.Log(d[1]);
                    cont++;
                }
            }
        }
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            if (contRoom < cont)
            {
                StartCoroutine(setImage(urlRoom[contRoom++]));
            }
        }
        /*// When trigger, we start the process
        if (Input.GetMouseButtonDown(0))
        {
            switch (cont)
            {
                case 0:
                    StartCoroutine(setImage());
                    cont++;
                    break;

                case 1:
                    url = "http://smupromp.altervista.org/f2.png";
                    StartCoroutine(setImage());
                    cont = 0;
                    break;
            }
        }*/
    }

    IEnumerator setImage(string url)
    {
        WWW www = new WWW(url);
                              //Texture myGUITexture = Resources.Load("23") as Texture;

        Debug.Log(www.bytesDownloaded);
        //Debug.Log(www.progress);
        string _download;
        while (!www.isDone)
        {
            _download = (www.progress*100).ToString();
            Debug.Log(_download);
            yield return null;
        }

        yield return www;
        Debug.Log("Importazione terminata");
        Debug.Log(www.texture);

        source = new Texture2D(www.texture.width, www.texture.height);
        // we put the downloaded image into the new texture
        www.LoadImageIntoTexture(source);

        // new cubemap 
        Cubemap c = new Cubemap(CubemapResolution, TextureFormat.RGBA32, false);

        Color[] CubeMapColors;

        for (int i = 0; i < 6; i++)
        {
            CubeMapColors = CreateCubemapTexture(CubemapResolution, (CubemapFace)i);
            c.SetPixels(CubeMapColors, (CubemapFace)i);
        }
        // we set the cubemap from the texture pixel by pixel
        c.Apply();

        //Destroy all unused textures
        DestroyImmediate(source);
        DestroyImmediate(www.texture);
        Texture2D[] texs = FindObjectsOfType<Texture2D>();
        for (int i = 0; i < texs.Length; i++)
        {
            DestroyImmediate(texs[i]);
        }

        // We change the Cubemap of the Skybox
        RenderSettings.skybox.SetTexture("_Tex", c);
    }

    /// <summary>
    /// Generates a Texture that represents the given face for the cubemap.
    /// </summary>
    /// <param name="resolution">The targetresolution in pixels</param>
    /// <param name="face">The target face</param>
    /// <returns></returns>
    private Color[] CreateCubemapTexture(int resolution, CubemapFace face)
    {
        Texture2D texture = new Texture2D(resolution, resolution, TextureFormat.RGB24, false);

        Vector3 texelX_Step = (faces[(int)face][1] - faces[(int)face][0]) / resolution;
        Vector3 texelY_Step = (faces[(int)face][3] - faces[(int)face][2]) / resolution;

        float texelSize = 1.0f / resolution;
        float texelIndex = 0.0f;

        //Create textured face
        Color[] cols = new Color[resolution];
        for (int y = 0; y < resolution; y++)
        {
            Vector3 texelX = faces[(int)face][0];
            Vector3 texelY = faces[(int)face][2];
            for (int x = 0; x < resolution; x++)
            {
                cols[x] = Project(Vector3.Lerp(texelX, texelY, texelIndex).normalized);
                texelX += texelX_Step;
                texelY += texelY_Step;
            }
            texture.SetPixels(0, y, resolution, 1, cols);
            texelIndex += texelSize;
        }
        texture.wrapMode = TextureWrapMode.Clamp;
        texture.Apply();

        Color[] colors = texture.GetPixels();
        DestroyImmediate(texture);

        return colors;
    }

    /// <summary>
    /// Projects a directional vector to the texture using spherical mapping
    /// </summary>
    /// <param name="direction">The direction in which you view</param>
    /// <returns></returns>
    private Color Project(Vector3 direction)
    {
        float theta = Mathf.Atan2(direction.z, direction.x) + Mathf.PI / 180.0f;
        float phi = Mathf.Acos(direction.y);

        int texelX = (int)(((theta / Mathf.PI) * 0.5f + 0.5f) * source.width);
        if (texelX < 0) texelX = 0;
        if (texelX >= source.width) texelX = source.width - 1;
        int texelY = (int)((phi / Mathf.PI) * source.height);
        if (texelY < 0) texelY = 0;
        if (texelY >= source.height) texelY = source.height - 1;

        return source.GetPixel(texelX, source.height - texelY - 1);
    }
}

The code that establishes the cubemap's texture dynamically is very slow; if you have any advice for that, it is well accepted.

PaulG
  • 13,871
  • 9
  • 56
  • 78
The_Buc
  • 13
  • 2
  • instead of `Start()` , use `Awake()` that can be helpful – Maifee Ul Asad Jun 04 '19 at 18:05
  • instead of dynamically downloading the textures why not download them all on app start and only assign them later? hwen you `DestroyImmediate(texs[i]);` do you maybe destroy all textures of any sprite as well? – derHugo Jun 05 '19 at 08:36
  • Yes, DestroyImmediate was the problems! For the slowness do you can help me? it's very very slow to load the cubemap (about 30 seconds) – The_Buc Jun 06 '19 at 09:09

0 Answers0