I want to create 2d water in Unity engine. Can you take me advice, where to begin? This is a example of 2d water https://www.youtube.com/watch?v=iBWwNHEHo3I . Thanks advance
One way to do it is with a shader utilizing a GrabPass. You'll need to make a quad where the water is, then apply a material with a shader to the quad. The shader should have a GrabPass, and then a pass that vertically flips and distorts the GrabPass. One example of such a shader is below (source):
Shader "Custom/WaterGrab"
{
Properties
{
_Colour ("Colour", Color) = (1,1,1,1)
_MainTex ("Noise text", 2D) = "bump" {}
_Magnitude ("Magnitude", Range(0,1)) = 0.05
}
SubShader
{
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Opaque"}
ZWrite On Lighting Off Cull Off Fog { Mode Off } Blend One Zero
GrabPass { "_GrabTexture" }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _GrabTexture;
fixed4 _Colour;
sampler2D _MainTex;
float _Magnitude;
struct vin
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : POSITION;
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0;
float4 uvgrab : TEXCOORD1;
};
float4 _MainTex_ST;
// Vertex function
v2f vert (vin v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.color = v.color;
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
#if UNITY_UV_STARTS_AT_TOP
float scale = -1.0;
#else
float scale = 1.0;
#endif
o.uvgrab.xy = (float2(o.vertex.x, (o.vertex.y)* scale) + o.vertex.w) * 0.5;
o.uvgrab.zw = o.vertex.zw;
float4 top = mul(UNITY_MATRIX_MVP, float4(0, 0.5, 0, 1));
top.xy /= top.w;
o.uvgrab.y = 1 - (o.uvgrab.y + top.y);
return o;
}
// Fragment function
half4 frag (v2f i) : COLOR
{
half4 bump = tex2D(_MainTex, i.texcoord );
half2 distortion = UnpackNormal(bump).rg;
i.uvgrab.xy += distortion * _Magnitude;
fixed4 col = tex2D( _GrabTexture, i.uvgrab);
return col * _Colour;
}
ENDCG
}
}
}
In shaders like this, you'll need to provide a bump map that tells the water how to distort the GrabPass. In the above example, it should be inserted as the _MainTex
texture. It should look something like this: