X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=dali-toolkit%2Finternal%2Fgraphics%2Fshaders%2Fgradient-visual-shader.frag;h=752ecdbd27b67b072e7ed53915bbac1a29f92c63;hb=663892e5b10e7c9b2f23e2e99b5b7865784f1d0a;hp=f859a24a1ffe9660c8cd8439a38b8ca6835e6b09;hpb=8f2687c154d9fc859c58e5ee2fb42a18f9fe6e78;p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git diff --git a/dali-toolkit/internal/graphics/shaders/gradient-visual-shader.frag b/dali-toolkit/internal/graphics/shaders/gradient-visual-shader.frag index f859a24..752ecdb 100644 --- a/dali-toolkit/internal/graphics/shaders/gradient-visual-shader.frag +++ b/dali-toolkit/internal/graphics/shaders/gradient-visual-shader.frag @@ -1,33 +1,28 @@ -#ifndef IS_REQUIRED_ROUNDED_CORNER -#define IS_REQUIRED_ROUNDED_CORNER 0 -#endif -#ifndef IS_REQUIRED_BORDERLINE -#define IS_REQUIRED_BORDERLINE 0 -#endif -#ifndef RADIAL -#define RADIAL 0 -#endif - INPUT mediump vec2 vTexCoord; -#if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE +#if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE) INPUT mediump vec2 vPosition; INPUT mediump vec2 vRectSize; INPUT mediump vec2 vOptRectSize; -#if IS_REQUIRED_ROUNDED_CORNER +INPUT mediump float vAliasMargin; +#ifdef IS_REQUIRED_ROUNDED_CORNER INPUT mediump vec4 vCornerRadius; #endif #endif +// scale factor to fit start and end position of gradient. +uniform mediump float uTextureCoordinateScaleFactor; + uniform sampler2D sTexture; // sampler1D? uniform lowp vec4 uColor; uniform lowp vec3 mixColor; -#if IS_REQUIRED_BORDERLINE +#ifdef IS_REQUIRED_BORDERLINE uniform mediump float borderlineWidth; uniform mediump float borderlineOffset; uniform lowp vec4 borderlineColor; +uniform lowp vec4 uActorColor; #endif -#if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE +#if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE) // Global values both rounded corner and borderline use // radius of rounded corner on this quadrant @@ -51,7 +46,7 @@ mediump float gMinInlinePotential = 0.0; void calculateCornerRadius() { -#if IS_REQUIRED_ROUNDED_CORNER +#ifdef IS_REQUIRED_ROUNDED_CORNER gRadius = mix( mix(vCornerRadius.x, vCornerRadius.y, sign(vPosition.x) * 0.5 + 0.5), @@ -65,7 +60,7 @@ void calculatePosition() { gFragmentPosition = abs(vPosition) - vRectSize; gCenterPosition = -gRadius; -#if IS_REQUIRED_BORDERLINE +#ifdef IS_REQUIRED_BORDERLINE gCenterPosition += borderlineWidth * (clamp(borderlineOffset, -1.0, 1.0) + 1.0) * 0.5; #endif gDiff = gFragmentPosition - gCenterPosition; @@ -78,12 +73,12 @@ void calculatePotential() void setupMinMaxPotential() { - gPotentialRange = 1.0; + gPotentialRange = vAliasMargin; gMaxOutlinePotential = gRadius + gPotentialRange; gMinOutlinePotential = gRadius - gPotentialRange; -#if IS_REQUIRED_BORDERLINE +#ifdef IS_REQUIRED_BORDERLINE gMaxInlinePotential = gMaxOutlinePotential - borderlineWidth; gMinInlinePotential = gMinOutlinePotential - borderlineWidth; #else @@ -92,8 +87,8 @@ void setupMinMaxPotential() #endif // reduce defect near edge of rounded corner. - gMaxOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0); - gMinOutlinePotential += clamp(-min(gDiff.x, gDiff.y)/ max(1.0, gRadius) , 0.0, 1.0); + gMaxOutlinePotential += clamp(-min(gDiff.x, gDiff.y) / max(1.0, gRadius), 0.0, 1.0); + gMinOutlinePotential += clamp(-min(gDiff.x, gDiff.y) / max(1.0, gRadius), 0.0, 1.0); } void PreprocessPotential() @@ -107,7 +102,7 @@ void PreprocessPotential() #endif -#if IS_REQUIRED_BORDERLINE +#ifdef IS_REQUIRED_BORDERLINE lowp vec4 convertBorderlineColor(lowp vec4 textureColor) { mediump float potential = gPotential; @@ -120,32 +115,58 @@ lowp vec4 convertBorderlineColor(lowp vec4 textureColor) { // potential is inside borderline range. borderlineOpacity = smoothstep(gMinInlinePotential, gMaxInlinePotential, potential); + + // Muliply borderlineWidth to resolve very thin borderline + borderlineOpacity *= min(1.0, borderlineWidth / gPotentialRange); } - //calculate inside of borderline when outilneColor.a < 1.0 - if(borderlineColor.a < 1.0) + lowp vec3 borderlineColorRGB = borderlineColor.rgb * uActorColor.rgb; + lowp float borderlineColorAlpha = borderlineColor.a * uActorColor.a; + // NOTE : gradient-visual is always preMultiplied. + borderlineColorRGB *= borderlineColorAlpha; + + // Calculate inside of borderline when alpha is between (0.0 1.0). So we need to apply texture color. + // If borderlineOpacity is exactly 0.0, we always use whole texture color. In this case, we don't need to run below code. + // But if borderlineOpacity > 0.0 and borderlineColor.a == 0.0, we need to apply tCornerRadius. + if(borderlineOpacity > 0.0 && borderlineColor.a * borderlineOpacity < 1.0) { - mediump float tCornerRadius = -gCenterPosition; + mediump float tCornerRadius = -gCenterPosition + gPotentialRange; mediump float MaxTexturelinePotential = tCornerRadius + gPotentialRange; mediump float MinTexturelinePotential = tCornerRadius - gPotentialRange; if(potential > MaxTexturelinePotential) { - // potential is out of texture range. use borderline color instead of texture - textureColor = vec4(borderlineColor.xyz, 0.0); + // potential is out of texture range. + textureColor = vec4(0.0); } - else if(potential > MinTexturelinePotential) + else { - // potential is in texture range - textureColor = mix(textureColor, vec4(borderlineColor.xyz, 0.0), smoothstep(MinTexturelinePotential, MaxTexturelinePotential, potential)); + // potential is in texture range. + lowp float textureAlphaScale = mix(1.0, 0.0, smoothstep(MinTexturelinePotential, MaxTexturelinePotential, potential)); + textureColor.a *= textureAlphaScale; + textureColor.rgb *= textureAlphaScale; } - borderlineOpacity *= borderlineColor.a; - return mix(textureColor, vec4(borderlineColor.xyz, 1.0), borderlineOpacity); + + // NOTE : gradient-visual is always preMultiplied. + borderlineColorAlpha *= borderlineOpacity; + borderlineColorRGB *= borderlineOpacity; + // We use pre-multiplied color to reduce operations. + // In here, textureColor and borderlineColorRGB is pre-multiplied color now. + + // Manual blend operation with premultiplied colors. + // Final alpha = borderlineColorAlpha + (1.0 - borderlineColorAlpha) * textureColor.a. + // (Final rgb * alpha) = borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb + // If preMultipliedAlpha == 1.0, just return vec4(rgb*alpha, alpha) + // Else, return vec4((rgb*alpha) / alpha, alpha) + + lowp float finalAlpha = mix(textureColor.a, 1.0, borderlineColorAlpha); + lowp vec3 finalMultipliedRGB = borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb; + return vec4(finalMultipliedRGB, finalAlpha); } - return mix(textureColor, borderlineColor, borderlineOpacity); + return mix(textureColor, vec4(borderlineColorRGB, borderlineColorAlpha), borderlineOpacity); } #endif -#if IS_REQUIRED_ROUNDED_CORNER +#ifdef IS_REQUIRED_ROUNDED_CORNER mediump float calculateCornerOpacity() { mediump float potential = gPotential; @@ -169,29 +190,35 @@ mediump float calculateCornerOpacity() void main() { -#if RADIAL - lowp vec4 textureColor = TEXTURE(sTexture, vec2(length(vTexCoord), 0.5)) * vec4(mixColor, 1.0); +#ifdef RADIAL + mediump float radialTexCoord = ((length(vTexCoord) - 0.5) * uTextureCoordinateScaleFactor) + 0.5; + lowp vec4 textureColor = TEXTURE(sTexture, vec2(radialTexCoord, 0.5)) * vec4(mixColor, 1.0) * uColor; #else - lowp vec4 textureColor = TEXTURE(sTexture, vec2(vTexCoord.y, 0.5)) * vec4(mixColor, 1.0); + lowp vec4 textureColor = TEXTURE(sTexture, vec2(vTexCoord.y, 0.5)) * vec4(mixColor, 1.0) * uColor; #endif -#if IS_REQUIRED_ROUNDED_CORNER || IS_REQUIRED_BORDERLINE +#if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE) // skip most potential calculate for performance if(abs(vPosition.x) < vOptRectSize.x && abs(vPosition.y) < vOptRectSize.y) { - OUT_COLOR = textureColor * uColor; - return; + OUT_COLOR = textureColor; } - PreprocessPotential(); + else + { + PreprocessPotential(); #endif -#if IS_REQUIRED_BORDERLINE - textureColor = convertBorderlineColor(textureColor); +#ifdef IS_REQUIRED_BORDERLINE + textureColor = convertBorderlineColor(textureColor); #endif - OUT_COLOR = textureColor * uColor; + OUT_COLOR = textureColor; -#if IS_REQUIRED_ROUNDED_CORNER - mediump float opacity = calculateCornerOpacity(); - OUT_COLOR *= opacity; +#ifdef IS_REQUIRED_ROUNDED_CORNER + mediump float opacity = calculateCornerOpacity(); + OUT_COLOR *= opacity; +#endif + +#if defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE) + } #endif }