[1777] | 1 | float4x4 viewProjMatrix;
|
---|
| 2 | float time;
|
---|
| 3 | float3 eyePosition;
|
---|
| 4 | float3 eyePositionPushedBack;
|
---|
| 5 | float3 lightPosition;
|
---|
| 6 |
|
---|
| 7 | float4x4 shipViewProjMatrix;
|
---|
| 8 | float4x4 shipWorldMatrix;
|
---|
| 9 |
|
---|
| 10 | float3 worldCornerCar0;
|
---|
| 11 | float3 worldCornerCar1;
|
---|
| 12 | float3 worldCornerCar2;
|
---|
| 13 | float3 worldCornerCar3;
|
---|
| 14 |
|
---|
| 15 | float4 diffuseColor;
|
---|
| 16 |
|
---|
| 17 | textureCUBE environmentCubeTexture;
|
---|
| 18 | texture2D waterTexture;
|
---|
| 19 |
|
---|
| 20 | samplerCUBE environmentCubeSampler = sampler_state
|
---|
| 21 | {
|
---|
| 22 | texture = <environmentCubeTexture>;
|
---|
| 23 | MipFilter = Linear;
|
---|
| 24 | MagFilter = Linear;
|
---|
| 25 | MinFilter = Linear;
|
---|
| 26 |
|
---|
| 27 | };
|
---|
| 28 |
|
---|
| 29 | sampler2D waterSampler = sampler_state
|
---|
| 30 | {
|
---|
| 31 | texture = <waterTexture>;
|
---|
| 32 | MipFilter = Linear;
|
---|
| 33 | MagFilter = Linear;
|
---|
| 34 | MinFilter = Linear;
|
---|
| 35 | AddressU = WRAP;
|
---|
| 36 | AddressV = WRAP;
|
---|
| 37 | };
|
---|
| 38 |
|
---|
| 39 | //illumination calculation:
|
---|
| 40 | //float4 Illumination(float3 light, float3 normal, float3 view, float2 texcoord, float3 lightIntensity)
|
---|
| 41 | float4 Illumination(float3 light, float3 normal, float3 view, float4 diffuse, float3 lightIntensity)
|
---|
| 42 | {
|
---|
| 43 | float3 hv = normalize(view + light);
|
---|
| 44 | float costheta = dot(normal, light);
|
---|
| 45 |
|
---|
| 46 | if(costheta < 0)
|
---|
| 47 | costheta = 0;
|
---|
| 48 |
|
---|
| 49 | float cosdelta = dot(hv, normal);
|
---|
| 50 | if(cosdelta < 0 || costheta < 0)
|
---|
| 51 | cosdelta = 0;
|
---|
| 52 |
|
---|
| 53 | //compute the diffuse color here using the textures... missing ATM
|
---|
| 54 | float3 ks = float3(1,1,1);
|
---|
| 55 | float shininess = 200;
|
---|
| 56 |
|
---|
| 57 | float3 color = (diffuse * costheta + ks * pow(cosdelta, shininess)) * lightIntensity;
|
---|
| 58 | return float4(color,1);
|
---|
| 59 |
|
---|
| 60 | };
|
---|
| 61 |
|
---|
| 62 | float4x4 shipLocations[2];
|
---|
| 63 |
|
---|
| 64 | #define WAVE_FX
|
---|
| 65 | #include "waves.h"
|
---|
| 66 | #undef WAVE_FX
|
---|
| 67 |
|
---|
| 68 | struct vsOceanInput
|
---|
| 69 | {
|
---|
| 70 | float4 pos : POSITION;
|
---|
| 71 | };
|
---|
| 72 |
|
---|
| 73 |
|
---|
| 74 | struct vsOceanOutput
|
---|
| 75 | {
|
---|
| 76 | float4 pos : POSITION;
|
---|
| 77 | float3 normal : NORMAL;
|
---|
| 78 | float3 worldPos : TEXCOORD0;
|
---|
| 79 | float3 tangent : TEXCOORD1;
|
---|
| 80 | float3 binormal : TEXCOORD2;
|
---|
| 81 |
|
---|
| 82 | float2 bumpCoord0 : TEXCOORD3;
|
---|
| 83 | float2 bumpCoord1 : TEXCOORD4;
|
---|
| 84 | float2 bumpCoord2 : TEXCOORD5;
|
---|
| 85 |
|
---|
| 86 | float foam : TEXCOORD6;
|
---|
| 87 | };
|
---|
| 88 |
|
---|
| 89 | vsOceanOutput vsOcean(vsOceanInput input)
|
---|
| 90 | {
|
---|
| 91 | vsOceanOutput output = (vsOceanOutput)0;
|
---|
| 92 | float3 inventedVariable = lerp(lerp(worldCornerCar0, worldCornerCar1, input.pos.x), lerp(worldCornerCar2, worldCornerCar3, input.pos.x), input.pos.z);
|
---|
| 93 | if(inventedVariable.y * eyePositionPushedBack.y > 0) //looking above the horizon
|
---|
| 94 | {
|
---|
| 95 | input.pos.xyz = eyePositionPushedBack + inventedVariable * 1000000;
|
---|
| 96 | input.pos.y = +1;
|
---|
| 97 | }
|
---|
| 98 | else
|
---|
| 99 | input.pos.xyz = eyePositionPushedBack - (eyePositionPushedBack.y/inventedVariable.y) * inventedVariable;
|
---|
| 100 |
|
---|
| 101 |
|
---|
| 102 | float t = fmod(time, 100.0);
|
---|
| 103 | output.bumpCoord0.xy = input.pos.xz*0.006 + t*(0.02);
|
---|
| 104 | output.bumpCoord1.xy = input.pos.xz*0.012 + t*(0.03);
|
---|
| 105 | output.bumpCoord2.xy = input.pos.xz*0.024 + t*(0.04);
|
---|
| 106 |
|
---|
| 107 | float3 du = float3(1, 0, 0);
|
---|
| 108 | float3 dv = float3(0, 0, 1);
|
---|
| 109 |
|
---|
| 110 | for (int i=0;i<NWAVES;i++)
|
---|
| 111 | {
|
---|
| 112 | float phase = (input.pos.x * wave[i].direction.x + input.pos.z * wave[i].direction.y) / wave[i].wavelength / 6.28 + time * sqrt(1.5915 * wave[i].wavelength);
|
---|
| 113 | input.pos.xyz += evaluateWave(wave[i],phase);
|
---|
| 114 | float3 da = evaluateWaveDerivative(wave[i], phase);
|
---|
| 115 | du += da * wave[i].direction.x / wave[i].wavelength / 6.28;
|
---|
| 116 | dv += da * wave[i].direction.y / wave[i].wavelength / 6.28;
|
---|
| 117 | output.foam += da;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | output.pos = mul(float4(input.pos.xyz, 1), viewProjMatrix);
|
---|
| 121 | output.worldPos = input.pos.xyz;
|
---|
| 122 |
|
---|
| 123 | output.binormal = normalize(du) * 0.3;
|
---|
| 124 | output.tangent = normalize(dv) * 0.3;
|
---|
| 125 | output.normal = normalize(cross(dv,du));
|
---|
| 126 |
|
---|
| 127 | return output;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | float4 psOcean(vsOceanOutput input) : COLOR0
|
---|
| 131 | {
|
---|
| 132 | // return float4(input.eyeVector,1);
|
---|
| 133 |
|
---|
| 134 | half4 t0 = tex2D(waterSampler, input.bumpCoord0) * 2.0 -1.0;
|
---|
| 135 | half4 t1 = tex2D(waterSampler, input.bumpCoord1) * 2.0 -1.0;
|
---|
| 136 | half4 t2 = tex2D(waterSampler, input.bumpCoord2) * 2.0 -1.0;
|
---|
| 137 |
|
---|
| 138 | half3 N = (t0 + t1 + t2).xzy;
|
---|
| 139 |
|
---|
| 140 | half3x3 m;
|
---|
| 141 | m[0] = input.tangent;
|
---|
| 142 | m[2] = input.binormal;
|
---|
| 143 | m[1] = input.normal;
|
---|
| 144 |
|
---|
| 145 | half3 normalWorld = mul(m, N);
|
---|
| 146 | normalWorld = normalize(normalWorld);
|
---|
| 147 |
|
---|
| 148 | // normalWorld = input.normal; //test
|
---|
| 149 |
|
---|
| 150 | // return float4(normalWorld,0);
|
---|
| 151 | // return float4(reflect(-normalize(input.eyeVector), normalWorld), 0);
|
---|
| 152 |
|
---|
| 153 | float3 viewDir = -normalize(eyePosition - input.worldPos);
|
---|
| 154 | float3 reflDir = reflect(viewDir, normalWorld);
|
---|
| 155 | // ray = worldPos + relfDir * t
|
---|
| 156 |
|
---|
| 157 |
|
---|
| 158 | float4 reflectedColor = texCUBE(environmentCubeSampler, reflDir);
|
---|
| 159 | for(int iShip=0; iShip<2; iShip++)
|
---|
| 160 | {
|
---|
| 161 | float3 rayOrigin = mul(shipLocations[iShip], float4(input.worldPos, 1));
|
---|
| 162 | float3 rayDir = mul(shipLocations[iShip], float4(reflDir, 0));
|
---|
| 163 | float3 delta = rayDir / float3(8.0, 10.0, 1.0);
|
---|
| 164 | float3 alpha = rayOrigin / float3(8.0, 10.0, 1.0);
|
---|
| 165 | float a = dot(delta, delta);
|
---|
| 166 | float b = 2 * dot(delta, alpha);
|
---|
| 167 | float c = dot(alpha, alpha) - 1;
|
---|
| 168 |
|
---|
| 169 | float discriminant = b*b - 4 * a * c;
|
---|
| 170 |
|
---|
| 171 | if(discriminant > 0)
|
---|
| 172 | {
|
---|
| 173 | discriminant = sqrt(discriminant);
|
---|
| 174 | float t1 = (-b + discriminant) / 2 / a;
|
---|
| 175 | float t2 = (-b - discriminant) / 2 / a;
|
---|
| 176 | if(min(t1,t2) > 0)
|
---|
| 177 | reflectedColor = float4(1.0, 0.5, 0.0, 1.0);
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | float facing = 1.0 - max(dot(-viewDir, normalWorld),0);
|
---|
| 182 |
|
---|
| 183 | float fresnel = .02 + .98 * pow(facing, 5);
|
---|
| 184 |
|
---|
| 185 | float4 shallowColor = float4 (0.34, 0.5, 0.33,0);
|
---|
| 186 | float4 deepColor = float4(0.07, 0.19, 0.16,0);
|
---|
| 187 |
|
---|
| 188 | float4 waterColor = lerp (deepColor, shallowColor, facing);
|
---|
| 189 |
|
---|
| 190 | return reflectedColor * fresnel * 0.5 + waterColor * .75;// * (1 + max(0, -0.5 - input.foam / 6.0));
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | /***************************************************************************************************************************/
|
---|
| 194 | struct vsShipInput
|
---|
| 195 | {
|
---|
| 196 | float4 pos : POSITION;
|
---|
| 197 | float3 normal : NORMAL0;
|
---|
| 198 | float3 oceanTangent : TEXCOORD0;
|
---|
| 199 | float3 oceanBinormal : TEXCOORD1;
|
---|
| 200 | float3 shipPos : TEXCOORD2;
|
---|
| 201 | };
|
---|
| 202 |
|
---|
| 203 | struct vsShipOutput
|
---|
| 204 | {
|
---|
| 205 | float4 pos : POSITION;
|
---|
| 206 | float3 normal : NORMAL;
|
---|
| 207 | float4 worldPos : TEXCOORD1;
|
---|
| 208 | float3 view : TEXCOORD2;
|
---|
| 209 | float2 texcoords : TEXCOORD3; //reserved
|
---|
| 210 | };
|
---|
| 211 |
|
---|
| 212 | vsShipOutput vsShip(vsShipInput input)
|
---|
| 213 | {
|
---|
| 214 | vsShipOutput output = (vsShipOutput)0;
|
---|
| 215 |
|
---|
| 216 | output.worldPos = mul(input.pos, shipWorldMatrix);
|
---|
| 217 | output.worldPos /= output.worldPos.w; //test the sphere ship
|
---|
| 218 |
|
---|
| 219 | float3 oceanNormal = normalize(cross(input.oceanTangent, input.oceanBinormal));
|
---|
| 220 |
|
---|
| 221 | output.worldPos.xyz = input.oceanTangent * output.worldPos.z + input.oceanBinormal * output.worldPos.x + oceanNormal * output.worldPos.y;
|
---|
| 222 |
|
---|
| 223 | output.worldPos += float4(input.shipPos, 0);
|
---|
| 224 | output.view = eyePosition - output.worldPos;
|
---|
| 225 | output.normal = mul(input.normal, shipWorldMatrix);
|
---|
| 226 | output.pos = mul(output.worldPos, viewProjMatrix);
|
---|
| 227 |
|
---|
| 228 | return output;
|
---|
| 229 | };
|
---|
| 230 |
|
---|
| 231 | float4 psShip(vsShipOutput input) : COLOR0
|
---|
| 232 | {
|
---|
| 233 | float3 light = normalize(lightPosition);
|
---|
| 234 | float3 normal = normalize(input.normal);
|
---|
| 235 | float3 view = normalize(input.view);
|
---|
| 236 |
|
---|
| 237 | //Illumination: (light, normal, view, diffuse(or texture color), light intensity)
|
---|
| 238 | return float4(1.0, 0.6, 0.3, 1.0);
|
---|
| 239 | return Illumination(light, normal, view, float4(0.1,0.1,0.6,1), float3(3,3,3));
|
---|
| 240 |
|
---|
| 241 | };
|
---|
| 242 |
|
---|
| 243 | technique ocean
|
---|
| 244 | {
|
---|
| 245 | pass P0
|
---|
| 246 | {
|
---|
| 247 | VertexShader = compile vs_3_0 vsOcean();
|
---|
| 248 | PixelShader = compile ps_3_0 psOcean();
|
---|
| 249 |
|
---|
| 250 | }
|
---|
| 251 | };
|
---|
| 252 |
|
---|
| 253 | technique ship
|
---|
| 254 | {
|
---|
| 255 | pass P0
|
---|
| 256 | {
|
---|
| 257 | VertexShader = compile vs_3_0 vsShip();
|
---|
| 258 | PixelShader = compile ps_3_0 psShip();
|
---|
| 259 |
|
---|
| 260 | }
|
---|
| 261 | };
|
---|
| 262 |
|
---|
| 263 | #include "terrain.fx" |
---|