From 9fd7552f89e8e50d8f9138d30b485f3680e5ccd4 Mon Sep 17 00:00:00 2001 From: nacho cossio Date: Wed, 24 Apr 2019 18:25:20 +0200 Subject: [PATCH 1/3] added class to handle per drawable shader --- src/main/java/hype/HMaterial.java | 48 +++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/main/java/hype/HMaterial.java diff --git a/src/main/java/hype/HMaterial.java b/src/main/java/hype/HMaterial.java new file mode 100644 index 00000000..bc6a14f7 --- /dev/null +++ b/src/main/java/hype/HMaterial.java @@ -0,0 +1,48 @@ +package hype; + +import javax.swing.text.StyledEditorKit.BoldAction; + +import processing.core.PApplet; +import processing.core.PGraphics; +import processing.opengl.PShader; + +public class HMaterial { + PShader shader; + boolean active = true; + + public HMaterial(){ + shader = null; + } + + public HMaterial(HMaterial copy){ + shader = copy.shader; + } + + public void bind(PGraphics pg){ + if(!active) + return; + if(shader != null) + { + pg.shader(shader); + } + } + + public boolean getActive() { + return active; + } + + public HMaterial setActive(boolean _active) { + this.active = _active; + return this; + } + public PShader getShader() { + return shader; + } + + public HMaterial setShader(PShader shader) { + this.shader = shader; + return this; + } + + +} From 72aad558a95a2dffedac1416907850b422c620d2 Mon Sep 17 00:00:00 2001 From: nacho cossio Date: Wed, 24 Apr 2019 18:26:28 +0200 Subject: [PATCH 2/3] changes to use HMaterial class --- src/main/java/hype/H.java | 4 ++++ src/main/java/hype/HDrawable.java | 21 +++++++++++++++++++++ src/main/java/hype/HStage.java | 10 +++++++++- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/main/java/hype/H.java b/src/main/java/hype/H.java index 423d26f6..246f5b0e 100644 --- a/src/main/java/hype/H.java +++ b/src/main/java/hype/H.java @@ -215,6 +215,10 @@ public static boolean mouseStarted() { return mouse.started(); } + public static void useMaterials(boolean use){ + stage.useMaterials(use); + } + /** * Cast an image to PImage * @param imgArg Type: PImage, HImageHolder, String diff --git a/src/main/java/hype/HDrawable.java b/src/main/java/hype/HDrawable.java index fb34eef4..f3dcd9f8 100644 --- a/src/main/java/hype/HDrawable.java +++ b/src/main/java/hype/HDrawable.java @@ -39,6 +39,7 @@ public abstract class HDrawable extends HNode implements HDirectable, /** The extras bundle of this drawable */ protected HBundle extras; + protected HMaterial material; /** The x location of this drawable */ protected float x; @@ -112,6 +113,8 @@ public HDrawable() { width = DEFAULT_WIDTH; height = DEFAULT_HEIGHT; + + material = new HMaterial(); } /** @@ -143,6 +146,7 @@ public void copyPropertiesFrom(HDrawable other) { stroke = other.stroke; strokeCap = other.strokeCap; strokeJoin = other.strokeJoin; + material = other.material; } /** @@ -2025,6 +2029,7 @@ protected void applyStyle(PGraphics g, float currAlphaPc) { g.strokeCap(strokeCap); g.strokeJoin(strokeJoin); } else g.noStroke(); + material.bind(g); } /** @@ -2058,9 +2063,17 @@ public void paintAll(PGraphics g, boolean usesZ, float currAlphaPc) { // Compute current alpha currAlphaPc *= alphaPc; + // TODO: check if the shader is already binded before doing it again. They are stored in pointShader, lineShader and polyShader here:https://github.com/processing/processing/blob/master/core/src/processing/opengl/PGraphicsOpenGL.java +// if(material.shader != null){ +// material.bind(g); +// } +// else +// g.resetShader(); // Draw self draw(g, usesZ,-anchorX(),-anchorY(),currAlphaPc); + if( material.active) + g.resetShader(); // Draw children HDrawable child = firstChild; while(child != null) { @@ -2087,6 +2100,14 @@ public abstract void draw( PGraphics g, boolean usesZ, float drawX, float drawY, float currAlphaPc); + public HDrawable material(HMaterial mat){ + this.material = mat; + return this; + } + + public HMaterial getMaterial(){ + return material; + } /** * An HIterator used for iterating through HDrawable's children. diff --git a/src/main/java/hype/HStage.java b/src/main/java/hype/HStage.java index 13ea5323..847299be 100644 --- a/src/main/java/hype/HStage.java +++ b/src/main/java/hype/HStage.java @@ -1,4 +1,4 @@ -package hype; + package hype; import hype.interfaces.HImageHolder; import hype.interfaces.HConstants; @@ -147,6 +147,14 @@ public void paintAll(PGraphics g, boolean usesZ, float currAlphaPc) { } + public void useMaterials(boolean use){ + + HDrawable child = firstChild; + while(child != null) { + child.getMaterial().setActive(use); + child = child.next(); + } + } // DEACTIVATED HDRAWABLE METHODS // @Override From 65039964f8e84cd57744049b70be610cf354e335 Mon Sep 17 00:00:00 2001 From: nacho cossio Date: Thu, 25 Apr 2019 12:20:47 +0200 Subject: [PATCH 3/3] Cchanged behaviour, HMaterial is inactive by default, gets activated when assigned to a HDrawable --- src/main/java/hype/HDrawable.java | 1 + src/main/java/hype/HMaterial.java | 5 +---- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/hype/HDrawable.java b/src/main/java/hype/HDrawable.java index f3dcd9f8..7e10ce1c 100644 --- a/src/main/java/hype/HDrawable.java +++ b/src/main/java/hype/HDrawable.java @@ -2102,6 +2102,7 @@ public abstract void draw( PGraphics g, boolean usesZ, public HDrawable material(HMaterial mat){ this.material = mat; + this.material.setActive(true); return this; } diff --git a/src/main/java/hype/HMaterial.java b/src/main/java/hype/HMaterial.java index bc6a14f7..fbad2acb 100644 --- a/src/main/java/hype/HMaterial.java +++ b/src/main/java/hype/HMaterial.java @@ -1,14 +1,11 @@ package hype; -import javax.swing.text.StyledEditorKit.BoldAction; - -import processing.core.PApplet; import processing.core.PGraphics; import processing.opengl.PShader; public class HMaterial { PShader shader; - boolean active = true; + boolean active = false; public HMaterial(){ shader = null;