Hi thanks for the reply.
I have the GPU gems bookmarked, that was my backup plan for in the event this ray casting approach failed.
I have made much better progress which you can see here: Imgur: The magic of the Internet
The problem is its doing refraction on the water behind the object even if it’s above the water. I tried doing depth comparison to skip where its above the water, but it doesn’t quite work.
So some one suggested (but it was not easy to understand) to make a render texture of only whats under the water and then also some how put the depth values in the alpha channel to rid of the depth buffer entirely.
The issue is i am not even sure how i would render a texture of objects under the water including objects “partially” through the water. I haven’t found much info on it for unity or at least the technique they are trying to describe. I also am not sure how it works if the water has animated waves.
Perhaps you know what they were talking about with that?
Also encase you are wondering why i am adamantly going down this route still at the moment, it’s because of this webGL demo that used the same technique: WebGL Water
They used the same approach and the refraction looks damn good in my opinion, but sadly they didn’t do a write up on it other than the caustics effects.
Current code i have:
//vertex shader
float3 worldNormal = UnityObjectToWorldNormal(v.normal);
float3 objToEye = WorldSpaceViewDir (v.vertex); //obj to camera in world space
float3 refraction = normalize( refract(-objToEye,worldNormal, 1.0/_RefractIndex));
float3 objRefraction = mul(unity_WorldToObject,refraction) * _RefractDistance; //zoom scale
float4 newvertex = UnityObjectToClipPos(float4(objRefraction,v.vertex.w));
o.refractuv = ComputeGrabScreenPos(newvertex);
COMPUTE_EYEDEPTH(o.refractuv.z);
And
//frag shader
float sceneDepth = tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD( i.refractuv) ).r; //sample depth texture
sceneDepth = LinearEyeDepth(sceneDepth); // from perspective to linear distribution
float waterDepth = (((sceneDepth-i.refractuv.z)/_FadeFactor));
float uvDepth = saturate (waterDepth);
fixed3 col = tex2D(_WaterDepth,float2(uvDepth * _ColorRange , 1)).rgb;
half alpha = tex2D(_WaterDepth,float2(uvDepth ,0)).r;
alpha = saturate(alpha + _MinimumAlpha);
if(sceneDepth<0) return (col,alpha);
float3 refractColor = tex2Dproj(_BackgroundTexture, UNITY_PROJ_COORD( i.refractuv )).rgb;
return fixed4(col * refractColor, alpha);