1 INPUT mediump vec2 vTexCoord;
2 #if defined(IS_REQUIRED_DEBUG_VISUAL_SHADER) || defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
3 INPUT mediump vec2 vPosition;
4 INPUT mediump vec2 vRectSize;
5 INPUT mediump vec2 vOptRectSize;
6 INPUT mediump float vAliasMargin;
7 #ifdef IS_REQUIRED_ROUNDED_CORNER
8 INPUT mediump vec4 vCornerRadius;
12 uniform sampler2D sTexture;
13 #if defined(IS_REQUIRED_YUV_TO_RGB) || defined(IS_REQUIRED_UNIFIED_YUV_AND_RGB)
14 uniform sampler2D sTextureU;
15 uniform sampler2D sTextureV;
18 #ifdef IS_REQUIRED_ALPHA_MASKING
19 uniform sampler2D sMaskTexture;
20 uniform lowp float uYFlipMaskTexture;
21 INPUT mediump vec2 vMaskTexCoord;
24 #ifdef ATLAS_DEFAULT_WARP
25 uniform mediump vec4 uAtlasRect;
26 #elif defined(ATLAS_CUSTOM_WARP)
27 // WrapMode -- 0: CLAMP; 1: REPEAT; 2: REFLECT;
28 uniform lowp vec2 wrapMode;
32 #if defined(IS_REQUIRED_DEBUG_VISUAL_SHADER)
33 uniform highp vec3 uScale;
36 uniform lowp vec4 uColor;
37 uniform lowp vec3 mixColor;
38 uniform lowp float preMultipliedAlpha;
39 #ifdef IS_REQUIRED_BORDERLINE
40 uniform mediump float borderlineWidth;
41 uniform mediump float borderlineOffset;
42 uniform lowp vec4 borderlineColor;
43 uniform lowp vec4 uActorColor;
46 #ifdef ATLAS_CUSTOM_WARP
47 mediump float wrapCoordinate( mediump vec2 range, mediump float coordinate, lowp float wrap )
50 if( wrap > 1.5 ) /* REFLECT */
51 coord = 1.0 - abs(fract(coordinate*0.5)*2.0 - 1.0);
52 else /* warp is 0 or 1 */
53 coord = mix(coordinate, fract(coordinate), wrap);
54 return clamp(mix(range.x, range.y, coord), range.x, range.y);
58 #if defined(IS_REQUIRED_DEBUG_VISUAL_SHADER) || defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
59 // Global values both rounded corner and borderline use
61 // radius of rounded corner on this quadrant
62 mediump float gRadius = 0.0;
64 // fragment coordinate. NOTE : vec2(0.0, 0.0) is vRectSize, the corner of visual
65 mediump vec2 gFragmentPosition = vec2(0.0, 0.0);
66 // center coordinate of rounded corner circle. vec2(gCenterPosition, gCenterPosition).
67 mediump float gCenterPosition = 0.0;
68 // relative coordinate of gFragmentPosition from gCenterPosition.
69 mediump vec2 gDiff = vec2(0.0, 0.0);
70 // potential value what our algorithm use.
71 mediump float gPotential = 0.0;
73 // threshold of potential
74 mediump float gPotentialRange = 0.0;
75 mediump float gMaxOutlinePotential = 0.0;
76 mediump float gMinOutlinePotential = 0.0;
77 mediump float gMaxInlinePotential = 0.0;
78 mediump float gMinInlinePotential = 0.0;
80 void calculateCornerRadius()
82 #ifdef IS_REQUIRED_ROUNDED_CORNER
85 mix(vCornerRadius.x, vCornerRadius.y, sign(vPosition.x) * 0.5 + 0.5),
86 mix(vCornerRadius.w, vCornerRadius.z, sign(vPosition.x) * 0.5 + 0.5),
87 sign(vPosition.y) * 0.5 + 0.5
92 void calculatePosition()
94 gFragmentPosition = abs(vPosition) - vRectSize;
95 gCenterPosition = -gRadius;
96 #ifdef IS_REQUIRED_BORDERLINE
97 gCenterPosition += borderlineWidth * (clamp(borderlineOffset, -1.0, 1.0) + 1.0) * 0.5;
99 gDiff = gFragmentPosition - gCenterPosition;
102 void calculatePotential()
104 gPotential = length(max(gDiff, 0.0)) + min(0.0, max(gDiff.x, gDiff.y));
107 void setupMinMaxPotential()
109 gPotentialRange = vAliasMargin;
111 gMaxOutlinePotential = gRadius + gPotentialRange;
112 gMinOutlinePotential = gRadius - gPotentialRange;
114 #ifdef IS_REQUIRED_BORDERLINE
115 gMaxInlinePotential = gMaxOutlinePotential - borderlineWidth;
116 gMinInlinePotential = gMinOutlinePotential - borderlineWidth;
118 gMaxInlinePotential = gMaxOutlinePotential;
119 gMinInlinePotential = gMinOutlinePotential;
122 // reduce defect near edge of rounded corner.
123 gMaxOutlinePotential += clamp(-min(gDiff.x, gDiff.y) / max(1.0, gRadius), 0.0, 1.0);
124 gMinOutlinePotential += clamp(-min(gDiff.x, gDiff.y) / max(1.0, gRadius), 0.0, 1.0);
127 void PreprocessPotential()
129 calculateCornerRadius();
131 calculatePotential();
133 setupMinMaxPotential();
137 #ifdef IS_REQUIRED_BORDERLINE
138 lowp vec4 convertBorderlineColor(lowp vec4 textureColor)
140 mediump float potential = gPotential;
142 // default opacity of borderline is 0.0
143 mediump float borderlineOpacity = 0.0;
145 // calculate borderline opacity by potential
146 if(potential > gMinInlinePotential)
148 // potential is inside borderline range.
149 borderlineOpacity = smoothstep(gMinInlinePotential, gMaxInlinePotential, potential);
151 // Muliply borderlineWidth to resolve very thin borderline
152 borderlineOpacity *= min(1.0, borderlineWidth / gPotentialRange);
155 lowp vec3 borderlineColorRGB = borderlineColor.rgb * uActorColor.rgb;
156 lowp float borderlineColorAlpha = borderlineColor.a * uActorColor.a;
157 borderlineColorRGB *= mix(1.0, borderlineColorAlpha, preMultipliedAlpha);
159 // Calculate inside of borderline when alpha is between (0.0 1.0). So we need to apply texture color.
160 // If borderlineOpacity is exactly 0.0, we always use whole texture color. In this case, we don't need to run below code.
161 // But if borderlineOpacity > 0.0 and borderlineColor.a == 0.0, we need to apply tCornerRadius.
162 if(borderlineOpacity > 0.0 && borderlineColor.a * borderlineOpacity < 1.0)
164 mediump float tCornerRadius = -gCenterPosition + gPotentialRange;
165 mediump float MaxTexturelinePotential = tCornerRadius + gPotentialRange;
166 mediump float MinTexturelinePotential = tCornerRadius - gPotentialRange;
167 if(potential > MaxTexturelinePotential)
169 // potential is out of texture range.
170 textureColor = vec4(0.0);
174 // potential is in texture range.
175 lowp float textureAlphaScale = mix(1.0, 0.0, smoothstep(MinTexturelinePotential, MaxTexturelinePotential, potential));
176 textureColor.a *= textureAlphaScale;
177 textureColor.rgb *= mix(textureColor.a, textureAlphaScale, preMultipliedAlpha);
180 borderlineColorAlpha *= borderlineOpacity;
181 borderlineColorRGB *= mix(borderlineColorAlpha, borderlineOpacity, preMultipliedAlpha);
182 // We use pre-multiplied color to reduce operations.
183 // In here, textureColor and borderlineColorRGB is pre-multiplied color now.
185 // Manual blend operation with premultiplied colors.
186 // Final alpha = borderlineColorAlpha + (1.0 - borderlineColorAlpha) * textureColor.a.
187 // (Final rgb * alpha) = borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb
188 // If preMultipliedAlpha == 1.0, just return vec4(rgb*alpha, alpha)
189 // Else, return vec4((rgb*alpha) / alpha, alpha)
191 lowp float finalAlpha = mix(textureColor.a, 1.0, borderlineColorAlpha);
192 lowp vec3 finalMultipliedRGB = borderlineColorRGB + (1.0 - borderlineColorAlpha) * textureColor.rgb;
193 // TODO : Need to find some way without division
194 return vec4(finalMultipliedRGB * mix(1.0 / finalAlpha, 1.0, preMultipliedAlpha), finalAlpha);
196 return mix(textureColor, vec4(borderlineColorRGB, borderlineColorAlpha), borderlineOpacity);
200 #ifdef IS_REQUIRED_ROUNDED_CORNER
201 mediump float calculateCornerOpacity()
203 mediump float potential = gPotential;
205 // default opacity is 1.0
206 mediump float opacity = 1.0;
208 // calculate borderline opacity by potential
209 if(potential > gMaxOutlinePotential)
211 // potential is out of borderline range. just discard here
214 else if(potential > gMinOutlinePotential)
216 opacity = 1.0 - smoothstep(gMinOutlinePotential, gMaxOutlinePotential, potential);
222 #if defined(IS_REQUIRED_YUV_TO_RGB) || defined(IS_REQUIRED_UNIFIED_YUV_AND_RGB)
223 lowp vec4 ConvertYuvToRgba(mediump vec2 texCoord)
225 #ifdef IS_REQUIRED_UNIFIED_YUV_AND_RGB
226 // Special case when shader use YUV but actual textures are not YUV format.
227 // In this case, just resturn sTexture.
228 if(textureSize(sTextureU, 0) != textureSize(sTextureV, 0))
230 return texture(sTexture, texCoord);
234 lowp float y = texture(sTexture, texCoord).r;
235 lowp float u = texture(sTextureU, texCoord).r - 0.5;
236 lowp float v = texture(sTextureV, texCoord).r - 0.5;
238 rgba.r = y + (1.403 * v);
239 rgba.g = y - (0.344 * u) - (0.714 * v);
240 rgba.b = y + (1.770 * u);
246 #ifdef IS_REQUIRED_DEBUG_VISUAL_SHADER
248 // Predefined values whether some macro defined or not.
249 // Since we make debug codes replace by macro,
250 // sharp if keyword cannot be used.
251 // Instead, let we use bool values so we can use define checked in script
252 #ifdef IS_REQUIRED_ROUNDED_CORNER
253 const bool IS_REQUIRED_ROUNDED_CORNER_BOOL = true;
255 const bool IS_REQUIRED_ROUNDED_CORNER_BOOL = false;
257 #ifdef IS_REQUIRED_BORDERLINE
258 const bool IS_REQUIRED_BORDERLINE_BOOL = true;
260 const bool IS_REQUIRED_BORDERLINE_BOOL = false;
262 #ifdef IS_REQUIRED_YUV_TO_RGB
263 const bool IS_REQUIRED_YUV_TO_RGB_BOOL = true;
265 const bool IS_REQUIRED_YUV_TO_RGB_BOOL = false;
267 #ifdef IS_REQUIRED_UNIFIED_YUV_AND_RGB
268 const bool IS_REQUIRED_UNIFIED_YUV_AND_RGB_BOOL = true;
270 const bool IS_REQUIRED_UNIFIED_YUV_AND_RGB_BOOL = false;
272 #ifdef IS_REQUIRED_ALPHA_MASKING
273 const bool IS_REQUIRED_ALPHA_MASKING_BOOL = true;
275 const bool IS_REQUIRED_ALPHA_MASKING_BOOL = false;
277 #ifdef ATLAS_DEFAULT_WARP
278 const bool ATLAS_DEFAULT_WARP_BOOL = true;
280 const bool ATLAS_DEFAULT_WARP_BOOL = false;
282 #ifdef ATLAS_CUSTOM_WARP
283 const bool ATLAS_CUSTOM_WARP_BOOL = true;
285 const bool ATLAS_CUSTOM_WARP_BOOL = false;
288 // These lines in the shader may be replaced with actual definitions by the debug-image-visual-shader-script.json.
289 // DEBUG_TRIGGER_CODE return bool type, and DEBUG_RATIO_CODE return float value which will be clamped between 0.0 and 1.0
290 // If DEBUG_TRIGGER_CODE return true, it mean we will change final color's channel value.
291 // If ratio is 0.0, debug color rate become MINIMUM_DEBUG_COLOR_RATE, and 1.0 than MAXIMUM_DEBUG_COLOR_RATE.
292 #define MINIMUM_DEBUG_COLOR_RATE
293 #define MAXIMUM_DEBUG_COLOR_RATE
294 #define DEBUG_TRIGGER_RED_CODE
295 #define DEBUG_TRIGGER_GREEN_CODE
296 #define DEBUG_TRIGGER_BLUE_CODE
297 #define DEBUG_RATIO_RED_CODE
298 #define DEBUG_RATIO_GREEN_CODE
299 #define DEBUG_RATIO_BLUE_CODE
301 const mediump float gMinDebugColorRate = MINIMUM_DEBUG_COLOR_RATE;
302 const mediump float gMaxDebugColorRate = MAXIMUM_DEBUG_COLOR_RATE;
304 bool DebugTriggerRed(mediump vec4 originColor)
306 DEBUG_TRIGGER_RED_CODE
309 bool DebugTriggerGreen(mediump vec4 originColor)
311 DEBUG_TRIGGER_GREEN_CODE
314 bool DebugTriggerBlue(mediump vec4 originColor)
316 DEBUG_TRIGGER_BLUE_CODE
319 mediump float DebugRatioRed(mediump vec4 originColor)
324 mediump float DebugRatioGreen(mediump vec4 originColor)
326 DEBUG_RATIO_GREEN_CODE
329 mediump float DebugRatioBlue(mediump vec4 originColor)
331 DEBUG_RATIO_BLUE_CODE
334 mediump vec3 ApplyDebugMixColor(mediump vec4 originColor)
336 mediump float debugColorRateRed = 0.0;
337 mediump float debugColorRateGreen = 0.0;
338 mediump float debugColorRateBlue = 0.0;
340 if(DebugTriggerRed(originColor))
342 debugColorRateRed = mix(gMinDebugColorRate, gMaxDebugColorRate, smoothstep(0.0, 1.0, DebugRatioRed(originColor)));
344 if(DebugTriggerGreen(originColor))
346 debugColorRateGreen = mix(gMinDebugColorRate, gMaxDebugColorRate, smoothstep(0.0, 1.0, DebugRatioGreen(originColor)));
348 if(DebugTriggerBlue(originColor))
350 debugColorRateBlue = mix(gMinDebugColorRate, gMaxDebugColorRate, smoothstep(0.0, 1.0, DebugRatioBlue(originColor)));
353 mediump float colorRate = max(debugColorRateRed, max(debugColorRateGreen, debugColorRateBlue));
354 mediump vec3 debugColor = vec3(debugColorRateRed, debugColorRateGreen, debugColorRateBlue);
356 debugColor *= mix(1.0, originColor.a, preMultipliedAlpha);
358 return originColor.rgb * (1.0 - colorRate) + debugColor;
364 #ifdef ATLAS_DEFAULT_WARP
365 mediump vec2 texCoord = clamp( mix( uAtlasRect.xy, uAtlasRect.zw, vTexCoord ), uAtlasRect.xy, uAtlasRect.zw );
366 #elif defined(ATLAS_CUSTOM_WARP)
367 mediump vec2 texCoord = vec2( wrapCoordinate( uAtlasRect.xz, vTexCoord.x, wrapMode.x ),
368 wrapCoordinate( uAtlasRect.yw, vTexCoord.y, wrapMode.y ) );
370 mediump vec2 texCoord = vTexCoord;
373 #if defined(IS_REQUIRED_YUV_TO_RGB) || defined(IS_REQUIRED_UNIFIED_YUV_AND_RGB)
374 lowp vec4 textureColor = ConvertYuvToRgba(texCoord) * vec4( mixColor, 1.0 ) * uColor;
376 lowp vec4 textureColor = TEXTURE( sTexture, texCoord ) * vec4( mixColor, 1.0 ) * uColor;
379 #ifdef IS_REQUIRED_ALPHA_MASKING
380 mediump vec2 maskTexCoord = vMaskTexCoord;
381 maskTexCoord.y = mix(maskTexCoord.y, 1.0-maskTexCoord.y, uYFlipMaskTexture);
382 mediump float maskAlpha = TEXTURE(sMaskTexture, maskTexCoord).a;
383 textureColor.a *= maskAlpha;
384 textureColor.rgb *= mix(1.0, maskAlpha, preMultipliedAlpha);
387 #if defined(IS_REQUIRED_DEBUG_VISUAL_SHADER) || defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
388 #ifndef IS_REQUIRED_DEBUG_VISUAL_SHADER
389 // skip most potential calculate for performance
390 if(abs(vPosition.x) < vOptRectSize.x && abs(vPosition.y) < vOptRectSize.y)
392 OUT_COLOR = textureColor;
397 PreprocessPotential();
400 #ifdef IS_REQUIRED_BORDERLINE
401 textureColor = convertBorderlineColor(textureColor);
403 OUT_COLOR = textureColor;
405 #ifdef IS_REQUIRED_ROUNDED_CORNER
406 mediump float opacity = calculateCornerOpacity();
407 OUT_COLOR.a *= opacity;
408 OUT_COLOR.rgb *= mix(1.0, opacity, preMultipliedAlpha);
411 #if defined(IS_REQUIRED_DEBUG_VISUAL_SHADER) || defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
415 #ifdef IS_REQUIRED_DEBUG_VISUAL_SHADER
416 OUT_COLOR.rgb = ApplyDebugMixColor(OUT_COLOR);