Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MatParam: format code and update license #2201

Closed
wants to merge 2 commits into from
Closed
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
66 changes: 39 additions & 27 deletions jme3-core/src/main/java/com/jme3/material/MatParam.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009-2021 jMonkeyEngine
* Copyright (c) 2009-2024 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -31,16 +31,26 @@
*/
package com.jme3.material;

import java.io.IOException;
import java.util.Arrays;

import com.jme3.asset.TextureKey;
import com.jme3.export.*;
import com.jme3.math.*;
import com.jme3.export.InputCapsule;
import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter;
import com.jme3.export.OutputCapsule;
import com.jme3.export.Savable;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Matrix3f;
import com.jme3.math.Matrix4f;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.math.Vector4f;
import com.jme3.shader.VarType;
import com.jme3.texture.Texture;
import com.jme3.texture.Texture.WrapMode;

import java.io.IOException;
import java.util.Arrays;

/**
* Describes a material parameter. This is used for both defining a name and type
* as well as a material parameter value.
Expand All @@ -58,8 +68,8 @@ public class MatParam implements Savable, Cloneable {
/**
* Create a new material parameter. For internal use only.
*
* @param type the type of the parameter
* @param name the desired parameter name
* @param type the type of the parameter
* @param name the desired parameter name
* @param value the desired parameter value (alias created)
*/
public MatParam(VarType type, String name, Object value) {
Expand All @@ -75,20 +85,19 @@ public MatParam(VarType type, String name, Object value) {
protected MatParam() {
}


public boolean isTypeCheckEnabled() {
return typeCheck;
}


/**
* Enable type check for this param.
* When type check is enabled a RuntimeException is thrown if
* an object of the wrong type is passed to setValue.
* @param v (default = true)
*
* @param typeCheck (default = true)
*/
public void setTypeCheckEnabled(boolean v) {
typeCheck = v;
public void setTypeCheckEnabled(boolean typeCheck) {
this.typeCheck = typeCheck;
}

/**
Expand All @@ -102,6 +111,7 @@ public VarType getVarType() {

/**
* Returns the name of the material parameter.
*
* @return the name of the material parameter.
*/
public String getName() {
Expand Down Expand Up @@ -158,8 +168,10 @@ public void setValue(Object value) {
}
}
if (!valid) {
throw new RuntimeException("Trying to assign a value of type " + value.getClass() + " to " + this.getName() + " of type " + type.name() + ". Valid types are "
+ Arrays.deepToString(type.getJavaType()));
throw new RuntimeException("Trying to assign a value of type " + value.getClass()
+ " to " + this.getName()
+ " of type " + type.name()
+ ". Valid types are " + Arrays.deepToString(type.getJavaType()));
}
}
}
Expand Down Expand Up @@ -274,12 +286,12 @@ public String getValueAsString() {
case TextureCubeMap:
Texture texVal = (Texture) value;
TextureKey texKey = (TextureKey) texVal.getKey();
if (texKey == null){
//throw new UnsupportedOperationException("The specified MatParam cannot be represented in J3M");
if (texKey == null) {
// throw new UnsupportedOperationException("The specified MatParam cannot be represented in J3M");
// this is used in toString and the above line causes blender materials to throw this exception.
// toStrings should be very robust IMO as even debuggers often invoke toString and logging code
// often does as well, even implicitly.
return texVal+":returned null key";
return texVal + ":returned null key";
}

String ret = "";
Expand All @@ -293,16 +305,16 @@ public String getValueAsString() {
ret += getWrapMode(texVal, Texture.WrapAxis.R);

//Min and Mag filter
Texture.MinFilter def = Texture.MinFilter.BilinearNoMipMaps;
if(texVal.getImage().hasMipmaps() || texKey.isGenerateMips()){
Texture.MinFilter def = Texture.MinFilter.BilinearNoMipMaps;
if (texVal.getImage().hasMipmaps() || texKey.isGenerateMips()) {
def = Texture.MinFilter.Trilinear;
}
if(texVal.getMinFilter() != def){
ret += "Min" + texVal.getMinFilter().name()+ " ";
if (texVal.getMinFilter() != def) {
ret += "Min" + texVal.getMinFilter().name() + " ";
}

if(texVal.getMagFilter() != Texture.MagFilter.Bilinear){
ret += "Mag" + texVal.getMagFilter().name()+ " ";
if (texVal.getMagFilter() != Texture.MagFilter.Bilinear) {
ret += "Mag" + texVal.getMagFilter().name() + " ";
}

return ret + "\"" + texKey.getName() + "\"";
Expand All @@ -315,12 +327,12 @@ private String getWrapMode(Texture texVal, Texture.WrapAxis axis) {
WrapMode mode = WrapMode.EdgeClamp;
try {
mode = texVal.getWrap(axis);
} catch (IllegalArgumentException e) {
//this axis doesn't exist on the texture
} catch (IllegalArgumentException ex) {
// this axis doesn't exist on the texture
return "";
}
if (mode != WrapMode.EdgeClamp) {
return"Wrap"+ mode.name() + "_" + axis.name() + " ";
return "Wrap" + mode.name() + "_" + axis.name() + " ";
}
return "";
}
Expand Down
55 changes: 23 additions & 32 deletions jme3-core/src/main/java/com/jme3/util/MaterialDebugAppState.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009-2021 jMonkeyEngine
* Copyright (c) 2009-2024 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -98,9 +98,8 @@ public class MaterialDebugAppState extends AbstractAppState {
private RenderManager renderManager;
private AssetManager assetManager;
private InputManager inputManager;
final private List<Binding> bindings = new ArrayList<>();
final private Map<Trigger,List<Binding>> fileTriggers = new HashMap<> ();

private final List<Binding> bindings = new ArrayList<>();
private final Map<Trigger, List<Binding>> fileTriggers = new HashMap<>();

@Override
public void initialize(AppStateManager stateManager, Application app) {
Expand All @@ -119,20 +118,18 @@ public void initialize(AppStateManager stateManager, Application app) {
* @param spat the spatial to reload
*/
public void registerBinding(Trigger trigger, final Spatial spat) {
if(spat instanceof Geometry){
GeometryBinding binding = new GeometryBinding(trigger, (Geometry)spat);
if (spat instanceof Geometry) {
GeometryBinding binding = new GeometryBinding(trigger, (Geometry) spat);
bindings.add(binding);
if (isInitialized()) {
bind(binding);
}
}else if (spat instanceof Node){
for (Spatial child : ((Node)spat).getChildren()) {
} else if (spat instanceof Node) {
for (Spatial child : ((Node) spat).getChildren()) {
registerBinding(trigger, child);
}
}
}



/**
* Will reload the filter's materials whenever the trigger is fired.
Expand All @@ -146,7 +143,6 @@ public void registerBinding(Trigger trigger, final Filter filter) {
bind(binding);
}
}


/**
* Will reload the filter's materials whenever the shader file is changed
Expand Down Expand Up @@ -174,7 +170,7 @@ private void bind(final Binding binding) {
if (binding.getTrigger() instanceof FileChangedTrigger) {
FileChangedTrigger t = (FileChangedTrigger) binding.getTrigger();
List<Binding> b = fileTriggers.get(t);
if(b == null){
if (b == null) {
t.init();
b = new ArrayList<Binding>();
fileTriggers.put(t, b);
Expand All @@ -186,7 +182,7 @@ private void bind(final Binding binding) {
@Override
public void onAction(String name, boolean isPressed, float tpf) {
if (actionName.equals(name) && isPressed) {
//reloading the material
// reloading the material
binding.reload();
}
}
Expand All @@ -197,42 +193,41 @@ public void onAction(String name, boolean isPressed, float tpf) {
}

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.
// 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
// creating a dummy mat with the mat def of the mat to reload
// Force the reloading of the asset, otherwise the new shader code will not be applied.
Material dummy = new Material(assetManager, mat.getMaterialDef().getAssetName());

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
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
// 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());
// compilation error, the shader code will be output to the console
// the following code will output the error
Logger.getLogger(MaterialDebugAppState.class.getName()).log(Level.SEVERE, e.getMessage());
return null;
}

Logger.getLogger(MaterialDebugAppState.class.getName()).log(Level.INFO, "Material successfully reloaded");
//System.out.println("Material successfully reloaded");
return dummy;
}

@Override
public void update(float tpf) {
super.update(tpf); //To change body of generated methods, choose Tools | Templates.
super.update(tpf);
for (Trigger trigger : fileTriggers.keySet()) {
if (trigger instanceof FileChangedTrigger) {
FileChangedTrigger t = (FileChangedTrigger) trigger;
Expand All @@ -243,7 +238,7 @@ public void update(float tpf) {
}
}
}
}
}
}

private interface Binding {
Expand All @@ -263,23 +258,21 @@ private class GeometryBinding implements Binding {
public GeometryBinding(Trigger trigger, Geometry geom) {
this.trigger = trigger;
this.geom = geom;

}

@Override
public void reload() {
Material reloadedMat = reloadMaterial(geom.getMaterial());
//if the reload is successful, we re setup the material with its params and reassign it to the box
// if the reload is successful, we re setup the material with its params and
// reassign it to the box
if (reloadedMat != null) {
// setupMaterial(reloadedMat);
geom.setMaterial(reloadedMat);
}
}

@Override
public String getActionName() {
return geom.getName() + "Reload";

}

@Override
Expand Down Expand Up @@ -319,12 +312,11 @@ public void reload() {
} else {
field.set(filter, mat);
}

}
if (field.getType().isInstance(p)) {
field.setAccessible(true);
p = (Filter.Pass) field.get(filter);
if (p!= null && p.getPassMaterial() != null) {
if (p != null && p.getPassMaterial() != null) {
Material mat = reloadMaterial(p.getPassMaterial());
if (mat == null) {
return;
Expand Down Expand Up @@ -352,7 +344,6 @@ public void reload() {
} catch (IllegalArgumentException | IllegalAccessException ex) {
Logger.getLogger(MaterialDebugAppState.class.getName()).log(Level.SEVERE, null, ex);
}

}

@Override
Expand Down
Loading