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 1f03bc9ae..72a85f760 100644 --- a/jme3-core/src/main/java/com/jme3/material/Material.java +++ b/jme3-core/src/main/java/com/jme3/material/Material.java @@ -711,19 +711,22 @@ public class Material implements CloneableSmartAsset, Cloneable, Savable { // supports all the caps. if (tech == null) { EnumSet rendererCaps = renderManager.getRenderer().getCaps(); - List techDefs = def.getSortedTechniqueDefs(name, renderManager); + List techDefs = def.getTechniqueDefs(name); if (techDefs == null || techDefs.isEmpty()) { throw new IllegalArgumentException( String.format("The requested technique %s is not available on material %s", name, def.getName())); } TechniqueDef lastTech = null; + float weight = 0; for (TechniqueDef techDef : techDefs) { if (rendererCaps.containsAll(techDef.getRequiredCaps())) { - // use the first one that supports all the caps - tech = new Technique(this, techDef); - techniques.put(name, tech); - break; + float techWeight = techDef.getWeight() + (techDef.getLightMode() == renderManager.getPreferredLightMode() ? 10f : 0); + if (techWeight > weight) { + tech = new Technique(this, techDef); + techniques.put(name, tech); + weight = techWeight; + } } lastTech = techDef; } @@ -734,6 +737,7 @@ public class Material implements CloneableSmartAsset, Cloneable, Savable { + "The capabilities %s are required.", name, def.getName(), lastTech.getRequiredCaps())); } + logger.log(Level.FINE, this.getMaterialDef().getName() + " selected technique def " + tech.getDef()); } else if (technique == tech) { // attempting to switch to an already // active technique. diff --git a/jme3-core/src/main/java/com/jme3/material/MaterialDef.java b/jme3-core/src/main/java/com/jme3/material/MaterialDef.java index bd01b8d06..537eac37b 100644 --- a/jme3-core/src/main/java/com/jme3/material/MaterialDef.java +++ b/jme3-core/src/main/java/com/jme3/material/MaterialDef.java @@ -55,7 +55,6 @@ public class MaterialDef{ private Map> techniques; private Map matParams; - private TechDefComparator comparator = new TechDefComparator(); /** * Serialization only. Do not use. @@ -188,17 +187,6 @@ public class MaterialDef{ return techniques.get(name); } - public List getSortedTechniqueDefs(String name, RenderManager rm) { - List techDefs = getTechniqueDefs(name); - if (techDefs == null) { - return null; - } - //Sorting the techdef depending on their weight (depending on their glsl version) and on the preferred light mode) - comparator.rm = rm; - Collections.sort(techDefs, comparator); - return techDefs; - } - /** * * @return the list of all the technique definitions names. @@ -207,17 +195,4 @@ public class MaterialDef{ return techniques.keySet(); } - public static class TechDefComparator implements Comparator { - - RenderManager rm; - - @Override - public int compare(TechniqueDef o1, TechniqueDef o2) { - float o1Weight = o1.getWeight() + (o1.getLightMode() == rm.getPreferredLightMode() ? 10f : 0); - float o2Weight = o2.getWeight() + (o2.getLightMode() == rm.getPreferredLightMode() ? 10f : 0); - return (int) Math.signum(o2Weight - o1Weight); - } - } - - } diff --git a/jme3-core/src/test/java/com/jme3/material/TestTechniqueDefOrdering.java b/jme3-core/src/test/java/com/jme3/material/TestTechniqueDefOrdering.java deleted file mode 100644 index 494d20b31..000000000 --- a/jme3-core/src/test/java/com/jme3/material/TestTechniqueDefOrdering.java +++ /dev/null @@ -1,167 +0,0 @@ -package com.jme3.material; - -import com.jme3.renderer.RenderManager; -import com.jme3.shader.Shader; -import com.jme3.system.NullRenderer; -import org.junit.Test; - -import java.util.*; - -import static org.junit.Assert.assertEquals; - -/** - * Created by Nehon on 14/01/2017. - */ -public class TestTechniqueDefOrdering { - - @Test - public void order() { - - RenderManager rm = new RenderManager(new NullRenderer()); - rm.setPreferredLightMode(TechniqueDef.LightMode.MultiPass); - MaterialDef.TechDefComparator comp = new MaterialDef.TechDefComparator(); - comp.rm = rm; - - - //random case - List defs = new ArrayList<>(); - TechniqueDef def = new TechniqueDef("tech", 1); - def.setShaderFile("", "", "GLSL100", "GLSL100"); - def.setLightMode(TechniqueDef.LightMode.SinglePass); - defs.add(def); - def = new TechniqueDef("tech2", 1); - def.setShaderFile("", "", "GLSL150", "GLSL150"); - def.setLightMode(TechniqueDef.LightMode.MultiPass); - defs.add(def); - def = new TechniqueDef("tech3", 1); - def.setShaderFile("", "", "GLSL110", "GLSL110"); - defs.add(def); - def = new TechniqueDef("tech4", 1); - def.setShaderFile("", "", "GLSL120", "GLSL120"); - defs.add(def); - def = new TechniqueDef("tech5", 1); - def.setShaderFile("", "", "GLSL130", "GLSL130"); - defs.add(def); - - Collections.sort(defs, comp); - - assertEquals(defs.get(0).getName(), "tech2"); - assertEquals(defs.get(1).getName(), "tech5"); - assertEquals(defs.get(2).getName(), "tech4"); - assertEquals(defs.get(3).getName(), "tech3"); - assertEquals(defs.get(4).getName(), "tech"); - - - //Test the unshaded material case: 2 disabled : 150 and 100 - defs = new ArrayList<>(); - def = new TechniqueDef("unshaded", 1); - def.setShaderFile("", "", "GLSL100", "GLSL100"); - defs.add(def); - def = new TechniqueDef("unshaded2", 1); - def.setShaderFile("", "", "GLSL150", "GLSL150"); - defs.add(def); - Collections.sort(defs, comp); - - assertEquals(defs.get(0).getName(), "unshaded2"); - assertEquals(defs.get(1).getName(), "unshaded"); - - //Test the lighting material case: 2 singlepass : 150 and 100, 2 multipass : 150 and 100 - defs = new ArrayList<>(); - def = new TechniqueDef("lighting1", 1); - def.setShaderFile("", "", "GLSL100", "GLSL100"); - def.setLightMode(TechniqueDef.LightMode.MultiPass); - defs.add(def); - def = new TechniqueDef("lighting2", 1); - def.setShaderFile("", "", "GLSL150", "GLSL150"); - def.setLightMode(TechniqueDef.LightMode.MultiPass); - defs.add(def); - def = new TechniqueDef("lighting3", 1); - def.setShaderFile("", "", "GLSL100", "GLSL100"); - def.setLightMode(TechniqueDef.LightMode.SinglePass); - defs.add(def); - def = new TechniqueDef("lighting4", 1); - def.setShaderFile("", "", "GLSL150", "GLSL150"); - def.setLightMode(TechniqueDef.LightMode.SinglePass); - defs.add(def); - Collections.sort(defs, comp); - - assertEquals(defs.get(0).getName(), "lighting2"); - assertEquals(defs.get(1).getName(), "lighting1"); - assertEquals(defs.get(2).getName(), "lighting4"); - assertEquals(defs.get(3).getName(), "lighting3"); - - //switching preferred lighting mode - rm.setPreferredLightMode(TechniqueDef.LightMode.SinglePass); - Collections.sort(defs, comp); - - assertEquals(defs.get(0).getName(), "lighting4"); - assertEquals(defs.get(1).getName(), "lighting3"); - assertEquals(defs.get(2).getName(), "lighting2"); - assertEquals(defs.get(3).getName(), "lighting1"); - - - //test setting source through the enumMaps method with random cases - rm.setPreferredLightMode(TechniqueDef.LightMode.MultiPass); - defs = new ArrayList<>(); - def = new TechniqueDef("lighting1", 1); - EnumMap em = new EnumMap<>(Shader.ShaderType.class); - em.put(Shader.ShaderType.Vertex, ""); - em.put(Shader.ShaderType.Fragment, ""); - em.put(Shader.ShaderType.Geometry, ""); - EnumMap l = new EnumMap<>(Shader.ShaderType.class); - l.put(Shader.ShaderType.Vertex, "GLSL100"); - l.put(Shader.ShaderType.Fragment, "GLSL100"); - l.put(Shader.ShaderType.Geometry, "GLSL100"); - def.setShaderFile(em, l); - def.setLightMode(TechniqueDef.LightMode.SinglePass); - defs.add(def); - - def = new TechniqueDef("lighting2", 1); - em = new EnumMap<>(Shader.ShaderType.class); - em.put(Shader.ShaderType.Vertex, ""); - em.put(Shader.ShaderType.Fragment, ""); - em.put(Shader.ShaderType.Geometry, ""); - l = new EnumMap<>(Shader.ShaderType.class); - l.put(Shader.ShaderType.Vertex, "GLSL100"); - l.put(Shader.ShaderType.Fragment, "GLSL100"); - l.put(Shader.ShaderType.Geometry, "GLSL100"); - def.setShaderFile(em, l); - def.setLightMode(TechniqueDef.LightMode.MultiPass); - defs.add(def); - - def = new TechniqueDef("lighting3", 1); - em = new EnumMap<>(Shader.ShaderType.class); - em.put(Shader.ShaderType.Vertex, ""); - em.put(Shader.ShaderType.Fragment, ""); - em.put(Shader.ShaderType.Geometry, ""); - l = new EnumMap<>(Shader.ShaderType.class); - l.put(Shader.ShaderType.Vertex, "GLSL150"); - l.put(Shader.ShaderType.Fragment, "GLSL150"); - l.put(Shader.ShaderType.Geometry, "GLSL150"); - def.setShaderFile(em, l); - def.setLightMode(TechniqueDef.LightMode.MultiPass); - defs.add(def); - - def = new TechniqueDef("lighting4", 1); - em = new EnumMap<>(Shader.ShaderType.class); - em.put(Shader.ShaderType.Vertex, ""); - em.put(Shader.ShaderType.Fragment, ""); - em.put(Shader.ShaderType.Geometry, ""); - l = new EnumMap<>(Shader.ShaderType.class); - l.put(Shader.ShaderType.Vertex, "GLSL130"); - l.put(Shader.ShaderType.Fragment, "GLSL130"); - l.put(Shader.ShaderType.Geometry, "GLSL110"); - def.setShaderFile(em, l); - def.setLightMode(TechniqueDef.LightMode.MultiPass); - defs.add(def); - - Collections.sort(defs, comp); - - assertEquals(defs.get(0).getName(), "lighting3"); - assertEquals(defs.get(1).getName(), "lighting4"); - assertEquals(defs.get(2).getName(), "lighting2"); - assertEquals(defs.get(3).getName(), "lighting1"); - - - } -}