/*============================================================================= MagmaShader.cg: Shaders for drawing element in magma Author:Lee =============================================================================*/ #include "Common.cg" #define MAX(a, b) (a > b ? a : b) #define MIN(a, b) (a < b ? a : b) sampler TileTexture; float4 upleftpoint; float4 uprightpoint; float4 downrightpoint; float4 downleftpoint; float4 UVRange; float1 MaskZ; float4 Coef; // texture blend struct pixelShaderInput { float4 Position : POSITION; float2 Texture : TEXCOORD0; float4 Color : TEXCOORD1; }; float4 RGBtoHSL(float4 RGBcolor) { float4 HSL = float4(0.f,0.f,0.f,0.f); float delta; float r = (float)RGBcolor.r; float g = (float)RGBcolor.g; float b = (float)RGBcolor.b; float cmax = max(r,max(g,b)); float cmin = min(r,min(g,b)); HSL.z = (cmax+cmin)/2.0;//valur:L if (cmax == cmin) { HSL.y = 0; HSL.x = 0; // it's really undefined } else { if (HSL.z < 0.5)//(*L < 0.5) HSL.y = (cmax-cmin)/(cmax+cmin);//*S = (cmax-cmin)/(cmax+cmin); else HSL.y = (cmax-cmin)/(2.0-cmax-cmin);//*S = (cmax-cmin)/(2.0-cmax-cmin); delta = cmax - cmin; if (r==cmax) HSL.x = (g-b)/delta;//*H = (g-b)/delta; else if (g==cmax) HSL.x = 2.0 +(b-r)/delta;//*H = 2.0 +(b-r)/delta; else HSL.x = 4.0+(r-g)/delta;//*H = 4.0+(r-g)/delta; HSL.x = HSL.x /6.f;//*H /= 6.0; if(HSL.x < 0.f)//if (*H < 0.0) HSL.x = HSL.x +1;//*H += 1; if(HSL.x > 1.f)//if (*H < 0.0) HSL.x = HSL.x - 1;//*H += 1; } return HSL; } float1 HuetoRGB(float1 q1, float1 q2, float1 hue) { float1 Value; if(hue>1.f) hue=hue-1.f; if(hue<0.f) hue=hue+1.f; if(6.f * hue<1.f) Value=q1+(q2-q1) * hue * 6.f; else if(2.f * hue<1.f) Value=q2; else if(3.f * hue<2.f) Value=q1+(q2-q1)*(2.f / 3.f -hue) * 6.f; else Value=q1; return Value; } float4 HLStoRGB(float4 HSL) { float r,g,b; float m1, m2; float H = HSL.x; float S = HSL.y; float L = HSL.z; if (S==0) { r=g=b=L; } else { if (L <= 0.5) m2 = L*(1.0+S); else m2 = L+S-L*S; m1 = 2.0*L-m2; r = HuetoRGB(m1, m2, H+1.f/3.f); g = HuetoRGB(m1, m2, H); b = HuetoRGB(m1, m2, H-1.f/3.f); } return float4((float1)r,(float1)g,(float1)b,0.f); } float2 CorrectUV( in float2 tc ) { //calculate UV float1 x1 = upleftpoint.x/upleftpoint.w; float1 y1 = upleftpoint.y/upleftpoint.w; float1 x2 = uprightpoint.x/uprightpoint.w; float1 y2 = uprightpoint.y/uprightpoint.w; float1 x3 = downrightpoint.x/downrightpoint.w; float1 y3 = downrightpoint.y/downrightpoint.w; float1 x4 = downleftpoint.x/downleftpoint.w; float1 y4 = downleftpoint.y/downleftpoint.w; float1 xc = tc.x; float1 yc = tc.y; float1 us = UVRange.x; float1 vs = UVRange.y; float1 ue = UVRange.z; float1 ve = UVRange.w; float1 newU = 0.f; float1 newV = 0.f; float1 xn = (xc - us) / (ue - us); float1 yn = (yc - vs) / (ve - vs); float1 coefuA = 0.f; float1 coefuB = 0.f; float1 coefuC = 0.f; float1 coefvA = 0.f; float1 coefvB = 0.f; float1 coefvC = 0.f; float1 nxc = 0.f; float1 nyc = 0.f; if(xn >= yn) //move dot 4 { float1 nx4 = ((x4 - x1)*(y3 - y2) - (y4 - y1)*(x3 - x2)) / ((x2 - x1)*(y3 - y2) - (x3 - x2)*(y2 - y1)); float1 ny4 = ((x4 - x1)*(y2 - y1) - (y4 - y1)*(x2 - x1)) / ((x3 - x2)*(y2 - y1) - (x2 - x1)*(y3 - y2)); nxc = xn; nyc = yn; coefuA = ny4 -1.f; coefuB = nx4*nyc - nxc*ny4 + nxc - ny4; coefuC = nxc*ny4 - nx4*nyc; coefvA = nx4; coefvB = ny4*nxc - nyc*nx4 - ny4 - nxc; coefvC = nyc; } else //move dot 2 { float1 nx2 = ((x2 - x1)*(y4 - y1) - (y2 - y1)*(x4 - x1)) / ((x3 - x4)*(y4 - y1) - (x4 - x1)*(y3 - y4)); float1 ny2 = ((x2 - x1)*(y3 - y4) - (y2 - y1)*(x3 - x4)) / ((x4 - x1)*(y3 - y4) - (x3 - x4)*(y4 - y1)); nxc = xn; nyc = yn; coefuA = ny2; coefuB = nx2*nyc - ny2*nxc - nyc - nx2; coefuC = nxc; coefvA = nx2 - 1.f; coefvB = nxc*ny2 - nx2*nyc + nyc - nx2 ; coefvC = nx2*nyc - nxc*ny2; } if(abs(coefuA) <= 0.0001f) { newU = nxc;//-coefuB/coefuC; } else { newU = (-coefuB - sqrt(coefuB*coefuB - 4*coefuA*coefuC))/(2*coefuA); } if(abs(coefvA) <= 0.0001f) { newV = nyc;//-coefvB/coefvC; } else { newV = (-coefvB - sqrt(coefvB*coefvB - 4*coefvA*coefvC))/(2*coefvA); } float2 result; result.x = newU*(ue-us)+us; result.y = newV*(ve-vs)+vs; return result; } // // DoTextureBlend // for simulate texture blend mode // float4 DoTextureBlend( in float2 tc, in float4 color, in sampler2D tex ) { float4 OutColor; OutColor = (Coef.x + Coef.y*color)*(Coef.z + Coef.w * tex2D(tex, tc)); return OutColor; // return float4(pow(OutColor.rgb, 2.2), OutColor.a); } float4 pixelshader(pixelShaderInput input) : COLOR0 { float4 Color; Color = tex2D(TileTexture, input.Texture); Color *= input.Color; return Color; } //////////////////////////////////////////////////////////////////////////////////////////////////// struct Bink_VertexShaderOutput { float4 Position : POSITION; float2 TexCoord : TEXCOORD0; }; Bink_VertexShaderOutput vertexshader_bink(float3 Position : POSITION, float2 TexCoord : TEXCOORD0) { Bink_VertexShaderOutput Result; Result.Position = float4(Position.x, Position.y, Position.z / 1000.0f, 1.0f); Result.TexCoord = TexCoord; return Result; } sampler BinkTextureY; sampler BinkTextureU; sampler BinkTextureV; sampler BinkTextureA; float UseAlpha; float2 TexAdjustY; float2 TexAdjustU; float4 pixelshader_bink(float4 Position : POSITION, float2 TexCoord : TEXCOORD0) : COLOR0 { const float4 crc = { 1.595794678, -0.813476563, 0.0, 0.0 }; const float4 crb = { 0.0, -0.391448975, 2.017822266, 0.0 }; const float4 adj = { -0.87065506, 0.529705048, -1.081668854, 0.0 }; float2 TexY = TexCoord * TexAdjustY; float2 TexU = TexCoord * TexAdjustU; float y = tex2D(BinkTextureY, TexY).r; float u = tex2D(BinkTextureU, TexU).r; float v = tex2D(BinkTextureV, TexU).r; float a = 1.0; if (UseAlpha > 0.0) { a = tex2D(BinkTextureA, TexY).r; } else if (UseAlpha < 0.0) { a = 0.0; } float4 color = y * 1.164123535; color += crc * u; color += crb * v; color += adj; color.a = a; return color; } //////////////////////////////////////////////////////////////////////////////////////////////////// float4 pixelshader_correctuv(pixelShaderInput input) : COLOR0 { //return tex2D(TileTexture,input.Texture) * input.Color; return DoTextureBlend(CorrectUV(input.Texture), input.Color, TileTexture); //float4 oColor = DoTextureBlend(CorrectUV(input.Texture), input.Color, TileTexture);; //oColor.a = 1.0f; //return tex2D(CorrectUV(input.Texture),TileTexture); //return float4(CorrectUV(input.Texture),0,1.f); } float4 pixelshader_blend(pixelShaderInput input) : COLOR0 //rectangle is assumed { //return tex2D(TileTexture,input.Texture) * input.Color; float4 upleft = upleftpoint/upleftpoint.w; float4 downright = downrightpoint/downrightpoint.w; float1 currentposX = upleft.x + (downright.x - upleft.x) * (input.Texture.x - UVRange.x) / (UVRange.z - UVRange.x); float1 currentposY = upleft.y + (downright.y - upleft.y) * (input.Texture.y - UVRange.y) / (UVRange.w - UVRange.y); float2 PreTexUV = float2((currentposX+1.f)/2.f, (-currentposY+1.f)/2.f); float4 targetRGB = DoTextureBlend(input.Texture, input.Color, TileTexture); float4 targetHSL = RGBtoHSL(targetRGB); float4 sourceRGB = tex2D(PreviousLightingTexture, PreTexUV); float4 sourceHSL = RGBtoHSL(sourceRGB); float4 blendHSL = float4(targetHSL.x, targetHSL.y, sourceHSL.z,0.f); float4 blendRGB = HLStoRGB(blendHSL); //blendRGB.a = targetRGB.a; blendRGB.a = 1.f; return blendRGB; } struct vertexShaderOutput { float4 Position : POSITION; float2 Texture : TEXCOORD0; float4 Color : TEXCOORD1; }; struct vertexShaderInput { float3 Position : POSITION; float2 Texture : TEXCOORD0; float4 Color : TEXCOORD1; }; vertexShaderOutput vertexshader(vertexShaderInput Input) { vertexShaderOutput Out; Out.Position = float4(Input.Position.x, Input.Position.y, Input.Position.z / 1000.0f, 1.0f); Out.Color = Input.Color.bgra; Out.Texture = Input.Texture; return Out; }