[2453] | 1 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 2 | //
|
---|
| 3 | // ## ######
|
---|
| 4 | // ###### ###
|
---|
| 5 | // ## ############### Shark 3D Engine (www.shark3d.com)
|
---|
| 6 | // ########## # # #
|
---|
| 7 | // ######## Copyright (c) 1996-2006 Spinor GmbH.
|
---|
| 8 | // ######### # # # All rights reserved.
|
---|
| 9 | // ## ##########
|
---|
| 10 | // ##
|
---|
| 11 | //
|
---|
| 12 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 13 |
|
---|
| 14 |
|
---|
| 15 | #define NITERATION 4
|
---|
| 16 |
|
---|
| 17 | struct PS_INPUT
|
---|
| 18 | {
|
---|
| 19 | float3 eye : TEXCOORD0;
|
---|
| 20 | float3 normal : TEXCOORD1;
|
---|
| 21 | float3 posView : TEXCOORD2;
|
---|
| 22 | };
|
---|
| 23 |
|
---|
| 24 | sampler colorSamp: register(s0);
|
---|
| 25 | sampler distanceSamp: register(s1);
|
---|
| 26 |
|
---|
| 27 | float readDistanceCubeMap(samplerCUBE m, float3 r)
|
---|
| 28 | {
|
---|
| 29 | float dist = texCUBE(m, r).r;
|
---|
| 30 | if(dist == 0) dist = 10;
|
---|
| 31 | return dist;
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | half3 Hit(samplerCUBE tex, half3 R, half3 p0)
|
---|
| 35 | {
|
---|
| 36 | float rl = texCUBE(tex, R).r;
|
---|
| 37 | float dp = rl - dot(p0, R);
|
---|
| 38 | float3 p = p0 + R * dp;
|
---|
| 39 | float ppp = length(p) / texCUBE(tex, p).r;
|
---|
| 40 | float dun = 0, dov = 0, pun = ppp, pov = ppp;
|
---|
| 41 | if (ppp < 1) dun = dp; else dov = dp;
|
---|
| 42 | float dl = max( dp + rl * (1 - ppp), 0);
|
---|
| 43 | float3 RR = p0 + R * dl;
|
---|
| 44 |
|
---|
| 45 | for(int i = 0; i < NITERATION; i++) {
|
---|
| 46 | float ddl;
|
---|
| 47 | float llp = length(RR) / readDistanceCubeMap(tex, RR);
|
---|
| 48 | if (llp < 1) {
|
---|
| 49 | dun = dl; pun = llp;
|
---|
| 50 | ddl = (dov == 0) ? rl * (1 - llp) :
|
---|
| 51 | (dl - dov) * (1 - llp)/(llp - pov);
|
---|
| 52 | } else {
|
---|
| 53 | dov = dl; pov = llp;
|
---|
| 54 | ddl = (dun == 0) ? rl * (1 - llp) :
|
---|
| 55 | (dl - dun) * (1 - llp)/(llp - pun);
|
---|
| 56 | }
|
---|
| 57 | dl = max(dl + ddl, 0);
|
---|
| 58 | RR = p0 + R * dl;
|
---|
| 59 | }
|
---|
| 60 | return RR;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 |
|
---|
| 64 |
|
---|
| 65 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 66 | // Pixelshader
|
---|
| 67 | // Profile: 3x0
|
---|
| 68 | // reflective
|
---|
| 69 |
|
---|
| 70 |
|
---|
| 71 | float4 main(PS_INPUT input): COLOR0
|
---|
| 72 | {
|
---|
| 73 | // return texCUBE(colorSamp, input.normal);
|
---|
| 74 | //return float4(input.normal, 1);
|
---|
| 75 |
|
---|
| 76 | const float g_FresnelCoeff = 1.0;
|
---|
| 77 | const float g_Transparency = 0.0;
|
---|
| 78 | const float g_RefractFactor = 1.0f;
|
---|
| 79 |
|
---|
| 80 | float3 normal = normalize(input.normal);
|
---|
| 81 | float3 eye = normalize(input.eye);
|
---|
| 82 |
|
---|
| 83 | float3 r1 = reflect(eye, normal);
|
---|
| 84 | float3 RR1 = Hit(distanceSamp, r1, input.posView);
|
---|
| 85 |
|
---|
| 86 | // fresnel
|
---|
| 87 | float3 fresnel = saturate(g_FresnelCoeff + (1.0f - g_FresnelCoeff) * pow(1 - dot(normal, -eye), 5));
|
---|
| 88 |
|
---|
| 89 | return texCUBE(colorSamp,RR1) * float4(fresnel, 1);
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 93 |
|
---|
| 94 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 95 | // Pixelshader
|
---|
| 96 | // Profile: 3x0
|
---|
| 97 | // refractive
|
---|
| 98 |
|
---|
| 99 | float4 refract(PS_INPUT input): COLOR0
|
---|
| 100 | {
|
---|
| 101 | // return texCUBE(colorSamp, input.normal);
|
---|
| 102 | //return float4(input.normal, 1);
|
---|
| 103 |
|
---|
| 104 | const float3 g_FresnelCoeff = 0.0;
|
---|
| 105 | const float g_Transparency = 0.3;
|
---|
| 106 | const float g_RefractFactor = 0.8f;
|
---|
| 107 |
|
---|
| 108 | float3 normal = normalize(input.normal);
|
---|
| 109 | float3 eye = normalize(input.eye);
|
---|
| 110 |
|
---|
| 111 | float3 r1 = reflect(eye, normal);
|
---|
| 112 | float3 r2 = refract(eye, normal, g_RefractFactor);
|
---|
| 113 | float3 RR1 = Hit(distanceSamp, r1, input.posView);
|
---|
| 114 | float3 RR2 = Hit(distanceSamp, r2, input.posView);
|
---|
| 115 |
|
---|
| 116 | // fresnel
|
---|
| 117 | float3 fresnel = saturate(g_FresnelCoeff + (1.0f - g_FresnelCoeff) * pow(1 - dot(normal, -eye), 5));
|
---|
| 118 |
|
---|
| 119 | //return 1;
|
---|
| 120 | return texCUBE(colorSamp,RR2)*(1- g_Transparency) + texCUBE(colorSamp, RR1) * g_Transparency;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 124 |
|
---|
| 125 | ///////////////////////////////////////////////////////////////////////////////
|
---|
| 126 | // Pixelshader
|
---|
| 127 | // Profile: 3x0
|
---|
| 128 | // metal
|
---|
| 129 | float4 Fresnel;
|
---|
| 130 |
|
---|
| 131 | float4 metal(PS_INPUT input): COLOR0
|
---|
| 132 | {
|
---|
| 133 |
|
---|
| 134 | //const float3 g_FresnelCoeff = 0.7;
|
---|
| 135 | const float g_Transparency = 0.0;
|
---|
| 136 | const float g_RefractFactor = 1.0f;
|
---|
| 137 |
|
---|
| 138 | float3 normal = normalize(input.normal);
|
---|
| 139 | float3 eye = normalize(input.eye);
|
---|
| 140 |
|
---|
| 141 | float3 r1 = reflect(eye, normal);
|
---|
| 142 | float3 RR1 = Hit(distanceSamp, r1, input.posView);
|
---|
| 143 |
|
---|
| 144 |
|
---|
| 145 | // fresnel
|
---|
| 146 | float4 fresnel = saturate(Fresnel + (1.0f - Fresnel) * pow(1 - dot(normal, -eye), 5));
|
---|
| 147 | // fresnel = float4(0.95, 0.63, 0.54,1);
|
---|
| 148 | return texCUBE(colorSamp,RR1) * fresnel;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | /////////////////////////////////////////////////////////////////////////////// |
---|