1. 程式人生 > >純Java遊戲引擎JME3入門

純Java遊戲引擎JME3入門

學安全方面久了,有點無聊,今天閒來無事

查了下除了Unity以外還有什麼好用的遊戲引擎,我尤其關注了Java寫的遊戲引擎

果然有,jMonkeyEngine,於是打算看看

 

JME簡介:

jMonkeyEngine是一款適合冒險的Java開發人員的3D遊戲引擎。 它是開源的,跨平臺的,前沿的。 3.2.0是jMonkeyEngine 3 SDK的最新穩定版本,是完整的遊戲開發套件。 該引擎由幾個商業遊戲工作室和電腦科學課程使用。 該引擎採用純Java編寫。   下面,開始基本的學習: 下載JME:從Github上面選擇對應作業系統下載即可   第一個Demo: 顯示一個小正方體
package
mygame; import com.jme3.app.SimpleApplication; import com.jme3.light.DirectionalLight; import com.jme3.scene.Mesh; import com.jme3.scene.shape.Box; import com.jme3.material.Material; import com.jme3.math.Vector3f; import com.jme3.scene.Geometry; public class Main extends SimpleApplication {
public static void main(String[] args) { Main app = new Main(); app.start(); } @Override public void simpleInitApp() { Mesh box=new Box(1,1,1); Material mat=new Material(assetManager,"Common/MatDefs/Light/Lighting.j3md"); Geometry geom = new Geometry("Box"); geom.setMesh(box); geom.setMaterial(mat); DirectionalLight sun
=new DirectionalLight(); sun.setDirection(new Vector3f(-1,-2,-3)); rootNode.attachChild(geom); rootNode.addLight(sun); } }

 

 父類SimpleApplication自帶一個第一人稱控制器,WASD直接操作:     或者在init中直接初始化相機座標和旋轉引數:
        cam.setLocation(new Vector3f(4.4114223f, 3.3620508f, 7.5415998f));
        cam.setRotation(new Quaternion(-0.046265673f, 0.9518722f, -0.1815604f, -0.2425582f));

 

可以提前設定好各種引數,直接啟動遊戲,跳過開始介面:
    public static void main(String[] args) {
        AppSettings settings = new AppSettings(true);
        settings.setTitle("一個正方體");
        settings.setResolution(1280, 1024);

        Main app = new Main();
        app.setSettings(settings);
        app.setShowSettings(false);
        app.start();
    }

 

  第二個Demo: 讓這個正方體自己旋轉
package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.light.DirectionalLight;
import com.jme3.scene.Mesh;
import com.jme3.scene.shape.Box;
import com.jme3.material.Material;
import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.system.AppSettings;

public class Main extends SimpleApplication {

    private Geometry geom;

    public static void main(String[] args) {
        AppSettings settings = new AppSettings(true);
        settings.setTitle("一個正方體");
        settings.setResolution(1280, 1024);

        Main app = new Main();
        app.setSettings(settings);
        app.setShowSettings(false);
        app.start();
    }

    @Override
    public void simpleInitApp() {
        Mesh box = new Box(1, 1, 1);
        Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
        geom = new Geometry("Box");
        geom.setMesh(box);
        geom.setMaterial(mat);

        DirectionalLight sun = new DirectionalLight();
        sun.setDirection(new Vector3f(-1, -2, -3));

        rootNode.attachChild(geom);
        rootNode.addLight(sun);

        cam.setLocation(new Vector3f(4.4114223f, 3.3620508f, 7.5415998f));
        cam.setRotation(new Quaternion(-0.046265673f, 0.9518722f, -0.1815604f, -0.2425582f));
    }

    @Override
    public void simpleUpdate(float deltaTime) {
        float speed = FastMath.TWO_PI;
        geom.rotate(0, deltaTime * speed, 0);
    }
}

 

 只是方塊沒有意思,我想辦法搞到了諾手的模型,下載好匯入:  匯入後的結果是這樣,不是很滿意,因為沒有貼圖:
package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial;
import com.jme3.system.AppSettings;

public class Main extends SimpleApplication {

    private Geometry geom;

    public static void main(String[] args) {
        AppSettings settings = new AppSettings(true);
        settings.setTitle("諾手");
        settings.setResolution(1280, 1024);

        Main app = new Main();
        app.setSettings(settings);
        app.setShowSettings(false);
        app.start();
    }

    @Override
    public void simpleInitApp() {
        cam.setLocation(new Vector3f(0.41600543f, 3.2057908f, 6.6927643f));
        cam.setRotation(new Quaternion(-0.00414816f, 0.9817784f, -0.18875499f, -0.021575727f));
        flyCam.setMoveSpeed(10);
        viewPort.setBackgroundColor(ColorRGBA.LightGray);
        Spatial model = assetManager.loadModel("Models/demo/Darius.obj");
        model.scale(0.03f);
        model.center();

        //定向光
        DirectionalLight sun = new DirectionalLight();
        sun.setDirection(new Vector3f(-1, -2, -3));

        //環境光
        AmbientLight ambient = new AmbientLight();
        //調整光照亮度
        ColorRGBA lightColor = new ColorRGBA();
        sun.setColor(lightColor.mult(0.6f));
        ambient.setColor(lightColor.mult(0.4f));

        rootNode.attachChild(model);
        rootNode.addLight(sun);
        rootNode.addLight(ambient);
    }
}

 

只有模型,沒有貼圖,可能是我沒有匯入吧。。。

 

 

無所謂了,我不是專業搞遊戲的,今天只是想到了,隨便看看

 

繼續我的Kali學習之路。