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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Extending Unity Built-In Skybox Panoramic Shader

Discussion in 'Shaders' started by stonstad, Jun 16, 2021.

  1. stonstad

    stonstad

    Joined:
    Jan 19, 2018
    Posts:
    599
    Hi there. I modified the built-in Unity skybox panoramic shader to support blending between two textures. This is helpful for cross-fading between two skies.

    Here is the original Unity shader:
    https://raw.githubusercontent.com/c...DefaultResourcesExtra/Skybox-Panoramic.shader

    Here is my modified version:
    https://gist.github.com/stonstad/28346c2b9d6668a2d697564a2e9909b9

    There are separate properties for each sky (texture, gamma, and tint) and a single blend property specifies the blend between each output.

    OK, it works! But I am conceptually confused at whether it would be possible to implement an independent rotation for each texture.


    To implement independent rotation... here is what I tried:

    1) Adding a new vertex property to v2f. This does not work for me because vertex stream processing is one vertex at a time. Not sure what I was thinking there.

    2) UV texture translation. This does not work for me because each texture is spherical HDR and 4096x2048 pixels.

    I thought about applying the original shader to two meshes and performing rotation via transforms in the hierarchy. But the potential downside to this approach is that the sky color would not contribute to scene lighting. I could then disable skybox environment lighting and use reflection probes, but then that seems like extra effort.

    Is there a technique or approach I might try? Thank you so much for your thoughts and expertise!
     
    alexu, Luferau and JJunior like this.
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,243
    Nothing dumb about that, this would totally work if you wanted to do it this way. But if you're going to stick to equirectangular ("Latitude Longitude Layout") maps there's a way, way easier method.

    First, more than half of the shader code exists to handle a "6 Frames Layout" texture. If you delete all of the shader code in the
    #ifdef _MAPPING_6_FRAMES_LAYOUT
    blocks, the original will end up being much more manageable. Rotating an equirectangular UV is as simple as adding to the
    uv.x
    , or
    tc.x
    in this shader.

    Code (csharp):
    1. float2 tc = ToRadialCoords(i.texcoord);
    2. half4 tex1 = tex2D (_Texture1, tc);
    3. // you want _Rotation2 to be a 0 to 360 like the original value
    4. tc.x = frac(tc.x + (_Rotation2 - _Rotation) / 360.0);
    5. half4 tex2 = tex2D (_Texture2, tc);
    And you're done.
     
    stonstad likes this.
  3. stonstad

    stonstad

    Joined:
    Jan 19, 2018
    Posts:
    599
    Luferau and Roman_Keivan like this.
  4. Emissaria

    Emissaria

    Joined:
    Dec 18, 2013
    Posts:
    4
    This work perfectly thanks a lot and great job!
    You rock :)
     
    stonstad likes this.
  5. JJunior

    JJunior

    Joined:
    May 22, 2019
    Posts:
    52
    Saved my life today! Thank you stonstad!
     
    stonstad likes this.
  6. Roman_Keivan

    Roman_Keivan

    Joined:
    May 31, 2019
    Posts:
    21
    Patvx1 and stonstad like this.
  7. alexu

    alexu

    Joined:
    Jun 19, 2014
    Posts:
    8
    Awesome, you're the GOAT!
     
    stonstad likes this.