Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Cloud shader/skybox

Discussion in 'Shaders' started by DylanCPL, Oct 4, 2011.

  1. DylanCPL

    DylanCPL

    Joined:
    Jul 14, 2011
    Posts:
    98
    Hi, I have no idea how shaders work, so I'm not entirely sure if this is possible. I have used quite a few different day/night systems, weather systems and cloud systems. The only problem is that since most of them require me to place a sphere surround my terrain, and my terrain is pretty big, I have to set the clipping plains max to a very large number, which slows down my game, or have had to use particle clouds (which look great, but are even slower) . So what I'm wondering is: is it possible to make a sky-sphere (as opposed to a skybox (to make it look more realistic)), that works like a skybox (no huge object surrounding the terrain, showing up behind objects yet looking infinitly far away ), except it will show up in front of the first skybox (optional), have moving textures, be able to have different colours and brightness (optional), and if possible, look more voluminus than a flat cloud texture (bump maps?)? Could anyone point me in the right direction? Thanks!
     
  2. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
  3. DylanCPL

    DylanCPL

    Joined:
    Jul 14, 2011
    Posts:
    98
    The codes given don't work; the first one constantly generates errors, and the second one, you can't see the terrain behind it.
     
  4. Ippokratis

    Ippokratis

    Joined:
    Oct 13, 2008
    Posts:
    1,521
    Hi,
    I tweaked the shader from apple motion, it works now.
    I make a sphere and place it in the same position as camera.
    If camera moves, create a script that makes sphere position to follow camera position.
    Hope that helps.
    Code (csharp):
    1.  
    2. Shader "Skybox"
    3. {
    4.     Properties
    5.     {
    6.         color_map     ("color_map", 2D) = "white" {}
    7.     }
    8.     SubShader
    9.     {
    10.         Pass
    11.         {
    12.             CULL FRONT
    13.             ZWrite Off
    14. CGPROGRAM
    15.  
    16.     #pragma vertex vert
    17.     #pragma fragment frag
    18.    
    19.     struct a2v
    20.     {
    21.         float4 vertex    : POSITION;
    22.         float4 texcoord    : TEXCOORD0;
    23.     };
    24.    
    25.     struct v2f
    26.     {
    27.         float4 position : POSITION;
    28.         float4 texcoord    : TEXCOORD0;
    29.     };
    30.    
    31.     v2f vert(a2v IN)
    32.     {
    33.         v2f OUT;
    34.        
    35.         OUT.position = mul(UNITY_MATRIX_MVP, IN.vertex);
    36.         OUT.texcoord = IN.texcoord;
    37.         return OUT;
    38.     }
    39.    
    40.      sampler2D color_map;
    41.     float4 col;
    42.    
    43.    
    44.     float4 frag( v2f IN ): COLOR
    45.     {
    46.         col.xyz = tex2D(color_map, IN.texcoord.xy);
    47.         col.w = 1;
    48.         //depth = 1;
    49.         return col;
    50.     }
    51.  
    52. ENDCG
    53.         }    
    54.     }
    55. }
    56.  
     
  5. DylanCPL

    DylanCPL

    Joined:
    Jul 14, 2011
    Posts:
    98
    Hi, thank you for your time, but I still cant see anything behind it, and if there's nothing behind it, it goes invisible, where as it should be the other way around.
     
  6. Ippokratis

    Ippokratis

    Joined:
    Oct 13, 2008
    Posts:
    1,521
    $Screen shot 2011-10-07 at 12.13.29 AM.png
    Make it slightly smaller than the desired clip plane size ?
    [Edit]
    Ok, I understand it now, you are right.
     
    Last edited: Oct 6, 2011
  7. Ippokratis

    Ippokratis

    Joined:
    Oct 13, 2008
    Posts:
    1,521
    Ok, this works on my mac.
    Code (csharp):
    1.  
    2. Shader "Skybox"
    3. {
    4.     Properties
    5.     {
    6.         color_map     ("color_map", 2D) = "white" {}
    7.     }
    8.     SubShader
    9.     {
    10.         Pass
    11.         {
    12.             CULL FRONT
    13.             ZWrite On
    14. CGPROGRAM
    15.  
    16.     #pragma vertex vert
    17.     #pragma fragment frag
    18.    
    19.     struct a2v
    20.     {
    21.         float4 vertex    : POSITION;
    22.         float4 texcoord    : TEXCOORD0;
    23.     };
    24.    
    25.     struct v2f
    26.     {
    27.         float4 position : POSITION;
    28.         float4 texcoord    : TEXCOORD0;
    29.     };
    30.    
    31.     v2f vert(a2v IN)
    32.     {
    33.         v2f OUT;
    34.        
    35.         OUT.position = mul(UNITY_MATRIX_MVP, IN.vertex);
    36.         OUT.texcoord = IN.texcoord;
    37.         return OUT;
    38.     }
    39.    
    40.     void frag( v2f IN, sampler2D color_map, out float4 col:COLOR, out float depth:DEPTH)
    41.     {
    42.         col.xyz = tex2D(color_map, IN.texcoord.xy);
    43.         col.w = 1;
    44.         depth = 1;
    45.        
    46.     }
    47.  
    48. ENDCG
    49.         }    
    50.     }
    51. }
    52.  
    $Screen shot 2011-10-07 at 12.51.35 AM.png
     
    noanoa and FlyingOstriche like this.
  8. DylanCPL

    DylanCPL

    Joined:
    Jul 14, 2011
    Posts:
    98
    I'm getting the following errors:
    Shader error in 'Skybox': D3D shader assembly failed with: (10): error X2022: scalar registers cannot be masked
    Shader warning in 'Skybox': No subshaders can run on this graphics card
    Material doesn't have a texture property 'color_map'

    You say it's running on your mac, I have a windows laptop, do you think it's a apple to microsoft problem? Or the fact that I'm running a laptop rather than a destop? Either way, thank you for your help.
     
  9. Ippokratis

    Ippokratis

    Joined:
    Oct 13, 2008
    Posts:
    1,521
    Hi,
    I was unsure on Iphone compatibility, not Windows.
    I tried it with Graphics emulation Shader model 2 and 3. It fails at Shader model 1 ( old cards ).
    If you do not mind, try it at another computer too.
    Also,
    Perhaps it could be solved as
    Code (csharp):
    1.  
    2. Shader "Skybox"
    3. {
    4.     Properties
    5.     {
    6.         color_map     ("color_map", 2D) = "white" {}
    7.     }
    8.     SubShader
    9.     {
    10.         Pass
    11.         {
    12.             CULL FRONT
    13.             ZWrite On
    14. CGPROGRAM
    15.  
    16.     #pragma vertex vert
    17.     #pragma fragment frag
    18.    
    19.     struct a2v
    20.     {
    21.         float4 vertex    : POSITION;
    22.         float4 texcoord    : TEXCOORD0;
    23.     };
    24.    
    25.     struct v2f
    26.     {
    27.         float4 position : POSITION;
    28.         float4 texcoord    : TEXCOORD0;
    29.     };
    30.    
    31.     v2f vert(a2v IN)
    32.     {
    33.         v2f OUT;
    34.        
    35.         OUT.position = mul(UNITY_MATRIX_MVP, IN.vertex);
    36.         OUT.texcoord = IN.texcoord;
    37.         return OUT;
    38.     }
    39.    
    40.     void frag( v2f IN, sampler2D color_map, out float4 col:COLOR, out float depth:DEPTH)
    41.     {
    42.         float4 color1;
    43.         color1 = tex2D(color_map, IN.texcoord.xy);
    44.         color1.w = 1;
    45.         col = color1;
    46.         depth = 1;
    47.        
    48.     }
    49.  
    50. ENDCG
    51.         }    
    52.     }
    53. }
    54.  
     
  10. DylanCPL

    DylanCPL

    Joined:
    Jul 14, 2011
    Posts:
    98
    Hmm, I still get:

    Material doesn't have a texture property 'color_map'

    I'll be able to transfer this to my desktop in about two days, I'll get back to you with the results. Thank you.
     
  11. DylanCPL

    DylanCPL

    Joined:
    Jul 14, 2011
    Posts:
    98
    Sorry for coming back late, but even on my desktop, I'm still getting the same error
    EDIT: by same error, I meant one in before post, see next post.
     
    Last edited: Oct 20, 2011
  12. DylanCPL

    DylanCPL

    Joined:
    Jul 14, 2011
    Posts:
    98
    Sorry, it's:
    Shader error in 'Skybox': D3D shader assembly failed with: (10): error X2022: scalar registers cannot be masked
    Shader warning in 'Skybox': No subshaders can run on this graphics card
     
  13. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    I don't think modifying the fragment depth is a good idea in this situation. It won't work on older graphics cards (or any Macs), and I don't think it will get the effect you want, anyway.

    Try this. All it does is draw early without writing to the Z buffer. Everything else will be drawn in front of it.

    Code (csharp):
    1. Shader "Custom/Background" {
    2.     Properties {
    3.         _MainTex ("Base (RGB)", 2D) = "white" {}
    4.     }
    5.     SubShader {
    6.         Tags { "RenderQueue"="Background" }
    7.        
    8.         Pass {
    9.             ZWrite Off
    10.             SetTexture [_MainTex] {}
    11.         }
    12.     }
    13. }
    14.  
     
  14. DylanCPL

    DylanCPL

    Joined:
    Jul 14, 2011
    Posts:
    98
    It's now, although not giving any errors, not visible from the inside, and only draws in front of things;
    I make the main texture an orange texture, assign the material to a sphere, and put the sphere in front (it's not visible from the inside) of the camera.
    The terrain, along with any other object is orange, but any place where there is no object (empty space; where the skybox is visible), it's transparent.

    I may have found a different approach.
    I attached a water material under the rendersettings skybox material, and it actually made a moving water affect, infinitley far away that did not get culled. Although, when I put some materials in front of it, it makes this stange affect where anything that is visible is drawn on there, without getting erased. Example: a cube flies across the screen. There is now a trail that does not disappear until something else is drawn on top of it. Would I be able to create a procedural cloud shader, attach it to a material, put it as the skybox material without getting that strange effect?
     
    Last edited: Oct 21, 2011
  15. Rook3D

    Rook3D

    Joined:
    Sep 27, 2011
    Posts:
    19
    For any other shader novices interested in a shader for this purpose, I have hacked the default skybox shader to just use a single texture ( as the shaders posted here were problematic ) :


    Code (csharp):
    1.  
    2. Shader "RenderFX/Skybox Single Texture" {
    3. Properties {
    4.     _Tint ("Tint Color", Color) = (.5, .5, .5, .5)
    5.     _FrontTex ("Front (+Z)", 2D) = "white" {}
    6.  
    7. }
    8.  
    9. SubShader {
    10.     Tags { "Queue"="Background" "RenderType"="Background" }
    11.     Cull Off ZWrite Off Fog { Mode Off }
    12.    
    13.     CGINCLUDE
    14.     #include "UnityCG.cginc"
    15.  
    16.     fixed4 _Tint;
    17.    
    18.     struct appdata_t {
    19.         float4 vertex : POSITION;
    20.         float2 texcoord : TEXCOORD0;
    21.     };
    22.     struct v2f {
    23.         float4 vertex : POSITION;
    24.         float2 texcoord : TEXCOORD0;
    25.     };
    26.     v2f vert (appdata_t v)
    27.     {
    28.         v2f o;
    29.         o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    30.         o.texcoord = v.texcoord;
    31.         return o;
    32.     }
    33.     fixed4 skybox_frag (v2f i, sampler2D smp)
    34.     {
    35.         fixed4 tex = tex2D (smp, i.texcoord);
    36.         fixed4 col;
    37.         col.rgb = tex.rgb + _Tint.rgb - 0.5;
    38.         col.a = tex.a * _Tint.a;
    39.         return col;
    40.     }
    41.     ENDCG
    42.    
    43.     Pass {
    44.         CGPROGRAM
    45.         #pragma vertex vert
    46.         #pragma fragment frag
    47.         #pragma fragmentoption ARB_precision_hint_fastest
    48.         sampler2D _FrontTex;
    49.         fixed4 frag (v2f i) : COLOR { return skybox_frag(i,_FrontTex); }
    50.         ENDCG
    51.     }
    52.  
    53. }  
    54.  
    55. SubShader {
    56.  
    57.     Tags { "Queue"="Background" "RenderType"="Background" }
    58.     Cull Off ZWrite Off Fog { Mode Off }
    59.     Color [_Tint]
    60.     Pass {
    61.         SetTexture [_FrontTex] { combine texture +- primary, texture * primary }
    62.     }
    63.  
    64. }
    65.  
    66. }
    67.  
    68.  

    Note:

    - You will need to make sure the normals of your skybox mesh are pointing inward ( may have been one of the previous poster's problems ).

    - Any existing Skyboxes will need to removed / unset ( Render Settings / Camera ).

    - You will likely need to attach a script to your camera to move the Skybox mesh along with it, something like:

    Code (csharp):
    1.  
    2.  
    3. void LateUpdate ( ) {
    4.  
    5.   Skybox.transform.position = transform.position;
    6.    
    7. }
    8.  
    9.  
    ( or you can child the skybox object to the camera, and then reset its orientation ( I haven't tested this myself though ) ).
     
  16. mradfo21

    mradfo21

    Joined:
    May 16, 2013
    Posts:
    194
    I'm unable to write depth in OSX, according to Unity they deny you this ability for what to me seems to be no reason. Did you actually get depth writing per fragment working? outputting outfloatdepth:DEPTH

    seems to just turn whatever mesh invisible.. did you really get depth writing working per fragment?


    heres the bullshit:

    • On OpenGL (Mac OS X), depth texture is the native OpenGL depth buffer (see ARB_depth_texture).
      • Graphics card must support OpenGL 1.4 or ARB_depth_texture extension.
      • Depth texture corresponds to Z buffer contents that are rendered, it does not use the result from the fragment program.