Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Refraction example

Discussion in 'Shaders' started by Toack, Feb 19, 2011.

  1. Toack

    Toack

    Joined:
    Dec 18, 2006
    Posts:
    88
    Please please please pleeeeeeease add an example to get simple refraction on the surface examples page: Examples

    Pleeease
     
  2. ScienceFiction

    ScienceFiction

    Joined:
    May 28, 2008
    Posts:
    392
  3. Toack

    Toack

    Joined:
    Dec 18, 2006
    Posts:
    88
    Yeah thanks! but it's implemented on a fragment shader and I need it to be on surface shader because I need to add this functionality to another surface shader of my own that does another stuff, not only refraction...

    Pleeeeease
     
  4. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,703
    If you have pro, you should check the Glass shader.
     
  5. Toack

    Toack

    Joined:
    Dec 18, 2006
    Posts:
    88
    Nice tip Aubergine but still it's a vert + fragment shader and I need it on surface shader, I'll try to integrate that though, thanks.
     
  6. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,703
    Here is the same glass shader with some extra stuff as a surface shader, the idea is the same:

    Code (csharp):
    1. Shader "Test" {
    2. Properties {
    3. _Color ("Main Color", Color) = (1,1,1,1)
    4. _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
    5. _Shininess ("Shininess", Range (0.03, 1)) = 0.3
    6. _MainTex ("Base (RGB)", 2D) = "white" {}
    7. _BumpMap ("Normalmap", 2D) = "bump" {}
    8. _DistAmt ("Distortion", range (0,128)) = 10
    9. }
    10. SubShader {
    11. GrabPass { }
    12.  
    13. Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Opaque" }
    14. LOD 200
    15.  
    16. CGPROGRAM
    17. #pragma exclude_renderers gles
    18. #pragma vertex vert
    19. #pragma surface surf BlinnPhong
    20. #include "UnityCG.cginc"
    21.  
    22. float4 _Color;
    23. float _Shininess;
    24. sampler2D _MainTex;
    25. sampler2D _BumpMap;
    26. sampler2D _GrabTexture;
    27. float _DistAmt;
    28. float4 _GrabTexture_TexelSize;
    29.  
    30. struct Input {
    31. float2 uv_MainTex;
    32. float2 uv_BumpMap;
    33. float4 proj : TEXCOORD;
    34. };
    35.  
    36. void vert (inout appdata_full v, out Input o) {
    37. float4 oPos = mul(UNITY_MATRIX_MVP, v.vertex);
    38. #if UNITY_UV_STARTS_AT_TOP
    39. float scale = -1.0;
    40. #else
    41. float scale = 1.0;
    42. #endif
    43. o.proj.xy = (float2(oPos.x, oPos.y*scale) + oPos.w) * 0.5;
    44. o.proj.zw = oPos.zw;
    45. }
    46.  
    47. void surf (Input IN, inout SurfaceOutput o) {
    48. half4 nor = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    49.  
    50. float2 offset = nor * _DistAmt * _GrabTexture_TexelSize.xy;
    51. IN.proj.xy = offset * IN.proj.z + IN.proj.xy;
    52. half4 col = tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(IN.proj));
    53.  
    54. half4 tex = tex2D(_MainTex, IN.uv_MainTex);
    55. o.Albedo = tex.rgb * _Color.rgb * col.rgb;
    56. o.Normal = nor.rgb;
    57. o.Specular = _Shininess;
    58. o.Gloss = tex.a;
    59. o.Alpha = tex.a * _Color.a;
    60. }
    61. ENDCG
    62. }
    63. FallBack "Diffuse"
    64. }
     
  7. Toack

    Toack

    Joined:
    Dec 18, 2006
    Posts:
    88
    Thanks! I'll test it as soon as I can :)
     
  8. kondrup

    kondrup

    Joined:
    Apr 23, 2009
    Posts:
    205
    Hmm.. I tried it but it did not work? - just pink.
     
  9. Toack

    Toack

    Joined:
    Dec 18, 2006
    Posts:
    88
    It was not what I was looking for.

    I needed a shader to refract also with the objects normals, this one needs a normal map and refract only the normal map.

    Luckily I founded another shaders that I combined to achieve what I want.

    The tips were great though, thanks very much!!

    Cheers,
     
  10. Toack

    Toack

    Joined:
    Dec 18, 2006
    Posts:
    88
    @kondrup, it works for me, Unity 3 pro.
     
  11. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,703
    For Unity3.2:

    Code (csharp):
    1. Shader "MyShaders/Glass-Seethrough" {
    2.     Properties {
    3.         _Color ("Main Color", Color) = (1,1,1,1)
    4.         _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
    5.         _Shininess ("Shininess", Range (0.03, 1)) = 0.3
    6.         _MainTex ("Base (RGB)", 2D) = "white" {}
    7.         _BumpMap ("Normalmap", 2D) = "bump" {}
    8.         _DistAmt  ("Distortion", range (0,128)) = 10
    9.     }
    10.     SubShader {
    11.         GrabPass { }
    12.        
    13.         Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Opaque" }
    14.         LOD 200
    15.        
    16.         CGPROGRAM
    17.         #pragma exclude_renderers gles
    18.         #pragma vertex vert
    19.         #pragma surface surf BlinnPhong
    20.         #include "UnityCG.cginc"
    21.  
    22.         float4 _Color;
    23.         float _Shininess;
    24.         sampler2D _MainTex;
    25.         sampler2D _BumpMap;
    26.         sampler2D _GrabTexture;
    27.         float _DistAmt;
    28.         float4 _GrabTexture_TexelSize;
    29.  
    30.         struct Input {
    31.             float2 uv_MainTex;
    32.             float2 uv_BumpMap;
    33.             float4 proj : TEXCOORD;
    34.         };
    35.        
    36.         void vert (inout appdata_full v, out Input o) {
    37.             float4 oPos = mul(UNITY_MATRIX_MVP, v.vertex);
    38.             #if UNITY_UV_STARTS_AT_TOP
    39.                 float scale = -1.0;
    40.             #else
    41.                 float scale = 1.0;
    42.             #endif
    43.             o.proj.xy = (float2(oPos.x, oPos.y*scale) + oPos.w) * 0.5;
    44.             o.proj.zw = oPos.zw;
    45.         }
    46.  
    47.         void surf (Input IN, inout SurfaceOutput o) {
    48.             half3 nor = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    49.            
    50.             float2 offset = nor * _DistAmt * _GrabTexture_TexelSize.xy;
    51.             IN.proj.xy = offset * IN.proj.z + IN.proj.xy;
    52.             half4 col = tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(IN.proj));
    53.            
    54.             half4 tex = tex2D(_MainTex, IN.uv_MainTex);
    55.             o.Albedo = tex.rgb * _Color.rgb * col.rgb;
    56.             o.Normal = nor.rgb;
    57.             o.Specular = _Shininess;
    58.             o.Gloss = tex.a;
    59.             o.Alpha = tex.a * _Color.a;
    60.         }
    61.         ENDCG
    62.     }
    63.     FallBack "Diffuse"
    64. }
     
  12. Frank Oz

    Frank Oz

    Joined:
    Oct 13, 2010
    Posts:
    1,561
    There's something very wrong with the 3.2 version of that shader Aubergine. Placing it on an object seems to prevent it moving, it also doesn't seem to play correctly with the lighting either. I have no idea what could be wrong with it though, don't know the shader language well enough at all.


    Edit: Sorry, my bad, the inability to move it wasn't due to the shader (didn't make sense either), was cause I had it set to static.... lol
     
    Last edited: Feb 26, 2011
  13. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,703
    So, it works for you now?
     
  14. Frank Oz

    Frank Oz

    Joined:
    Oct 13, 2010
    Posts:
    1,561
    Well the moving bit yeah, that was entirely my fault lol

    But the shader itself. It's not transparent I guess you could say, it only appears to be in direct light, yet the rest is black rather than transparent. What I don't get is why a transparent, reflective shader with specularity and distortion seems to be next to impossible now, yet was entirely possible in the older setup. Though it is a small price to pay for everything else I guess. :D
     
  15. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,703
    It is not transparent literally but it is actually transparent. It grabs the scene behind it with a grabpass. Sooo you see whatever is behind it. Sooo its transparent. I posted another glass shader somewhere, maybe you should search for it as it also does fake refraction and transparent as you want.
     
  16. Frank Oz

    Frank Oz

    Joined:
    Oct 13, 2010
    Posts:
    1,561
    It's not transparent though, it blocks light. Darker parts are shaded when they shouldn't be.

    Anyway this river water shader in Unity itself would work perfectly for what a lot of folk are wanting, it has refraction/distortion, it's transparent, it has reflection. The problem is it also has animation, and an annoying fade to it when near other objects (works fine for it's original purpose, but not for example, glass). I've stopped it animating, but am now stuck on how to remove the shore/fade effect, and getting it to accept the textures color and not the vertex color.

    Code (csharp):
    1.  
    2. Shader "Reflective/River Water" {
    3. Properties {
    4.     _Color ("Main Color", Color) = (1,1,1,1)
    5.     _SpecColor ("Specular Color", Color) = (0.5,0.5,0.5,1)
    6.     _Shininess ("Shininess", Range (0.01, 1)) = 0.078125
    7.     _ChromaticDispersion ("_ChromaticDispersion", Range(0.0,4.0)) = 0.1
    8.     _Refraction ("Refraction", Range (0.00, 100.0)) = 1.0
    9.     _ReflToRefrExponent ("_ReflToRefrExponent", Range(0.00,4.00)) = 1.0
    10.     _ReflectColor ("Reflection Color", Color) = (1,1,1,0.5)
    11.     _BumpReflectionStr ("_BumpReflectionStr", Range(0.00,1.00)) = 0.5
    12.     _MainTex ("Base (RGB) RefStrGloss (A)", 2D) = "white" {}
    13.     _ReflectionTex ("_ReflectionTex", CUBE) = "white" {}
    14.     _BumpMap ("Normalmap", 2D) = "bump" {}
    15.     //_Up ("_Up", Vector) = (0,1,0,1)
    16. }
    17.  
    18. SubShader
    19. {  
    20.    
    21.     Tags { "RenderType"="Transparent" }
    22.     LOD 400
    23.    
    24.     GrabPass
    25.     {
    26.        
    27.     }
    28.    
    29.     //Pass {
    30.  
    31. CGPROGRAM
    32.  
    33. #pragma surface surf BlinnPhong alpha
    34. #pragma target 3.0
    35.  
    36. sampler2D _GrabTexture : register(s0);
    37. sampler2D _MainTex : register(s1);
    38. sampler2D _BumpMap : register(s2);
    39. samplerCUBE _ReflectionTex : register(s3);
    40.  
    41. sampler2D _CameraDepthTexture; // : register(s4);
    42.  
    43. float4 _Color;
    44. float4 _ReflectColor;
    45. float _ChromaticDispersion;
    46. float _Shininess;
    47. float _WeirdScale;
    48. float _Refraction;
    49. float _BumpReflectionStr;
    50. float _ReflToRefrExponent;
    51.  
    52. float4 _GrabTexture_TexelSize;
    53. float4 _CameraDepthTexture_TexelSize;
    54.  
    55.  
    56. struct Input {
    57.     float4 color : COLOR;
    58.     float2 uv_MainTex;
    59.     float2 uv_BumpMap;
    60.     float3 worldRefl;
    61.     float4 screenPos;
    62.     INTERNAL_DATA
    63. };
    64.  
    65. void surf (Input IN, inout SurfaceOutput o)
    66. {
    67.     half4 tex = half4(1,1,1,1); //tex2D(_MainTex, IN.uv_MainTex);
    68.     tex.g = tex2D(_MainTex, IN.uv_MainTex + _CameraDepthTexture_TexelSize.xy*_ChromaticDispersion).g;
    69.    
    70.    
    71.     // shore blending
    72.     float z1 = tex2Dproj(_CameraDepthTexture,  IN.screenPos); // in /.w space
    73.     z1 =  LinearEyeDepth(z1);  
    74.     float z2 = (IN.screenPos.z);
    75.     z1 = saturate( 0.125 * (abs(z2-z1)) );
    76.    
    77.     half4 c = tex * _Color;
    78.     o.Albedo = c.rgb;
    79.    
    80.     o.Gloss = tex.a;
    81.     o.Specular = _Shininess;
    82.    
    83.     o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    84.     o.Normal += UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap * _GrabTexture_TexelSize.xy));
    85.     o.Normal *= 0.5;
    86.  
    87.     float2 offset = o.Normal * _Refraction * z1 * _GrabTexture_TexelSize.xy;
    88.     IN.screenPos.xy = offset * IN.screenPos.z + IN.screenPos.xy;   
    89.    
    90.     float3 worldRefl = WorldReflectionVector(IN, o.Normal*half3(_BumpReflectionStr,_BumpReflectionStr,_BumpReflectionStr));
    91.     half4 reflcol = texCUBE(_ReflectionTex, worldRefl);
    92.     reflcol *= tex.a;
    93.    
    94.     float3 reflColor = reflcol.rgb * _ReflectColor.rgb;
    95.     float3 refrColor = tex2Dproj(_GrabTexture, IN.screenPos);
    96.    
    97.     o.Alpha = saturate(z1*1.0); // this magic constant might needed to be tweaked
    98.    
    99.     float refl2Refr = abs(dot(o.Normal,normalize(worldRefl)));
    100.    
    101.     // clamp to always have a little bit of everything
    102.     o.Emission = c * lerp(reflColor,refrColor, clamp(pow(refl2Refr,_ReflToRefrExponent),0.1,0.9));
    103.     o.Albedo = o.Emission;
    104.     o.Emission *= 0.5;
    105. }
    106. ENDCG
    107. //}
    108. }
    109.  
    110.    
    111. FallBack "Reflective/Bumped Diffuse"
    112. }
    113.  
     
  17. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,703
    i really dont understand what you want or what you want to achieve. But here is a completely transparent one if thats what you want.

    Code (csharp):
    1. Shader "MyShaders/Glass-Seethrough" {
    2.     Properties {
    3.         _Color ("Main Color", Color) = (1,1,1,1)
    4.         _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
    5.         _Shininess ("Shininess", Range (0.03, 1)) = 0.3
    6.         _MainTex ("Base (RGB)", 2D) = "white" {}
    7.         _BumpMap ("Normalmap", 2D) = "bump" {}
    8.         _DistAmt  ("Distortion", range (0,128)) = 10
    9.     }
    10.     SubShader {
    11.         GrabPass { }
    12.        
    13.         Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Opaque" }
    14.         LOD 200
    15.        
    16.         CGPROGRAM
    17.         #pragma exclude_renderers gles
    18.         #pragma vertex vert
    19.         #pragma surface surf BlinnPhong alpha
    20.         #include "UnityCG.cginc"
    21.  
    22.         float4 _Color;
    23.         float _Shininess;
    24.         sampler2D _MainTex;
    25.         sampler2D _BumpMap;
    26.         sampler2D _GrabTexture;
    27.         float _DistAmt;
    28.         float4 _GrabTexture_TexelSize;
    29.  
    30.         struct Input {
    31.             float2 uv_MainTex;
    32.             float2 uv_BumpMap;
    33.             float4 proj : TEXCOORD;
    34.         };
    35.        
    36.         void vert (inout appdata_full v, out Input o) {
    37.             float4 oPos = mul(UNITY_MATRIX_MVP, v.vertex);
    38.             #if UNITY_UV_STARTS_AT_TOP
    39.                 float scale = -1.0;
    40.             #else
    41.                 float scale = 1.0;
    42.             #endif
    43.             o.proj.xy = (float2(oPos.x, oPos.y*scale) + oPos.w) * 0.5;
    44.             o.proj.zw = oPos.zw;
    45.         }
    46.  
    47.         void surf (Input IN, inout SurfaceOutput o) {
    48.             half3 nor = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    49.            
    50.             float2 offset = nor * _DistAmt * _GrabTexture_TexelSize.xy;
    51.             IN.proj.xy = offset * IN.proj.z + IN.proj.xy;
    52.             half4 col = tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(IN.proj));
    53.            
    54.             half4 tex = tex2D(_MainTex, IN.uv_MainTex);
    55.             o.Albedo = tex.rgb * _Color.rgb * col.rgb;
    56.             o.Normal = nor.rgb;
    57.             o.Specular = _Shininess;
    58.             o.Gloss = col.a;
    59.             o.Alpha = col.a;
    60.         }
    61.         ENDCG
    62.     }
    63.     FallBack "Diffuse"
    64. }
     
  18. Earlybird

    Earlybird

    Joined:
    Mar 30, 2013
    Posts:
    14
    Last edited: Jul 22, 2014
unityunity