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..7e10ce1c 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,15 @@ public abstract void draw( PGraphics g, boolean usesZ, float drawX, float drawY, float currAlphaPc); + public HDrawable material(HMaterial mat){ + this.material = mat; + this.material.setActive(true); + return this; + } + + public HMaterial getMaterial(){ + return material; + } /** * An HIterator used for iterating through HDrawable's children. diff --git a/src/main/java/hype/HMaterial.java b/src/main/java/hype/HMaterial.java new file mode 100644 index 00000000..fbad2acb --- /dev/null +++ b/src/main/java/hype/HMaterial.java @@ -0,0 +1,45 @@ +package hype; + +import processing.core.PGraphics; +import processing.opengl.PShader; + +public class HMaterial { + PShader shader; + boolean active = false; + + 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; + } + + +} 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