3维空间曲线
JME3提供了Curve类,用于生成曲线网格。利用它可以很容易绘制3维空间曲线。
package com.ruanko.jme3;
import java.util.Random;
import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.debug.Arrow;
import com.jme3.scene.debug.Grid;
import com.jme3.scene.shape.Curve;
import com.jme3.system.AppSettings;
/**
* 3维空间曲线
* @author yanmaoyuan@ruanko.com
*/
public class ThreeDCurve extends SimpleApplication {
private Geometry curveGeom;// 曲线A
private Vector3f camLocVctr = new Vector3f(100.0f, 32.0f, 0.0f);
//
public static void main(String[] args) {
ThreeDCurve app = new ThreeDCurve();
// 初始化应用程序
AppSettings settings = new AppSettings(true);
settings.setHeight(768);
settings.setWidth(1024);
app.setSettings(settings);
app.setShowSettings(false);
app.start();
}
@Override
public void simpleInitApp() {
this.setDisplayFps(false);
this.setDisplayStatView(false);
// 背景色
viewPort.setBackgroundColor(ColorRGBA.White);
// 摄像机初始位置
cam.setLocation(camLocVctr);
cam.lookAt(Vector3f.ZERO, cam.getUp());
flyCam.setMoveSpeed(100.0f);
// 显示3维坐标系
showNodeAxes(250);
// 生成坐标系网格
Geometry g = new Geometry("wireFrameDebugGrid1", new Grid(100, 100, 5.0f));// 5WU
Material m = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
m.getAdditionalRenderState().setWireframe(true);
m.setColor("Color", ColorRGBA.LightGray);
g.setMaterial(m);
g.center().move(Vector3f.ZERO);// 中心坐标
rootNode.attachChild(g);
// 生成一个有100个顶点的曲线
Vector3f[] points = new Vector3f[100];
for (float i = 0; i < 100; i++) {
// 这里利用三角函数生成顶点。
// 随便写的,可以改掉。
points[(int) i] = new Vector3f(i, i + 10
* FastMath.sin(i / 10 * FastMath.PI), i + 10
* FastMath.sin(i / 25 * FastMath.PI));
}
Curve curve = new Curve(points, 1);// 曲线A
curveGeom = new Geometry("曲线A", curve);
// 设置材质
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Red);
curveGeom.setMaterial(mat);
rootNode.attachChild(curveGeom);
}
@Override
public void simpleUpdate(float tpf) {
/////// 下面的代码会重新生成曲线A的所有顶点 ///////////
Random rand = new Random();
Vector3f[] points = new Vector3f[100];
float base = rand.nextFloat() * 10;
for (int i = 0; i < 100; i++) {
// 这里利用三角函数生成顶点。
// 随便写的,可以改掉。
float x = i + base;
float y = i + rand.nextFloat() * 10
* FastMath.sin(i / 10 * FastMath.PI);
float z = i + rand.nextFloat() * 10 + rand.nextFloat() * 10
* FastMath.sin(i / 25 * FastMath.PI);
points[i] = new Vector3f(x, y, z);
}
///////////////////////////////////
// 刷新曲线A
Curve curve = new Curve(points, 1);// 曲线A
curveGeom.setMesh(curve);
curveGeom.updateModelBound();
}
public void showNodeAxes(float axisLen) {
//
Vector3f v = new Vector3f(axisLen, 0, 0);
Arrow a = new Arrow(v);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Red);
Geometry geom = new Geometry(rootNode.getName() + "XAxis", a);
geom.setMaterial(mat);
rootNode.attachChild(geom);
//
v = new Vector3f(0, axisLen, 0);
a = new Arrow(v);
mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Green);
geom = new Geometry(rootNode.getName() + "YAxis", a);
geom.setMaterial(mat);
rootNode.attachChild(geom);
//
v = new Vector3f(0, 0, axisLen);
a = new Arrow(v);
mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Blue);
geom = new Geometry(rootNode.getName() + "ZAxis", a);
geom.setMaterial(mat);
rootNode.attachChild(geom);
}
}
效果
