I used Unshaded.j3md material, but it can not be converted.
Please look at all my codes:
SelectionManager.java
[java]
package com.longyg.jme3.selection;
import com.jme3.asset.AssetManager;
import com.jme3.material.Material;
import com.jme3.material.MaterialDef;
import com.jme3.material.RenderState.BlendMode;
import com.jme3.math.ColorRGBA;
import com.jme3.scene.Geometry;
public class SelectionManager {
private Geometry targetGeo;
private Material savedMat;
private AssetManager assetManager;
public SelectionManager(Geometry targetGeo, AssetManager assetManager) {
this.targetGeo = targetGeo;
this.assetManager = assetManager;
}
void doSelected() {
// make it highlight
Material targetMat = targetGeo.getMaterial();
MaterialDef matDef = targetMat.getMaterialDef();
//System.out.println(matDef.getAssetName() + matDef.getName());
if (MaterialManager.isLightingMat(matDef)) {
targetMat.setBoolean("Minnaert", true);
targetMat.getAdditionalRenderState().setBlendMode(BlendMode.Additive);
}
targetGeo.getMesh().setLineWidth(3f);
savedMat = targetMat.clone();
Material newMat = new MaterialManager(assetManager).convertToLightBlow(targetMat);
newMat.setBoolean("Toon", true);
newMat.setFloat("EdgeSize", 0.2f);
newMat.setColor("EdgesColor", ColorRGBA.Blue);
newMat.setBoolean("Fog_Edges", true);
newMat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
newMat.getAdditionalRenderState().setWireframe(true);
targetGeo.setMaterial(newMat);
// tell the entity manager that is the current picked entity.
// rememeber the oldParentNode and the translation
// attach to characterLookNode
}
void doDeselected() {
// make it normal
Material targetMat = targetGeo.getMaterial();
MaterialDef matDef = targetMat.getMaterialDef();
//System.out.println(matDef.getAssetName() + matDef.getName());
if (MaterialManager.isLightingMat(matDef)) {
targetMat.setBoolean("Minnaert", false);
targetMat.getAdditionalRenderState().setBlendMode(BlendMode.Off);
}
if (MaterialManager.isLightBlowMat(matDef)) {
targetMat.setBoolean("Toon", false);
targetMat.setFloat("EdgeSize", 0f);
targetMat.setColor("EdgesColor", ColorRGBA.Blue);
targetMat.setBoolean("Fog_Edges", false);
targetMat.getAdditionalRenderState().setBlendMode(BlendMode.Off);
targetMat.getAdditionalRenderState().setWireframe(false);
}
}
}
[/java]
MaterialManager.java
[java]
package com.longyg.jme3.selection;
import com.jme3.asset.AssetManager;
import com.jme3.material.Material;
import com.jme3.material.MaterialDef;
import com.jme3.texture.Texture;
public class MaterialManager {
private AssetManager assetManager;
public MaterialManager(AssetManager assetManager) {
this.assetManager = assetManager;
}
public static boolean isLightBlowMat(MaterialDef matDef) {
return (matDef.getAssetName().endsWith("LightBlow.j3md"));
}
public static boolean isLightingMat(MaterialDef matDef) {
return (matDef.getAssetName().endsWith("Lighting.j3md"));
}
public Material convertToLightBlow(Material orgMat) {
// check if org is Lighting.jmd
if (isLightingMat(orgMat.getMaterialDef())) {
// construct the clone Mat
Material newMat = new Material(assetManager,
"MatDefs/LightBlow/LightBlow.j3md");
Texture diffuseMap = orgMat.getTextureParam("DiffuseMap")
.getTextureValue();
newMat.setTexture("DiffuseMap", diffuseMap);
return newMat;
} else {
return orgMat;
}
}
}
[/java]
SelectionApplication.java
[java]
package com.longyg.jme3.selection;
import com.jme3.app.SimpleApplication;
import com.jme3.collision.CollisionResult;
import com.jme3.collision.CollisionResults;
import com.jme3.font.BitmapText;
import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Ray;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
public class SelectionApplication extends SimpleApplication {
Node selectables;
Geometry mark;
SelectionManager selectionManager;
@Override
public void simpleInitApp() {
initCrossHairs();
initKeys();
selectables = new Node("Selectables");
rootNode.attachChild(selectables);
selectables.attachChild(makeCube("a Dragon", -2f, 0f, 1f));
selectables.attachChild(makeCube("a tin can", 1f, -2f, 0f));
selectables.attachChild(makeCube("the Sheriff", 0f, 1f, -2f));
selectables.attachChild(makeCube("the Deputy", 1f, 0f, -4f));
selectables.attachChild(makeFloor());
selectables.attachChild(makeCharacter());
}
protected void initCrossHairs() {
guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
BitmapText ch = new BitmapText(guiFont, false);
ch.setSize(guiFont.getCharSet().getRenderedSize() * 2);
ch.setText("+");
ch.setLocalTranslation(settings.getWidth() / 2, settings.getHeight() / 2, 0);
guiNode.attachChild(ch);
}
private void initKeys() {
inputManager.addMapping("Select", new KeyTrigger(KeyInput.KEY_SPACE),
new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.addListener(actionListener, "Select");
}
private ActionListener actionListener = new ActionListener() {
public void onAction(String name, boolean keyPressed, float tpf) {
if (name.equals("Select") && !keyPressed) {
CollisionResults results = new CollisionResults();
Ray ray = new Ray(cam.getLocation(), cam.getDirection());
selectables.collideWith(ray, results);
if (results.size() > 0) {
CollisionResult closest = results.getClosestCollision();
selectionManager = new SelectionManager(closest.getGeometry(), assetManager);
selectionManager.doSelected();
} else {
if (selectionManager != null) {
selectionManager.doDeselected();
}
}
}
}
};
protected Geometry makeCube(String name, float x, float y, float z) {
Box box = new Box(new Vector3f(x, y, z), 1, 1, 1);
Geometry cube = new Geometry(name, box);
Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat1.setColor("Color", ColorRGBA.randomColor());
cube.setMaterial(mat1);
return cube;
}
protected Geometry makeFloor() {
Box box = new Box(new Vector3f(0, -4, -5), 15, .2f, 15);
Geometry floor = new Geometry("the Floor", box);
Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat1.setColor("Color", ColorRGBA.Gray);
floor.setMaterial(mat1);
return floor;
}
protected Spatial makeCharacter() {
Spatial golem = assetManager.loadModel("Models/Oto/Oto.mesh.xml");
golem.scale(0.5f);
golem.setLocalTranslation(-1.0f, -1.5f, -0.6f);
DirectionalLight sun = new DirectionalLight();
sun.setDirection(new Vector3f(-0.1f, -0.7f, -1.0f));
golem.addLight(sun);
return golem;
}
public static void main(String[] args) {
SelectionApplication app = new SelectionApplication();
app.start();
}
}
[/java]
In the scene, some cubes are attached to rootNode, i want to can select them.