| 1 | // oceanHLSL_Cg.frag | 
|---|
| 2 | // fragment program for Ocean water simulation | 
|---|
| 3 | // 04 Aug 2005 | 
|---|
| 4 | // adapted for Ogre by nfz | 
|---|
| 5 | // original shader source from Render Monkey 1.6 Reflections Refractions.rfx | 
|---|
| 6 | // can be used in both Cg and HLSL compilers | 
|---|
| 7 |  | 
|---|
| 8 | // 06 Aug 2005: moved uvw calculation from fragment program into vertex program  | 
|---|
| 9 |  | 
|---|
| 10 | float4 main(float3 uvw: TEXCOORD0, float3 normal: TEXCOORD1, float3 vVec: TEXCOORD2, | 
|---|
| 11 |         uniform float fadeBias, | 
|---|
| 12 |         uniform float fadeExp, | 
|---|
| 13 |         uniform float4 waterColor, | 
|---|
| 14 |         uniform sampler Noise, | 
|---|
| 15 |         uniform sampler skyBox | 
|---|
| 16 |  | 
|---|
| 17 | ) : COLOR | 
|---|
| 18 | { | 
|---|
| 19 |  | 
|---|
| 20 |    float3 noisy = tex3D(Noise, uvw).xyz; | 
|---|
| 21 |  | 
|---|
| 22 |    // convert to Signed noise  | 
|---|
| 23 |    float3 bump = 2 * noisy - 1; | 
|---|
| 24 |    bump.xz *= 0.15; | 
|---|
| 25 |    // Make sure the normal always points upwards | 
|---|
| 26 |    // note that Ogres y axis is vertical (RM Z axis is vertical) | 
|---|
| 27 |    bump.y = 0.8 * abs(bump.y) + 0.2; | 
|---|
| 28 |    // Offset the surface normal with the bump | 
|---|
| 29 |    bump = normalize(normal + bump); | 
|---|
| 30 |  | 
|---|
| 31 |    // Find the reflection vector | 
|---|
| 32 |    float3 normView = normalize(vVec); | 
|---|
| 33 |    float3 reflVec = reflect(normView, bump); | 
|---|
| 34 |    // Ogre has z flipped for cubemaps | 
|---|
| 35 |    reflVec.z = -reflVec.z; | 
|---|
| 36 |    float4 refl = texCUBE(skyBox, reflVec); | 
|---|
| 37 |  | 
|---|
| 38 |    // set up for fresnel calc | 
|---|
| 39 |    float lrp = 1 - dot(-normView, bump); | 
|---|
| 40 |     | 
|---|
| 41 |    // Interpolate between the water color and reflection for fresnel effect | 
|---|
| 42 |    return lerp(waterColor, refl, saturate(fadeBias + pow(lrp, fadeExp))); | 
|---|
| 43 |  | 
|---|
| 44 | } | 
|---|