Search Unity

Skybox 6-Sided Shader

Discussion in 'Shaders' started by aer0ace, Jun 20, 2019.

  1. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,513
    I'm trying to apply the Skybox 6-Sided shader to a piece of geometry. The results are that the texture doesn't look like it's applied correctly.

    I included a copy of the shader I'm talking about, below. (Although, I'm still on Unity 5.6.3)
    It looks to me that there is certainly a dependency on the uv tex coords as indicated by the vertex shader output.

    Does anyone know, or if it's documented somewhere, what the actual skybox geometry looks like when Unity applies it as the background? I'm a little confused.

    Code (shader):
    1. // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
    2.  
    3. Shader "Skybox/6 Sided" {
    4. Properties {
    5.     _Tint ("Tint Color", Color) = (.5, .5, .5, .5)
    6.     [Gamma] _Exposure ("Exposure", Range(0, 8)) = 1.0
    7.     _Rotation ("Rotation", Range(0, 360)) = 0
    8.     [NoScaleOffset] _FrontTex ("Front [+Z]   (HDR)", 2D) = "grey" {}
    9.     [NoScaleOffset] _BackTex ("Back [-Z]   (HDR)", 2D) = "grey" {}
    10.     [NoScaleOffset] _LeftTex ("Left [+X]   (HDR)", 2D) = "grey" {}
    11.     [NoScaleOffset] _RightTex ("Right [-X]   (HDR)", 2D) = "grey" {}
    12.     [NoScaleOffset] _UpTex ("Up [+Y]   (HDR)", 2D) = "grey" {}
    13.     [NoScaleOffset] _DownTex ("Down [-Y]   (HDR)", 2D) = "grey" {}
    14. }
    15.  
    16. SubShader {
    17.     Tags { "Queue"="Background" "RenderType"="Background" "PreviewType"="Skybox" }
    18.     Cull Off ZWrite Off
    19.  
    20.     CGINCLUDE
    21.     #include "UnityCG.cginc"
    22.  
    23.     half4 _Tint;
    24.     half _Exposure;
    25.     float _Rotation;
    26.  
    27.     float3 RotateAroundYInDegrees (float3 vertex, float degrees)
    28.     {
    29.         float alpha = degrees * UNITY_PI / 180.0;
    30.         float sina, cosa;
    31.         sincos(alpha, sina, cosa);
    32.         float2x2 m = float2x2(cosa, -sina, sina, cosa);
    33.         return float3(mul(m, vertex.xz), vertex.y).xzy;
    34.     }
    35.  
    36.     struct appdata_t {
    37.         float4 vertex : POSITION;
    38.         float2 texcoord : TEXCOORD0;
    39.         UNITY_VERTEX_INPUT_INSTANCE_ID
    40.     };
    41.     struct v2f {
    42.         float4 vertex : SV_POSITION;
    43.         float2 texcoord : TEXCOORD0;
    44.         UNITY_VERTEX_OUTPUT_STEREO
    45.     };
    46.     v2f vert (appdata_t v)
    47.     {
    48.         v2f o;
    49.         UNITY_SETUP_INSTANCE_ID(v);
    50.         UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
    51.         float3 rotated = RotateAroundYInDegrees(v.vertex, _Rotation);
    52.         o.vertex = UnityObjectToClipPos(rotated);
    53.         o.texcoord = v.texcoord;
    54.         return o;
    55.     }
    56.     half4 skybox_frag (v2f i, sampler2D smp, half4 smpDecode)
    57.     {
    58.         half4 tex = tex2D (smp, i.texcoord);
    59.         half3 c = DecodeHDR (tex, smpDecode);
    60.         c = c * _Tint.rgb * unity_ColorSpaceDouble.rgb;
    61.         c *= _Exposure;
    62.         return half4(c, 1);
    63.     }
    64.     ENDCG
    65.  
    66.     Pass {
    67.         CGPROGRAM
    68.         #pragma vertex vert
    69.         #pragma fragment frag
    70.         #pragma target 2.0
    71.         sampler2D _FrontTex;
    72.         half4 _FrontTex_HDR;
    73.         half4 frag (v2f i) : SV_Target { return skybox_frag(i,_FrontTex, _FrontTex_HDR); }
    74.         ENDCG
    75.     }
    76.     Pass{
    77.         CGPROGRAM
    78.         #pragma vertex vert
    79.         #pragma fragment frag
    80.         #pragma target 2.0
    81.         sampler2D _BackTex;
    82.         half4 _BackTex_HDR;
    83.         half4 frag (v2f i) : SV_Target { return skybox_frag(i,_BackTex, _BackTex_HDR); }
    84.         ENDCG
    85.     }
    86.     Pass{
    87.         CGPROGRAM
    88.         #pragma vertex vert
    89.         #pragma fragment frag
    90.         #pragma target 2.0
    91.         sampler2D _LeftTex;
    92.         half4 _LeftTex_HDR;
    93.         half4 frag (v2f i) : SV_Target { return skybox_frag(i,_LeftTex, _LeftTex_HDR); }
    94.         ENDCG
    95.     }
    96.     Pass{
    97.         CGPROGRAM
    98.         #pragma vertex vert
    99.         #pragma fragment frag
    100.         #pragma target 2.0
    101.         sampler2D _RightTex;
    102.         half4 _RightTex_HDR;
    103.         half4 frag (v2f i) : SV_Target { return skybox_frag(i,_RightTex, _RightTex_HDR); }
    104.         ENDCG
    105.     }
    106.     Pass{
    107.         CGPROGRAM
    108.         #pragma vertex vert
    109.         #pragma fragment frag
    110.         #pragma target 2.0
    111.         sampler2D _UpTex;
    112.         half4 _UpTex_HDR;
    113.         half4 frag (v2f i) : SV_Target { return skybox_frag(i,_UpTex, _UpTex_HDR); }
    114.         ENDCG
    115.     }
    116.     Pass{
    117.         CGPROGRAM
    118.         #pragma vertex vert
    119.         #pragma fragment frag
    120.         #pragma target 2.0
    121.         sampler2D _DownTex;
    122.         half4 _DownTex_HDR;
    123.         half4 frag (v2f i) : SV_Target { return skybox_frag(i,_DownTex, _DownTex_HDR); }
    124.         ENDCG
    125.     }
    126. }
    127. }
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    The Skybox 6-Sided shader is a very odd legacy thing that only works correctly when used as a skybox and cannot be used on any geometry in the scene and get anything useful.

    When Unity sees that shader is being used for a skybox it switches to some legacy skybox code that renders 6 specially oriented quads with each successive pass of the shader. Nothing else in Unity works that way.

    If you want something like that, you should be using a cube map and the Skybox/Cubemap shader. Note, do not use the legacy Cubemap asset that lets you build a cubemap from 6 textures, you need to composite your 6 textures into one of the accepted layouts and import it as a cubemap texture type.
    https://docs.unity3d.com/Manual/class-Cubemap.html
     
  3. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,513
    @bgolus Thanks so much for the info!
     
  4. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,513
    Anyone happen to know of a tool that takes 6 individual pngs and combines them into a single texture for cubemaps? I'm using the AllSky textures, and they're all laid out as individual pngs. Failing that, I guess I can write a tool myself.

    EDIT:

    Er, nevermind. AllSky already includes cubemaps as .EXR files

    EDIT2:

    Actually, the included .EXR files are low-res, most likely due to their intended use as reflection maps, and not skybox textures, so the question still stands.
     
    Last edited: Jun 21, 2019
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Use the equirectangular texture that are included. The ones that are single texture panoramas.