Search Unity

Transparent Shader UnderWater

Discussion in 'Shaders' started by Dreo, Dec 26, 2012.

  1. Dreo

    Dreo

    Joined:
    Nov 9, 2012
    Posts:
    24
    I have a problem with a custom shader I did.

    I created a shader for my Character Sprite. But when it is submerged in a transparent water , he is not rendered properly.

    SS example :
    https://www.dropbox.com/s/xpxxls4s79kx44i/errorunity.png

    My Shader:
    Code (csharp):
    1. Shader "Toon/Lighted with Alpha" {
    2.     Properties {
    3.         _Color ("Main Color", Color) = (0.5,0.5,0.5,1)
    4.         _MainTex ("Base (RGB)", 2D) = "white" {}
    5.         _Ramp ("Toon Ramp (RGB)", 2D) = "gray" {}
    6.     }
    7.  
    8.     SubShader {
    9.        Tags {"Queue"="Transparent" "RenderType"="Transparent"}
    10.        Blend SrcAlpha OneMinusSrcAlpha
    11.        LOD 200
    12.        
    13. CGPROGRAM
    14. #pragma surface surf ToonRamp
    15.  
    16. sampler2D _Ramp;
    17.  
    18. // custom lighting function that uses a texture ramp based
    19. // on angle between light direction and normal
    20. #pragma lighting ToonRamp exclude_path:prepass
    21. inline half4 LightingToonRamp (SurfaceOutput s, half3 lightDir, half atten)
    22. {
    23.     #ifndef USING_DIRECTIONAL_LIGHT
    24.     lightDir = normalize(lightDir);
    25.     #endif
    26.    
    27.     half d = dot (s.Normal, lightDir)*0.5 + 0.5;
    28.     half3 ramp = tex2D (_Ramp, float2(d,d)).rgb;
    29.    
    30.     half4 c;
    31.     c.rgb = s.Albedo * (_LightColor0.rgb*2) * ramp * (atten * 2);
    32.     c.a = s.Alpha;///-> test
    33.     return c;
    34. }
    35.  
    36.  
    37. sampler2D _MainTex;
    38. float4 _Color;
    39.  
    40. struct Input {
    41.     float2 uv_MainTex : TEXCOORD0;
    42. };
    43.  
    44. void surf (Input IN, inout SurfaceOutput o) {
    45.     half4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    46.     o.Albedo = c.rgb;
    47.     o.Alpha = c.a;
    48. }
    49. ENDCG
    50.  
    51.     }
    52.  
    53.     Fallback "Diffuse"
    54. }
    55.  
    I wish that when it is submerged rendering equals the cube.
    Does anyone have any idea how to fix?
     
  2. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,880
    change your tag to
    Tags {"Queue"="Transparent-10" "RenderType"="Transparent"}

    or

    Tags {"Queue"="Transparent+10" "RenderType"="Transparent"}

    or some other value until it works.
     
    Last edited: Dec 26, 2012
  3. Dreo

    Dreo

    Joined:
    Nov 9, 2012
    Posts:
    24
    https://www.dropbox.com/s/hwlqg4p2xg17qs5/errorunity2.png

    Any negative number works (tested several -10 ~ -200).
    But this bug appears at the top.
     
  4. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,880
    I dont see what kind of bug it is in the photo.
     
  5. Dreo

    Dreo

    Joined:
    Nov 9, 2012
    Posts:
    24
    See you behind the character's head, where not is submerged in water, he does not rendered the water, only the grass.

    This is the part where is opaque with grass texture should be transparent.

    In Original Sprite, and before putting the -10, was transparent.
    Ex (without '-10'): https://www.dropbox.com/s/xpxxls4s79kx44i/errorunity.png
     
    Last edited: Dec 27, 2012
  6. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,880
    I dont see any grass in that picture. Just a head a cube and probably the water.
    Drawing order is Opaque.1.2.3...etc -> Transparent cutout.1.2.3...etc ->Transparent.1.2.3...etc

    Try zwrite off on your character model with the same transparency level as your water.
     
  7. Dreo

    Dreo

    Joined:
    Nov 9, 2012
    Posts:
    24
    Sorry.
    The grass in the image1 : https://www.dropbox.com/s/hwlqg4p2xg17qs5/errorunity2.png

    the image2(https://www.dropbox.com/s/xpxxls4s79kx44i/errorunity.png) is only for you see the original transparence of character.

    Dont work the zwrite off. ( it is as if he had always submerged even when it is surface )


    **i understand the sequence. The sequence looks like it's right, water should render after. But it is not rendered in the transparent part of the character sprite.
    :S
     
    Last edited: Dec 27, 2012
  8. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,880
    eh, i didnt check your shader before.
    remove the blend srcalpha oneminussrcalpha from your shader
    and change your pragma state to
    #pragma surface surf ToonRamp alpha
     
  9. Dreo

    Dreo

    Joined:
    Nov 9, 2012
    Posts:
    24
    CODE NOW:

    Code (csharp):
    1. Shader "Toon/Lighted with Alpha" {
    2.     Properties {
    3.         _Color ("Main Color", Color) = (0.5,0.5,0.5,1)
    4.         _MainTex ("Base (RGB)", 2D) = "white" {}
    5.         _Ramp ("Toon Ramp (RGB)", 2D) = "gray" {}
    6.     }
    7.  
    8.     SubShader {
    9.       Tags {"Queue"="Transparent-10" "RenderType"="Transparent"}      
    10.       LOD 200
    11.        
    12. CGPROGRAM
    13. #pragma surface surf ToonRamp alpha
    14.  
    15. sampler2D _Ramp;
    16.  
    17. // custom lighting function that uses a texture ramp based
    18. // on angle between light direction and normal
    19. #pragma lighting ToonRamp exclude_path:prepass
    20. inline half4 LightingToonRamp (SurfaceOutput s, half3 lightDir, half atten)
    21. {
    22.     #ifndef USING_DIRECTIONAL_LIGHT
    23.     lightDir = normalize(lightDir);
    24.     #endif
    25.    
    26.     half d = dot (s.Normal, lightDir)*0.5 + 0.5;
    27.     half3 ramp = tex2D (_Ramp, float2(d,d)).rgb;
    28.    
    29.     half4 c;
    30.     c.rgb = s.Albedo * (_LightColor0.rgb*2) * ramp * (atten * 2);
    31.     c.a = s.Alpha;///-> test
    32.     return c;
    33. }
    34.  
    35.  
    36. sampler2D _MainTex;
    37. float4 _Color;
    38.  
    39. struct Input {
    40.     float2 uv_MainTex : TEXCOORD0;
    41. };
    42.  
    43. void surf (Input IN, inout SurfaceOutput o) {
    44.     half4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    45.     o.Albedo = c.rgb;
    46.     o.Alpha = c.a;
    47. }
    48. ENDCG
    49.  
    50.     }
    51.  
    52.     Fallback "Diffuse"
    53. }
    54.  

    same problem as zwrite off.
    it is as if he had always submerged even when it is surface.


    https://www.dropbox.com/s/82c1ac3hpxnwttl/error3.png
    In this example the character is not submerged but depending on the camera position/angle, the water is rendered over him.

    ***I tried to modify the number "" Queue "=" Transparent-10 "." also without success.

    Thanks for the help!
     
  10. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,880
    Just speculating without seeing the actual scene and all your files used in it.
    Is your character mesh a plane object which rotates towards the camera all the time?
    If so, when you look at him from an angle above, obviously the plane object rotates down under the water mesh and it looks submerged.

    If that is not the case, i need to see a sample scene.
     
  11. Dreo

    Dreo

    Joined:
    Nov 9, 2012
    Posts:
    24
    Understand.
    Is not the case my friend.
    I'm packing the scene, but found a problem. You have the 2d Toolkit?
     
  12. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,880
    No i dont have it, and if you are using a commercial product like 2d toolkit, you should ask to its owner about your problem.