1 INPUT mediump vec2 aPosition;
2 OUTPUT mediump vec2 vTexCoord;
3 #if defined(IS_REQUIRED_DEBUG_VISUAL_SHADER) || defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
4 OUTPUT highp vec2 vPosition;
5 OUTPUT highp vec2 vRectSize;
6 OUTPUT highp vec2 vOptRectSize;
7 OUTPUT highp float vAliasMargin;
8 #ifdef IS_REQUIRED_ROUNDED_CORNER
9 OUTPUT highp vec4 vCornerRadius;
13 #ifdef IS_REQUIRED_DEBUG_VISUAL_SHADER
14 #define DEBUG_EXTRA_ATTRIBUTES
15 #define DEBUG_EXTRA_VARYINGS
17 DEBUG_EXTRA_ATTRIBUTES
21 uniform highp mat4 uMvpMatrix;
22 uniform highp vec3 uSize;
23 uniform highp vec4 pixelArea;
25 #if defined(IS_REQUIRED_DEBUG_VISUAL_SHADER) || defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
26 // Be used when we calculate anti-alias range near 1 pixel.
27 uniform highp vec3 uScale;
30 //Visual size and offset
31 uniform highp vec2 offset;
32 uniform highp vec2 size;
33 uniform mediump vec4 offsetSizeMode;
34 uniform mediump vec2 origin;
35 uniform mediump vec2 anchorPoint;
36 #ifdef IS_REQUIRED_BORDERLINE
37 uniform highp float borderlineWidth;
38 uniform highp float borderlineOffset;
40 #ifdef IS_REQUIRED_ROUNDED_CORNER
41 uniform highp vec4 cornerRadius;
42 uniform mediump float cornerRadiusPolicy;
44 #ifdef IS_REQUIRED_ALPHA_MASKING
45 OUTPUT mediump vec2 vMaskTexCoord;
46 uniform lowp float cropToMask;
47 uniform mediump vec2 maskTextureRatio;
49 uniform highp vec2 extraSize;
51 vec4 ComputeVertexPosition()
53 highp vec2 visualSize = mix(size * uSize.xy, size, offsetSizeMode.zw) + extraSize;
54 highp vec2 visualOffset = mix(offset * uSize.xy, offset, offsetSizeMode.xy);
56 #if defined(IS_REQUIRED_DEBUG_VISUAL_SHADER) || defined(IS_REQUIRED_ROUNDED_CORNER) || defined(IS_REQUIRED_BORDERLINE)
57 vRectSize = visualSize * 0.5;
58 vOptRectSize = vRectSize;
60 // Set soft anti-alias range at most 10% of visual size.
61 // The range should be inverse proportion with scale of view.
62 // To avoid divid-by-zero, let we allow minimum scale value is 0.001 (0.1%)
63 vAliasMargin = min(1.0, max(visualSize.x, visualSize.y) * 0.1) / max(0.001, max(uScale.x, uScale.y));
65 highp float vertexMargin = 0.0;
68 #ifdef IS_REQUIRED_BORDERLINE
69 // Extend size of visual by borderline.
70 highp float outerBorderlineSize = (1.0 + clamp(borderlineOffset, -1.0, 1.0)) * borderlineWidth;
73 #ifdef IS_REQUIRED_ROUNDED_CORNER
74 #ifdef IS_REQUIRED_BORDERLINE
75 highp float maxSize = max(visualSize.x, visualSize.y) + outerBorderlineSize;
76 highp float minSize = min(visualSize.x, visualSize.y) + outerBorderlineSize;
78 highp float maxSize = max(visualSize.x, visualSize.y);
79 highp float minSize = min(visualSize.x, visualSize.y);
81 vCornerRadius = mix(cornerRadius * minSize, cornerRadius, cornerRadiusPolicy);
82 vCornerRadius = min(vCornerRadius, minSize * 0.5);
83 // Optimize fragment shader. 0.2929 ~= 1.0 - sqrt(0.5)
84 highp float maxRadius = max(max(vCornerRadius.x, vCornerRadius.y), max(vCornerRadius.z, vCornerRadius.w));
85 highp float minRadius = min(min(vCornerRadius.x, vCornerRadius.y), min(vCornerRadius.z, vCornerRadius.w));
86 vOptRectSize -= 0.2929 * maxRadius + 1.0;
88 // Set vertex margin as vAliasMargin if we need to make some more fragments for alias.
89 // Do not increase margin if the minRadius is small enough rather than maxSize.
90 // TODO : We should change the magic parameter, 0.49
91 vertexMargin = 2.0 * vAliasMargin * smoothstep(maxSize * 0.49, maxSize * 0.5, minRadius);
94 #ifdef IS_REQUIRED_BORDERLINE
95 vPosition = aPosition * (visualSize + outerBorderlineSize + vertexMargin);
96 vOptRectSize -= (borderlineWidth - outerBorderlineSize * 0.5) + 1.0;
97 #elif defined(IS_REQUIRED_DEBUG_VISUAL_SHADER) || defined(IS_REQUIRED_ROUNDED_CORNER)
98 vPosition = aPosition * (visualSize + vertexMargin);
100 highp vec2 vPosition = aPosition * visualSize;
103 highp vec4 finalPixelArea = pixelArea;
104 #ifdef IS_REQUIRED_ALPHA_MASKING
105 finalPixelArea = mix(pixelArea,
107 vec2(0.5) + (pixelArea.xy - vec2(0.5)) * maskTextureRatio,
108 pixelArea.zw * maskTextureRatio
111 vMaskTexCoord = pixelArea.xy + pixelArea.zw * (vec2(0.5) + aPosition.xy
112 #ifdef IS_REQUIRED_BORDERLINE
113 * (1.0 + (outerBorderlineSize + vertexMargin) / visualSize)
114 #elif defined(IS_REQUIRED_ROUNDED_CORNER)
115 * (1.0 + vertexMargin / visualSize)
119 vTexCoord = finalPixelArea.xy + finalPixelArea.zw * (vec2(0.5) + aPosition.xy
120 #ifdef IS_REQUIRED_BORDERLINE
121 * (1.0 + (outerBorderlineSize + vertexMargin) / visualSize)
122 #elif defined(IS_REQUIRED_ROUNDED_CORNER)
123 * (1.0 + vertexMargin / visualSize)
127 return vec4(vPosition + anchorPoint * visualSize + visualOffset + origin * uSize.xy, 0.0, 1.0);
130 #ifdef IS_REQUIRED_DEBUG_VISUAL_SHADER
131 // These lines in the shader may be replaced with actual definitions by the debug-image-visual-shader-script.json.
132 #define DEBUG_APPLY_VARYING_CODE
133 #define DEBUG_EXTRA_UNIFORMS
137 void ApplyDebugVarying()
139 DEBUG_APPLY_VARYING_CODE
144 gl_Position = uMvpMatrix * ComputeVertexPosition();
146 #ifdef IS_REQUIRED_DEBUG_VISUAL_SHADER