HackMD
  • Prime
    Prime  Full-text search on all paid plans
    Search anywhere and reach everything in a Workspace with Prime plan.
    Got it
      • Create new note
      • Create a note from template
    • Prime  Full-text search on all paid plans
      Prime  Full-text search on all paid plans
      Search anywhere and reach everything in a Workspace with Prime plan.
      Got it
      • Sharing Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • More (Comment, Invitee)
      • Publishing
        Everyone on the web can find and read all notes of this public team.
        After the note is published, everyone on the web can find and read this note.
        See all published notes on profile page.
      • Commenting Enable
        Disabled Forbidden Owners Signed-in users Everyone
      • Permission
        • Forbidden
        • Owners
        • Signed-in users
        • Everyone
      • Invitee
      • No invitee
      • Options
      • Versions and GitHub Sync
      • Transfer ownership
      • Delete this note
      • Template
      • Save as template
      • Insert from template
      • Export
      • Dropbox
      • Google Drive
      • Gist
      • Import
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
      • Download
      • Markdown
      • HTML
      • Raw HTML
    Menu Sharing Create Help
    Create Create new note Create a note from template
    Menu
    Options
    Versions and GitHub Sync Transfer ownership Delete this note
    Export
    Dropbox Google Drive Gist
    Import
    Dropbox Google Drive Gist Clipboard
    Download
    Markdown HTML Raw HTML
    Back
    Sharing
    Sharing Link copied
    /edit
    View mode
    • Edit mode
    • View mode
    • Book mode
    • Slide mode
    Edit mode View mode Book mode Slide mode
    Note Permission
    Read
    Only me
    • Only me
    • Signed-in users
    • Everyone
    Only me Signed-in users Everyone
    Write
    Only me
    • Only me
    • Signed-in users
    • Everyone
    Only me Signed-in users Everyone
    More (Comment, Invitee)
    Publishing
    Everyone on the web can find and read all notes of this public team.
    After the note is published, everyone on the web can find and read this note.
    See all published notes on profile page.
    More (Comment, Invitee)
    Commenting Enable
    Disabled Forbidden Owners Signed-in users Everyone
    Permission
    Owners
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Invitee
    No invitee
       owned this note    owned this note      
    Published Linked with GitHub
    Like1 BookmarkBookmarked
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Grease Pencil Core Rewrite - Developer Discussion ## Introduction When grease pencil was [first introduced](https://web.archive.org/web/20100706205911/http://www.blender.org/development/release-logs/blender-248/grease-pencil/) to Blender back in 2008(?) by Joshua Leung it was designed around drawing annotations into the viewport. Over a decade later, the community has built a feature-rich 2D animation tool out of the initial draft of grease pencil. Today, there are some challenges and limitations that we face with how grease pencil is structured. I'd like to summarize the issues and propose a way forward. ## Problems *TODO* - Linked lists - Duplicated code - Lots of nested FOREACH loops that can be simplified with macros, callbacks or any other method. - Eval object duplication, undo - Grouping strokes/support for holes in fill shapes - Not compatible with Geometry Nodes - Code needs a general review and cleanup of obsolete functions/variables ## New design requirements - We need a structure that can hold a set of curves (strokes) per frame in the scene - We need layers of strokes that can be used to draw strokes in a specific order - Strokes can be grouped -> support holes in triangulation - Layers can be grouped - Adding/duplicating/removing/moving strokes from the data structure should be fast. Same applies to frames but it doesn't need to be as quick as for strokes and for layers it's even less important (e.g. it's ok if that takes 20 milliseconds). - Unify looping code. Try to reduce nested FOREACH code. ## Proposal The core idea is to replace `struct bGPDstroke` with `struct CurvesGeometry` and move to using C++ classes. This would mean that the current data stored on each point (position, pressure, strength, uv data, vertex color, time and flags such as selection) would become `CustomData` attributes. The same would apply to the strokes themselves, so thickness, hardness, material index, uv data, fill color, fill opacity, and selection would also be stored as attributes. The triangle data as well as the bounding box should be part of the runtime data and populated on load (although triangluation can be expensive so there might be some optimizations that could be done here). By default, every stroke would be treated as a polyline curve (`CURVE_TYPE_POLY`). The structure of `bGPdata` would be kept so there would still be a set of layers, which contain a set of frames, which contain the strokes. But we would replace all the actual linked-lists data structure with arrays and replace `bGPDstroke` with `CurvesGeometry`. Now `CurvesGeometry` could already contain multiple "strokes", so it might make sense to only put a single `CurvesGeometry` into a frame. I would suggest to keep it as an array though, so we can support groups of strokes and thus allow for holes and more. It would be good to use C++ functionalities as much as possible instead of custom made solutions, and define wrapper classes for Layers, Frames and Strokes and Points. ## Draw Engine The conversion must include the impact in the actual Draw Engine and the implications for the new Eevee rewrite. Maybe this topic need a separated document. ## Points to consider in new design There are some points that must be considered in the new design because it can be important to be supported in the future. - Layer Groups - Stroke Groups - Active Stroke (almost implemented already) - Active Vertex - What we do with GPencil modifers, move to mesh modifiers or do a custom GN modifier for grease pencil? ## Implementation details ### Initial approach: For layers I imagine something like ``` typedef struct bGPDlayer { bGPDframe *frames; int totframes; ... } bGPDlayer; ``` for the DNA struct. And then a wrapper c++ class: ``` namespace blender::bke::gpencil { class GPLayer : public ::bGPDlayer { ... MutableSpan<GPFrame> frames(); ... } ``` ### Idea from Hans: Instead of having a lot of small arrays in `CurvesGeometry` you would put the entire frame in a single `CurvesGeometry`. Additionally, there would not be a structure for layers but an index attribute on each stroke. So it would look something like: ``` struct bGPDlayer { string name; ... }; struct bGPdata { ... Array<CurvesGeometry> frames; CustomData frame_data; // for selection, frame_number, keytype, etc. int frame_active_index; Array<bGPDlayer> layers; int layer_active_index; } ``` ### Points to consider in the all Strokes by Frame in one array element Some points that must be reviewed to find a solution in the new data structure design. #### Operators by Layers Actually, there are lots of operators that are applied to only active layer. The logic now is: ``` LISTBASE_FOREACH (bGPDlayer *, gpl, &gpd->layers) { LISTBASE_FOREACH (bGPDframe *, gpf, &gpl->frames) { LISTBASE_FOREACH (bGPDstroke *, gps, &gpf->strokes) { ``` or ``` LISTBASE_FOREACH (bGPDlayer *, gpl, &gpd->layers) { bGPDframe *gpf = gpl->actframe; LISTBASE_FOREACH (bGPDstroke *, gps, &gpf->strokes) { ``` With the actual structure is very easy to select only the frames and strokes of one layer, but with the new structure we would need to read all frames and all strokes to determine what strokes are affected. The second case can be solved with the index using a code like this: ``` for (GPStroke gps : gpd->frames[frame_active_index].strokes()) { GPLayer gpl = gps->layer(); ``` One possible improvement would be to have an offset of the start element (stroke) for each layer in the frame. If we have 20 layers, instead to read all strokes of the frame and filter, we could "jump" directly to the first stroke of the layer and stop reading when the layer index is different (it assumes strokes are sorted by layer). #### Dopesheet The Dopesheet visualize the keys by layer, so the logic is read one layer and for each layer read the frames. This is done with something like: ``` LISTBASE_FOREACH (bGPDlayer *, gpl, &gpd->layers) { LISTBASE_FOREACH (bGPDframe *, gpf, &gpl->frames) { ``` With the new approach this is not possible because we need read all data to determine in what frames the layer has keys (strokes). If the strokes in the frame array are sorted, we could stop reading when the stroke layer index is greater than actual index, but if the strokes are not sorted, then we need read all. If we cannot do that, maybe the dopesheet can slow all UI of Blender. #### Draw Engine The draw engine needs to draw the layer strokes sorted, so the loop read layer, and for each layer all strokes. If you hide a layer in the UI, it's very easy for the draw engine skip all data... ``` LISTBASE_FOREACH (bGPDlayer *, gpl, &gpd->layers) { if (gpl->flag & HIDE) { continue; ``` If we cannot do this, the impact in the draw engine can be noticeable because all times needs to draw all data. #### Empty Frames A layer can have frames without strokes. This is very common. #### Sort Frames In some functionalities is critical to keep the array of frames sorted because some operations use a relative number of frames before or after current. Now this is easy because we have all frames of the layer linked to layer. Other problem is when you move keyframes in the dopesheet. For example, you have 3 layers with strokes in frame 24. Now you move the layer 2 keyframe to frame 20...now you need remove the strokes of the layer 2 from frame array element and move the strokes to other frame array element. #### Sort of Layers We need keep the layers sorted from top to bottom for some operations. Also, if we move the layer position in the stack, the frame array will be unsorted because the array of strokes in theory is sorted with the previous layer sort. #### Annotations Annotations are using internally GPencil data structures, but there are differences in some variables and how they are used. The logic would be move to new structure but we need to be careful and don't overcomplicated the design because we need support annotations. ## Plan of action I think there are basically two main approaches that we could take. 1. Work on a branch that is kept up-to-date with master. At some point merge all the changes at once. 2. Start working on master immediately doing incremental changes. There are pros and cons for both approaches but I think I personally would prefer 2. Not a big fan of a massive merge commit. For 2. we could start by writing the new DNA structs, create conversion functions to convert between the old DNA and new ones, and then use these convert functions when reading and writing to and from the `.blend` file. So this would be in `greasepencil_blend_write` and `greasepencil_blend_read_data`. We would keep using the old DNA when the file is read, but we would store the new DNA structs. Another option would be to only convert to the new structs at runtime and adapt only parts of the code. The downside of doing these conversions would be a decrease in performance (e.g. files might load longer, global undo would be slower). I would expect a conversion to take as long as a full copy of the data-block. We might be able to do these conversions in places, where a small delay would not be as noticeable.

    Import from clipboard

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lost their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.


    Upgrade

    All
    • All
    • Team
    No template.

    Create a template


    Upgrade

    Delete template

    Do you really want to delete this template?

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Tutorials

    Book Mode Tutorial

    Slide Example

    YAML Metadata

    Contacts

    Facebook

    Twitter

    Feedback

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions

    Versions and GitHub Sync

    Sign in to link this note to GitHub Learn more
    This note is not linked with GitHub Learn more
     
    Add badge Pull Push GitHub Link Settings
    Upgrade now

    Version named by    

    More Less
    • Edit
    • Delete

    Note content is identical to the latest version.
    Compare with
      Choose a version
      No search result
      Version not found

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub

        Please sign in to GitHub and install the HackMD app on your GitHub repo. Learn more

         Sign in to GitHub

        HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Available push count

        Upgrade

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Upgrade

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully