processing / processing/processing4

Support for #include in PShader (Enhancement)

Open
#572 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

core help wanted opengl
Dominant language
Java
Stars
494
Forks
183
Avg merge
4h 39m
Merged PRs (30d)
3

Description

Created by: patriciogonzalezvivo

Although #include routines are defined by Khronos as an extension they are left for the environment/framework running the GL driver to implement. The reason for that is the required access to file system. Implementing #include routines are fairly straight forward with a couple of gotchas to avoid, like: relative paths and infinite recursions.
Most serious CG engines support them because their advantages on modularity, reusability and clearer code.
For Processing this also could mean support for LYGIA a rich shader library pack with useful resources for creative coders and artist.
I made a simple implementations of a #include parser in this repository together with some examples on how it can be use.

void loadSource(String current_folder, String filename, ArrayList<String> source) {
  println("search for", filename, "in", current_folder);
  File file = new File(current_folder,filename);
  String url = file.getPath().substring(1);
  println("open", url);
  String[] lines = loadStrings(url); 
  
  for (int i = 0; i < lines.length; i++) {
    String line = lines[i];
    String line_trim = line.trim();
    if (line_trim.startsWith("#include")){
      String include_file = line_trim.substring("#include".length()).replace("\"", "").trim();
      File f = new File(current_folder,include_file);
      loadSource(f.getParent(), f.getName(), source);
    } else {
      source.add(line + System.getProperty("line.separator") );
    }
  }
}

Adding this feature to createShader() to parse both vert and frag shaders shouldn't be hard and potentially bring numerous advantages.

cc @codeanticode @SableRaf @cacheflowe

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating createShader() and the code paths that load vertex and fragment PShaders. Read the linked lygia_p5_examples include parser for its handling of relative paths and recursion, then identify existing shader-loading tests or examples. Done means both shader types can expand #include files safely, including nested relative includes, without infinite recursion.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
computer-graphics
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.