ソースを参照

Better handling of default values for shader nodes input

Nehon 7 年 前
コミット
514e809326

+ 30 - 30
jme3-core/src/main/java/com/jme3/shader/Glsl100ShaderGenerator.java

@@ -228,6 +228,7 @@ public class Glsl100ShaderGenerator extends ShaderGenerator {
      * 
      * 4. mapping outputs to global output if needed<br>
      * 
+     *
      *<br>
      * All of this is embed in a #if conditional statement if needed
      */
@@ -241,11 +242,35 @@ public class Glsl100ShaderGenerator extends ShaderGenerator {
 
         final List<String> declaredInputs = new ArrayList<>();
 
+        // Decalring variables with default values first
+        final ShaderNodeDefinition definition = shaderNode.getDefinition();
+
+        for (final ShaderNodeVariable var : definition.getInputs()) {
+
+            if (var.getType().startsWith("sampler")) {
+                continue;
+            }
+
+            final String fullName = shaderNode.getName() + "_" + var.getName();
+
+            final ShaderNodeVariable variable = new ShaderNodeVariable(var.getType(), shaderNode.getName(),
+                    var.getName(), var.getMultiplicity());
+
+            if (!isVarying(info, variable)) {
+                declareVariable(source, variable, var.getDefaultValue(), true, null);
+            }
+
+            nodeSource = replaceVariableName(nodeSource, variable);
+            declaredInputs.add(fullName);
+        }
+
         for (VariableMapping mapping : shaderNode.getInputMapping()) {
 
             final ShaderNodeVariable rightVariable = mapping.getRightVariable();
             final ShaderNodeVariable leftVariable = mapping.getLeftVariable();
 
+            String newName = shaderNode.getName() + "_" + leftVariable.getName();
+            boolean isDeclared = declaredInputs.contains(newName);
             //Variables fed with a sampler matparam or world param are replaced by the matparam itself
             //It avoids issue with samplers that have to be uniforms.
             if (rightVariable != null && isWorldOrMaterialParam(rightVariable) && rightVariable.getType().startsWith("sampler")) {
@@ -255,41 +280,16 @@ public class Glsl100ShaderGenerator extends ShaderGenerator {
                 if (leftVariable.getType().startsWith("sampler")) {
                     throw new IllegalArgumentException("a Sampler must be a uniform");
                 }
-
-                map(mapping, source);
+                map(mapping, source, !isDeclared);
             }
 
-            String newName = shaderNode.getName() + "_" + leftVariable.getName();
-            if (!declaredInputs.contains(newName)) {
+            if (!isDeclared) {
                 nodeSource = replace(nodeSource, leftVariable, newName);
                 declaredInputs.add(newName);
             }
         }
 
-        final ShaderNodeDefinition definition = shaderNode.getDefinition();
-
-        for (final ShaderNodeVariable var : definition.getInputs()) {
-
-            if (var.getDefaultValue() == null) {
-                continue;
-            }
-
-            final String fullName = shaderNode.getName() + "_" + var.getName();
-
-            if (declaredInputs.contains(fullName)) {
-                continue;
-            }
-
-            final ShaderNodeVariable variable = new ShaderNodeVariable(var.getType(), shaderNode.getName(),
-                    var.getName(), var.getMultiplicity());
 
-            if (!isVarying(info, variable)) {
-                declareVariable(source, variable, var.getDefaultValue(), true, null);
-            }
-
-            nodeSource = replaceVariableName(nodeSource, variable);
-            declaredInputs.add(fullName);
-        }
 
         for (ShaderNodeVariable var : definition.getOutputs()) {
             ShaderNodeVariable v = new ShaderNodeVariable(var.getType(), shaderNode.getName(), var.getName(), var.getMultiplicity());
@@ -304,7 +304,7 @@ public class Glsl100ShaderGenerator extends ShaderGenerator {
         source.append(nodeSource);
    
         for (VariableMapping mapping : shaderNode.getOutputMapping()) {
-            map(mapping, source);
+            map(mapping, source, true);
         }
         endCondition(shaderNode.getCondition(), source);
         comment(source, shaderNode, "End");
@@ -423,7 +423,7 @@ public class Glsl100ShaderGenerator extends ShaderGenerator {
      * @param mapping the VariableMapping to append
      * @param source  the StringBuilder to use
      */
-    protected void map(VariableMapping mapping, StringBuilder source) {
+    protected void map(VariableMapping mapping, StringBuilder source, boolean declare) {
 
         final ShaderNodeVariable leftVariable = mapping.getLeftVariable();
         final ShaderNodeVariable rightVariable = mapping.getRightVariable();
@@ -431,7 +431,7 @@ public class Glsl100ShaderGenerator extends ShaderGenerator {
 
         startCondition(mapping.getCondition(), source);
         appendIndent(source);
-        if (!leftVariable.isShaderOutput()) {
+        if (!leftVariable.isShaderOutput() &&  declare) {
             source.append(leftVariable.getType());
             source.append(" ");
         }

+ 3 - 3
jme3-core/src/main/resources/Common/MatDefs/ShaderNodes/Basic/ColorMix.j3sn

@@ -10,9 +10,9 @@ ShaderNodeDefinitions{
             @output outColor the mixed color
         }
         Input {
-            vec4 color1
-            vec4 color2
-            float factor
+            vec4 color1 vec4(1.0)
+            vec4 color2 vec4(1.0)
+            float factor 0.5
         }
         Output {
             vec4 outColor

+ 2 - 2
jme3-core/src/main/resources/Common/MatDefs/ShaderNodes/Basic/ColorMult.j3sn

@@ -9,8 +9,8 @@ ShaderNodeDefinitions{
             @output outColor the resulting color
         }
         Input {
-            vec4 color1
-            vec4 color2            
+            vec4 color1 vec4(1.0)
+            vec4 color2 vec4(1.0)
         }
         Output {
             vec4 outColor

+ 2 - 2
jme3-core/src/main/resources/Common/MatDefs/ShaderNodes/Math/Mult.j3sn

@@ -9,8 +9,8 @@ ShaderNodeDefinitions{
             @output outFloat the resulting coord
         }
         Input {
-            float float1
-            float float2           
+            float float1 1.0
+            float float2 1.0
         }
         Output {
             float outFloat

+ 2 - 2
jme3-core/src/main/resources/Common/MatDefs/ShaderNodes/Math/MultVec2.j3sn

@@ -9,8 +9,8 @@ ShaderNodeDefinitions{
             @output outCoord the resulting coord
         }
         Input {
-            vec2 coord1
-            vec2 coord2            
+            vec2 coord1 vec2(1.0)
+            vec2 coord2 vec2(1.0)
         }
         Output {
             vec2 outCoord

+ 2 - 2
jme3-core/src/main/resources/Common/MatDefs/ShaderNodes/Math/MultVec3.j3sn

@@ -9,8 +9,8 @@ ShaderNodeDefinitions{
             @output outPos the resulting position
         }
         Input {
-            vec2 pos1
-            vec2 pos2            
+            vec3 pos1 vec3(1.0)
+            vec3 pos2 vec3(1.0)
         }
         Output {
             vec2 outPos