[drape] manual line anti-aliasing

This commit is contained in:
ExMix 2015-07-02 13:49:49 +03:00 committed by r.kuznetsov
parent 84732359eb
commit b4bbe6484b
5 changed files with 39 additions and 19 deletions

View file

@ -1,4 +1,5 @@
varying vec2 v_colorTexCoord;
varying vec2 v_halfLength;
uniform sampler2D u_colorTex;
uniform float u_opacity;
@ -7,5 +8,8 @@ void main(void)
{
vec4 color = texture2D(u_colorTex, v_colorTexCoord);
color.a *= u_opacity;
float currentW = abs(v_halfLength.x);
float diff = v_halfLength.y - currentW;
color.a *= mix(0.3, 1.0, clamp(diff / 2.0, 0.0, 1.0));
gl_FragColor = color;
}

View file

@ -1,5 +1,5 @@
attribute vec3 a_position;
attribute vec2 a_normal;
attribute vec3 a_normal;
attribute vec2 a_colorTexCoord;
uniform mat4 modelView;
@ -7,18 +7,21 @@ uniform mat4 projection;
varying vec2 v_colorTexCoord;
varying vec2 v_maskTexCoord;
varying vec2 v_halfLength;
void main(void)
{
float halfWidth = length(a_normal);
vec2 normal = a_normal.xy;
float halfWidth = length(normal);
vec2 transformedAxisPos = (vec4(a_position.xy, 0.0, 1.0) * modelView).xy;
if (halfWidth != 0.0)
{
vec4 glbShiftPos = vec4(a_position.xy + a_normal, 0.0, 1.0);
vec4 glbShiftPos = vec4(a_position.xy + normal, 0.0, 1.0);
vec2 shiftPos = (glbShiftPos * modelView).xy;
transformedAxisPos = transformedAxisPos + normalize(shiftPos - transformedAxisPos) * halfWidth;
}
v_colorTexCoord = a_colorTexCoord;
v_halfLength = vec2(sign(a_normal.z) * halfWidth, abs(a_normal.z));
gl_Position = vec4(transformedAxisPos, a_position.z, 1.0) * projection;
}

View file

@ -194,7 +194,7 @@ uint32_t TextDynamicVertex::GetDynamicStreamID()
LineVertex::LineVertex()
: m_position(0.0, 0.0, 0.0)
, m_normal(0.0, 0.0)
, m_normal(0.0, 0.0, 0.0)
, m_colorTexCoord(0.0, 0.0)
{
}
@ -218,7 +218,9 @@ DashedLineVertex::DashedLineVertex()
DashedLineVertex::DashedLineVertex(TPosition const & position, TNormal const & normal,
TTexCoord const & color, TTexCoord const & mask)
: LineVertex(position, normal, color)
: m_position(position)
, m_normal(normal)
, m_colorTexCoord(color)
, m_maskTexCoord(mask)
{
}

View file

@ -10,9 +10,9 @@ namespace gpu
struct BaseVertex
{
typedef glsl::vec3 TPosition;
typedef glsl::vec2 TNormal;
typedef glsl::vec2 TTexCoord;
using TPosition = glsl::vec3;
using TNormal = glsl::vec2;
using TTexCoord = glsl::vec2;
};
struct SolidTexturingVertex : BaseVertex
@ -61,6 +61,8 @@ typedef buffer_vector<TextDynamicVertex, 128> TTextDynamicVertexBuffer;
struct LineVertex : BaseVertex
{
using TNormal = glsl::vec3;
LineVertex();
LineVertex(TPosition const & position, TNormal const & normal, TTexCoord const & color);
@ -71,12 +73,15 @@ struct LineVertex : BaseVertex
static dp::BindingInfo const & GetBindingInfo();
};
struct DashedLineVertex : LineVertex
struct DashedLineVertex : BaseVertex
{
DashedLineVertex();
DashedLineVertex(TPosition const & position, TNormal const & normal,
TTexCoord const & color, TTexCoord const & mask);
TPosition m_position;
TNormal m_normal;
TTexCoord m_colorTexCoord;
TTexCoord m_maskTexCoord;
static dp::BindingInfo const & GetBindingInfo();

View file

@ -122,6 +122,7 @@ protected:
class SolidLineBuilder : public BaseLineBuilder<gpu::LineVertex>
{
using TBase = BaseLineBuilder<gpu::LineVertex>;
using TNormal = gpu::LineVertex::TNormal;
public:
SolidLineBuilder(dp::TextureManager::ColorRegion const & color, float const pxHalfWidth)
@ -137,12 +138,12 @@ public:
}
void SubmitVertex(LineSegment const & segment, glsl::vec3 const & pivot,
glsl::vec2 const & normal, float offsetFromStart)
glsl::vec2 const & normal, bool isLeft, float offsetFromStart)
{
UNUSED_VALUE(segment);
UNUSED_VALUE(offsetFromStart);
m_geometry.emplace_back(V(pivot, m_pxHalfWidth * normal, m_colorCoord));
m_geometry.emplace_back(V(pivot, TNormal(m_pxHalfWidth * normal, m_pxHalfWidth * GetSide(isLeft)), m_colorCoord));
}
void SubmitJoin(glsl::vec3 const & pivot, vector<glsl::vec2> const & normals)
@ -151,11 +152,16 @@ public:
for (int j = 0; j < trianglesCount; j++)
{
size_t baseIndex = 3 * j;
m_joinGeom.push_back(V(pivot, normals[baseIndex + 0], m_colorCoord));
m_joinGeom.push_back(V(pivot, normals[baseIndex + 1], m_colorCoord));
m_joinGeom.push_back(V(pivot, normals[baseIndex + 2], m_colorCoord));
m_joinGeom.push_back(V(pivot, TNormal(normals[baseIndex + 0], m_pxHalfWidth), m_colorCoord));
m_joinGeom.push_back(V(pivot, TNormal(normals[baseIndex + 1], m_pxHalfWidth), m_colorCoord));
m_joinGeom.push_back(V(pivot, TNormal(normals[baseIndex + 2], m_pxHalfWidth), m_colorCoord));
}
}
float GetSide(bool isLeft)
{
return isLeft ? 1.0 : -1.0;
}
};
class DashedLineBuilder : public BaseLineBuilder<gpu::DashedLineVertex>
@ -189,7 +195,7 @@ public:
}
void SubmitVertex(LineSegment const & segment, glsl::vec3 const & pivot,
glsl::vec2 const & normal, float offsetFromStart)
glsl::vec2 const & normal, bool /*isLeft*/, float offsetFromStart)
{
float distance = GetProjectionLength(pivot.xy() + m_glbHalfWidth * normal,
segment.m_points[StartPoint],
@ -273,10 +279,10 @@ void LineShape::Draw(TBuilder & builder, ref_ptr<dp::Batcher> batcher) const
glsl::vec2 const leftNormal = GetNormal(segments[i], true /* isLeft */, BaseNormal);
glsl::vec2 const rightNormal = GetNormal(segments[i], false /* isLeft */, BaseNormal);
builder.SubmitVertex(segments[i], currentStartPivot, rightNormal, offsetFromStart);
builder.SubmitVertex(segments[i], currentStartPivot, leftNormal, offsetFromStart);
builder.SubmitVertex(segments[i], newPivot, rightNormal, offsetFromStart);
builder.SubmitVertex(segments[i], newPivot, leftNormal, offsetFromStart);
builder.SubmitVertex(segments[i], currentStartPivot, rightNormal, false /* isLeft */, offsetFromStart);
builder.SubmitVertex(segments[i], currentStartPivot, leftNormal, true /* isLeft */, offsetFromStart);
builder.SubmitVertex(segments[i], newPivot, rightNormal, false /* isLeft */, offsetFromStart);
builder.SubmitVertex(segments[i], newPivot, leftNormal, true /* isLeft */, offsetFromStart);
currentStartPivot = newPivot;
}