use of com.jme3.material.MatParam in project jmonkeyengine by jMonkeyEngine.
the class ShaderNodeLoaderDelegate method updateRightFromUniforms.
/**
* updates the right variable of the given mapping from a MatParam (a
* WorldParam) it checks if the uniform hasn't already been loaded, add it
* to the maps if not.
*
* @param param the MatParam
* @param mapping the mapping
* @param map the map of uniforms to search into
* @return true if the param was added to the map
*/
public boolean updateRightFromUniforms(MatParam param, VariableMapping mapping, Map<String, DeclaredVariable> map, Statement statement) throws MatParseException {
ShaderNodeVariable right = mapping.getRightVariable();
DeclaredVariable dv = map.get(param.getPrefixedName());
if (dv == null) {
right.setType(param.getVarType().getGlslType());
right.setName(param.getPrefixedName());
if (mapping.getLeftVariable().getMultiplicity() != null) {
if (!param.getVarType().name().endsWith("Array")) {
throw new MatParseException(param.getName() + " is not of Array type", statement);
}
String multiplicity = mapping.getLeftVariable().getMultiplicity();
try {
Integer.parseInt(multiplicity);
} catch (NumberFormatException nfe) {
//multiplicity is not an int attempting to find for a material parameter.
MatParam mp = findMatParam(multiplicity);
if (mp != null) {
addDefine(multiplicity, VarType.Int);
multiplicity = multiplicity.toUpperCase();
} else {
throw new MatParseException("Wrong multiplicity for variable" + mapping.getLeftVariable().getName() + ". " + multiplicity + " should be an int or a declared material parameter.", statement);
}
}
right.setMultiplicity(multiplicity);
}
dv = new DeclaredVariable(right);
map.put(right.getName(), dv);
dv.addNode(shaderNode);
mapping.setRightVariable(right);
return true;
}
dv.addNode(shaderNode);
mapping.setRightVariable(dv.var);
return false;
}
use of com.jme3.material.MatParam in project jmonkeyengine by jMonkeyEngine.
the class TestMaterialDefWrite method testWriteMat.
@Test
public void testWriteMat() throws Exception {
Material mat = new Material(assetManager, "example.j3md");
final ByteArrayOutputStream stream = new ByteArrayOutputStream();
J3mdExporter exporter = new J3mdExporter();
try {
exporter.save(mat.getMaterialDef(), stream);
} catch (IOException e) {
e.printStackTrace();
}
// System.err.println(stream.toString());
J3MLoader loader = new J3MLoader();
AssetInfo info = new AssetInfo(assetManager, new AssetKey("test")) {
@Override
public InputStream openStream() {
return new ByteArrayInputStream(stream.toByteArray());
}
};
MaterialDef matDef = (MaterialDef) loader.load(info);
MaterialDef ref = mat.getMaterialDef();
for (MatParam refParam : ref.getMaterialParams()) {
MatParam matParam = matDef.getMaterialParam(refParam.getName());
assertTrue(refParam != null);
assertEquals(refParam, matParam);
}
for (String key : ref.getTechniqueDefsNames()) {
List<TechniqueDef> refDefs = ref.getTechniqueDefs(key);
List<TechniqueDef> defs = matDef.getTechniqueDefs(key);
assertNotNull(defs);
assertTrue(refDefs.size() == defs.size());
for (int i = 0; i < refDefs.size(); i++) {
assertEqualTechniqueDefs(refDefs.get(i), defs.get(i));
}
}
}
use of com.jme3.material.MatParam in project jmonkeyengine by jMonkeyEngine.
the class J3MOutputCapsule method formatMatParam.
private String formatMatParam(MatParam param) {
VarType type = param.getVarType();
Object val = param.getValue();
switch(type) {
case Boolean:
case Float:
case Int:
return val.toString();
case Vector2:
Vector2f v2 = (Vector2f) val;
return v2.getX() + " " + v2.getY();
case Vector3:
Vector3f v3 = (Vector3f) val;
return v3.getX() + " " + v3.getY() + " " + v3.getZ();
case Vector4:
// can be either ColorRGBA, Vector4f or Quaternion
if (val instanceof Vector4f) {
Vector4f v4 = (Vector4f) val;
return v4.getX() + " " + v4.getY() + " " + v4.getZ() + " " + v4.getW();
} else if (val instanceof ColorRGBA) {
ColorRGBA color = (ColorRGBA) val;
return color.getRed() + " " + color.getGreen() + " " + color.getBlue() + " " + color.getAlpha();
} else if (val instanceof Quaternion) {
Quaternion quat = (Quaternion) val;
return quat.getX() + " " + quat.getY() + " " + quat.getZ() + " " + quat.getW();
} else {
throw new UnsupportedOperationException("Unexpected Vector4 type: " + val);
}
default:
// parameter type not supported in J3M
return null;
}
}
Aggregations