Chaos Project

General => Electronic and Computer Section => Programming / Scripting / Web => Topic started by: stripe103 on November 28, 2011, 01:46:01 pm

Title: [Unity3D] [Shaders] I need help with a water shader
Post by: stripe103 on November 28, 2011, 01:46:01 pm
Okay, so me and a couple of friends are making a Hack and Slash game in Unity3D for a school project, but right now, I'm struggling with getting the water shader look right.
Here is a screen of the result I currently have.
3 MB: ShowHide
(http://img401.imageshack.us/img401/4791/watershader.png)

Shader(not mine): ShowHide


Shader "Custom/Water (awesome)" {
Properties {
_horizonColor ("Horizon color", COLOR)  = ( .172 , .463 , .435 , 0)
_WaveScale ("Wave scale", Range (0.02,0.30)) = .07
_ColorControl ("Reflective color (RGB) fresnel (A) ", 2D) = "" { }
_ColorControlCube ("Reflective color cube (RGB) fresnel (A) ", Cube) = "" { TexGen CubeReflect }
_BumpMap ("Waves Bumpmap (RGB) ", 2D) = "" { }
WaveSpeed ("Wave speed (map1 x,y; map2 x,y)", Vector) = (19,9,-16,-7)
_MainTex ("Fallback texture", 2D) = "" { }
_RippleMap ("RippleMap (RGB) ", 2D) = "ripple" {}
    RipplePosition ("Ripple (x,y offset; x,y scale)", Vector) = (0.5, 0.5, 0.05, 0.05)
}

CGINCLUDE
// Upgrade NOTE: excluded shader from Xbox360; has structs without semantics (struct v2f members rippleuv)
#pragma exclude_renderers xbox360
// -----------------------------------------------------------
// This section is included in all program sections below

#include "UnityCG.cginc"

uniform float4 _horizonColor;

uniform float4 WaveSpeed;
uniform float4 RipplePosition;
uniform float _WaveScale;

struct appdata {
float4 vertex : POSITION;
float3 normal : NORMAL;
};

struct v2f {
float4 pos : SV_POSITION;
float2 bumpuv[2] : TEXCOORD0;
float2 rippleuv[1];
float3 viewDir : TEXCOORD2;
};

v2f vert(appdata v)
{
v2f o;
float4 s;

o.pos = mul (UNITY_MATRIX_MVP, v.vertex);

// scroll bump waves
float4 temp;
temp.xyzw = (v.vertex.xzxz + _Time.x * WaveSpeed.xyzw) * _WaveScale;
o.bumpuv[0] = temp.xy * float2(.4, .45);
o.bumpuv[1] = temp.wz;
o.rippleuv[0] = v.vertex.xz * float2(RipplePosition.w, RipplePosition.z) + float2(RipplePosition.x, RipplePosition.y);

// object space view direction
o.viewDir.xzy = normalize( ObjSpaceViewDir(v.vertex) );

return o;
}

ENDCG

// -----------------------------------------------------------
// Fragment program

Subshader {
Tags { "Queue" = "Transparent" }
Blend SrcAlpha OneMinusSrcAlpha
ColorMask RGB

Lighting On
Pass {

CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#pragma fragmentoption ARB_fog_exp2

sampler2D _BumpMap;
sampler2D _RippleMap;
sampler2D _ColorControl;



half4 frag( v2f i ) : COLOR
{
half3 bump1 = tex2D( _BumpMap, i.bumpuv[0] ).rgb;
half3 bump2 = tex2D( _BumpMap, i.bumpuv[1] ).rgb;
half3 bump3 = tex2D( _RippleMap, i.rippleuv[0] ).rgb;
    half3 bump = bump1 + bump2 + bump3 - 1.5;

half fresnel = dot( i.viewDir, bump );
half4 water = tex2D( _ColorControl, float2(fresnel,fresnel) );

half4 col;
col.rgb = lerp( water.rgb, _horizonColor.rgb, water.a );
col.a = _horizonColor.a;
col.a = water.a;
return col;
}


ENDCG
}
}

// -----------------------------------------------------------
// Radeon 9000

#warning Upgrade NOTE: SubShader commented out because of manual shader assembly
/*Subshader {
Tags { "Queue" = "Transparent" }
Blend SrcAlpha OneMinusSrcAlpha
ColorMask RGB

Pass {
CGPROGRAM
// Upgrade NOTE: excluded shader from OpenGL ES 2.0 because it does not contain a surface program or both vertex and fragment programs.
#pragma exclude_renderers gles
#pragma vertex vert
// just define 'vert' as a vertex shader, the code is included
// from the section on top
ENDCG

Program "" {
SubProgram {
Local 0, [_horizonColor]

"!!ATIfs1.0
StartConstants;
CONSTANT c0 = program.local[0];
EndConstants;

StartPrelimPass;
SampleMap r0, t0.str;
SampleMap r1, t1.str;
PassTexCoord r2, t2.str;

ADD r1, r0.bias, r1.bias; # bump = bump1 + bump2 - 1
DOT3 r2, r1, r2; # fresnel: dot (bump, viewer-pos)
EndPass;

StartOutputPass;
SampleMap r2, r2.str;

LERP r0.rgb, r2.a, c0, r2; # fade in reflection
col.a = water.a;
MOV r0.a, c0.a, r2.a;
EndPass;
"
}
}
SetTexture [_BumpMap] {}
SetTexture [_BumpMap] {}
SetTexture [_ColorControl] {}
}
}*/

// -----------------------------------------------------------
//  Old cards

// three texture, cubemaps
Subshader {
Tags { "Queue" = "Transparent" }
Blend SrcAlpha OneMinusSrcAlpha
ColorMask RGB
Pass {
Color (0.5,0.5,0.5,0.5)
SetTexture [_MainTex] {
Matrix [_WaveMatrix]
combine texture * primary
}
SetTexture [_MainTex] {
Matrix [_WaveMatrix2]
combine texture * primary + previous
}
SetTexture [_ColorControlCube] {
combine texture +- previous, primary
Matrix [_Reflection]
}
}
}

// dual texture, cubemaps
Subshader {
Tags { "Queue" = "Transparent" }
Blend SrcAlpha OneMinusSrcAlpha
ColorMask RGB
Pass {
Color (0.5,0.5,0.5,0.5)
SetTexture [_MainTex] {
Matrix [_WaveMatrix]
combine texture
}
SetTexture [_ColorControlCube] {
combine texture +- previous, primary
Matrix [_Reflection]
}
}
}

// single texture
Subshader {
Tags { "RenderType"="Opaque" }
Pass {
Color (0.5,0.5,0.5,0)
SetTexture [_MainTex] {
Matrix [_WaveMatrix]
combine texture, primary
}
}
}

}


Thing it that we don't have access to reflections or post processing, but reflections aren't really needed and the post processing(that is needed to make it look darker the deeper it goes) can be drawn to the terrain instead.

The thing I really need it proper bumpmaps or another shader that is better. If it's possible and simple, displacement of some sort would be welcome as well.

I hope you guys can help us with this.

Regards
Stripe103
Title: Re: [Unity3D] [Shaders] I need help with a water shader
Post by: stripe103 on November 30, 2011, 03:50:35 pm
Noone knows about shaders?
Title: Re: [Unity3D] [Shaders] I need help with a water shader
Post by: Blizzard on November 30, 2011, 06:31:28 pm
I know how they work, but I can't really write any. ._.
Title: Re: [Unity3D] [Shaders] I need help with a water shader
Post by: stripe103 on December 01, 2011, 02:10:02 am
Then you know more than me, at least. :haha:
But do you know where you could get some good water bumpmaps and such for the shader I've got?
Maybe someone at Cateia? *push* *push*  :ninja:
Title: Re: [Unity3D] [Shaders] I need help with a water shader
Post by: Blizzard on December 01, 2011, 04:01:24 am
Nope. ._. I'm probably the one who knows most about them since we're mostly making 2D based games. ._.
But there are other examples for shaders out there. Maybe you should try some of them.
Title: Re: [Unity3D] [Shaders] I need help with a water shader
Post by: stripe103 on December 01, 2011, 12:37:06 pm
Ok. I will try that. Thanks :)