- Home /
How to change material skybox at runtime ?
Hello !
I have some issues changing the skybox at runtime. Actually, the problem is quite complex (at least for me). What I would like to do : the user can load an image (360° image, such as from street view) and the environment change around him (virtual reality application). In my script, I load the image and create a cubemap from it, make a material containing this cubemap and finally set the new skybox material with this one.
The code, looks like that :
Texture2D skyboxTexture;
Material skyboxMaterial;
public Cubemap cubemap;
private Color[] CubeMapColors;
void Start () {
skyboxMaterial = new Material(Shader.Find("Skybox/Cubemap"));
cubemap = new Cubemap(128, TextureFormat.RGBA32, false);
}
public void OnClickChangeEnvironment()
{
WWW www = new WWW (FileBrowser.OpenSingleFile("Select picture to load", System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyPictures), "jpg"));
skyboxTexture = www.texture;
CubeMapColors = skyboxTexture.GetPixels();
cubemap.SetPixels(CubeMapColors, CubemapFace.PositiveX);
cubemap.Apply();
skyboxMaterial.SetTexture("_Cube", cubemap);
RenderSettings.skybox = skyboxMaterial;
}
What I get, is a new material that has a skybox/cubemap as shader, but with no cubemap. I think it failed to create cubemap... Do you have some idea what is wrong with my code ? I've spent some time to look for a solution, and many people say that it's impossible to do it using script. However, I really need to do it even if the solution is very complex. Thank you very much for your help !
Answer by Feelnside · Jun 15, 2018 at 03:46 PM
Looks like you can use a custom skybox shader which allows to use a 360 texture. Take a look at the answer to the following question.
So, as result you don't need to convert a 360 texture into the cubemap. You can just assign the texture into the new skybox shader.
Answer by lchobaz · Jun 18, 2018 at 11:20 AM
Thank you for your answer ! It works perfectly, I just had to use this new shader (added in the project settings/Graphics), then the part of the code changed :
Texture2D skyboxTexture;
Material skyboxMaterial;
void Start () {
skyboxMaterial = new Material(Shader.Find("SkyboxEquirectangular"));
}
public void OnClickChangeEnvironment()
{
WWW www = new WWW (FileBrowser.OpenSingleFile("Select picture to load", System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyPictures), "jpg"));
skyboxTexture = www.texture;
skyboxMaterial.SetTexture("_Tex",skyboxTexture);
RenderSettings.skybox = skyboxMaterial;
DynamicGI.UpdateEnvironment();
}
I put my code in any case someone need to do the same thing. Finally, there was no need of cubemap, which is quite challenging to create by script.
Your answer
Follow this Question
Related Questions
Runtime cubemap generation from 1x256 gradient image? 1 Answer
changing cubemap at runtime 0 Answers
Can't Make HDRP Skybox 0 Answers
Zoom-able skybox 0 Answers
Amplify material editor: adjust property at runtime? 1 Answer