1

Hi I am trying to load a glb file in my android app for 3d view. I found about android filament and tried to implement it but I am getting Couldn't create Engine error while using Engine.create().

java.lang.RuntimeException: Unable to start activity ComponentInfo{ConfigurationActivity}: java.lang.IllegalStateException: Couldn't create Engine

This is my code:

public class ConfigurationActivity extends AppCompatActivity  implements Choreographer.FrameCallback {
private SurfaceView surfaceView;
private Choreographer choreographer;
private ModelViewer modelViewer;
private Engine engine;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_configuration);
    //Filament.init();
    Utils.INSTANCE.init();
    initialiseComponents();
    initialise();
}

private void initialiseComponents() {
    surfaceView = findViewById(R.id.configuration_sv_main);
}

private void initialise() {
    choreographer = Choreographer.getInstance();
    engine = Engine.create();
    modelViewer= new ModelViewer(surfaceView, engine, null);
    surfaceView.setOnTouchListener(modelViewer);

    choreographer.postFrameCallback(this);

    loadModel();
}

private void loadModel() {
    try {
        InputStream buffer = getAssets().open("test_final.glb");
        byte[] bytes = new byte[buffer.available()];
        buffer.read(bytes);
        ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
        modelViewer.loadModelGlb(byteBuffer);
        modelViewer.transformToUnitCube(new Float3(0,0,0));
    } catch (IOException e) {
        e.printStackTrace();
    }
}


@Override
public void doFrame(long l) {
    choreographer.postFrameCallback(this);
    modelViewer.render(l);
}

@Override
protected void onResume() {
    super.onResume();
    choreographer.postFrameCallback(this);
}

@Override
protected void onPause() {
    super.onPause();
    choreographer.removeFrameCallback(this);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    choreographer.removeFrameCallback(this);
    engine.destroy();
}
}

And my xml is:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ConfigurationActivity">

<SurfaceView
    android:id="@+id/configuration_sv_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</RelativeLayout>

Below is the dependencies I used:

implementation 'com.google.android.filament:filament-android:1.15.1'
implementation 'com.google.android.filament:filament-utils-android:1.15.1'
implementation 'com.google.android.filament:gltfio-android:1.15.1'

And android studio version is 4.2.1. Any help is appreciated. Thank you.

Ujjwal Jung Thapa
  • 604
  • 2
  • 8
  • 31

1 Answers1

0

This is kotlin:

fun loadEnviromentt(context: Context, ibl2: String) {
    // Create the sky box and add it to the scene.
    val buffer = readAsset(context, ibl2)
    KTXLoader.createSkybox(modelViewer.engine, buffer).apply {
        modelViewer.scene.skybox = this
    }
}
Hadi Note
  • 1,386
  • 17
  • 16