Stroke path with non-rotating brush

164 views
Skip to first unread message

Andy Huska

unread,
Dec 31, 2023, 4:06:43 PM12/31/23
to skia-discuss
I'm creating a CNC tool path visualizer and until now the round endcap stroke feature has been perfect.  Now I have a rectangular tool head to implement that has a fixed orientation like a chisel-tipped marker.  When I render the path it needs to look similar to this when it changes direction:
nonrotatingbrush.png
I've been trying to decide between a few approaches, which I believe are in order of most-future proof for whatever unique tool shapes I may have down the road:  1) custom shader, 2) leveraging this experimental Skia code in my own fork: Variable Stroke Width, or  3) calculating the path outline manually then using DrawVertices.  

In an ideal world I could use vertices or a bitmap to define the tool.  I've not written any SkSL before so I thought I'd see if anyone could share some wisdom to help me choose the best path.

Thank you!
Andy

John Stiles

unread,
Dec 31, 2023, 6:43:52 PM12/31/23
to skia-d...@googlegroups.com
Your example path was entirely straight lines; does your tool always work with straight lines? You can draw a matching path in Skia by building an SkPath consisting of rectangles at each of the line points (where the rectangle is the size of the brush), and then adding trapezoids between each rectangle edge and the next rectangle's matching edge. This approach seems like it would work for any polygonal brush shape, but doesn't extrapolate to curves.

It's a bit verbose, but this fiddle seems to match your example picture. I kept things simple and just used moveTo and lineTo instead of leveraging the more advanced SkPath APIs:

Using the logic from the variable-width stroker also seems like a good option if you want to support curves; feel free to adapt it to your app as necessary. 

Unfortunately, we don't have SkSL support for path effects. SkSL can be used to write custom color filters, or custom shaders, but you can't generate a custom path with SkSL at this time. SkMesh does support applying a shader to vertex data, but it only takes a mesh as input, and is limited in various other ways (e.g. there isn't any CPU rendering support at all). 

  

--
You received this message because you are subscribed to the Google Groups "skia-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to skia-discuss...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/skia-discuss/5e133f80-03a6-44c9-8f64-e9a4729437cfn%40googlegroups.com.

Andy Huska

unread,
Feb 8, 2024, 12:37:00 PM2/8/24
to skia-discuss
Hi John, 

Thanks for your input!  I apologize for the late reply, apparently I did not have notifications for this thread setup and I just checked in this morning.  

Yes at this time our machine does only move in straight lines with the directional (rectangular) tools.  

Really appreciate your time spent creating the Fiddle and explaining some of the limits of the other approaches.

Best,
Andy

John Stiles

unread,
Feb 9, 2024, 2:58:41 PM2/9/24
to skia-d...@googlegroups.com
I'm glad that this approach will work for you! Paths are a pretty powerful tool.

Reply all
Reply to author
Forward
0 new messages