Please please please pleeeeeeease add an example to get simple refraction on the surface examples page: Examples Pleeease
The heatwave texture from the gravity gun has a good refraction shader, except it never seems to go totally transparent in the negative/black space of your texture! Argh. http://forum.unity3d.com/threads/66771-Gravity-Gun-Physx-Sample?highlight=gravity+gun
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
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.
Here is the same glass shader with some extra stuff as a surface shader, the idea is the same: Code (csharp): Shader "Test" { Properties { _Color ("Main Color", Color) = (1,1,1,1) _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1) _Shininess ("Shininess", Range (0.03, 1)) = 0.3 _MainTex ("Base (RGB)", 2D) = "white" {} _BumpMap ("Normalmap", 2D) = "bump" {} _DistAmt ("Distortion", range (0,128)) = 10 } SubShader { GrabPass { } Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Opaque" } LOD 200 CGPROGRAM #pragma exclude_renderers gles #pragma vertex vert #pragma surface surf BlinnPhong #include "UnityCG.cginc" float4 _Color; float _Shininess; sampler2D _MainTex; sampler2D _BumpMap; sampler2D _GrabTexture; float _DistAmt; float4 _GrabTexture_TexelSize; struct Input { float2 uv_MainTex; float2 uv_BumpMap; float4 proj : TEXCOORD; }; void vert (inout appdata_full v, out Input o) { float4 oPos = mul(UNITY_MATRIX_MVP, v.vertex); #if UNITY_UV_STARTS_AT_TOP float scale = -1.0; #else float scale = 1.0; #endif o.proj.xy = (float2(oPos.x, oPos.y*scale) + oPos.w) * 0.5; o.proj.zw = oPos.zw; } void surf (Input IN, inout SurfaceOutput o) { half4 nor = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap)); float2 offset = nor * _DistAmt * _GrabTexture_TexelSize.xy; IN.proj.xy = offset * IN.proj.z + IN.proj.xy; half4 col = tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(IN.proj)); half4 tex = tex2D(_MainTex, IN.uv_MainTex); o.Albedo = tex.rgb * _Color.rgb * col.rgb; o.Normal = nor.rgb; o.Specular = _Shininess; o.Gloss = tex.a; o.Alpha = tex.a * _Color.a; } ENDCG } FallBack "Diffuse" }
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,
For Unity3.2: Code (csharp): Shader "MyShaders/Glass-Seethrough" { Properties { _Color ("Main Color", Color) = (1,1,1,1) _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1) _Shininess ("Shininess", Range (0.03, 1)) = 0.3 _MainTex ("Base (RGB)", 2D) = "white" {} _BumpMap ("Normalmap", 2D) = "bump" {} _DistAmt ("Distortion", range (0,128)) = 10 } SubShader { GrabPass { } Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Opaque" } LOD 200 CGPROGRAM #pragma exclude_renderers gles #pragma vertex vert #pragma surface surf BlinnPhong #include "UnityCG.cginc" float4 _Color; float _Shininess; sampler2D _MainTex; sampler2D _BumpMap; sampler2D _GrabTexture; float _DistAmt; float4 _GrabTexture_TexelSize; struct Input { float2 uv_MainTex; float2 uv_BumpMap; float4 proj : TEXCOORD; }; void vert (inout appdata_full v, out Input o) { float4 oPos = mul(UNITY_MATRIX_MVP, v.vertex); #if UNITY_UV_STARTS_AT_TOP float scale = -1.0; #else float scale = 1.0; #endif o.proj.xy = (float2(oPos.x, oPos.y*scale) + oPos.w) * 0.5; o.proj.zw = oPos.zw; } void surf (Input IN, inout SurfaceOutput o) { half3 nor = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap)); float2 offset = nor * _DistAmt * _GrabTexture_TexelSize.xy; IN.proj.xy = offset * IN.proj.z + IN.proj.xy; half4 col = tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(IN.proj)); half4 tex = tex2D(_MainTex, IN.uv_MainTex); o.Albedo = tex.rgb * _Color.rgb * col.rgb; o.Normal = nor.rgb; o.Specular = _Shininess; o.Gloss = tex.a; o.Alpha = tex.a * _Color.a; } ENDCG } FallBack "Diffuse" }
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
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.
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.
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): Shader "Reflective/River Water" { Properties { _Color ("Main Color", Color) = (1,1,1,1) _SpecColor ("Specular Color", Color) = (0.5,0.5,0.5,1) _Shininess ("Shininess", Range (0.01, 1)) = 0.078125 _ChromaticDispersion ("_ChromaticDispersion", Range(0.0,4.0)) = 0.1 _Refraction ("Refraction", Range (0.00, 100.0)) = 1.0 _ReflToRefrExponent ("_ReflToRefrExponent", Range(0.00,4.00)) = 1.0 _ReflectColor ("Reflection Color", Color) = (1,1,1,0.5) _BumpReflectionStr ("_BumpReflectionStr", Range(0.00,1.00)) = 0.5 _MainTex ("Base (RGB) RefStrGloss (A)", 2D) = "white" {} _ReflectionTex ("_ReflectionTex", CUBE) = "white" {} _BumpMap ("Normalmap", 2D) = "bump" {} //_Up ("_Up", Vector) = (0,1,0,1) } SubShader { Tags { "RenderType"="Transparent" } LOD 400 GrabPass { } //Pass { CGPROGRAM #pragma surface surf BlinnPhong alpha #pragma target 3.0 sampler2D _GrabTexture : register(s0); sampler2D _MainTex : register(s1); sampler2D _BumpMap : register(s2); samplerCUBE _ReflectionTex : register(s3); sampler2D _CameraDepthTexture; // : register(s4); float4 _Color; float4 _ReflectColor; float _ChromaticDispersion; float _Shininess; float _WeirdScale; float _Refraction; float _BumpReflectionStr; float _ReflToRefrExponent; float4 _GrabTexture_TexelSize; float4 _CameraDepthTexture_TexelSize; struct Input { float4 color : COLOR; float2 uv_MainTex; float2 uv_BumpMap; float3 worldRefl; float4 screenPos; INTERNAL_DATA }; void surf (Input IN, inout SurfaceOutput o) { half4 tex = half4(1,1,1,1); //tex2D(_MainTex, IN.uv_MainTex); tex.g = tex2D(_MainTex, IN.uv_MainTex + _CameraDepthTexture_TexelSize.xy*_ChromaticDispersion).g; // shore blending float z1 = tex2Dproj(_CameraDepthTexture, IN.screenPos); // in /.w space z1 = LinearEyeDepth(z1); float z2 = (IN.screenPos.z); z1 = saturate( 0.125 * (abs(z2-z1)) ); half4 c = tex * _Color; o.Albedo = c.rgb; o.Gloss = tex.a; o.Specular = _Shininess; o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap)); o.Normal += UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap * _GrabTexture_TexelSize.xy)); o.Normal *= 0.5; float2 offset = o.Normal * _Refraction * z1 * _GrabTexture_TexelSize.xy; IN.screenPos.xy = offset * IN.screenPos.z + IN.screenPos.xy; float3 worldRefl = WorldReflectionVector(IN, o.Normal*half3(_BumpReflectionStr,_BumpReflectionStr,_BumpReflectionStr)); half4 reflcol = texCUBE(_ReflectionTex, worldRefl); reflcol *= tex.a; float3 reflColor = reflcol.rgb * _ReflectColor.rgb; float3 refrColor = tex2Dproj(_GrabTexture, IN.screenPos); o.Alpha = saturate(z1*1.0); // this magic constant might needed to be tweaked float refl2Refr = abs(dot(o.Normal,normalize(worldRefl))); // clamp to always have a little bit of everything o.Emission = c * lerp(reflColor,refrColor, clamp(pow(refl2Refr,_ReflToRefrExponent),0.1,0.9)); o.Albedo = o.Emission; o.Emission *= 0.5; } ENDCG //} } FallBack "Reflective/Bumped Diffuse" }
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): Shader "MyShaders/Glass-Seethrough" { Properties { _Color ("Main Color", Color) = (1,1,1,1) _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1) _Shininess ("Shininess", Range (0.03, 1)) = 0.3 _MainTex ("Base (RGB)", 2D) = "white" {} _BumpMap ("Normalmap", 2D) = "bump" {} _DistAmt ("Distortion", range (0,128)) = 10 } SubShader { GrabPass { } Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Opaque" } LOD 200 CGPROGRAM #pragma exclude_renderers gles #pragma vertex vert #pragma surface surf BlinnPhong alpha #include "UnityCG.cginc" float4 _Color; float _Shininess; sampler2D _MainTex; sampler2D _BumpMap; sampler2D _GrabTexture; float _DistAmt; float4 _GrabTexture_TexelSize; struct Input { float2 uv_MainTex; float2 uv_BumpMap; float4 proj : TEXCOORD; }; void vert (inout appdata_full v, out Input o) { float4 oPos = mul(UNITY_MATRIX_MVP, v.vertex); #if UNITY_UV_STARTS_AT_TOP float scale = -1.0; #else float scale = 1.0; #endif o.proj.xy = (float2(oPos.x, oPos.y*scale) + oPos.w) * 0.5; o.proj.zw = oPos.zw; } void surf (Input IN, inout SurfaceOutput o) { half3 nor = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap)); float2 offset = nor * _DistAmt * _GrabTexture_TexelSize.xy; IN.proj.xy = offset * IN.proj.z + IN.proj.xy; half4 col = tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(IN.proj)); half4 tex = tex2D(_MainTex, IN.uv_MainTex); o.Albedo = tex.rgb * _Color.rgb * col.rgb; o.Normal = nor.rgb; o.Specular = _Shininess; o.Gloss = col.a; o.Alpha = col.a; } ENDCG } FallBack "Diffuse" }
Hi I keep getting this error when using this shader: "vert: output parameter 'o' not completely initialized " im not sure how to correct this? any help please -- *update* I found the solution I was looking for here: http://answers.unity3d.com/questions/754151/vert-output-parameter-o-not-completely-initialized.html