-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Line Stipple Shader #68
base: develop
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I look forward to see if you've turned this into a working example - filtering vertices based on the position on screen could work, but I have my concerns with this approach (see comments below).
An alternative could be to modify the line_tesselator to output multiple triangle strips, rather than just one - the idea is the same, but working at the level of the input vertices.
src/shaders/line_stipple.frag
Outdated
|
||
void main() { | ||
float step = 1.0 / line_stipple_factor; | ||
float pattern = mod(position.x * step, line_stipple_pattern); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Relying on the x-coordinate alone will surely break things for completely vertical lines, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I see! I can add a second mod()
function to check the modulo of the y-coordinate of the position vector with the line stipple pattern. So by changing the condition on line 14, it will allow stippling for vertical lines too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, I don't think even that will be enough! We have to think in terms of stippling along the vector of the line we're drawing, rather than a cardinal (screen) direction, otherwise our results won't be consistent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I’ve come up with a good way to do this using dot products, and by letting the shader take vertex position and texture coordinates as inputs, while outputting the fragment texture coordinates. I'll send you my idea as a commit once I run some tests.
src/material.cpp
Outdated
case (FragmentShaderType::LineStipple): | ||
shader3->setFragmentShaderCode( | ||
Qt3DRender::QShaderProgram::loadSource(QUrl(QStringLiteral("qrc:/shaders/shaders/line_stipple.frag")))); | ||
break; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the line stipple is to be a fragment shader then we would have to have a version for each type of colouring (e.g. Phong) we want to be "stipplable". Do we think this is the best approach?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay yes! I think I can implement a similar approach to the line stippling code, but using a geometry shader instead. Then we would only need one geometry shader, regardless of how the lines are coloured. I'll add the updated commit here;
This Line Stipple Shader takes three uniforms:
line_width
: The width of the line in pixels.line_stipple_factor
: The number of pixels between each stipple.line_stipple_pattern
: A bit pattern that determines which pixels are stippled.The shader uses the
mod()
function to determine if a pixel should be stippled. If the pixel's x-coordinate is divisible by theline_stipple_factor
, then it is stippled.The shader also uses the
discard
keyword to discard any pixels that are stippled.The
line_width
,line_stipple_factor
, andline_stipple_pattern
uniforms have been set to enable the stippled line to be drawn on the output Example Binary.