{"id":3835,"date":"2016-09-06T16:07:04","date_gmt":"2016-09-06T16:07:04","guid":{"rendered":"http:\/\/www.drmop.com\/?p=3835"},"modified":"2016-09-15T10:36:28","modified_gmt":"2016-09-15T10:36:28","slug":"2d-water-shader-in-unity-3d","status":"publish","type":"post","link":"http:\/\/www.drmop.com\/index.php\/2016\/09\/06\/2d-water-shader-in-unity-3d\/","title":{"rendered":"2D water shader in Unity 3D"},"content":{"rendered":"<p>I recently found the need to spruce up the water in a new 2D game that I am working on, so I decided to have a play around with Unity shaders again. Here&#8217;s the code to the shader:<\/p>\n<p>[sourcecode language=&#8221;js&#8221;]<br \/>\nShader &quot;Unlit\/WaterTexture&quot;<br \/>\n{<br \/>\n\tProperties<br \/>\n\t{<br \/>\n\t\t_MainTex (&quot;Texture&quot;, 2D) = &quot;white&quot; {}<br \/>\n\t\t_ScrollX (&quot;X Scroll Speed&quot;, Range(-10, 10)) = 0<br \/>\n\t\t_WaveSpeed(&quot;WaveSpeed&quot;, Range(0, 100)) = 10<br \/>\n\t\t_FrequencyX(&quot;X Axis Frequency&quot;, Range(0, 100)) = 34<br \/>\n\t\t_AmplitudeX(&quot;X Axis Amplitude&quot;, Range(0, 100)) = 0.005<br \/>\n\t}<br \/>\n\tSubShader<br \/>\n\t{<br \/>\n\t\tTags { &quot;RenderType&quot;=&quot;Opaque&quot; }<br \/>\n\t\tLOD 100<\/p>\n<p>\t\tPass<br \/>\n\t\t{<br \/>\n\t\t\tCGPROGRAM<br \/>\n\t\t\t#pragma vertex vert<br \/>\n\t\t\t#pragma fragment frag<\/p>\n<p>\t\t\t#include &quot;UnityCG.cginc&quot;<\/p>\n<p>\t\t\tstruct appdata<br \/>\n\t\t\t{<br \/>\n\t\t\t\tfloat4 vertex : POSITION;<br \/>\n\t\t\t\tfloat2 uv : TEXCOORD0;<br \/>\n\t\t\t};<\/p>\n<p>\t\t\tstruct v2f<br \/>\n\t\t\t{<br \/>\n\t\t\t\tfloat2 uv : TEXCOORD0;<br \/>\n\t\t\t\tfloat4 vertex : SV_POSITION;<br \/>\n\t\t\t};<\/p>\n<p>\t\t\tsampler2D _MainTex;<br \/>\n\t\t\tfloat4 _MainTex_ST;<br \/>\n\t\t\tfixed _ScrollX;<br \/>\n\t\t\tfixed _FrequencyX;<br \/>\n\t\t\tfixed _AmplitudeX;<br \/>\n\t\t\tfixed _WaveSpeed;<\/p>\n<p>\t\t\tv2f vert (appdata v)<br \/>\n\t\t\t{<br \/>\n\t\t\t\tv2f o;<br \/>\n\t\t\t\to.vertex = mul(UNITY_MATRIX_MVP, v.vertex);<br \/>\n\t\t\t\to.uv = TRANSFORM_TEX(v.uv, _MainTex);<br \/>\n\t\t\t\treturn o;<br \/>\n\t\t\t}<\/p>\n<p>\t\t\tfixed4 frag(v2f i) : SV_Target<br \/>\n\t\t\t{<br \/>\n\t\t\t\tfixed time = _Time * _WaveSpeed;<br \/>\n\t\t\t\tfixed2 uvs = i.uv;<br \/>\n\t\t\t\tfixed offset = (_ScrollX * _Time);<br \/>\n\t\t\t\tuvs.x += offset;<br \/>\n\t\t\t\tuvs.x = fmod(uvs.x, 1);<br \/>\n\t\t\t\tuvs.x += sin(uvs.y * _FrequencyX + time) * _AmplitudeX;<br \/>\n\t\t\t\tfixed2 uvs2 = i.uv;<br \/>\n\t\t\t\tuvs2.x -= offset;<br \/>\n\t\t\t\tuvs2.x = fmod(uvs2.x, 1);<br \/>\n\t\t\t\tif (uvs2.x &lt; 0)<br \/>\n\t\t\t\t\tuvs2.x += 1;<br \/>\n\t\t\t\tuvs2.y = 1 &#8211; i.uv.y;<br \/>\n\t\t\t\tuvs2.x += sin(uvs2.y * _FrequencyX + time) * _AmplitudeX;<br \/>\n\t\t\t\tfixed4 col = tex2D(_MainTex, uvs);<br \/>\n\t\t\t\tfixed4 col2 = tex2D(_MainTex, uvs2);<br \/>\n\t\t\t\tcol = (col + col2) \/ 2;<br \/>\n\t\t\t\treturn col;<br \/>\n\t\t\t}<\/p>\n<p>\t\t\tENDCG<br \/>\n\t\t}<br \/>\n\t}<br \/>\n}<br \/>\n[\/sourcecode]<\/p>\n<p>The main section of the code is the fragment shader, it plays with the textures U coordinates by applying a sine wave to them, it then does the same with the pixel on the opposite side of the V axis then merges them both together, it also scrolls the textures, this produces a nice running water effect.<\/p>\n<p>You can grab all shaders that I make public from <a title=\"Free Unity shaders on Github\" href=\"https:\/\/github.com\/mrmop\/UnityShaders\" rel=\"nofollow\">Github<\/a><\/p>\n<p>Feel free to re-use the shader in your own creations.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently found the need to spruce up the water in a new 2D game that I am working on, so I decided to have a play around with Unity shaders again. Here&#8217;s the code to the shader: [sourcecode language=&#8221;js&#8221;] Shader &quot;Unlit\/WaterTexture&quot; { Properties { _MainTex (&quot;Texture&quot;, 2D) = &quot;white&quot; {} _ScrollX (&quot;X Scroll Speed&quot;, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[630,625],"tags":[631],"class_list":["post-3835","post","type-post","status-publish","format-standard","hentry","category-shaders","category-unity3d","tag-cg-shaders"],"_links":{"self":[{"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/posts\/3835","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/comments?post=3835"}],"version-history":[{"count":4,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/posts\/3835\/revisions"}],"predecessor-version":[{"id":3870,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/posts\/3835\/revisions\/3870"}],"wp:attachment":[{"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/media?parent=3835"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/categories?post=3835"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/tags?post=3835"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}