Search Unity

Fog

Discussion in 'Shaders' started by Shuttle, Mar 1, 2015.

  1. Shuttle

    Shuttle

    Joined:
    Dec 25, 2014
    Posts:
    4
    how to combine these two shader

    Code (JavaScript):
    1.  
    2.  
    3. Shader "fluids"{
    4. Properties {
    5.   _Color ("Main Color", Color) = (1,1,1,1)
    6.   _WaveScale ("Wave scale", Range (0.0002,0.0007)) = .07
    7.   _ColorControl ("Reflective color (RGB) fresnel (A) ", 2D) = "" { }
    8.   _BumpMap ("Waves Normalmap ", 2D) = "" { }
    9.   WaveSpeed ("Wave speed (map1 x,y; map2 x,y)", Vector) = (19,9,-16,-7)
    10.   _ColorControlCube ("Reflective color cube (RGB) fresnel (A) ", Cube) = "" { TexGen CubeReflect }
    11.   _MainTex ("Fallback texture", 2D) = "" { }
    12. }
    13. CGINCLUDE
    14. // -----------------------------------------------------------
    15. // This section is included in all program sections below
    16. #include "UnityCG.cginc"
    17. uniform float4 _Color;
    18. uniform float4 WaveSpeed;
    19. uniform float _WaveScale;
    20. uniform float4 _WaveOffset;
    21. struct appdata {
    22.   float4 vertex : POSITION;
    23.   float3 normal : NORMAL;
    24. };
    25. struct v2f {
    26.   float4 pos : SV_POSITION;
    27.   float2 bumpuv[2] : TEXCOORD0;
    28.   float3 viewDir : TEXCOORD2;
    29. };
    30. v2f vert(appdata v)
    31. {
    32.   v2f o;
    33.   float4 s;
    34.   o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
    35.   // scroll bump waves
    36.   float4 temp;
    37.   temp.xyzw = v.vertex.xzxz * _WaveScale / unity_Scale.w + _WaveOffset;
    38.   o.bumpuv[0] = temp.xy * float2(.5, .5);
    39.   o.bumpuv[1] = temp.wz;
    40.   // object space view direction
    41.   o.viewDir.xzy = normalize( ObjSpaceViewDir(v.vertex) );
    42.   return o;
    43. }
    44. ENDCG
    45.    
    46. // -----------------------------------------------------------
    47. // Fragment program
    48. Subshader {
    49.   Alphatest Greater 0
    50.   Tags {Queue=Transparent}
    51.   Blend SrcAlpha OneMinusSrcAlpha
    52.   Pass {
    53. CGPROGRAM
    54. #pragma vertex vert
    55. #pragma fragment frag
    56. #pragma fragmentoption ARB_precision_hint_fastest
    57. sampler2D _BumpMap;
    58. sampler2D _ColorControl;
    59. half4 frag( v2f i ) : COLOR
    60. {
    61.   half3 bump1 = UnpackNormal(tex2D( _BumpMap, i.bumpuv[0] )).rgb;
    62.   half3 bump2 = UnpackNormal(tex2D( _BumpMap, i.bumpuv[1] )).rgb;
    63.   half3 bump = (bump1 + bump2) * 0.5;
    64.    
    65.   half fresnel = dot( i.viewDir, bump );
    66.   half4 water = tex2D( _ColorControl, float2(fresnel,fresnel) );
    67.    
    68.   half4 col;
    69.   col.rgb = water.rgb;
    70.   col.a = _Color.a;
    71.   return col;
    72. }
    73. ENDCG
    74.   }
    75. }
    76.   Fallback "Diffuse"
    77.   }

    &


    Code (JavaScript):
    1. Shader "Fog"{
    2.   Properties {
    3.   _MainTex ("Base (RGB)", 2D) = "white" {}
    4.   }
    5.   SubShader {
    6.    
    7.   Alphatest Greater 0
    8.   Tags {Queue=Transparent}
    9.   Blend SrcAlpha OneMinusSrcAlpha
    10.    
    11.    
    12.    
    13.   CGPROGRAM
    14.   #pragma surface surf Lambert vertex:myvert
    15.   sampler2D _MainTex;
    16.   uniform half4 unity_FogStart;
    17.   uniform half4 unity_FogEnd;
    18.   struct Input {
    19.   float2 uv_MainTex;
    20.   half fog;
    21.   };
    22.    
    23.   void myvert (inout appdata_full v, out Input data) {
    24.   UNITY_INITIALIZE_OUTPUT(Input,data);
    25.   float pos = length(mul (UNITY_MATRIX_MV, v.vertex).xyz);
    26.   float diff = unity_FogEnd.x - unity_FogStart.x;
    27.   float invDiff = 1.0f / diff;
    28.   data.fog = clamp ((unity_FogEnd.x - pos) * invDiff, 0.0, 1.0);
    29.   }
    30.   void surf (Input IN, inout SurfaceOutput o) {
    31.   half4 c = tex2D (_MainTex, IN.uv_MainTex);
    32.   o.Albedo = c.rgb;
    33.   o.Alpha = lerp (0, 1, IN.fog);
    34.   }
    35.   ENDCG
    36.   }
    37.   FallBack "Diffuse"
    38. }
    39.  
     
  2. Shuttle

    Shuttle

    Joined:
    Dec 25, 2014
    Posts:
    4
    Code (JavaScript):
    1.     CGPROGRAM
    2.     #pragma surface surf Lambert vertex:myvert
    3.  
    4.     sampler2D _MainTex;
    5.     uniform half4 unity_FogStart;
    6.     uniform half4 unity_FogEnd;
    7.  
    8.     struct Input {
    9.       float2 uv_MainTex;
    10.       half fog;
    11.     };
    12.  
    13.    
    14.     void myvert (inout appdata_full v, out Input data) {
    15.       UNITY_INITIALIZE_OUTPUT(Input,data);
    16.       float pos = length(mul (UNITY_MATRIX_MV, v.vertex).xyz);
    17.       float diff = unity_FogEnd.x - unity_FogStart.x;
    18.       float invDiff = 1.0f / diff;
    19.       data.fog = clamp ((unity_FogEnd.x - pos) * invDiff, 0.0, 1.0);
    20.     }
    21.  
    22.     void surf (Input IN, inout SurfaceOutput o) {
    23.       half4 c = tex2D (_MainTex, IN.uv_MainTex);
    24.       o.Albedo = c.rgb;
    25.       o.Alpha = lerp (0, 1, IN.fog);
    26.     }
    27.     ENDCG
    need to separate this part for pass shader, also