So, here is a basic test shader and script for using flow maps on water surfaces. No water here, just the flow of the normals.
NoiseTex is an average noise texture you can make one with render clouds option in photoshop.
NormTex0 and 1 are just average wave normal textures, you can use the one supplied with unity water.
Shader:
Shader "Aubergine/ObjSpace/Flow" {
Properties {
_FlowTex ("_FlowTex", 2D) = "white" {}
_NoiseTex ("_NoiseTex", 2D) = "white" {}
_NormTex0 ("_NormTex0", 2D) = "" {}
_NormTex1 ("_NormTex1", 2D) = "" {}
}
SubShader {
Pass {
Tags { "RenderType"="Opaque" }
Lighting Off Fog { Mode Off }
LOD 200
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
sampler2D _FlowTex, _NoiseTex, _NormTex0, _NormTex1;
uniform float _FlowOffset0, _FlowOffset1, _HalfCycle;
struct a2f {
float4 vertex : POSITION;
float4 texcoord : TEXCOORD0;
};
struct v2f {
float4 Pos : SV_POSITION;
float2 Uv : TEXCOORD0;
};
v2f vert (a2f v) {
v2f o;
o.Pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.Uv = v.texcoord.xy;
return o;
}
half4 frag( v2f i ) : COLOR {
half2 flowTex = tex2D(_FlowTex, i.Uv).rg * 2.0f - 1.0f;
float cycleOffs = tex2D(_NoiseTex, i.Uv).r;
//float phase0 = 0.5f + _FlowOffset0;
//float phase1 = 0.5f + _FlowOffset1;
float phase0 = cycleOffs * 0.5f + _FlowOffset0;
float phase1 = cycleOffs * 0.5f + _FlowOffset1;
float3 norm0 = tex2D(_NormTex0, (i.Uv * 4) + flowTex * phase0);
float3 norm1 = tex2D(_NormTex1, (i.Uv * 2) + flowTex * phase1);
float f = (abs(_HalfCycle - _FlowOffset0) / _HalfCycle);
float3 normT = lerp(norm0, norm1, f);
return float4(normT, 1.0f);
}
ENDCG
}
}
FallBack Off
}
The script
using UnityEngine;
using System.Collections;
public class Flow : MonoBehaviour {
public float Cycle = .15f;
public float FlowSpeed = .05f;
private float HalfCycle = 0f;
private float FlowMapOffset0, FlowMapOffset1;
// Use this for initialization
void Start () {
HalfCycle = Cycle * .5f;
FlowMapOffset0 = 0.0f;
FlowMapOffset1 = HalfCycle;
renderer.sharedMaterial.SetFloat("_HalfCycle", HalfCycle);
renderer.sharedMaterial.SetFloat("_FlowOffset0", FlowMapOffset0);
renderer.sharedMaterial.SetFloat("_FlowOffset1", FlowMapOffset1);
}
// Update is called once per frame
void Update () {
FlowMapOffset0 += FlowSpeed * Time.deltaTime;
FlowMapOffset1 += FlowSpeed * Time.deltaTime;
if (FlowMapOffset0 >= Cycle)
FlowMapOffset0 = 0.0f;
if (FlowMapOffset1 >= Cycle)
FlowMapOffset1 = 0.0f;
renderer.sharedMaterial.SetFloat("_FlowOffset0", FlowMapOffset0);
renderer.sharedMaterial.SetFloat("_FlowOffset1", FlowMapOffset1);
}
}
My question is, the flowmap is a picture of 2d vectors. And i want to make a custom flow map generator for my personal usage but i am having hard times understanding what color represents what direction.
I found out these yet:
rgb(128, 128, 128) = Vector(0, 0)
rgb(0, 0, 0) = Vector(-1, -1)
rgb(255, 0, 0) = Vector(1, 0) or (1, 1) i dont understand.
Anyways, is there any resource that explains how flowmap colors actually work?