Search Unity

Skybox shader

Discussion in 'Shaders' started by Filto, Apr 22, 2010.

  1. Filto

    Filto

    Joined:
    Mar 15, 2009
    Posts:
    713
    Hi. I want to write a shader that works the same as the skyboxshader. Full emission, isn't affected by light or fog. How do I do that? I tried to copy from the skyboxshader but it didn't work at all
     
  2. apple_motion

    apple_motion

    Joined:
    Jul 2, 2009
    Posts:
    169
    This is how I make the "skybox". In fact, the sky is not necessary as a box :p You could make it as any shape you want. My example is a sphere.

    The most important part of code are "CULL FRONT", "out float depth:DEPTH" and "depth = 1". That will make all object looking like always inside the skybox.


    (fog is enabled in the scene too)

    Code (csharp):
    1. Shader "BlueSky/Skybox"
    2. {
    3.     Properties
    4.     {
    5.         color_map   ("color_map", 2D) = "white" {}
    6.     }
    7.     SubShader
    8.     {
    9.         Pass
    10.         {
    11.             CULL FRONT
    12. CGPROGRAM //--------------
    13.  
    14. #pragma vertex   vertex_shader
    15. #pragma fragment fragment_shader
    16.  
    17. struct a2v
    18. {
    19.     float4 vertex   : POSITION;
    20.     float4 texcoord : TEXCOORD0;
    21. };
    22.  
    23. struct v2f
    24. {
    25.     float4 position : POSITION;
    26.     float4 texcoord : TEXCOORD1;
    27. };
    28.  
    29. v2f vertex_shader(a2v IN)
    30. {
    31.     v2f OUT;
    32.    
    33.     OUT.position = mul(glstate.matrix.mvp, IN.vertex);
    34.     OUT.texcoord     = IN.texcoord;
    35.     return OUT;
    36. }
    37.  
    38. void fragment_shader(   v2f IN,
    39.                         uniform sampler2D color_map,
    40.                         out float4 col:COLOR,
    41.                         out float depth:DEPTH )
    42. {
    43.     col.xyz = tex2D(color_map, IN.texcoord.xyz);
    44.     col.w = 1;
    45.     depth = 1;
    46. }
    47.  
    48. ENDCG //--------------
    49.         }  
    50.     }
    51. }
     
  3. Filto

    Filto

    Joined:
    Mar 15, 2009
    Posts:
    713
    Thanks alot for all the effort! I'll try to implement the shadercode in my project.
     
  4. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    Here's a simpler shader, which might be easier to understand if you don't know Cg:
    Code (csharp):
    1. Shader "Skybox" {
    2.     Properties {
    3.         _Color ("Main Color", Color) = (1,1,1,1)
    4.         _MainTex ("Base (RGB)", 2D) = "white" {}
    5.     }
    6.     SubShader {
    7.         Pass {
    8.             Tags {"Queue" = "1000"}
    9.             Cull Front
    10.             ZWrite Off
    11.             SetTexture [_MainTex] {
    12.                 ConstantColor [_Color]
    13.                 combine texture*constant
    14.             }
    15.         }
    16.     }
    17. }
     
  5. gonza

    gonza

    Joined:
    May 9, 2010
    Posts:
    9
    Thanks Daniel, but terrain not visible throw skydome mesh with you shader. How made this?
     
  6. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    Try changing 1000 to something less, like 500.
     
  7. gonza

    gonza

    Joined:
    May 9, 2010
    Posts:
    9
    Not work - 500/10/5/0 - terrain not vision