- Home /
How can I distort a cubemap in a shader?
Ok, so I'm new to programming shaders, but am learning quickly (I think) and I've managed to make this shader that alternates between a Cubemap and a texture based on the color of each vertex, to create puddles on a floor:
The thing is, I would like the puddles to be distorted with time, to give it more of a sense of water. Something like this:
I assume I have to distort it with a noise texture map, but I can't figure out how to do it. Any help??
Here's the shader so far:
Shader "Wet Vertex Blend"
{
Properties
{
_MainTex ("Main Texture", 2D) = "white" {}
_BumpMap ("Normal Texture", 2D) = "bump" {}
_Cube ("Cube Map", Cube) = "" {}
_NoiseTex ("Noise (Cloud) Texture", 2D) = "white" {}
}
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
//user defined variables
samplerCUBE _Cube;
//Base Input Structs
struct vertexInput {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct vertexOutput {
float4 pos : SV_POSITION;
float3 normalDir : TEXCOORD0;
float3 viewDir : TEXCOORD1;
};
//vertex function
vertexOutput vert(vertexInput v){
vertexOutput o;
o.normalDir = normalize( mul(float4(v.normal, 0.0), _World2Object).xyz );
o.viewDir = float3(mul(_Object2World, v.vertex) - _WorldSpaceCameraPos).xyz;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
return o;
}
//fragment function
float4 frag(vertexOutput i) : COLOR{
//reflect the ray based on the normals to get the cube coordinates
float3 reflectDir = reflect(i.viewDir, i.normalDir);
//texture maps
float4 texC = texCUBE(_Cube, reflectDir);
return texC;
}
ENDCG
}
Pass
{
Blend SrcAlpha OneMinusSrcAlpha
Fog { Mode Off }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _NoiseTex;
float4 _NoiseTex_ST;
sampler2D _BumpMap;
// vertex input: position, color
struct appdata
{
float4 vertex : POSITION;
fixed4 color : COLOR;
float4 texcoord : TEXCOORD0;
float3 normal : NORMAL;
};
//vertex output
struct v2f
{
float4 pos : SV_POSITION;
fixed4 color : COLOR;
float4 tex : TEXCOORD0;
};
v2f vert (appdata v)
{
v2f o;
o.tex = v.texcoord;
o.pos = mul( UNITY_MATRIX_MVP, v.vertex );
o.color = v.color;
return o;
}
fixed4 frag (v2f i) : COLOR0
{
float4 tex = float4(0.0,0.0,0.0,0.0);
float4 noiseTex = tex2D(_NoiseTex, i.tex.xy * _NoiseTex_ST.xy + _NoiseTex_ST.zw);
//Texture Maps
if(i.color.r >= 0.5)
{
tex = tex2D(_MainTex, i.tex.xy * _MainTex_ST.xy + _MainTex_ST.zw)*i.color.r;
}
if(i.color.r < 0.5)
{
tex = tex2D(_MainTex, i.tex.xy * _MainTex_ST.xy + _MainTex_ST.zw)*i.color.r*noiseTex.r;
}
return tex;
//return i.color;
}
ENDCG
}
}
}
I'd be using the noise map to modify the reflection vector and then have an offset into the noise map (so you update the uv lookup into the noise by the offset) that you use to simulate it over time.
And how could I modify the vector with a noise texture exactly?
Well you make a vector out of whatever's in the noise texture and modify the existing reflection vector by doing something like sum$$anonymous$$g them and normalizing the result.
Your answer
Follow this Question
Related Questions
How I do a Pixel Distort Shader 0 Answers
Rendering a G-Buffer for a cubemap 0 Answers
Shader add vertex displacement 0 Answers
Cubemap on Android 1 Answer
Why is my Pro Water Material Grey? 1 Answer