Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question URP : Using Shader Graph for skybox in VR

Discussion in 'VR' started by taikey00, Jun 25, 2021.

  1. taikey00

    taikey00

    Joined:
    May 16, 2019
    Posts:
    3
    Is it possible to use shader graph to make a skybox texture for a VR camera? My attempt works in scene view and game view until I hit play, the game view basically removes the skybox and renders nothing for the skybox. See the images below. Any help would be appreciated!

     
  2. the_real_apoxol

    the_real_apoxol

    Unity Technologies

    Joined:
    Dec 18, 2020
    Posts:
    467
    It should work I would think. The best bet would be to report a bug via `Help - > Report a bug` and include a sample project that demonstrates the issue. If you do submit a bug reply here with the issue # so we can take a look at it quicker.
     
  3. DINmatin

    DINmatin

    Joined:
    Aug 21, 2014
    Posts:
    14
    have the same problem. i put a scene together for bugReport , but then found online:
    issue #1328062 and probably related #1299691
     
  4. TheBlatantOne

    TheBlatantOne

    Joined:
    Feb 2, 2014
    Posts:
    4
    fuzzy3d likes this.
  5. DINmatin

    DINmatin

    Joined:
    Aug 21, 2014
    Posts:
    14
    @TheBlatantOne
    Thank you very much! Your solution works.
    it's not as convenient as doing it through shadergraph - leaves some work for the unity team :)
     
  6. gamedevbill

    gamedevbill

    Joined:
    May 25, 2018
    Posts:
    43
    It seems shader graph has updated itself to now have a few other blockers. Just changing one line in the generated code didn't work. So I lost interest and just wrote my shader from code.

    If you want a super basic code shader that works with URP skyboxes, here's a sample to use as a starting point:

    Code (CSharp):
    1.  
    2. Shader "Skybox/Minimal"
    3. {
    4.     Properties
    5.     {    
    6.         _TopOTheWorld("TopOTheWorld", Color) = (0.3, 0.6, 1, 1)
    7.         _Bottom("Bottom", Color) = (0.3,0.3,0.3, 1)
    8.     }
    9.     SubShader
    10.     {
    11.         Tags { "Queue"="Background" "RenderType"="Background" "PreviewType"="Skybox" }
    12.         Cull Off ZWrite Off
    13.  
    14.         Pass {
    15.             CGPROGRAM
    16.             #pragma vertex vert
    17.             #pragma fragment frag
    18.  
    19.             #include "UnityCG.cginc"
    20.          
    21.             uniform half3 _TopOTheWorld;
    22.             uniform half3 _Bottom;
    23.  
    24.             struct appdata_t
    25.             {
    26.                 float4 vertex : POSITION;
    27.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    28.             };
    29.  
    30.             struct v2f
    31.             {
    32.                 float4  pos              : SV_POSITION;
    33.                 float3  posWS            : TEXCOORD0;
    34.             };
    35.          
    36.             v2f vert (appdata_t v)
    37.             {
    38.                 v2f OUT;
    39.                 UNITY_SETUP_INSTANCE_ID(v);
    40.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
    41.                 OUT.pos = UnityObjectToClipPos(v.vertex);
    42.                 OUT.posWS = mul((float3x3)unity_ObjectToWorld, v.vertex.xyz);
    43.                 return OUT;
    44.             }
    45.             half4 frag (v2f IN) : SV_Target
    46.             {
    47.                 float3 normPos = normalize(IN.posWS);
    48.                 float val = smoothstep(-0.1, 0.2, normPos.y);
    49.                 half3 col = lerp(_Bottom, _TopOTheWorld, val);
    50.  
    51.                 return half4(col,1.0);
    52.             }
    53.             ENDCG
    54.         }
    55.     }
    56. }
    57.  
     
    Last edited: Aug 31, 2022
  7. Farl_Lee

    Farl_Lee

    Joined:
    Mar 20, 2014
    Posts:
    4
    I try the shader that @gamedevbill post. But have a error when building.

    Error building Player: Shader error in 'Skybox/Minimal': invalid subscript 'stereoTargetEyeIndex' at line ## (on gles3)


    Just insert the macros that in Unity single-pass custom shader document
    Here is the fixed shader:
    Code (CSharp):
    1. Shader "Skybox/GradientSkybox"
    2. {
    3.     Properties
    4.     {  
    5.         _TopOTheWorld("TopOTheWorld", Color) = (0.3, 0.6, 1, 1)
    6.         _Bottom("Bottom", Color) = (0.3,0.3,0.3, 1)
    7.     }
    8.     SubShader
    9.     {
    10.         Tags { "Queue"="Background" "RenderType"="Background" "PreviewType"="Skybox" }
    11.         Cull Off
    12.         ZWrite Off
    13.         Pass {
    14.             CGPROGRAM
    15.             #pragma vertex vert
    16.             #pragma fragment frag
    17.             #include "UnityCG.cginc"
    18.        
    19.             uniform half3 _TopOTheWorld;
    20.             uniform half3 _Bottom;
    21.             struct appdata_t
    22.             {
    23.                 float4 vertex : POSITION;
    24.  
    25.                 // https://docs.unity3d.com/2021.3/Documentation/Manual/SinglePassInstancing.html
    26.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    27.             };
    28.             struct v2f
    29.             {
    30.                 float4  pos              : SV_POSITION;
    31.                 float3  posWS            : TEXCOORD0;
    32.  
    33.                 // https://docs.unity3d.com/2021.3/Documentation/Manual/SinglePassInstancing.html
    34.                 UNITY_VERTEX_OUTPUT_STEREO
    35.             };
    36.        
    37.             v2f vert (appdata_t v)
    38.             {
    39.                 v2f OUT;
    40.  
    41.                 // https://docs.unity3d.com/2021.3/Documentation/Manual/SinglePassInstancing.html
    42.                 UNITY_SETUP_INSTANCE_ID(v);
    43.                 UNITY_INITIALIZE_OUTPUT(v2f, OUT);
    44.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
    45.              
    46.                 OUT.pos = UnityObjectToClipPos(v.vertex);
    47.                 OUT.posWS = mul((float3x3)unity_ObjectToWorld, v.vertex.xyz);
    48.                 return OUT;
    49.             }
    50.             half4 frag (v2f IN) : SV_Target
    51.             {
    52.                 float3 normPos = normalize(IN.posWS);
    53.                 float val = smoothstep(-0.1, 0.2, normPos.y);
    54.                 half3 col = lerp(_Bottom, _TopOTheWorld, val);
    55.                 return half4(col,1.0);
    56.             }
    57.             ENDCG
    58.         }
    59.     }
    60. }