From cf00a333060471f381ffd6529507ae1e701f30d2 Mon Sep 17 00:00:00 2001 From: "Eunki, Hong" Date: Fri, 22 Jan 2021 18:20:05 +0900 Subject: [PATCH] ColorVisual can use both BLUR_RADIUS and CORNER_RADIUS modify shader to use blur_radius and corner_radius both of them Change-Id: I65315709a7dff4e574f645b370c950e03d00fcfe Signed-off-by: Eunki, Hong --- .../shaders/color-visual-blur-edge-shader.frag | 55 ++++++++++++++++++++-- .../shaders/color-visual-blur-edge-shader.vert | 16 +++++-- 2 files changed, 63 insertions(+), 8 deletions(-) diff --git a/dali-toolkit/internal/graphics/shaders/color-visual-blur-edge-shader.frag b/dali-toolkit/internal/graphics/shaders/color-visual-blur-edge-shader.frag index f2025a7..df817b2 100644 --- a/dali-toolkit/internal/graphics/shaders/color-visual-blur-edge-shader.frag +++ b/dali-toolkit/internal/graphics/shaders/color-visual-blur-edge-shader.frag @@ -1,5 +1,6 @@ INPUT mediump vec2 vPosition; INPUT mediump vec2 vRectSize; +INPUT mediump float vCornerRadius; uniform lowp vec4 uColor; uniform lowp vec3 mixColor; @@ -7,7 +8,55 @@ uniform mediump float blurRadius; void main() { - mediump vec2 blur = 1.0 - smoothstep( vRectSize - blurRadius * 2.0, vRectSize, abs( vPosition ) ); OUT_COLOR = vec4(mixColor, 1.0) * uColor; - OUT_COLOR.a *= blur.x * blur.y; -} \ No newline at end of file + + mediump vec2 v = abs(vPosition) - vRectSize; + mediump float cy = vCornerRadius + blurRadius; + mediump float cr = vCornerRadius + blurRadius; + + cy = min(cy, min(vRectSize.x, vRectSize.y)); + v = vec2(min(v.x, v.y), max(v.x, v.y)); + v = v + cy; + + mediump float blur = 1.0; + mediump float potential = 0.0; + mediump float alias = min(vCornerRadius, 1.0); + mediump float potentialMin = cy + vCornerRadius - blurRadius - alias; + mediump float potentialMax = cy + vCornerRadius + blurRadius + alias; + + // move center of circles for reduce defact + mediump float cyDiff = min(cy, 0.2 * blurRadius); + cy -= cyDiff; + cr += cyDiff; + + mediump float diffFromBaseline = cy * v.y - (cy + cr) * v.x; + + if(diffFromBaseline > 0.0) + { + // out of calculation bound. + potential = v.y; + + // for anti-alias when blurRaidus = 0.0 + mediump float heuristicBaselineScale = max(1.0 , cr * (cr + cy)); + mediump float potentialDiff = min(alias, diffFromBaseline / heuristicBaselineScale); + potentialMin += potentialDiff; + potentialMax -= potentialDiff; + } + else + { + // get some circle centered (x, x) and radius (r = cr / cy * x) + // s.t. point v is on that circle + // highest point of that circle is (x, x + r) and potential is x + r + + // solve (v.x - x)^2 + (v.y - x)^2 = (cr / cy * x)^2 + + mediump float A = (cr * cr - 2.0 * cy * cy); + mediump float B = cy * (v.x + v.y); + mediump float V = dot(v,v); + mediump float D = B * B + A * V; + potential = V * (cr + cy) / (sqrt(D) + B); + } + + blur = 1.0 - smoothstep(potentialMin, potentialMax, potential); + OUT_COLOR.a *= blur; +} diff --git a/dali-toolkit/internal/graphics/shaders/color-visual-blur-edge-shader.vert b/dali-toolkit/internal/graphics/shaders/color-visual-blur-edge-shader.vert index 65849eb..e20110b 100644 --- a/dali-toolkit/internal/graphics/shaders/color-visual-blur-edge-shader.vert +++ b/dali-toolkit/internal/graphics/shaders/color-visual-blur-edge-shader.vert @@ -1,8 +1,9 @@ INPUT mediump vec2 aPosition; OUTPUT mediump vec2 vPosition; OUTPUT mediump vec2 vRectSize; +OUTPUT mediump float vCornerRadius; -uniform highp mat4 uMvpMatrix; +uniform highp mat4 uMvpMatrix; uniform highp vec3 uSize; //Visual size and offset @@ -13,17 +14,22 @@ uniform mediump vec4 offsetSizeMode; uniform mediump vec2 origin; uniform mediump vec2 anchorPoint; uniform mediump float blurRadius; +uniform mediump float cornerRadius; +uniform mediump float cornerRadiusPolicy; vec4 ComputeVertexPosition() { - vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw ) + extraSize + blurRadius * 2.0; + vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw ) + extraSize; vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy); - vRectSize = visualSize / 2.0; - vPosition = aPosition* visualSize; + mediump float minSize = min( visualSize.x, visualSize.y ); + vCornerRadius = mix( cornerRadius * minSize, cornerRadius, cornerRadiusPolicy ); + vCornerRadius = min( vCornerRadius, minSize * 0.5 ); + vRectSize = visualSize / 2.0 - vec2( vCornerRadius ); + vPosition = aPosition * (visualSize + 2.0 * blurRadius); return vec4( vPosition + anchorPoint*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 ); } void main() { gl_Position = uMvpMatrix * ComputeVertexPosition(); -} \ No newline at end of file +} -- 2.7.4