ThreeDView in Android import obj , fbx
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
import org.jetbrains.annotations.NotNull;
import three.core.Object3D;
import three.core.Scene;
import three.loaders.FBXLoader;
import three.loaders.OBJLoader;
import three.materials.Material;
import three.materials.MeshBasicMaterial;
import three.objects.Mesh;
import three.renderers.WebGLRenderer;
public class ThreeDView extends GLSurfaceView {
private WebGLRenderer renderer;
private Scene scene;
private Camera camera;
private Mesh mesh;
public ThreeDView(Context context) {
super(context);
init();
}
public ThreeDView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
setEGLContextClientVersion(2);
renderer = new WebGLRenderer();
setRenderer(renderer);
}
// Load a 3D model file in OBJ format
public void loadOBJModel(Context context, int resourceId) {
OBJLoader loader = new OBJLoader();
loader.load(context, resourceId, new OBJLoader.OnLoadCallback() {
@Override
public void onLoad(@NotNull Object3D object3D) {
// The model has loaded successfully. Now we can add it to the scene.
addModelToScene(object3D);
}
@Override
public void onError(Throwable throwable) {
// Handle any errors that occur during loading.
}
});
}
// Load a 3D model file in FBX format
public void loadFBXModel(Context context, int resourceId) {
FBXLoader loader = new FBXLoader();
loader.load(context, resourceId, new FBXLoader.OnLoadCallback() {
@Override
public void onLoad(@NotNull Object3D object3D) {
// The model has loaded successfully. Now we can add it to the scene.
addModelToScene(object3D);
}
@Override
public void onError(Throwable throwable) {
// Handle any errors that occur during loading.
}
});
}
private void addModelToScene(Object3D object3D) {
// Create a new scene if one doesn't exist yet.
if (scene == null) {
scene = new Scene();
}
// Add the loaded object to the scene.
scene.add(object3D);
// Create a camera and position it to view the model.
camera = new Camera();
camera.position.set(0, 0, 5);
// Create a material for the model.
Material material = new MeshBasicMaterial();
// Create a mesh using the loaded model and the material.
mesh = new Mesh(object3D.geometry, material);
scene.add(mesh);
}
@Override
public void onDrawFrame(GL10 gl10) {
super.onDrawFrame(gl10);
if (scene != null && camera != null) {
// Render the scene with the camera.
renderer.render(scene, camera);
}
}
@Override
public void onResume() {
super.onResume();
setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
}
@Override
public void onPause() {
super.onPause();
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}
// This method can be used to set the camera position.
public void setCameraPosition(float x, float y, float z) {
if (camera != null) {
camera.position.set(x, y, z);
}
}
// This method can be used to rotate the model.
public void rotateModel(float x, float y, float z) {
if (mesh != null) {
mesh.rotation.set(x, y, z);
}
}