tech/metaverse

21/09/10 How to Change Skybox through Code in Unity3D

tech-lover 2021. 9. 10. 12:51

https://youtu.be/asMBcXrJcEw

 

How to Change Skybox through Code in Unity3D

In this video you will learn how you can change the Skybox material with the help of C# script. Download Script : https://drive.google.com/open?id=0B__1zp7jwQOKeWE0Z3NxUURGdzg Download SkyBox : https://www.assetstore.unity3d.com/en/#!/content/60004

youtu.be

 

Changing skybox material through script -unity Tutorial

https://youtu.be/bftQ9W2a9TY

 

Changing skybox material through script -unity Tutorial

we are going to see how to change by Changing skybox material through script by using button or after game starts

youtu.be

Unity C# skybox cubemap generator (source code)

https://youtu.be/_fnfivQbRlA

 

Unity C# skybox cubemap generator (source code)

Source code: https://github.com/przemyslawzaworski/Unity3D-C-programming/blob/master/cubemap_generator.cs

youtu.be

 

- References

 

What the code basically does is:

  • Get the panorama texture from www
  • For each face, calculates the texture
  • Assign the generated texture to the cubemap
  • Collect garbage
  • Assign the cubemap to the shader

```

using System.Collections;

using UnityEngine;

 

public class ReplaceCubemap : MonoBehaviour

{

    public string url = "your file name";

    public int CubemapResolution = 256;

 

    private Texture2D source;

 

    /// <summary>

    /// These are the faces of a cube

    /// </summary>

    private Vector3[][] faces =

    {

        new Vector3[] {

            new Vector3(1.0f1.0f, -1.0f),

            new Vector3(1.0f1.0f1.0f),

            new Vector3(1.0f, -1.0f, -1.0f),

            new Vector3(1.0f, -1.0f1.0f)

        },

        new Vector3[] {

            new Vector3(-1.0f1.0f1.0f),

            new Vector3(-1.0f1.0f, -1.0f),

            new Vector3(-1.0f, -1.0f1.0f),

            new Vector3(-1.0f, -1.0f, -1.0f)

        },

        new Vector3[] {

            new Vector3(-1.0f1.0f1.0f),

            new Vector3(1.0f1.0f1.0f),

            new Vector3(-1.0f1.0f, -1.0f),

            new Vector3(1.0f1.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.0f1.0f),

            new Vector3(1.0f, -1.0f1.0f)

        },

        new Vector3[] {

            new Vector3(-1.0f1.0f, -1.0f),

            new Vector3(1.0f1.0f, -1.0f),

            new Vector3(-1.0f, -1.0f, -1.0f),

            new Vector3(1.0f, -1.0f, -1.0f)

        },

        new Vector3[] {

            new Vector3(1.0f1.0f1.0f),

            new Vector3(-1.0f1.0f1.0f),

            new Vector3(1.0f, -1.0f1.0f),

            new Vector3(-1.0f, -1.0f1.0f)

        }

    };

 

    void Update()

    {

        // When trigger, we start the process

        if (Input.GetKeyDown(KeyCode.F))

        {

 

            // start Coroutine to handle the WWW asynchronous process

            StartCoroutine(setImage());

        }

    }

 

    IEnumerator setImage()

    {

        WWW www = new WWW(url);

        //Texture myGUITexture = Resources.Load("23") as Texture;

 

        Debug.Log(www.bytesDownloaded);

        Debug.Log(www.progress);

        Debug.Log(www.texture);

 

        yield return www;

 

        source = new Texture2D(www.texture.widthwww.texture.height);

        // we put the downloaded image into the new texture

        www.LoadImageIntoTexture(source);

 

        // new cubemap 

        Cubemap c = new Cubemap(CubemapResolutionTextureFormat.RGBA32false);

 

        Color[] CubeMapColors;

 

        for (int i = 0i < 6i++)

        {

            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 = 0i < texs.Lengthi++)

        {

            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 resolutionCubemapFace face)

    {

        Texture2D texture = new Texture2D(resolutionresolutionTextureFormat.RGB24false);

 

        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 = 0y < resolutiony++)

        {

            Vector3 texelX = faces[(int)face][0];

            Vector3 texelY = faces[(int)face][2];

            for (int x = 0x < resolutionx++)

            {

                cols[x] = Project(Vector3.Lerp(texelXtexelYtexelIndex).normalized);

                texelX += texelX_Step;

                texelY += texelY_Step;

            }

            texture.SetPixels(0yresolution1cols);

            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.zdirection.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 < 0texelX = 0;

        if (texelX >= source.widthtexelX = source.width - 1;

        int texelY = (int)((phi / Mathf.PI) * source.height);

        if (texelY < 0texelY = 0;

        if (texelY >= source.heighttexelY = source.height - 1;

 

        return source.GetPixel(texelXsource.height - texelY - 1);

    }

}

```

 

- image intensity error change

```

using System.Collections;

using UnityEngine;

 

public class ReplaceCubemap : MonoBehaviour

{

    public string url = "your file name";

    public int CubemapResolution = 256;

 

    private Texture2D source;

 

    /// <summary>

    /// These are the faces of a cube

    /// </summary>

    private Vector3[][] faces =

    {

        new Vector3[] {

            new Vector3(1.0f1.0f, -1.0f),

            new Vector3(1.0f1.0f1.0f),

            new Vector3(1.0f, -1.0f, -1.0f),

            new Vector3(1.0f, -1.0f1.0f)

        },

        new Vector3[] {

            new Vector3(-1.0f1.0f1.0f),

            new Vector3(-1.0f1.0f, -1.0f),

            new Vector3(-1.0f, -1.0f1.0f),

            new Vector3(-1.0f, -1.0f, -1.0f)

        },

        new Vector3[] {

            new Vector3(-1.0f1.0f1.0f),

            new Vector3(1.0f1.0f1.0f),

            new Vector3(-1.0f1.0f, -1.0f),

            new Vector3(1.0f1.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.0f1.0f),

            new Vector3(1.0f, -1.0f1.0f)

        },

        new Vector3[] {

            new Vector3(-1.0f1.0f, -1.0f),

            new Vector3(1.0f1.0f, -1.0f),

            new Vector3(-1.0f, -1.0f, -1.0f),

            new Vector3(1.0f, -1.0f, -1.0f)

        },

        new Vector3[] {

            new Vector3(1.0f1.0f1.0f),

            new Vector3(-1.0f1.0f1.0f),

            new Vector3(1.0f, -1.0f1.0f),

            new Vector3(-1.0f, -1.0f1.0f)

        }

    };

 

    void Update()

    {

        // When trigger, we start the process

        if (Input.GetKeyDown(KeyCode.F))

        {

 

            // start Coroutine to handle the WWW asynchronous process

            StartCoroutine(setImage());

        }

    }

 

    IEnumerator setImage()

    {

        WWW www = new WWW(url);

        //Texture myGUITexture = Resources.Load("23") as Texture;

 

        Debug.Log(www.bytesDownloaded);

        Debug.Log(www.progress);

        Debug.Log(www.texture);

 

        yield return www;

 

        source = new Texture2D(www.texture.widthwww.texture.height);

        // we put the downloaded image into the new texture

        www.LoadImageIntoTexture(source);

 

        // new cubemap 

        Cubemap c = new Cubemap(CubemapResolutionDefaultFormat.LDRTextureCreationFlags.None);

 

        Color[] CubeMapColors;

 

        for (int i = 0i < 6i++)

        {

            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 = 0i < texs.Lengthi++)

        {

            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 resolutionCubemapFace face)

    {

        Texture2D texture = new Texture2D(resolutionresolutionTextureFormat.RGB24false);

 

        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 = 0y < resolutiony++)

        {

            Vector3 texelX = faces[(int)face][0];

            Vector3 texelY = faces[(int)face][2];

            for (int x = 0x < resolutionx++)

            {

                cols[x] = Project(Vector3.Lerp(texelXtexelYtexelIndex).normalized);

                texelX += texelX_Step;

                texelY += texelY_Step;

            }

            texture.SetPixels(0yresolution1cols);

            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.zdirection.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 < 0texelX = 0;

        if (texelX >= source.widthtexelX = source.width - 1;

        int texelY = (int)((phi / Mathf.PI) * source.height);

        if (texelY < 0texelY = 0;

        if (texelY >= source.heighttexelY = source.height - 1;

 

        return source.GetPixel(texelXsource.height - texelY - 1);

    }

}

```

 

https://stackoverflow.com/questions/45032579/editing-a-cubemap-skybox-from-remote-image

 

Editing a Cubemap Skybox from remote image

I need to download an image from a server, then transform it in a Cubemap and finally put this CubeMap in my Skybox. I work with C#. I came up with this code : public string url = "image/url.jpg...

stackoverflow.com