1. 资源下载:https://github.com/xoppa/blog/tree/master/tutorials/assets/loadmodels/data
把资源文件放到android/assets/data下
别忘了将Run Config的working directory指向assets
2. 代码
1 package com.mygdx.game;
2
3 import com.badlogic.gdx.ApplicationAdapter;
4 import com.badlogic.gdx.Gdx;
5 import com.badlogic.gdx.assets.AssetManager;
6 import com.badlogic.gdx.graphics.GL20;
7 import com.badlogic.gdx.graphics.PerspectiveCamera;
8 import com.badlogic.gdx.graphics.g3d.*;
9 import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
10 import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight;
11 import com.badlogic.gdx.graphics.g3d.utils.CameraInputController;
12 import com.badlogic.gdx.utils.Array;
13
14 public class MyGdxGame2 extends ApplicationAdapter {
15 public Environment environment;//可以包含点光源集合和线光源集合
16 public PerspectiveCamera cam;//3D视角
17 public CameraInputController camController;//视角控制器
18
19 public AssetManager assets;
20 public Array
21 public ModelBatch modelBatch;
22
23 public boolean loading;
24
25 @Override
26 public void create () {
27 environment = new Environment();
28 environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f));//环境光
29 environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f));//直线光源
30
31 modelBatch = new ModelBatch();
32 cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());//67可以理解成一个定值,视角宽度(67度)
33 cam.position.set(10f, 10f, 10f);
34 cam.lookAt(0,0,0);
35 cam.near = 1f;
36 cam.far = 300f;
37 cam.update();
38
39 camController = new CameraInputController(cam);
40 Gdx.input.setInputProcessor(camController);
41
42 assets = new AssetManager();
43 assets.load("data/ship.obj", Model.class);
44 loading = true;
45
46 }
47
48 private void doneLoading() {
49 Model ship = assets.get("data/ship.obj", Model.class);
50 ModelInstance shipInstance = new ModelInstance(ship);
51 instances.add(shipInstance);
52 loading = false;
53 }
54
55 @Override
56 public void render () {
57 if (loading && assets.update()){
58 doneLoading();
59 }
60
61 camController.update();
62 Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
63 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
64
65 modelBatch.begin(cam);
66 modelBatch.render(instances,environment);
67 modelBatch.end();
68 }
69
70 @Override
71 public void dispose() {
72 modelBatch.dispose();
73 instances.clear();
74 assets.dispose();
75 super.dispose();
76 }
77 }
3. 截图
4. 多个实例
1 private void doneLoading() {
2 Model ship = assets.get("data/ship.obj", Model.class);
3 for (float x &#61; -5f; x <&#61; 5f; x &#43;&#61; 2f) {
4 for (float z &#61; -5f; z <&#61; 5f; z &#43;&#61; 2f) {
5 ModelInstance shipInstance &#61; new ModelInstance(ship);
6 shipInstance.transform.setToTranslation(x, 0, z);//设置位置
7 instances.add(shipInstance);
8 }
9 }
10 loading &#61; false;
11 }
5. obj文件对于目前测试用是没有问题的&#xff0c;但是实际应用中&#xff0c;模型要复杂得多&#xff0c;obj貌似支持不了。
libgdx支持两种文件格式&#xff0c;g3db(二进制)和g3dj(json)。
可以使用 fbx-conv工具进行格式转换&#xff0c;支持的格式FBX/Collada/Obj。FBX是大多数3d建模软件都支持的。
工具主页&#xff1a;https://github.com/libgdx/fbx-conv