Visual glitch when crossing a GreasedLine

Hello everyone!
It has been a while since my last post, but I am still working with this amazing engine :slight_smile:
I have been playing around with GreasedLine (which is wonderful by the way) but I am noticing some strange behaviour when crossing the line.
With sizeAttenuation=true the line gets really small, with sizeAttenuation=false it disappears.

It can be tested in a playground available in the greasedLine documentation ; just position yourself so that you cross the line when the camera moves automatically and you get something like this:

Ok, so that was quite an introduction. I think the problem starts at this line and continues until the calculation of grlDir, around line 76.

This is because, when the camera crosses the line, grlPrevious and/or grlNext are outside the frustum, so their projections in the screen space are invalid.

What we want is a valid grlDir, i.e. a valid direction in the screen space; hopefully the code is great, so we have everything we need.
Here is my solution, replace lines 57 to 77 with this:

  vec3 worldDir = normalize(grlNext - grlPrevious);
  vec3 nearPosition = positionUpdated + (worldDir * 0.1);

  mat4 grlMatrix = viewProjection * finalWorld;
  vec4 grlFinalPosition = grlMatrix * vec4(positionUpdated , 1.0);
  vec4 screenNearPos = grlMatrix * vec4(nearPosition, 1.0);

  vec2 grlLinePosition = grlFix(grlFinalPosition, grlAspect);
  vec2 grlLineNearPosition = grlFix(screenNearPos, grlAspect);
  vec2 grlDir = normalize(grlLineNearPosition - grlLinePosition);

I use grlNext and grlPrevious to calculate the direction in world space.
Then, to compute the direction in screen space, my first point is the projection of positionUpdated, aka grlFinalPosition, the other is a projection of a point slightly further away in 3D space from positionUpdated, in the direction previously computed (which, from what I tested, is close enough to always be in the frustum).

I know some lines are the same, I wanted to keep the logic of the variable declaration already in the shader as it makes it easier to read.

Maybe I will find the time to do the PR… (Proud father of a two month old second son, my time is very limited)

Hope you still find this useful, thanks again for the GreasedLine <3

2 Likes

cc @roland the mastermind behing the greasedline

1 Like

Hi!

Many thanks for the detailed bug report and for the posibble solution! I’ll change the part of the shader code with your code snippet and do some tests.

Your code works straight out of the box :wink: Thanks!

One thing I had to change is to lowering the nearPosition value and multiplying the worldDir by 0.001:

Thanks again for the fix <3

PR on the way.

1 Like

EDIT: guys I messed up something in my local repo in another branch and I accidentally removed package.json from all branches LOL. I’ll never use git push --force again :see_no_evil: I closed this PR and I am creating a new one…

1 Like

You’re welcome!
I knew you’d do the PR, I read the thread on the right-handed issue before I posted :wink:.

1 Like
1 Like

2 Likes