diff --git a/android/gltfio-android/src/main/cpp/AssetLoader.cpp b/android/gltfio-android/src/main/cpp/AssetLoader.cpp index 4d97837461b..77231418d18 100644 --- a/android/gltfio-android/src/main/cpp/AssetLoader.cpp +++ b/android/gltfio-android/src/main/cpp/AssetLoader.cpp @@ -263,6 +263,48 @@ Java_com_google_android_filament_gltfio_AssetLoader_nCreateAsset(JNIEnv* env, jc buffer.getSize()); } + +extern "C" JNIEXPORT jlong JNICALL +Java_com_google_android_filament_gltfio_AssetLoader_nCreateAssetLoaderExtended(JNIEnv* env, jclass, + jlong nativeEngine, jobject provider, jlong nativeEntities, jstring filePath) { + Engine* engine = (Engine*) nativeEngine; + MaterialProvider* materialProvider = nullptr; + + // First check for a fast path that passes a native MaterialProvider into the loader. + // This drastically reduces the number of JNI calls while the asset is being loaded. + jclass klass = env->GetObjectClass(provider); + jmethodID getNativeObject = env->GetMethodID(klass, "getNativeObject", "()J"); + if (getNativeObject) { + materialProvider = (MaterialProvider*) env->CallLongMethod(provider, getNativeObject); + } else { + env->ExceptionClear(); + } + + if (materialProvider == nullptr) { + materialProvider = new JavaMaterialProvider(env, provider); + } + + EntityManager* entities = (EntityManager*) nativeEntities; + NameComponentManager* names = new NameComponentManager(*entities); + + const char *nativeFilePath = env->GetStringUTFChars(filePath, nullptr); + + AssetConfigurationExtended ext = { + .gltfPath = nativeFilePath + }; + + AssetConfiguration config = { + .engine = engine, + .materials = materialProvider, + .names = names, + .ext = &ext, + }; + + env->ReleaseStringUTFChars(filePath, nativeFilePath); + + return (jlong) AssetLoader::create(config); +} + extern "C" JNIEXPORT jlong JNICALL Java_com_google_android_filament_gltfio_AssetLoader_nCreateInstancedAsset(JNIEnv* env, jclass, jlong nativeLoader, jobject javaBuffer, jint remaining, jlongArray instances) { diff --git a/android/gltfio-android/src/main/java/com/google/android/filament/gltfio/AssetLoader.java b/android/gltfio-android/src/main/java/com/google/android/filament/gltfio/AssetLoader.java index 60da99720ad..5882e8b6b2e 100644 --- a/android/gltfio-android/src/main/java/com/google/android/filament/gltfio/AssetLoader.java +++ b/android/gltfio-android/src/main/java/com/google/android/filament/gltfio/AssetLoader.java @@ -103,6 +103,25 @@ public AssetLoader(@NonNull Engine engine, @NonNull MaterialProvider provider, mMaterialCache = provider; } + public AssetLoader(@NonNull Engine engine, @NonNull MaterialProvider provider, + @NonNull EntityManager entities, String filePath) { + + long nativeEngine = engine.getNativeObject(); + long nativeEntities = entities.getNativeObject(); + if (filePath == null) { + mNativeObject = nCreateAssetLoader(nativeEngine, provider, nativeEntities); + } else { + mNativeObject = nCreateAssetLoaderExtended(nativeEngine, provider, nativeEntities, filePath); + } + + if (mNativeObject == 0) { + throw new IllegalStateException("Unable to parse glTF asset."); + } + + mEngine = engine; + mMaterialCache = provider; + } + /** * Frees all memory consumed by the native AssetLoader * @@ -190,6 +209,8 @@ public void destroyAsset(@NonNull FilamentAsset asset) { private static native long nCreateAssetLoader(long nativeEngine, Object provider, long nativeEntities); + private static native long nCreateAssetLoaderExtended(long nativeEngine, Object provider, + long nativeEntities, String filePath); private static native void nDestroyAssetLoader(long nativeLoader); private static native long nCreateAsset(long nativeLoader, Buffer buffer, int remaining); private static native long nCreateInstancedAsset(long nativeLoader, Buffer buffer, int remaining, diff --git a/libs/gltfio/src/AssetLoader.cpp b/libs/gltfio/src/AssetLoader.cpp index 8c179c4b450..eafb0c9095c 100644 --- a/libs/gltfio/src/AssetLoader.cpp +++ b/libs/gltfio/src/AssetLoader.cpp @@ -1690,8 +1690,10 @@ void FAssetLoader::importSkins(FFilamentInstance* instance, const cgltf_data* gl } } +// Including support for Android +// See https://github.com/google/filament/discussions/7851#discussioncomment-9453369 bool AssetConfigurationExtended::isSupported() { -#if defined(__ANDROID__) || defined(IOS) || defined(__EMSCRIPTEN__) +#if defined(IOS) || defined(__EMSCRIPTEN__) return false; #else return true;