use of com.jme3.material.MatParam in project jmonkeyengine by jMonkeyEngine.
the class Material method applyOverrides.
private int applyOverrides(Renderer renderer, Shader shader, SafeArrayList<MatParamOverride> overrides, int unit) {
for (MatParamOverride override : overrides.getArray()) {
VarType type = override.getVarType();
MatParam paramDef = def.getMaterialParam(override.getName());
if (paramDef == null || paramDef.getVarType() != type || !override.isEnabled()) {
continue;
}
Uniform uniform = shader.getUniform(override.getPrefixedName());
if (override.getValue() != null) {
if (type.isTextureType()) {
renderer.setTexture(unit, (Texture) override.getValue());
uniform.setValue(VarType.Int, unit);
unit++;
} else {
uniform.setValue(type, override.getValue());
}
} else {
uniform.clearValue();
}
}
return unit;
}
use of com.jme3.material.MatParam in project jmonkeyengine by jMonkeyEngine.
the class Material method updateShaderMaterialParameters.
private int updateShaderMaterialParameters(Renderer renderer, Shader shader, SafeArrayList<MatParamOverride> worldOverrides, SafeArrayList<MatParamOverride> forcedOverrides) {
int unit = 0;
if (worldOverrides != null) {
unit = applyOverrides(renderer, shader, worldOverrides, unit);
}
if (forcedOverrides != null) {
unit = applyOverrides(renderer, shader, forcedOverrides, unit);
}
for (int i = 0; i < paramValues.size(); i++) {
MatParam param = paramValues.getValue(i);
VarType type = param.getVarType();
Uniform uniform = shader.getUniform(param.getPrefixedName());
if (uniform.isSetByCurrentMaterial()) {
continue;
}
if (type.isTextureType()) {
renderer.setTexture(unit, (Texture) param.getValue());
uniform.setValue(VarType.Int, unit);
unit++;
} else {
uniform.setValue(type, param.getValue());
}
}
//TODO HACKY HACK remove this when texture unit is handled by the uniform.
return unit;
}
use of com.jme3.material.MatParam in project jmonkeyengine by jMonkeyEngine.
the class MaterialDebugAppState method reloadMaterial.
public Material reloadMaterial(Material mat) {
//clear the entire cache, there might be more clever things to do, like clearing only the matdef, and the associated shaders.
assetManager.clearCache();
//creating a dummy mat with the mat def of the mat to reload
Material dummy = new Material(mat.getMaterialDef());
for (MatParam matParam : mat.getParams()) {
dummy.setParam(matParam.getName(), matParam.getVarType(), matParam.getValue());
}
dummy.getAdditionalRenderState().set(mat.getAdditionalRenderState());
//creating a dummy geom and assigning the dummy material to it
Geometry dummyGeom = new Geometry("dummyGeom", new Box(1f, 1f, 1f));
dummyGeom.setMaterial(dummy);
try {
//preloading the dummyGeom, this call will compile the shader again
renderManager.preloadScene(dummyGeom);
} catch (RendererException e) {
//compilation error, the shader code will be output to the console
//the following code will output the error
//System.err.println(e.getMessage());
Logger.getLogger(MaterialDebugAppState.class.getName()).log(Level.SEVERE, e.getMessage());
return null;
}
Logger.getLogger(MaterialDebugAppState.class.getName()).log(Level.INFO, "Material succesfully reloaded");
//System.out.println("Material succesfully reloaded");
return dummy;
}
use of com.jme3.material.MatParam in project jmonkeyengine by jMonkeyEngine.
the class ShaderNodeLoaderDelegate method readInputMapping.
/**
* reads an input mapping
*
* @param statement1 the statement being read
* @return the mapping
* @throws IOException
*/
public VariableMapping readInputMapping(Statement statement1) throws IOException {
VariableMapping mapping = null;
try {
mapping = parseMapping(statement1, new boolean[] { false, true });
} catch (Exception e) {
throw new MatParseException("Unexpected mapping format", statement1, e);
}
ShaderNodeVariable left = mapping.getLeftVariable();
ShaderNodeVariable right = mapping.getRightVariable();
if (!updateVariableFromList(left, shaderNode.getDefinition().getInputs())) {
throw new MatParseException(left.getName() + " is not an input variable of " + shaderNode.getDefinition().getName(), statement1);
}
if (left.getType().startsWith("sampler") && !right.getNameSpace().equals("MatParam")) {
throw new MatParseException("Samplers can only be assigned to MatParams", statement1);
}
if (right.getNameSpace().equals("Global")) {
//Globals are all vec4 for now (maybe forever...)
right.setType("vec4");
// updateCondition(right, mapping);
storeGlobal(right, statement1);
} else if (right.getNameSpace().equals("Attr")) {
if (shaderNode.getDefinition().getType() == Shader.ShaderType.Fragment) {
throw new MatParseException("Cannot have an attribute as input in a fragment shader" + right.getName(), statement1);
}
updateVarFromAttributes(mapping.getRightVariable(), mapping);
// updateCondition(mapping.getRightVariable(), mapping);
storeAttribute(mapping.getRightVariable());
} else if (right.getNameSpace().equals("MatParam")) {
MatParam param = findMatParam(right.getName());
if (param == null) {
throw new MatParseException("Could not find a Material Parameter named " + right.getName(), statement1);
}
if (shaderNode.getDefinition().getType() == Shader.ShaderType.Vertex) {
if (updateRightFromUniforms(param, mapping, vertexDeclaredUniforms, statement1)) {
storeVertexUniform(mapping.getRightVariable());
}
} else {
if (updateRightFromUniforms(param, mapping, fragmentDeclaredUniforms, statement1)) {
if (mapping.getRightVariable().getType().contains("|")) {
String type = fixSamplerType(left.getType(), mapping.getRightVariable().getType());
if (type != null) {
mapping.getRightVariable().setType(type);
} else {
throw new MatParseException(param.getVarType().toString() + " can only be matched to one of " + param.getVarType().getGlslType().replaceAll("\\|", ",") + " found " + left.getType(), statement1);
}
}
storeFragmentUniform(mapping.getRightVariable());
}
}
} else if (right.getNameSpace().equals("WorldParam")) {
UniformBinding worldParam = findWorldParam(right.getName());
if (worldParam == null) {
throw new MatParseException("Could not find a World Parameter named " + right.getName(), statement1);
}
if (shaderNode.getDefinition().getType() == Shader.ShaderType.Vertex) {
if (updateRightFromUniforms(worldParam, mapping, vertexDeclaredUniforms)) {
storeVertexUniform(mapping.getRightVariable());
}
} else {
if (updateRightFromUniforms(worldParam, mapping, fragmentDeclaredUniforms)) {
storeFragmentUniform(mapping.getRightVariable());
}
}
} else {
ShaderNode node = nodes.get(right.getNameSpace());
if (node == null) {
throw new MatParseException("Undeclared node" + right.getNameSpace() + ". Make sure this node is declared before the current node", statement1);
}
ShaderNodeVariable var = findNodeOutput(node.getDefinition().getOutputs(), right.getName());
if (var == null) {
throw new MatParseException("Cannot find output variable" + right.getName() + " form ShaderNode " + node.getName(), statement1);
}
right.setNameSpace(node.getName());
right.setType(var.getType());
right.setMultiplicity(var.getMultiplicity());
mapping.setRightVariable(right);
storeVaryings(node, mapping.getRightVariable());
}
checkTypes(mapping, statement1);
return mapping;
}
use of com.jme3.material.MatParam in project jmonkeyengine by jMonkeyEngine.
the class Material method getSortId.
/**
* Returns the sorting ID or sorting index for this material.
*
* <p>The sorting ID is used internally by the system to sort rendering
* of geometries. It sorted to reduce shader switches, if the shaders
* are equal, then it is sorted by textures.
*
* @return The sorting ID used for sorting geometries for rendering.
*/
public int getSortId() {
if (sortingId == -1 && technique != null) {
sortingId = technique.getSortId() << 16;
int texturesSortId = 17;
for (int i = 0; i < paramValues.size(); i++) {
MatParam param = paramValues.getValue(i);
if (!param.getVarType().isTextureType()) {
continue;
}
Texture texture = (Texture) param.getValue();
if (texture == null) {
continue;
}
Image image = texture.getImage();
if (image == null) {
continue;
}
int textureId = image.getId();
if (textureId == -1) {
textureId = 0;
}
texturesSortId = texturesSortId * 23 + textureId;
}
sortingId |= texturesSortId & 0xFFFF;
}
return sortingId;
}
Aggregations