Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions jme3-core/src/main/java/com/jme3/material/Material.java
Original file line number Diff line number Diff line change
Expand Up @@ -1150,6 +1150,7 @@ public void write(JmeExporter ex) throws IOException {
oc.write(def.getAssetName(), "material_def", null);
oc.write(additionalState, "render_state", null);
oc.write(transparent, "is_transparent", false);
oc.write(receivesShadows, "receives_shadows", false);
oc.write(name, "name", null);
oc.writeStringSavableMap(paramValues, "parameters", null);
}
Expand All @@ -1162,6 +1163,7 @@ public void read(JmeImporter im) throws IOException {
name = ic.readString("name", null);
additionalState = (RenderState) ic.readSavable("render_state", null);
transparent = ic.readBoolean("is_transparent", false);
receivesShadows = ic.readBoolean("receives_shadows", false);

// Load the material def
String defName = ic.readString("material_def", null);
Expand Down
84 changes: 48 additions & 36 deletions jme3-core/src/plugins/java/com/jme3/material/plugins/J3MLoader.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009-2022 jMonkeyEngine
* Copyright (c) 2009-2025 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -62,7 +62,6 @@
public class J3MLoader implements AssetLoader {

private static final Logger logger = Logger.getLogger(J3MLoader.class.getName());
// private ErrorLogger errors;
private ShaderNodeLoaderDelegate nodesLoaderDelegate;
boolean isUseNodes = false;
int langSize = 0;
Expand All @@ -86,7 +85,6 @@ public J3MLoader() {
shaderNames = new EnumMap<>(Shader.ShaderType.class);
}


// <TYPE> <LANG> : <SOURCE>
private void readShaderStatement(String statement) throws IOException {
String[] split = statement.split(":");
Expand All @@ -103,7 +101,6 @@ private void readShaderStatement(String statement) throws IOException {
}
}


private void readShaderDefinition(Shader.ShaderType shaderType, String name, String... languages) {
shaderNames.put(shaderType, name);

Expand All @@ -129,15 +126,15 @@ private void readLightMode(String statement) throws IOException{
LightMode lm = LightMode.valueOf(split[1]);
technique.setLightMode(lm);
}


// LightMode <SPACE>
private void readLightSpace(String statement) throws IOException{
String[] split = statement.split(whitespacePattern);
if (split.length != 2){
throw new IOException("LightSpace statement syntax incorrect");
}
TechniqueDef.LightSpace ls = TechniqueDef.LightSpace.valueOf(split[1]);
TechniqueDef.LightSpace ls = TechniqueDef.LightSpace.valueOf(split[1]);
technique.setLightSpace(ls);
}

Expand Down Expand Up @@ -297,7 +294,7 @@ private Texture parseTextureType(final VarType type, final String value) {
for (final TextureOptionValue textureOptionValue : textureOptionValues) {
textureOptionValue.applyToTexture(texture);
}
}
}
return texture;
}

Expand All @@ -311,28 +308,28 @@ private Object readValue(final VarType type, final String value) throws IOExcept
if (split.length != 1){
throw new IOException("Float value parameter must have 1 entry: " + value);
}
return Float.parseFloat(split[0]);
return Float.parseFloat(split[0]);
case Vector2:
if (split.length != 2){
throw new IOException("Vector2 value parameter must have 2 entries: " + value);
}
return new Vector2f(Float.parseFloat(split[0]),
Float.parseFloat(split[1]));
Float.parseFloat(split[1]));
case Vector3:
if (split.length != 3){
throw new IOException("Vector3 value parameter must have 3 entries: " + value);
}
return new Vector3f(Float.parseFloat(split[0]),
Float.parseFloat(split[1]),
Float.parseFloat(split[2]));
Float.parseFloat(split[1]),
Float.parseFloat(split[2]));
case Vector4:
if (split.length != 4){
throw new IOException("Vector4 value parameter must have 4 entries: " + value);
}
return new ColorRGBA(Float.parseFloat(split[0]),
Float.parseFloat(split[1]),
Float.parseFloat(split[2]),
Float.parseFloat(split[3]));
Float.parseFloat(split[1]),
Float.parseFloat(split[2]),
Float.parseFloat(split[3]));
case Int:
if (split.length != 1){
throw new IOException("Int value parameter must have 1 entry: " + value);
Expand Down Expand Up @@ -536,12 +533,12 @@ private void readDefine(String statement) throws IOException{
MatParam param = materialDef.getMaterialParam(paramName);
if (param == null) {
logger.log(Level.WARNING, "In technique ''{0}'':\n"
+ "Define ''{1}'' mapped to non-existent"
+ " material parameter ''{2}'', ignoring.",
+ "Define ''{1}'' mapped to non-existent"
+ " material parameter ''{2}'', ignoring.",
new Object[]{technique.getName(), defineName, paramName});
return;
}

VarType paramType = param.getVarType();
technique.addShaderParamDefine(paramName, paramType, defineName);
}else{
Expand All @@ -553,7 +550,6 @@ private void readDefines(List<Statement> defineList) throws IOException{
for (Statement statement : defineList){
readDefine(statement.getLine());
}

}

private void readTechniqueStatement(Statement statement) throws IOException{
Expand Down Expand Up @@ -600,12 +596,23 @@ private void readTechniqueStatement(Statement statement) throws IOException{
}
}

private void readTransparentStatement(String statement) throws IOException{
private void readTransparentStatement(String statement) throws IOException {
boolean transparent = readBooleanStatement(statement, "Transparent");
material.setTransparent(transparent);
}

private void readReceivesShadowsStatement(String statement) throws IOException {
boolean receivesShadows = readBooleanStatement(statement, "ReceivesShadows");
material.setReceivesShadows(receivesShadows);
}

private boolean readBooleanStatement(String statement, String propertyName) throws IOException {
String[] split = statement.split(whitespacePattern);
if (split.length != 2){
throw new IOException("Transparent statement syntax incorrect");
if (split.length != 2) {
throw new IOException(propertyName + " statement syntax incorrect");
}
material.setTransparent(parseBoolean(split[1]));

return parseBoolean(split[1]);
}

private static String createShaderPrologue(List<String> presetDefines) {
Expand Down Expand Up @@ -665,7 +672,7 @@ private void readTechnique(Statement techStat) throws IOException{

if(isUseNodes){
//used for caching later, the shader here is not a file.

// KIRILL 9/19/2015
// Not sure if this is needed anymore, since shader caching
// is now done by TechniqueDef.
Expand Down Expand Up @@ -722,9 +729,11 @@ private void loadFromRoot(List<Statement> roots) throws IOException{
boolean extending = false;
Statement materialStat = roots.get(0);
String materialName = materialStat.getLine();

if (materialName.startsWith("MaterialDef")){
materialName = materialName.substring("MaterialDef ".length()).trim();
extending = false;

}else if (materialName.startsWith("Material")){
materialName = materialName.substring("Material ".length()).trim();
extending = true;
Expand Down Expand Up @@ -753,7 +762,7 @@ private void loadFromRoot(List<Statement> roots) throws IOException{
material = new Material(def);
material.setKey(key);
material.setName(split[0].trim());
// material.setAssetName(fileName);

}else if (split.length == 1){
if (extending){
throw new MatParseException("Expected ':', got '{'", materialStat);
Expand All @@ -765,24 +774,26 @@ private void loadFromRoot(List<Statement> roots) throws IOException{
throw new MatParseException("Cannot use colon in material name/path", materialStat);
}

for (Statement statement : materialStat.getContents()){
for (Statement statement : materialStat.getContents()) {
split = statement.getLine().split("[ \\{]");
String statType = split[0];
if (extending){
if (statType.equals("MaterialParameters")){
if (extending) {
if (statType.equals("MaterialParameters")) {
readExtendingMaterialParams(statement.getContents());
}else if (statType.equals("AdditionalRenderState")){
} else if (statType.equals("AdditionalRenderState")) {
readAdditionalRenderState(statement.getContents());
}else if (statType.equals("Transparent")){
} else if (statType.equals("Transparent")) {
readTransparentStatement(statement.getLine());
} else if (statType.equals("ReceivesShadows")) {
readReceivesShadowsStatement(statement.getLine());
}
}else{
if (statType.equals("Technique")){
} else {
if (statType.equals("Technique")) {
readTechnique(statement);
}else if (statType.equals("MaterialParameters")){
} else if (statType.equals("MaterialParameters")) {
readMaterialParams(statement.getContents());
}else{
throw new MatParseException("Expected material statement, got '"+statType+"'", statement);
} else {
throw new MatParseException("Expected material statement, got '" + statType + "'", statement);
}
}
}
Expand All @@ -797,6 +808,7 @@ public Object load(AssetInfo info) throws IOException {
key = info.getKey();
if (key.getExtension().equals("j3m") && !(key instanceof MaterialKey)) {
throw new IOException("Material instances must be loaded via MaterialKey");

} else if (key.getExtension().equals("j3md") && key instanceof MaterialKey) {
throw new IOException("Material definitions must be loaded via AssetKey");
}
Expand Down Expand Up @@ -968,4 +980,4 @@ public void applyToTexture(final Texture texture) {
textureOption.applyToTexture(value, texture);
}
}
}
}
Loading