Libgdx 导入blender 3D动画
创始人
2024-05-28 20:04:54

导入blender3D模型可以看上篇libgdx导入blender模型

本篇3D动画在上篇的基础上。

具体参考官网 3D 动画和蒙皮
blender动画参考 八个案例教程带你从0到1入门blender【已完结】

打开上次的blender的,选则物体属性
在这里插入图片描述
点击位置和旋转x,y,z后面的小点,确定初始x,y,z位置,和初始角度
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
更改数值,并点击锁后面的方块
在这里插入图片描述
点击播放,查看动画
在这里插入图片描述
导出fbx,与上篇导出模型一致。导出时需要选择自定义属性
在这里插入图片描述
在这里插入图片描述

转换为g3dj,这里看不明白的去看我上篇,不会blender的建模的去看视频教程
上篇libgdx导入blender模型
blender动画参考 八个案例教程带你从0到1入门blender【已完结】
在这里插入图片描述

项目中使用,将导出的cube.g3dj放入项目assets/data下

package top.wuliaodebaozi2.blockgame.blockgameinternal;import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.graphics.g3d.Environment;
import com.badlogic.gdx.graphics.g3d.Model;
import com.badlogic.gdx.graphics.g3d.ModelBatch;
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight;
import com.badlogic.gdx.graphics.g3d.Material;
import com.badlogic.gdx.graphics.g3d.loader.G3dModelLoader;
import com.badlogic.gdx.graphics.g3d.utils.AnimationController;
import com.badlogic.gdx.graphics.g3d.utils.CameraInputController;
import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
import com.badlogic.gdx.utils.JsonReader;/*** Example program that imports "myModel.g3dj" from the assets folder and renders it onto the screen.*/
public class ImportG3DJ implements ApplicationListener {private Environment environment;private PerspectiveCamera camera;private CameraInputController cameraController;private ModelBatch modelBatch;private Model model;private ModelInstance instance;private AnimationController controller;@Overridepublic void create() {// Create an environment so we have some lightingenvironment = new Environment();environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f));environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f));modelBatch = new ModelBatch();// Create a perspective camera with some sensible defaultscamera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());camera.position.set(10f, 10f, 10f);camera.lookAt(0, 0, 0);camera.near = 1f;camera.far = 300f;camera.update();// Import and instantiate our model (called "myModel.g3dj")ModelBuilder modelBuilder = new ModelBuilder();model = new G3dModelLoader(new JsonReader()).loadModel(Gdx.files.internal("data/cube.g3dj"));instance = new ModelInstance(model);controller = new AnimationController(instance);controller.setAnimation("Cube|CubeAction",-1);cameraController = new CameraInputController(camera);Gdx.input.setInputProcessor(cameraController);}@Overridepublic void render() {cameraController.update();controller.update(Gdx.graphics.getDeltaTime());// Clear the stuff that is left over from the previous render cycleGdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);// Let our ModelBatch take care of efficient rendering of our ModelInstancemodelBatch.begin(camera);modelBatch.render(instance, environment);modelBatch.end();}@Overridepublic void dispose() {modelBatch.dispose();model.dispose();}@Overridepublic void resize(int width, int height) { }@Overridepublic void pause() { }@Overridepublic void resume() { }
}

这里主要与上篇导入模型的代码区别在于,增加了

ModelInstance instance;
AnimationController controller;
public void create() {...instance = new ModelInstance(model);controller = new AnimationController(instance);controller.setAnimation("Cube|CubeAction",-1);
}
public void render() {...controller.update(Gdx.graphics.getDeltaTime());...
}

代码中controller.setAnimation(“Cube|CubeAction”,-1); 动画的名字
用文本方式打开导出的cube.g3dj 可以看到"animations"数据块,看到id,就是动画的名字
在这里插入图片描述

效果:
https://live.csdn.net/v/280148
请添加图片描述

相关内容

热门资讯

天越冷,越爱吃。后悔没早点买.... 稍微上档次一点的饭店里,有一个食材总会出现,那就是三文鱼。不管是日料店的三文鱼刺身,还是西餐店的香煎...
“私人影院有陪侍服务”,三亚、... (来源:上观新闻)据江苏省广播电视总台荔枝新闻消息,近日,记者在西安、成都、三亚、大理等地探访发现,...
泽连斯基满意 (来源:上观新闻)据央视新闻消息,当地时间12月21日,美国总统特使威特科夫表示,过去三天,乌克兰代...
申科股份聘任41岁张晓非为副总... 12月22日,申科股份公告,聘任张晓非先生为公司副总经理。资料显示,张晓非,男,汉族,1984年6月...
机构岁末布局路线图浮现 202... 转自:经济参考报  临近岁末,机构调研火力全开,调研图谱清晰勾勒出资本对实体经济转型升级的高度关注。...