Skip to content

Commit

Permalink
Include the desktop fix, for incorrect model appearance, in android (#6
Browse files Browse the repository at this point in the history
…) (#12)
  • Loading branch information
mbalajee authored Jul 15, 2024
1 parent 7c74457 commit 4257269
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
42 changes: 42 additions & 0 deletions android/gltfio-android/src/main/cpp/AssetLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>AssetLoader</code>
*
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion libs/gltfio/src/AssetLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 4257269

Please sign in to comment.