Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include the desktop fix, for incorrect model appearance, in android (#6) #12

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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