Skip to content

Commit

Permalink
Merge branch '171-bug-the-linedashlength-has-a-wrong-behavior-if-the-…
Browse files Browse the repository at this point in the history
…line-is-made-of-multiple-segments' into 'master'

Resolve "Bug: the `lineDashLength` has a wrong behavior if the line is made of multiple segments"

Closes aerys#171

See merge request aerys/smartshape-engine!170
  • Loading branch information
jpx committed May 9, 2019
2 parents 42487fb + 853aa7d commit 955e7d7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
3 changes: 2 additions & 1 deletion framework/asset/effect/Line.effect
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"attributes" : {
"aStartPosition" : "geometry[$geometryUuid].startPosition",
"aStopPosition" : "geometry[$geometryUuid].stopPosition",
"aWeights" : "geometry[$geometryUuid].weights"
"aWeights" : "geometry[$geometryUuid].weights",
"aDashOffset" : "geometry[$geometryUuid].dashOffset"
},

"uniforms" : {
Expand Down
6 changes: 5 additions & 1 deletion framework/asset/effect/Line.vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
attribute vec3 aStartPosition;
attribute vec3 aStopPosition;
attribute vec3 aWeights;
attribute float aDashOffset;

uniform mat4 uModelToWorldMatrix;
uniform mat4 uWorldToScreenMatrix;
Expand All @@ -26,23 +27,26 @@ main()
float wStart = aWeights.x;
float wStop = aWeights.y;
float lineSpread = aWeights.z;
vec4 dashVector = vec4(aDashOffset, 0.0, 0.0, 0.0);

vec4 startPos = vec4(aStartPosition, 1.0);
vec4 stopPos = vec4(aStopPosition, 1.0);

#ifdef MODEL_TO_WORLD
startPos = uModelToWorldMatrix * startPos;
stopPos = uModelToWorldMatrix * stopPos;
dashVector = uModelToWorldMatrix * dashVector;
#endif // MODEL_TO_WORLD

startPos = uWorldToScreenMatrix * startPos;
stopPos = uWorldToScreenMatrix * stopPos;
dashVector = uWorldToScreenMatrix * dashVector;

vec4 pos = wStart * startPos + wStop * stopPos;
float posW = pos.w;

vPosW = posW;
vWeight = (wStop - wStart) * distance(startPos, stopPos);
vWeight = length(dashVector);

// account for perspective division for screen-space offsetting
startPos /= startPos.w;
Expand Down
2 changes: 2 additions & 0 deletions framework/include/minko/geometry/LineGeometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ namespace minko
static const std::string ATTRNAME_START_POS;
static const std::string ATTRNAME_STOP_POS;
static const std::string ATTRNAME_WEIGHTS;
static const std::string ATTRNAME_DASHOFFSET;

float _currentX;
float _currentY;
float _currentZ;
float _currentLength;
uint _numLines;

std::shared_ptr<render::VertexBuffer> _vertexBuffer;
Expand Down
15 changes: 13 additions & 2 deletions framework/src/minko/geometry/LineGeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ using namespace minko::render;
/*static*/ const std::string LineGeometry::ATTRNAME_START_POS = "startPosition";
/*static*/ const std::string LineGeometry::ATTRNAME_STOP_POS = "stopPosition";
/*static*/ const std::string LineGeometry::ATTRNAME_WEIGHTS = "weights";
/*static*/ const std::string LineGeometry::ATTRNAME_DASHOFFSET = "dashOffset";

LineGeometry::LineGeometry():
Geometry("line"),
_currentX(0.f),
_currentY(0.f),
_currentZ(0.f),
_currentLength(0.f),
_numLines(0),
_vertexBuffer(nullptr),
_indexBuffer(nullptr)
Expand All @@ -55,6 +57,7 @@ LineGeometry::initialize(AbstractContext::Ptr context, int numSegments)
_vertexBuffer->addAttribute(ATTRNAME_START_POS, 3, 0);
_vertexBuffer->addAttribute(ATTRNAME_STOP_POS, 3, 3);
_vertexBuffer->addAttribute(ATTRNAME_WEIGHTS, 3, 6);
_vertexBuffer->addAttribute(ATTRNAME_DASHOFFSET, 1, 9);

if (numSegments > 0)
{
Expand Down Expand Up @@ -101,6 +104,7 @@ LineGeometry::lineTo(float x, float y, float z, unsigned int numSegments)
const float stepX = (x - _currentX) * invNumSegments;
const float stepY = (y - _currentY) * invNumSegments;
const float stepZ = (z - _currentZ) * invNumSegments;
const float stepLength = math::length(math::vec3(stepX, stepY, stepZ));
unsigned int vid = oldVertexDataSize;
unsigned int iid = oldIndexDataSize;

Expand All @@ -116,12 +120,15 @@ LineGeometry::lineTo(float x, float y, float z, unsigned int numSegments)
const float nextX = _currentX + stepX;
const float nextY = _currentY + stepY;
const float nextZ = _currentZ + stepZ;
const float nextLength = _currentLength + stepLength;

for (unsigned int k = 0; k < 4; ++k)
{
const float wStart = k < 2 ? 1.f : 0.f;
const float wStop = k < 2 ? 0.f : 1.f;
const bool isStart = k < 2;
const float wStart = isStart ? 1.f : 0.f;
const float wStop = isStart ? 0.f : 1.f;
const float lineSpread = 0 < k && k < 3 ? 1.f : -1.f;
const float dashOffset = (isStart ? _currentLength : nextLength) * 2.f;

// start position
vertexData[vid++] = _currentX;
Expand All @@ -137,6 +144,9 @@ LineGeometry::lineTo(float x, float y, float z, unsigned int numSegments)
vertexData[vid++] = wStart;
vertexData[vid++] = wStop;
vertexData[vid++] = lineSpread;

// dash offset attribute
vertexData[vid++] = dashOffset;
}

const unsigned int iOffset = (_numLines << 2);
Expand All @@ -151,6 +161,7 @@ LineGeometry::lineTo(float x, float y, float z, unsigned int numSegments)
_currentX = nextX;
_currentY = nextY;
_currentZ = nextZ;
_currentLength = nextLength;
++_numLines;
}

Expand Down

0 comments on commit 955e7d7

Please sign in to comment.