From 50658b8e539e93bbc3ac0eaa979b712738947ef3 Mon Sep 17 00:00:00 2001 From: Kirill Vainer Date: Tue, 8 Mar 2016 15:14:00 -0500 Subject: [PATCH] MPO: add ability to disable an override --- .../com/jme3/material/MatParamOverride.java | 34 +++++++++++++++++++ .../main/java/com/jme3/material/Material.java | 2 +- .../java/com/jme3/material/Technique.java | 3 ++ ...java => MaterialMatParamOverrideTest.java} | 27 +++++++++++++-- 4 files changed, 62 insertions(+), 4 deletions(-) rename jme3-core/src/test/java/com/jme3/material/{TechniqueDefMatParamOverrideTest.java => MaterialMatParamOverrideTest.java} (94%) diff --git a/jme3-core/src/main/java/com/jme3/material/MatParamOverride.java b/jme3-core/src/main/java/com/jme3/material/MatParamOverride.java index 0843f3093..5b64ccdd0 100644 --- a/jme3-core/src/main/java/com/jme3/material/MatParamOverride.java +++ b/jme3-core/src/main/java/com/jme3/material/MatParamOverride.java @@ -35,6 +35,8 @@ import com.jme3.shader.VarType; public final class MatParamOverride extends MatParam { + private boolean enabled = true; + public MatParamOverride() { super(); } @@ -43,4 +45,36 @@ public final class MatParamOverride extends MatParam { super(type, name, value); } + @Override + public boolean equals(Object obj) { + return super.equals(obj) && this.enabled == ((MatParamOverride) obj).enabled; + } + + @Override + public int hashCode() { + int hash = super.hashCode(); + hash = 59 * hash + (enabled ? 1 : 0); + return hash; + } + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + @Override + public void write(JmeExporter ex) throws IOException { + super.write(ex); + OutputCapsule oc = ex.getCapsule(this); + oc.write(enabled, "enabled", true); + } + + @Override + public void read(JmeImporter im) throws IOException { + super.read(im); + InputCapsule ic = im.getCapsule(this); + enabled = ic.readBoolean("enabled", true); + } } diff --git a/jme3-core/src/main/java/com/jme3/material/Material.java b/jme3-core/src/main/java/com/jme3/material/Material.java index adc7669e2..efb9fcd09 100644 --- a/jme3-core/src/main/java/com/jme3/material/Material.java +++ b/jme3-core/src/main/java/com/jme3/material/Material.java @@ -792,7 +792,7 @@ public class Material implements CloneableSmartAsset, Cloneable, Savable { VarType type = override.getVarType(); MatParam paramDef = def.getMaterialParam(override.getName()); - if (paramDef == null || paramDef.getVarType() != type) { + if (paramDef == null || paramDef.getVarType() != type || !override.isEnabled()) { continue; } diff --git a/jme3-core/src/main/java/com/jme3/material/Technique.java b/jme3-core/src/main/java/com/jme3/material/Technique.java index aecf5ab97..fcadef9ab 100644 --- a/jme3-core/src/main/java/com/jme3/material/Technique.java +++ b/jme3-core/src/main/java/com/jme3/material/Technique.java @@ -128,6 +128,9 @@ public final class Technique { dynamicDefines.setAll(paramDefines); for (MatParamOverride override : overrides) { + if (!override.isEnabled()) { + continue; + } Integer defineId = def.getShaderParamDefineId(override.name); if (defineId != null) { if (def.getDefineIdType(defineId) == override.type) { diff --git a/jme3-core/src/test/java/com/jme3/material/TechniqueDefMatParamOverrideTest.java b/jme3-core/src/test/java/com/jme3/material/MaterialMatParamOverrideTest.java similarity index 94% rename from jme3-core/src/test/java/com/jme3/material/TechniqueDefMatParamOverrideTest.java rename to jme3-core/src/test/java/com/jme3/material/MaterialMatParamOverrideTest.java index 1be918492..2cc71b3cc 100644 --- a/jme3-core/src/test/java/com/jme3/material/TechniqueDefMatParamOverrideTest.java +++ b/jme3-core/src/test/java/com/jme3/material/MaterialMatParamOverrideTest.java @@ -55,7 +55,7 @@ import com.jme3.texture.Texture2D; import java.util.HashMap; import java.util.Map; -public class TechniqueDefMatParamOverrideTest { +public class MaterialMatParamOverrideTest { private static final HashSet IGNORED_UNIFORMS = new HashSet( Arrays.asList(new String[]{"m_ParallaxHeight", "m_Shininess"})); @@ -119,6 +119,27 @@ public class TechniqueDefMatParamOverrideTest { outUniforms(uniform("AlphaDiscardThreshold", VarType.Float, 2.79f)); } + @Test + public void testMpoDisable() { + material("Common/MatDefs/Light/Lighting.j3md"); + inputMp(mpoFloat("AlphaDiscardThreshold", 3.12f)); + + MatParamOverride override = mpoFloat("AlphaDiscardThreshold", 2.79f); + inputMpo(override); + outDefines(def("DISCARD_ALPHA", VarType.Float, 2.79f)); + outUniforms(uniform("AlphaDiscardThreshold", VarType.Float, 2.79f)); + + reset(); + override.setEnabled(false); + outDefines(def("DISCARD_ALPHA", VarType.Float, 3.12f)); + outUniforms(uniform("AlphaDiscardThreshold", VarType.Float, 3.12f)); + + reset(); + override.setEnabled(true); + outDefines(def("DISCARD_ALPHA", VarType.Float, 2.79f)); + outUniforms(uniform("AlphaDiscardThreshold", VarType.Float, 2.79f)); + } + @Test public void testIntMpoOnly() { material("Common/MatDefs/Light/Lighting.j3md"); @@ -349,13 +370,13 @@ public class TechniqueDefMatParamOverrideTest { private final NullRenderer renderer = new NullRenderer() { @Override public void setShader(Shader shader) { - TechniqueDefMatParamOverrideTest.this.usedShader = shader; + MaterialMatParamOverrideTest.this.usedShader = shader; evaluated = true; } @Override public void setTexture(int unit, Texture texture) { - TechniqueDefMatParamOverrideTest.this.usedTextures[unit] = texture; + MaterialMatParamOverrideTest.this.usedTextures[unit] = texture; } }; private final RenderManager renderManager = new RenderManager(renderer);