Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to undo rotation and flip from Cubemap.getPixel

Discussion in 'Scripting' started by xchen774, May 26, 2020.

  1. xchen774

    xchen774

    Joined:
    Apr 7, 2020
    Posts:
    3
    Hi, I have a script that moves a camera, captures the cubemap at its location every certain time interval, and stores each of 6 faces of the cubemap as images, where the script is attached below (cubemap is assigned as Camera.main.RenderToCubemap(cubemap):

    Code (CSharp):
    1. void getTexture(CubemapFace face, string folder) {
    2.         Color[] colors = cubemap.GetPixels(face);
    3.         Texture2D tex = new Texture2D(1024, 1024);
    4.         tex.SetPixels(colors);
    5.         byte[] image = tex.EncodeToPNG();
    6.         string imagePath = Path.Combine(folderPath + "/" + folder + "/" + count + ".png");
    7.         File.WriteAllBytes(imagePath, image);
    8.     }
    9.  
    The problem is that the captured cubemap image is both a 180 degree rotated and horizontally flipped version of what the camera supposedly captures. For instance, I get the positive X face of cubemap as
    0.png

    when the game view is
    Screen Shot 2020-05-25 at 10.48.29 PM.png

    I don't see any additional parameters for Cubemap.GetPixels that can solve this problem and I wonder if there is any workaround. Thank you!
     

    Attached Files:

  2. Cannist

    Cannist

    Joined:
    Mar 31, 2020
    Posts:
    64
    It looks to me only horizontally flipped, not additionally rotated. From the documentation it looks like Cubemap lays out its pixel rows top to bottom whereas Texture2D does it bottom to top. The straight forward solution would be to reorder the data in your
    colors
    array.
     
    xchen774 likes this.