Unity Community


Results 1 to 2 of 2

  1. Posts
    35

    Opaque Alpha in a transparent surface shader (for Water)

    Hello. I use the free version of Unity. I thought the built-in water shaders in Unity free look a bit strange until it occurred to me that they must represent deep bodies of water viewed from far away, where as I'm trying to make a little pond or puddle.

    I'm trying to make a modified version of the water shader to incorporate some features that I would like. I don't really know the Unity shader language, but I was able to figure out how to add some surface properties by looking at the examples in the documentation and also this forum, and then just cobbling things together. So far I've added:

    transparency
    Specular highlights based on the bump map
    cube map reflection that is perturbed by the bump map.

    the problem that I am having is that I would like the specular highlights to be opaque when they are really bright. Right now they are as transparent as the rest of the water. Here is a web player example, so you can see what I mean.
    http://www.kellygallagherprojects.co...mos/water.html
    This uses the standard WASD, mouse look controls
    Step into the puddle and turn around to see the specular highlights.

    my code
    Code:  
    1. Shader "FX/Water (kdg) 4" {
    2. Properties {
    3.     _MainAlpha ("Tansparency", Range(0.0,1.0)) = 0.5
    4.     _WaveScale ("Wave scale", Range (0.02,0.15)) = .07
    5.     _ColorControl ("Reflective color (RGB) fresnel (A) ", 2D) = "" { }
    6.     _ColorControlCube ("Reflective color cube (RGB) fresnel (A) ", Cube) = "" { TexGen CubeReflect }
    7.     _BumpMap ("Waves Normalmap ", 2D) = "" { }
    8.     WaveSpeed ("Wave speed (map1 x,y; map2 x,y)", Vector) = (19,9,-16,-7)
    9.     _MainTex ("Fallback texture", 2D) = "" { }
    10.    
    11.     _Specular ("Specular", Range (0,1)) = .07
    12.     _Gloss ("Gloss", Range (0,128)) = 1
    13. }
    14.  
    15.    
    16. // -----------------------------------------------------------
    17. // Fragment program
    18.  
    19. Subshader {
    20.     Tags {"Queue"="Transparent" "RenderType"="Transparent"}
    21.    
    22. CGPROGRAM
    23.         #pragma surface surf SimpleSpecular alpha vertex:vert
    24.         //uniform float4 _horizonColor;
    25.  
    26.         uniform float _MainAlpha;
    27.         uniform float4 WaveSpeed;
    28.         uniform float _WaveScale;
    29.         uniform float4 _WaveOffset;
    30.        
    31.         uniform float _Specular;
    32.         uniform float _Gloss;
    33.        
    34.         sampler2D _BumpMap;
    35.         sampler2D _ColorControl;
    36.         samplerCUBE _ColorControlCube;
    37.        
    38.         #include "UnityCG.cginc"
    39.  
    40.         struct Input {
    41.              
    42.              half2 bumpuv0 : TEXCOORD0;
    43.             half2 bumpuv1 : TEXCOORD1;
    44.             half3 vDir : TEXCOORD2;
    45.             half3 worldRefl;
    46.             INTERNAL_DATA
    47.         };
    48.        
    49.         void vert (inout appdata_full v, out Input o) {
    50.             float4 s;
    51.  
    52.             // scroll bump waves
    53.             float4 temp;
    54.             temp.xyzw = v.vertex.xzxz * _WaveScale / unity_Scale.w + _WaveOffset;
    55.             o.bumpuv0 = temp.xy * float2(.4, .45);
    56.             o.bumpuv1 = temp.wz;
    57.  
    58.             // object space view direction
    59.             o.vDir = normalize( ObjSpaceViewDir(v.vertex) ).xzy;
    60.         }
    61.  
    62.         void surf (Input IN, inout SurfaceOutput o) {
    63.          
    64.             half3 bump1 = UnpackNormal(tex2D( _BumpMap, IN.bumpuv0 )).rgb;
    65.             half3 bump2 = UnpackNormal(tex2D( _BumpMap, IN.bumpuv1 )).rgb;
    66.             half3 bump = (bump1 + bump2) * 0.5;
    67.             o.Normal = bump;
    68.              
    69.             half3 worldRefl = WorldReflectionVector (IN, o.Normal);
    70.             half fresnel = dot( IN.vDir, bump);
    71.             half4 water = tex2D( _ColorControl, float2(fresnel,fresnel) );
    72.             half3 R = IN.vDir - ( 2 * dot(IN.vDir, o.Normal )) * o.Normal;
    73.             half4 reflcol = texCUBE (_ColorControlCube, R);
    74.          
    75.             half4 col;
    76.        
    77.             col.rgb = water.rgb;
    78.                         //make 0.5 into a slider eventually
    79.             col.rgb = lerp( reflcol.rgb, col.rgb, 0.5 );
    80.            
    81.             col.a = _MainAlpha;
    82.            
    83.             o.Albedo = 0.0;
    84.            
    85.             o.Alpha = col.a;
    86.             o.Emission = col;
    87.         }
    88.        
    89.         half4 LightingSimpleSpecular (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
    90.         {
    91.                     half3 h = normalize (lightDir + viewDir);
    92.  
    93.                     half diff = max (0, dot (s.Normal, lightDir));
    94.  
    95.                     float nh = max (0, dot (s.Normal, h));
    96.                     float spec = pow (nh, _Gloss);
    97.          
    98.                      half4 c;
    99.                     c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * spec) * (atten * 2) * _Specular;
    100.                     //I have alpha set to 1 for debugging. Really it doesn't seem to matter what I put here
    101.                     c.a = 1;
    102.                     return c;
    103.                     }
    104.         ENDCG
    105. }
    106.  
    107. // -----------------------------------------------------------
    108. //  Old cards (you can ignore anything below here)
    109.  
    110. // three texture, cubemaps
    111. Subshader {
    112.     //Tags { "RenderType"="Opaque" }
    113.     Tags {"Queue"="Transparent" "RenderType"="Transparent"}
    114.     Pass {
    115.         Color (0.5,0.5,0.5,0.5)
    116.         SetTexture [_MainTex] {
    117.             Matrix [_WaveMatrix]
    118.             combine texture * primary
    119.         }
    120.         SetTexture [_MainTex] {
    121.             Matrix [_WaveMatrix2]
    122.             combine texture * primary + previous
    123.         }
    124.         SetTexture [_ColorControlCube] {
    125.             combine texture +- previous, primary
    126.             Matrix [_Reflection]
    127.         }
    128.     }
    129. }
    130.  
    131. // dual texture, cubemaps
    132. Subshader {
    133.     //Tags { "RenderType"="Opaque" }
    134.     Tags {"Queue"="Transparent" "RenderType"="Transparent"}
    135.     Pass {
    136.         Color (0.5,0.5,0.5,0.5)
    137.         SetTexture [_MainTex] {
    138.             Matrix [_WaveMatrix]
    139.             combine texture
    140.         }
    141.         SetTexture [_ColorControlCube] {
    142.             combine texture +- previous, primary
    143.             Matrix [_Reflection]
    144.         }
    145.     }
    146. }
    147.  
    148. // single texture
    149. Subshader {
    150.     //Tags { "RenderType"="Opaque" }
    151.     Tags {"Queue"="Transparent" "RenderType"="Transparent"}
    152.     Pass {
    153.         Color (0.5,0.5,0.5,0)
    154.         SetTexture [_MainTex] {
    155.             Matrix [_WaveMatrix]
    156.             combine texture, primary
    157.         }
    158.     }
    159. }
    160.  
    161. }

    So my lighting model is called LightingSimpleSpecular, which comes right out of the example surface shader with the same name. I want to set my alpha to something like (c.r+c.g+c.b)/3 (the luminosity), but it doesn't seem to make any difference what I set the alpha to in the lighting model function. I've tried removing the alpha assignment in the surface function also but that doesn't seem to help. As far as I know I don't have access to that lighting info from the surface function either.

    Anyone know how to do this?


  2. Posts
    35
    Hello again. I did some searching around. It seems that someone else had the exact same problem before.

    http://forum.unity3d.com/threads/74397-Alpha-Blending-and-Surface-Shader-Lighting-Models?highlight=surface+alpha+specular


    So it looks like the output of the custom light model internally is intentionally masked to include RGB only (ColorMask RGB), if I understand this post correctly. The person who responded suggested opening the compiled shader and removing the directive which masks it. This code could then be used to make a shader with the desired effect. I think this solution would be difficult to work with, though.

    I was wondering if it was not possible to access this directive from just the regular surface shader code?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •