Hey guys,
I wrote a shader that should animate water using a flow map and a noise map, everything is based on the shader developed by Valve for LF4 and Graphics Runner DirectX Sample
The shader itself is really simple, but for some reason a cannot animate the water. I would really appreciate any help, in fact would be cool having a water animated using a flow map in unity free version.
this is the shader:
Shader "WaterTestShader1"
{
Properties
{
_Color ("Main Color", Color) = (1,1,1,0.5)
_MainTex ("Base (RGB)", 2D) = "white" {}
_MainTex2 ("Base (RGB)", 2D) = "white" {}
_FlowMap ("FlowMap (RGB)", 2D) = "white" {}
_NoiseMap ("NoiseMap (RGB)", 2D) = "white" {}
_WaveScale ("Wave scale", Range (0.02,0.15)) = .07
_WaveSpeed ("Wave speed (map1 x,y; map2 x,y)", Vector) = (19,9,-16,-7)
}
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float4 _Color;
sampler2D _MainTex, _MainTex2, _FlowMap, _NoiseMap;
float4 _MainTex_ST;
float4 _MainTex2_ST;
float3 _WaveSpeed;
float _WaveScale;
//Flow map offsets used to scroll the wave maps
float flowMapOffset0;
float flowMapOffset1;
//scale used on the wave maps
float waveScale = 1.0f;
float halfCycle;
struct v2f
{
float4 pos : SV_POSITION;
float2 uv0 : TEXCOORD0;
float2 uv1 : TEXCOORD1;
float3 col : COLOR0;
};
v2f vert (appdata_base v)
{
v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.uv0 = TRANSFORM_TEX (v.texcoord, _MainTex);
o.uv1 = TRANSFORM_TEX (v.texcoord, _MainTex2);
o.col = v.normal * 0.5 + 0.5; //shift the colour in the range [0, 1]
return o;
}
half4 frag (v2f i) : COLOR
{
//get and uncompress the flow vector for this pixel
float2 flowmap = tex2D( _FlowMap, i.uv0 ).rg * 2.0f - 1.0f;
float cycleOffset = tex2D( _NoiseMap, i.uv0 ).r;
float phase0 = cycleOffset * .5f + flowMapOffset0;
float phase1 = cycleOffset * .5f + flowMapOffset1;
// Sample normal map.
float3 normalT0 = tex2D(_MainTex, ( i.uv0 * waveScale ) + flowmap * phase0 ).rgb;
float3 normalT1 = tex2D(_MainTex2, ( i.uv0 * waveScale ) + flowmap * phase1 ).rgb;
float f = ( abs( halfCycle - flowMapOffset0 ) / halfCycle );
float3 normalT = lerp( normalT0, normalT1, f );
return float4( normalT, 1.0f );
}
ENDCG
}
}
Fallback "VertexLit"
}
and this is the script I use to update overtime the scrolling textures:
using UnityEngine;
[ExecuteInEditMode]
public class UVScroller : MonoBehaviour
{
float m_fFlowMapOffset0 = 0.0f;
float m_fFlowMapOffset1 = 0.0f;
float m_fFlowSpeed = 0.05f;
float m_fCycle = 0.15f;
float m_fWaveMapScale = 2.0f;
public void Update ()
{
//update the flow map offsets for both layers
m_fFlowMapOffset0 += m_fFlowSpeed * Time.deltaTime;
m_fFlowMapOffset1 += m_fFlowSpeed * Time.deltaTime;
if ( m_fFlowMapOffset0 >= m_fCycle )
m_fFlowMapOffset0 = 0.0f;
if ( m_fFlowMapOffset1 >= m_fCycle )
m_fFlowMapOffset1 = 0.0f;
float _fHalfCycle = m_fCycle*0.5f;
Shader.SetGlobalFloat("fFlowMapOffset0", m_fFlowMapOffset0);
Shader.SetGlobalFloat("fFlowMapOffset1", m_fFlowMapOffset1);
Shader.SetGlobalFloat("halfCycle", _fHalfCycle);
Shader.SetGlobalFloat("fWaveSpeed", m_fFlowSpeed);
Shader.SetGlobalFloat("fWaveScale", m_fWaveMapScale);
}
}
At the moment I am not considering the light I just want to make the flow working.
The flow is scrolling,but there is a problem with normals, apparently I do not unpack normals properly and when the texture reached the endCycle is resetting itself
What I want to do is scrolling normal maps and not the actual texture, this means unpack normals and?
Thank you very much in advance
Finally a pic as reference:




