1. 交互控制
两个步骤:
1)名字与触发器关联:
inputManager.addMapping("Rotate", new KeyTrigger(KeyInput.KEY_SPACE),new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
2)触发器与监听器关联:
inputManager.addListener(analogListener/actionListener, new String[]{"Left", "Right", "Rotate"});
3)实现动作的具体细节:
private ActionListener actionListener = new ActionListener() {
public void onAction(String name, boolean keyPressed, float tpf) {
if (name.equals("Pause") && !keyPressed) {
isRunning = !isRunning;
}
}
};
analogListener与actionListener的区别:
1)analogListener:用于连续的动作,记录的是当前触发与上次触发的时间
2)actionListener:用于中断的动作,记录的是当前的状态,如开/关
判断按键释放的时刻:&& !keyPressed
在文档中找触发器类型:src/core/com/jme3/input/KeyInput.java, JoyInput.java
, andMouseInput.java
2. 材质
材质包括:颜色、纹理、光、透明度等。
材质(.j3m)基于材质定义(.j3md)来创建,默认的材质定义有Unshaded.j3md和Lighting.j3md
加载材质的方法有两种:
1)在代码中根据材质定义new一个Material:
Material mat_stl = new Material(assetManager, "Common/Unshaded.j3md");
然后配置它的各个属性:
Texture tex_ml = assetManager.loadTexture("Interface/gray.jpg");
mat_stl.setTexture("ColorMap", tex_ml);
2)创建j3m文件LeakThrough.j3m:
Material Leak Through : Common/MatDefs/Misc/Unshaded.j3md {
MaterialParameters {
Color : 1 0 1 1
ColorMap : Flip Textures/ColoredTex/Monkey.png
}
}
直接加载到Geometry上:
cube.setMaterial((Material) assetManager.loadMaterial( "Materials/LeakThrough.j3m"));
要注意文件的路径。
纹理材质:
Texture tex_ml = assetManager.loadTexture("Interface/gray.jpg");
mat_stl.setTexture("ColorMap", tex_ml);
纹理材质上加透明度:前提是纹理图片必须有alpha层。
mat_tt.getAdditionalRenderState().setBlendMode(BlendMode.Alpha); // activate transparency
光照和不平的表面:如崎岖的道路
1) 先要设置材质定义为Lighting.j3md
Material mat_lit = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
2)设置shininess属性
mat_lit.setFloat("Shininess", 5f); // [1,128]
3)在物体上附加材质,并且在场景中加光源
shiny_rock.setMaterial(mat_lit);
DirectionalLight sun = new DirectionalLight();
sun.setDirection(new Vector3f(1,0,-2).normalizeLocal());
sun.setColor(ColorRGBA.White);
rootNode.addLight(sun);
3. 练习
![](https://img-my.csdn.net/uploads/201209/08/1347090473_9900.jpg)
public class ShineTest extends SimpleApplication {
PointLight pl;
float angle;
@Override
public void simpleUpdate(float tpf) {
angle += tpf;
angle %= FastMath.TWO_PI;
pl.setPosition(new Vector3f(FastMath.cos(angle) * 2f, 0.5f, FastMath.sin(angle) * 2f));
}
public static void main(String[] args) {
java.util.logging.Logger.getLogger("").setLevel(Level.WARNING);
ShineTest app = new ShineTest();
app.setShowSettings(false); //开始时的
app.start();
}
@Override
public void simpleInitApp() {
viewPort.setBackgroundColor(ColorRGBA.Blue);
Geometry box = new Geometry("Light Box", new Box(Vector3f.ZERO, 1f, 1f, 1f));
Material mat = new Material(assetManager, "Common/Lighting.j3md");
mat.setTexture("DiffuseMap", assetManager.loadTexture("Interface/gray.jpg"));
mat.setFloat("Shininess", 5f);
box.setMaterial(mat);
box.rotate(1f, 1f, 1f);
rootNode.attachChild(box);
/** Must add a light to make the lit object visible! */
DirectionalLight sun = new DirectionalLight();
sun.setDirection(new Vector3f(5,0,-2).normalizeLocal());
sun.setColor(ColorRGBA.White);
rootNode.addLight(sun);
pl = new PointLight();
pl.setColor(ColorRGBA.Green);
pl.setRadius(4f);
rootNode.addLight(pl);
}
}
public class ShineTest extends SimpleApplication {
PointLight pl;
float angle;
@Override
public void simpleUpdate(float tpf) {
angle += tpf;
angle %= FastMath.TWO_PI;
pl.setPosition(new Vector3f(FastMath.cos(angle) * 2f, 0.5f, FastMath.sin(angle) * 2f));
}
public static void main(String[] args) {
java.util.logging.Logger.getLogger("").setLevel(Level.WARNING);
ShineTest app = new ShineTest();
app.setShowSettings(false); //开始时的
app.start();
}
@Override
public void simpleInitApp() {
viewPort.setBackgroundColor(ColorRGBA.Blue);
Geometry box = new Geometry("Light Box", new Box(Vector3f.ZERO, 1f, 1f, 1f));
Material mat = new Material(assetManager, "Common/Lighting.j3md");
mat.setTexture("DiffuseMap", assetManager.loadTexture("Interface/gray.jpg"));
mat.setFloat("Shininess", 5f);
box.setMaterial(mat);
box.rotate(1f, 1f, 1f);
rootNode.attachChild(box);
/** Must add a light to make the lit object visible! */
DirectionalLight sun = new DirectionalLight();
sun.setDirection(new Vector3f(5,0,-2).normalizeLocal());
sun.setColor(ColorRGBA.White);
rootNode.addLight(sun);
pl = new PointLight();
pl.setColor(ColorRGBA.Green);
pl.setRadius(4f);
rootNode.addLight(pl);
}
}