Skip to content

Commit

Permalink
Added ModelRenderable.builder().setAsyncLoadEnabled() to enable or di…
Browse files Browse the repository at this point in the history
…sable textures async loading after first rendering
  • Loading branch information
ThomasGorisse committed Apr 30, 2021
1 parent b7f0ba5 commit 085588d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:4.1.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// if (project.hasProperty('mavenCentralRepositoryUsername') && project.hasProperty('mavenCentralRepositoryPassword')) {
//classpath 'com.vanniktech:gradle-maven-publish-plugin:0.14.2'
// }
if (project.hasProperty('mavenCentralRepositoryUsername') && project.hasProperty('mavenCentralRepositoryPassword')) {
classpath 'com.vanniktech:gradle-maven-publish-plugin:0.14.2'
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public abstract class Renderable {
// Data that can be shared between Renderables with makeCopy()
private final IRenderableInternalData renderableData;

protected boolean asyncLoadEnabled;

// Data that is unique per-Renderable.
private final ArrayList<Material> materialBindings = new ArrayList<>();
private final ArrayList<String> materialNames = new ArrayList<>();
Expand Down Expand Up @@ -72,6 +74,7 @@ protected Renderable(Renderable.Builder<? extends Renderable, ? extends Builder<
if (builder.definition != null) {
updateFromDefinition(builder.definition);
}
asyncLoadEnabled = builder.asyncLoadEnabled;
animationFrameRate = builder.animationFrameRate;
}

Expand Down Expand Up @@ -101,6 +104,7 @@ protected Renderable(Renderable other) {
collisionShape = other.collisionShape.makeCopy();
}

asyncLoadEnabled = other.asyncLoadEnabled;
animationFrameRate = other.animationFrameRate;

changeId.update();
Expand Down Expand Up @@ -352,6 +356,7 @@ abstract static class Builder<T extends Renderable, B extends Builder<T, B>> {
private RenderableDefinition definition = null;
private boolean isGltf = false;
private boolean isFilamentAsset = false;
private boolean asyncLoadEnabled = false;
@Nullable
private LoadGltfListener loadGltfListener;
@Nullable
Expand Down Expand Up @@ -413,6 +418,15 @@ public B setIsFilamentGltf(boolean isFilamentGltf) {
return getSelf();
}

/**
* Enable textures async loading after first rendering.
* Default is false.
*/
public B setAsyncLoadEnabled(boolean asyncLoadEnabled) {
this.asyncLoadEnabled = asyncLoadEnabled;
return getSelf();
}

/**
* Sets the number of frames per seconds defined in the asset.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,11 @@ void createFilamentAssetModelInstance() {

FilamentAsset createdAsset = renderableData.isGltfBinary ? loader.createAssetFromBinary(renderableData.gltfByteBuffer)
: loader.createAssetFromJson(renderableData.gltfByteBuffer);
renderableData.resourceLoader.asyncBeginLoad(createdAsset);
if(renderable.asyncLoadEnabled) {
renderableData.resourceLoader.asyncBeginLoad(createdAsset);
} else {
renderableData.resourceLoader.loadResources(createdAsset);
}

if (createdAsset == null) {
throw new IllegalStateException("Failed to load gltf");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public void loadModels() {
ModelRenderable.builder()
.setSource(this, Uri.parse("https://storage.googleapis.com/ar-answers-in-search-models/static/Tiger/model.glb"))
.setIsFilamentGltf(true)
.setAsyncLoadEnabled(true)
.build()
.thenAccept(model -> {
MainActivity activity = weakActivity.get();
Expand Down

0 comments on commit 085588d

Please sign in to comment.