char water_fp_uw_reg [] = "!!ARBfp1.0" "OPTION ARB_precision_hint_fastest;"//uniform vec3 waterfogcolor; "PARAM c[6] = { program.local[0..1],"//uniform time; "{ 0.5, 0, 0.2, 0.15000001 },"//[2] "{ 0.13, 0.11, 0.17, 0.14 },"//[3] "{ 0.16, 1, 0.30000001 },"//[4] "{ 0.06666667, 0 } };"//[5] "TEMP R0;" "TEMP R1;" "TEMP R2;" "MOV R1, c[3];" //R1={0.13, 0.11, 0.17, 0.14}; "MAD R2.xw, R1.yyzz, c[1].x, fragment.texcoord[0].yyzx;"//R2.x = 0.11 * time + g_TexCoord[0].y; R2.w = 0.17 * time + g_TexCoord[0].x; "MOV R0.zw, c[2];" "MAD R0.zw, R0, c[1].x, fragment.texcoord[0].xyxy;" //R0.zw = vec2(0.2, 0.15) * time + g_TexCoord[0];//vec2 vNormTexCoord1 = vec2(0.2, 0.15) * time + g_TexCoord[0]; "MAD R0.x, R1, -c[1], fragment.texcoord[0];" //R0.x = -0.13 * time + g_TexCoord[0].x; "MOV R0.y, R2.x;" //R0.y = 0.11 * time + g_TexCoord[0].y;//vec2 vNormTexCoord2 = vec2(-0.13, 0.11) * time + g_TexCoord[0]; "TEX R1.xyz, R0, texture[0], 2D;" //R1.xyz = texture2D(normalmap, R0.xy);//vec3 vNorm2 = texture2D(normalmap, vNormTexCoord2); "TEX R0.xyz, R0.zwzw, texture[0], 2D;" //R0.xyz = texture2D(normalmap, R0.zw);//vec3 vNorm1 = texture2D(normalmap, vNormTexCoord1); "ADD R2.xyz, R0, R1;" //R2.xyz = R0.xyz+R1.xyz//vec3 vNormal = vNorm1 + vNorm2; "MOV R0.y, R0.w;" //R0.y = R0.w = 0.15 * time + g_TexCoord[0].y; "MOV R0.w, c[4].x;" //R0.w = 0.16; "MAD R1.x, R1.w, -c[1], fragment.texcoord[0];" //R1.x = -0.14 * time + g_TexCoord[0].x; "MAD R1.y, R0.w, -c[1].x, fragment.texcoord[0];" //R1.y = 0.16 * -time + g_TexCoord[0].y;//vec2 vNormTexCoord4 = vec2(-0.14, 0.16) * time + g_TexCoord[0]; "MOV R0.x, R2.w;" //R0.x = R2.w = 0.17 * time + g_TexCoord[0].x;//vec2 vNormTexCoord3 = vec2(0.17, 0.15) * time + g_TexCoord[0]; "TEX R0.xyz, R0, texture[0], 2D;" //R0.xyz = texture2D(normalmap, R0);//vec3 vNorm3 = texture2D(normalmap, vNormTexCoord3); "TEX R1.xyz, R1, texture[0], 2D;" //R1.xyz = texture2D(normalmap, R1);//vec3 vNorm4 = texture2D(normalmap, vNormTexCoord4); "ADD R0.xyz, R2, R0;" // "ADD R0.xyz, R0, R1;" //R0.xyz = R0.xyz + R1.xyz + R2.xyz//vNormal += vNorm3 + vNorm4; "MUL R0.xyz, R0, c[2].x;" //vNormal *= 0.5; "ADD R0.xyz, R0, -c[4].y;" //vNormal -= 1.0; "DP3 R0.z, R0, R0;" // "RSQ R0.z, R0.z;" // "MUL R0.zw, R0.z, R0.xyxy;" //R0.zw = R0.xy * R0.z//vec2 vOffsetTexCoord = normalize(vNormal.xyz).xy; "RCP R0.x, fragment.texcoord[1].w;" //R0.x = (1.0 / projpos.w); "MUL R0.zw, R0, c[4].z;" //vOffsetTexCoord *= 0.3; "MUL R0.xy, R0.x, fragment.texcoord[1];" //R0.xy = projpos.xy * R0.x//vec2 vBaseTexCoord = projpos.xy * (1.0 / projpos.w); "MAD R0.xy, R0, c[2].x, R0.zwzw;" //R0.xy += R0.zw * 0.5//vec2 vRefractTexCoord = vBaseTexCoord * 0.5 + OffsetTexCoord "ADD R0.xy, R0, c[2].x;" //R0.xy += 0.5//vRefractTexCoord += 0.5; "TEX R0, R0, texture[1], 2D;" //R0 = vRefractColor = texture2D(refractmap, vRefractTexCoord); "ADD R2.x, R0, R0.y;" //R2.x = vRefractColor.x + vRefractColor.y "MOV R1.w, c[4].y;" //R1.w = 1.0; "MOV R1.xyz, c[0];" //R1.xyz = waterfogcolor "ADD R1, -R0, R1;" //R1 -= R0;//waterfogcolor = waterfogcolor - vRefractColor; "ADD R2.x, R2, R0.z;" //R2.x = R2.x + R0.Z//R2.x = vRefractColor.x + vRefractColor.y + vRefractColor.z; "MUL R1, R2.x, R1;" //R1 = (waterfogcolor - vRefractColor) * (vRefractColor.x + vRefractColor.y + vRefractColor.z * 1.0) "MAD result.color, R1, c[5].x, R0;" //g_FragColor = (waterfogcolor - vRefractColor) * (vRefractColor.x + vRefractColor.y + vRefractColor.z) * (1.0/15.0) + vRefractColor; "END"; //float fLerp = (vRefractColor.x + vRefractColor.y + vRefractColor.z) / 15.0;g_FragColor = vRefractColor * (1.0 - fLerp) + vec4(waterfogcolor, 1.0) * fLerp;
整理结果:
//Fragment Shader uniform vec3 waterfogcolor; uniform float time; uniform sampler2D normalmap; uniform sampler2D refractmap; varying vec4 projpos; void main() { //calculate the normal texcoord and sample the normal vector from texture vec2 vNormTexCoord1 = vec2(0.2, 0.15) * time + g_TexCoord[0]; vec2 vNormTexCoord2 = vec2(-0.13, 0.11) * time + g_TexCoord[0]; vec2 vNormTexCoord3 = vec2(-0.14, -0.16) * time + g_TexCoord[0]; vec2 vNormTexCoord4 = vec2(0.17, 0.15) * time + g_TexCoord[0]; vec4 vNorm1 = texture2D(normalmap, vNormTexCoord1); vec4 vNorm2 = texture2D(normalmap, vNormTexCoord2); vec4 vNorm3 = texture2D(normalmap, vNormTexCoord3); vec4 vNorm4 = texture2D(normalmap, vNormTexCoord4); vec4 vNormal = vNorm1 + vNorm2 + vNorm3 + vNorm4; vNormal = vNormal * 0.5 - 1.0; //texcoord offset vec2 vOffsetTexCoord = normalize(vNormal.xyz).xy * 0.3; //sample the refract color vec2 vBaseTexCoord = projpos.xy * (1.0 / projpos.w) * 0.5 + 0.5; vec2 vRefractTexCoord = vBaseTexCoord + vOffsetTexCoord; vec4 vRefractColor = texture2D(refractmap, vRefractTexCoord); //lerp waterfog color and refraction color float fLerp = (vRefractColor.x + vRefractColor.y + vRefractColor.z) / 15.0; g_FragColor = vRefractColor * (1.0 - fLerp) + vec4(waterfogcolor, 1.0) * fLerp; }