Skip to content
This repository has been archived by the owner on Jul 28, 2020. It is now read-only.

Commit

Permalink
Fix glTF loading in with Filament gltfio in Sceneform 1.16 (#1029)
Browse files Browse the repository at this point in the history
* Fix support for standard glTF which was broken (only glb worked).
* Use correct Resource Loader for Filament gltf loads.
  • Loading branch information
tpsiaki authored Mar 27, 2020
1 parent d591694 commit 4d7b660
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,20 @@ public CompletableFuture<T> downloadAndProcessRenderable(
// Download byte buffer via thread pool
() -> {
try {
byte[] gltfByteBuffer =
SceneformBufferUtils.inputStreamCallableToByteArray(inputStreamCreator);
return ByteBuffer.wrap(gltfByteBuffer);
return SceneformBufferUtils.inputStreamCallableToByteArray(inputStreamCreator);
} catch (Exception e) {
throw new CompletionException(e);
}
},
ThreadPools.getThreadPoolExecutor())
.thenApplyAsync(
gltfByteBuffer -> {
this.renderableData.gltfByteBuffer = gltfByteBuffer;
// Check for glb header
this.renderableData.isGltfBinary = gltfByteBuffer[0] == 0x67
&& gltfByteBuffer[1] == 0x6C
&& gltfByteBuffer[2] == 0x54
&& gltfByteBuffer[3] == 0x46;
this.renderableData.gltfByteBuffer = ByteBuffer.wrap(gltfByteBuffer);
return renderable;
},
ThreadPools.getMainExecutor());
Expand Down Expand Up @@ -92,19 +95,7 @@ static Uri getUriFromMissingResource(
// Build uri to missing resource.
String decodedMissingResPath = Preconditions.checkNotNull(decodedMissingResUri.getPath());
Uri decodedParentUri = Uri.parse(Uri.decode(parentUri.toString()));
String scheme = Preconditions.checkNotNull(decodedParentUri.getScheme());
String authority = Preconditions.checkNotNull(decodedParentUri.getAuthority());
String path = Preconditions.checkNotNull(decodedParentUri.getPath());
// Remove root file
path = path.replace(Preconditions.checkNotNull(decodedParentUri.getLastPathSegment()), "");

Uri uri =
new Uri.Builder()
.scheme(scheme)
.authority(authority)
.path(path)
.appendPath(decodedMissingResPath)
.build();
Uri uri = decodedParentUri.buildUpon().appendPath("..").appendPath(decodedMissingResPath).build();
// Normalize and return Uri.
return Uri.parse(Uri.decode(URI.create(uri.toString()).normalize().toString()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

import java.nio.IntBuffer;
import java.util.concurrent.Callable;
import java.util.function.Function;

Expand Down Expand Up @@ -122,7 +123,8 @@ void createFilamentAssetModelInstance() {
RenderableInternalFilamentAssetData.getMaterialProvider(),
EntityManager.get());

FilamentAsset createdAsset = loader.createAssetFromBinary(renderableData.gltfByteBuffer);
FilamentAsset createdAsset = renderableData.isGltfBinary ? loader.createAssetFromBinary(renderableData.gltfByteBuffer)
: loader.createAssetFromJson(renderableData.gltfByteBuffer);

if (createdAsset == null) {
throw new IllegalStateException("Failed to load gltf");
Expand All @@ -138,8 +140,6 @@ void createFilamentAssetModelInstance() {
new Vector3(center[0], center[1], center[2]));
}

ResourceLoader resourceLoader = new ResourceLoader(engine);
Preconditions.checkState(createdAsset.getResourceUris().length == 0);
Function<String, Uri> urlResolver = renderableData.urlResolver;
for (String uri : createdAsset.getResourceUris()) {
if (urlResolver == null) {
Expand All @@ -149,7 +149,7 @@ void createFilamentAssetModelInstance() {
Uri dataUri = urlResolver.apply(uri);
try {
Callable<InputStream> callable = LoadHelper.fromUri(renderableData.context, dataUri);
resourceLoader.addResourceData(
renderableData.resourceLoader.addResourceData(
uri, ByteBuffer.wrap(SceneformBufferUtils.inputStreamCallableToByteArray(callable)));
} catch (Exception e) {
Log.e(TAG, "Failed to download data uri " + dataUri, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class RenderableInternalFilamentAssetData implements IRenderableInternalD

Context context;
Buffer gltfByteBuffer;
boolean isGltfBinary;
ResourceLoader resourceLoader;
@Nullable Function<String, Uri> urlResolver;
static MaterialProvider materialProvider;
Expand Down

0 comments on commit 4d7b660

Please sign in to comment.