- Home /
How to get caustics only show underwater?
I am trying to get a nice caustic effect. Right now I am using an extra directional light with a animated cookie script. Also using a underwater script which enables the caustic light along with a few other effects when I go below a certain value. This works and looks nice but still there are no caustics above the water but magically appears as soon as I am underwater.
My question is if there is anyway to get the caustics to always be enabled but only show under the water. I know it is possible but I am not sure how. I am using the terrain system so having two separate meshes above and below the water will not work.
Any help will be greatly appreciated. Thanks
I'm a bit confused by the question. You have something that only shows caustics underwater, but now you want to change it so that...it only shows caustics underwater? Why?
And what's wrong with enabling/disabling scripts or gameobjects?
I want the caustics to show on the ground below the water when I am standing on a dock and looking down at the water. Not only when I am underwater. It looks like crap when the caustics all of the sudden appear when the camera goes below the water. I want to have the caustics constantly showing but only on the ground below the water.
Answer by Pangamini · Mar 25, 2015 at 08:53 AM
Try using an orthogonal projector instead of directional light. With some additive shader it will create light like effect, however it with lack of shadows. Or at least i don't know about projector having the shadowmap feature supported by the unity, however it could be scripted. So just create an ortho projector facing down and spreading over the water surface. The default unity additive projector shader doesn't clip correctly what's behind, so here's what I am using:
Shader "Projector/Additive"
{
Properties
{
_Decal ("Decal", 2D) = "white" { TexGen ObjectLinear }
_Color ("Color", Color) = (0.5, 0.5, 0.5, 1)
}
Subshader
{
Pass
{
ZWrite On
Fog { Color (1, 1, 1) }
Offset -1, -1
Blend One One
Cull Back
Tags { "RenderType"="Geometry-10" }
CGPROGRAM
#include "UnityCG.cginc"
#pragma vertex vert
#pragma fragment frag
sampler2D _Decal;
float4x4 _Projector;
float4x4 _ProjectorClip;
float4 _Color;
struct vin
{
float4 pos : POSITION;
float4 uv : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
float4 uv : TEXCOORD0;
float4 pclip : TEXCOORD1;
};
v2f vert (vin i)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, i.pos);
o.uv = mul(_Projector, i.pos);
o.pclip = mul(_ProjectorClip, i.pos);
return o;
}
float4 frag(v2f i) : COLOR
{
clip(1-i.pclip.z);
clip(i.pclip.z);
clip(i.uv.x);
clip(1-i.uv.x);
clip(i.uv.y);
clip(1-i.uv.y);
float4 col = tex2Dproj(_Decal, i.uv) * _Color;
return col;
}
ENDCG
}
}
}
You may want to modify the shader a bit to support tiling
Tell me how did it go, i might need to do something similar myself in the near future :)
I have not animated or set up tiling yet but I put the projector at the waters surface and applied a material with your shader. It does not clip parts of the terrain like before. It works. Thanks
The only problem I am having now is any animate projector script I had or can find does not work in Unity 5. I would fix the error it had and it would not animate at all. The animate cookie scripts work fine still just not projectors.
At least it is working underwater. That is what I needed the most. Thanks for the help
Your answer
Follow this Question
Related Questions
Why does my water display itself above my trees? 1 Answer
Rivers on procedurally terrain 0 Answers
need water 1 Answer
underwater shader 1 Answer
Google Earth, Sketchup, Global Mapper - best way to generate terrain 1 Answer