@ -50,7 +50,7 @@ public class Glsl100ShaderGenerator extends ShaderGenerator {
* the indentation characters 1à tabulation characters
* the indentation characters 1à tabulation characters
* /
* /
private final static String INDENTCHAR = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" ;
private final static String INDENTCHAR = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" ;
private ShaderNodeVariable inPosTmp ;
protected ShaderNodeVariable inPosTmp ;
/ * *
/ * *
* creates a Glsl100ShaderGenerator
* creates a Glsl100ShaderGenerator
@ -110,7 +110,7 @@ public class Glsl100ShaderGenerator extends ShaderGenerator {
protected void generateVaryings ( StringBuilder source , ShaderGenerationInfo info , ShaderType type ) {
protected void generateVaryings ( StringBuilder source , ShaderGenerationInfo info , ShaderType type ) {
source . append ( "\n" ) ;
source . append ( "\n" ) ;
for ( ShaderNodeVariable var : info . getVaryings ( ) ) {
for ( ShaderNodeVariable var : info . getVaryings ( ) ) {
declareVarying ( source , var , type = = ShaderType . Vertex ? false : true ) ;
declareVarying ( source , var , type ! = ShaderType . Vertex ) ;
}
}
}
}
@ -141,7 +141,7 @@ public class Glsl100ShaderGenerator extends ShaderGenerator {
@Override
@Override
protected void generateStartOfMainSection ( StringBuilder source , ShaderGenerationInfo info , ShaderType type ) {
protected void generateStartOfMainSection ( StringBuilder source , ShaderGenerationInfo info , ShaderType type ) {
source . append ( "\n" ) ;
source . append ( "\n" ) ;
source . append ( "void main(){\n" ) ;
source . append ( "void main() {\n" ) ;
indent ( ) ;
indent ( ) ;
appendIndent ( source ) ;
appendIndent ( source ) ;
if ( type = = ShaderType . Vertex ) {
if ( type = = ShaderType . Vertex ) {
@ -237,27 +237,59 @@ public class Glsl100ShaderGenerator extends ShaderGenerator {
comment ( source , shaderNode , "Begin" ) ;
comment ( source , shaderNode , "Begin" ) ;
startCondition ( shaderNode . getCondition ( ) , source ) ;
startCondition ( shaderNode . getCondition ( ) , source ) ;
List < String > declaredInputs = new ArrayList < String > ( ) ;
final List < String > declaredInputs = new ArrayList < > ( ) ;
for ( VariableMapping mapping : shaderNode . getInputMapping ( ) ) {
for ( VariableMapping mapping : shaderNode . getInputMapping ( ) ) {
final ShaderNodeVariable rightVariable = mapping . getRightVariable ( ) ;
final ShaderNodeVariable leftVariable = mapping . getLeftVariable ( ) ;
//Variables fed with a sampler matparam or world param are replaced by the matparam itself
//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.
//It avoids issue with samplers that have to be uniforms.
if ( isWorldOrMaterialParam ( mapping . getRightVariable ( ) ) & & mapping . getRightVariable ( ) . getType ( ) . startsWith ( "sampler" ) ) {
if ( isWorldOrMaterialParam ( rightVariable ) & & rightVariable . getType ( ) . startsWith ( "sampler" ) ) {
nodeSource = replace ( nodeSource , mapping . getLeftVariable ( ) , mapping . getRightVariable ( ) . getPrefix ( ) + mapping . getRightVariable ( ) . getName ( ) ) ;
nodeSource = replace ( nodeSource , leftVariable , rightVariable . getPrefix ( ) + rightVariable . getName ( ) ) ;
} else {
} else {
if ( mapping . getLeftVariable ( ) . getType ( ) . startsWith ( "sampler" ) ) {
if ( leftVariable . getType ( ) . startsWith ( "sampler" ) ) {
throw new IllegalArgumentException ( "a Sampler must be a uniform" ) ;
throw new IllegalArgumentException ( "a Sampler must be a uniform" ) ;
}
}
map ( mapping , source ) ;
map ( mapping , source ) ;
String newName = shaderNode . getName ( ) + "_" + mapping . getLeftVariable ( ) . getName ( ) ;
}
String newName = shaderNode . getName ( ) + "_" + leftVariable . getName ( ) ;
if ( ! declaredInputs . contains ( newName ) ) {
if ( ! declaredInputs . contains ( newName ) ) {
nodeSource = replace ( nodeSource , mapping . getLeftVariable ( ) , newName ) ;
nodeSource = replace ( nodeSource , l eftVariable, newName ) ;
declaredInputs . add ( 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 : shaderNode . getDefinition ( ) . getOutputs ( ) ) {
for ( ShaderNodeVariable var : definition . getOutputs ( ) ) {
ShaderNodeVariable v = new ShaderNodeVariable ( var . getType ( ) , shaderNode . getName ( ) , var . getName ( ) , var . getMultiplicity ( ) ) ;
ShaderNodeVariable v = new ShaderNodeVariable ( var . getType ( ) , shaderNode . getName ( ) , var . getName ( ) , var . getMultiplicity ( ) ) ;
if ( ! declaredInputs . contains ( shaderNode . getName ( ) + "_" + var . getName ( ) ) ) {
if ( ! declaredInputs . contains ( shaderNode . getName ( ) + "_" + var . getName ( ) ) ) {
if ( ! isVarying ( info , v ) ) {
if ( ! isVarying ( info , v ) ) {
@ -603,7 +635,7 @@ public class Glsl100ShaderGenerator extends ShaderGenerator {
* makes sure inPosition attribute is of type vec3 or vec4
* makes sure inPosition attribute is of type vec3 or vec4
* @param var the inPosition attribute
* @param var the inPosition attribute
* /
* /
private void fixInPositionType ( ShaderNodeVariable var ) {
protected void fixInPositionType ( ShaderNodeVariable var ) {
if ( ! var . getType ( ) . equals ( "vec3" ) | | ! var . getType ( ) . equals ( "vec4" ) ) {
if ( ! var . getType ( ) . equals ( "vec3" ) | | ! var . getType ( ) . equals ( "vec4" ) ) {
var . setType ( "vec3" ) ;
var . setType ( "vec3" ) ;
}
}