Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
1
Question by KjipGamer · Mar 17, 2020 at 04:47 PM · textureskyboxcubemap

Change Texture Shape of a texture through script

Hey, is there any way to change the texture shape of a texture through a script?

I know how to do it using the Import settings (see image) but I was wondering how to do this inside of a script, at runtime.

alt text

Thanks a lot!

ashampoo-snap-20200317-17h24m45s-001.png (8.6 kB)
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

Answer by KjipGamer · Mar 18, 2020 at 11:42 PM

Update: After digging through endless forum posts, I've solved my original question of getting a URL to a skybox.

I hope this helps someone with the same problem!

 using System.Collections;
 using UnityEngine;
 
 public class LoadImageToSkybox : MonoBehaviour
 {
     public string url = "Your URL Here";
     public int CubemapResolution = 512;
     Cubemap c;
 
     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)
         }
     };
 
 
     private void Start() { // When the game starts, apply the skybox texture
         StartCoroutine(setImage());
     }
 
     IEnumerator setImage()
     {
         WWW www = new WWW(url);
 
         yield return www;
         Debug.Log(www.bytesDownloaded);
         Debug.Log(www.progress);
         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 
         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
         Material cubeMapMaterial = new Material(Shader.Find("Skybox/Cubemap"));
         cubeMapMaterial.SetTexture("_Tex", c);
         RenderSettings.skybox = cubeMapMaterial;
     }
 
     /// <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);
     }
 }
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
0

Answer by mpautzke · Mar 19 at 06:54 PM

This worked! I needed to change my sky dynamically for a computer vision perception project. Here is a quick and dirty snippet applying this code to my use case. Note, I modified the CreateCubeMapTexture and Project methods to take the texture as parameter instead of using a class variable.

 volumeObject.GetComponent<Volume>().profile.TryGet(out sky);
 var c = new Cubemap(CubemapResolution, TextureFormat.RGBA32, false);
 Color[] CubeMapColors;
 for (int i = 0; i < 6; i++)
 {
      CubeMapColors = CreateCubemapTexture(CubemapResolution, (CubemapFace)i, _texture);
      c.SetPixels(CubeMapColors, (CubemapFace)i);
 }
 c.Apply();
 sky.hdriSky.Override(c);
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

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

237 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 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 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

Skybox rendred texture quality is poor 1 Answer

Textures stretched on Tree Creator's trees 0 Answers

Building cubemap from runtime-loaded image 0 Answers

Converting Cubemaps to Panoramic images? 0 Answers

How use TextureImporterGenerateCubemap 0 Answers


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