如何在Android Java中展示.gltf行星3D模型并实现旋转
在Android中用Java展示可旋转的glTF行星模型方案
推荐可用库
- Google Filament:Android官方推荐的轻量实时渲染引擎,原生支持glTF/GLB格式,API简洁,性能适配移动端,适合快速实现3D展示需求。
- Sceneform:虽然官方停止维护,但社区仍有分支支持,封装度高,对glTF的加载和渲染做了大量简化,适合快速集成。
基于Filament的代码实现方案
以下是完整的实现步骤,包含太空背景、模型加载和自动旋转效果:
1. 添加依赖
在Module级别的build.gradle中引入Filament相关依赖:
dependencies { implementation 'com.google.android.filament:filament-android:1.32.0' implementation 'com.google.android.filament:gltfio-android:1.32.0' implementation 'com.google.android.filament:utils-android:1.32.0' }
2. 布局文件
使用FrameLayout叠加背景图和Filament视图,让3D模型显示在太空背景上方:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 太空背景图 --> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" android:src="@drawable/space_background"/> <!-- Filament 3D渲染视图 --> <com.google.android.filament.FilamentView android:id="@+id/filamentView" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/transparent"/> </FrameLayout>
3. Java逻辑实现
加载glTF模型并实现自动旋转,关闭Filament默认天空盒以显示背景图:
import android.os.Bundle; import android.view.Choreographer; import androidx.appcompat.app.AppCompatActivity; import com.google.android.filament.Engine; import com.google.android.filament.FilamentView; import com.google.android.filament.gltfio.FilamentAsset; import com.google.android.filament.gltfio.ResourceLoader; import com.google.android.filament.gltfio.StreamAssetLoader; import java.io.IOException; import java.io.InputStream; public class MainActivity extends AppCompatActivity { private Engine engine; private FilamentView filamentView; private ResourceLoader resourceLoader; private FilamentAsset planetModel; private float rotationY = 0f; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); filamentView = findViewById(R.id.filamentView); // 初始化Filament引擎 engine = Engine.create(); filamentView.setEngine(engine); // 初始化资源加载器 resourceLoader = new ResourceLoader(engine); StreamAssetLoader assetLoader = new StreamAssetLoader(getAssets(), resourceLoader); // 加载assets目录下的行星模型(示例:planets.gltf) try (InputStream inputStream = getAssets().open("planets.gltf")) { planetModel = assetLoader.createAsset(inputStream); // 关闭默认天空盒,让背景图显示 planetModel.getScene().setSkybox(null); filamentView.setScene(planetModel.getScene()); // 启动模型旋转动画 Choreographer.getInstance().postFrameCallback(this::updateModelRotation); } catch (IOException e) { e.printStackTrace(); } } private void updateModelRotation(long frameTimeNanos) { rotationY += 0.5f; if (rotationY >= 360f) rotationY = 0f; // 让模型绕Y轴旋转(取模型第一个实体作为旋转根节点) if (planetModel != null && planetModel.getEntityCount() > 0) { float[] transform = new float[16]; android.opengl.Matrix.setRotateM(transform, 0, rotationY, 0, 1, 0); engine.getTransformManager().setTransform( engine.getTransformManager().getInstance(planetModel.getEntities()[0]), transform ); filamentView.render(); } // 循环触发下一帧更新 Choreographer.getInstance().postFrameCallback(this::updateModelRotation); } @Override protected void onDestroy() { super.onDestroy(); // 释放资源,避免内存泄漏 if (planetModel != null) { resourceLoader.destroyAsset(planetModel); } resourceLoader.destroy(); engine.destroy(); } }
注意事项
- 将太空背景图放入
drawable目录,命名为space_background。 - 把glTF模型文件(如
planets.gltf)放入assets目录,若使用GLB格式,直接替换文件名即可。 - 若需要调整视角,可通过Filament的Camera API固定相机位置,无需手动管理相机逻辑。
内容的提问来源于stack exchange,提问作者sidheart
相关产品推荐
相关产品推荐

