Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by lchobaz · Jun 15, 2018 at 02:52 PM · materialruntimeskyboxcubemap

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 !

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Feelnside · Jun 18, 2018 at 12:11 PM 0
Share

Cool, happy to know!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

99 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges