-
Notifications
You must be signed in to change notification settings - Fork 350
Draw pen lines via fragment shader #438
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,57 @@ | ||
precision mediump float; | ||
|
||
#ifdef DRAW_MODE_line | ||
uniform vec2 u_stageSize; | ||
uniform float u_lineThickness; | ||
uniform vec4 u_penPoints; | ||
|
||
// Add this to divisors to prevent division by 0, which results in NaNs propagating through calculations. | ||
// Smaller values can cause problems on some mobile devices. | ||
const float epsilon = 1e-3; | ||
#endif | ||
|
||
#ifndef DRAW_MODE_line | ||
uniform mat4 u_projectionMatrix; | ||
uniform mat4 u_modelMatrix; | ||
attribute vec2 a_texCoord; | ||
#endif | ||
|
||
attribute vec2 a_position; | ||
attribute vec2 a_texCoord; | ||
|
||
varying vec2 v_texCoord; | ||
|
||
#ifdef DRAW_MODE_lineSample | ||
uniform float u_positionScalar; | ||
#endif | ||
|
||
void main() { | ||
#ifdef DRAW_MODE_lineSample | ||
vec2 position = a_position; | ||
position.y = clamp(position.y * u_positionScalar, -0.5, 0.5); | ||
gl_Position = u_projectionMatrix * u_modelMatrix * vec4(position, 0, 1); | ||
#else | ||
gl_Position = u_projectionMatrix * u_modelMatrix * vec4(a_position, 0, 1); | ||
#endif | ||
v_texCoord = a_texCoord; | ||
#ifdef DRAW_MODE_line | ||
// Calculate a rotated ("tight") bounding box around the two pen points. | ||
// Yes, we're doing this 6 times (once per vertex), but on actual GPU hardware, | ||
// it's still faster than doing it in JS combined with the cost of uniformMatrix4fv. | ||
|
||
// Expand line bounds by sqrt(2) / 2 each side-- this ensures that all antialiased pixels | ||
// fall within the quad, even at a 45-degree diagonal | ||
vec2 position = a_position; | ||
float expandedRadius = (u_lineThickness * 0.5) + 1.4142135623730951; | ||
|
||
float lineLength = length(u_penPoints.zw - u_penPoints.xy); | ||
|
||
position.x *= lineLength + (2.0 * expandedRadius); | ||
position.y *= 2.0 * expandedRadius; | ||
|
||
// Center around first pen point | ||
position -= expandedRadius; | ||
|
||
// Rotate quad to line angle | ||
vec2 normalized = (u_penPoints.zw - u_penPoints.xy + epsilon) / (lineLength + epsilon); | ||
position = mat2(normalized.x, normalized.y, -normalized.y, normalized.x) * position; | ||
// Translate quad | ||
position += u_penPoints.xy; | ||
|
||
// Apply view transform | ||
position *= 2.0 / u_stageSize; | ||
|
||
gl_Position = vec4(position, 0, 1); | ||
v_texCoord = position * 0.5 * u_stageSize; | ||
#else | ||
gl_Position = u_projectionMatrix * u_modelMatrix * vec4(a_position, 0, 1); | ||
v_texCoord = a_texCoord; | ||
#endif | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.