Skip to content

Commit

Permalink
Avoid using previously bound texture if view texture isn't ready (#317)
Browse files Browse the repository at this point in the history
* Revert "Initialize ViewRenderable with transparent texture and set it in material right away (#273)"

This reverts commit 6a15adc

* Avoid using previously bound texture if view texture isn't ready
  • Loading branch information
grassydragon authored Feb 19, 2022
1 parent b2ac2d9 commit 0b07343
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
12 changes: 10 additions & 2 deletions core/assets-sources/materials/sceneform_view_material.mat
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ material {
"type" : "samplerExternal",
"name" : "viewTexture"
},
{
"type" : "bool",
"name" : "viewTextureReady"
},
{
"type" : "float2",
"name" : "offsetUv"
Expand Down Expand Up @@ -37,7 +41,11 @@ fragment {
uv.x = uv.x + materialParams.offsetUv.x * (1.0 - 2.0 * uv.x);
uv.y = uv.y + materialParams.offsetUv.y * (1.0 - 2.0 * uv.y);

material.baseColor = texture(materialParams_viewTexture, uv);
material.baseColor.rgb = inverseTonemapSRGB(material.baseColor.rgb);
if (materialParams.viewTextureReady) {
material.baseColor = texture(materialParams_viewTexture, uv);
material.baseColor.rgb = inverseTonemapSRGB(material.baseColor.rgb);
} else {
material.baseColor = vec4(0.0);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,10 @@ public interface OnViewSizeChangedListener {

externalTexture = new ExternalTexture();

initializeWithTransparentTexture();

this.view = view;
addView(view);
}

private void initializeWithTransparentTexture() {
externalTexture.getSurfaceTexture().setDefaultBufferSize(1, 1);

// Sanity that the surface is valid.
Surface targetSurface = externalTexture.getSurface();
if (!targetSurface.isValid()) {
return;
}

Canvas surfaceCanvas = targetSurface.lockCanvas(null);
surfaceCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
targetSurface.unlockCanvasAndPost(surfaceCanvas);
}

/**
* Register a callback to be invoked when the size of the view changes.
*
Expand All @@ -97,6 +81,10 @@ ExternalTexture getExternalTexture() {
return externalTexture;
}

public boolean isViewTextureReady() {
return externalTexture.getFilamentStream().getTimestamp() > 0;
}

boolean hasDrawnToSurfaceTexture() {
return hasDrawnToSurfaceTexture;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,15 +233,24 @@ void prepareForDraw() {
ViewRenderableInternalData data = Preconditions.checkNotNull(viewRenderableData);
RenderViewToExternalTexture renderViewToExternalTexture = data.getRenderView();

getMaterial().setExternalTexture("viewTexture", renderViewToExternalTexture.getExternalTexture());
getMaterial().setBoolean("viewTextureReady", renderViewToExternalTexture.isViewTextureReady());

if (!renderViewToExternalTexture.isAttachedToWindow()
|| !renderViewToExternalTexture.isLaidOut()) {
// Wait for the view to finish attachment.
return;
}

// Wait until one frame after the surface texture has been drawn to for the first time.
// Fixes an issue where the ViewRenderable would render black for a frame before displaying.
boolean hasDrawnToSurfaceTexture = renderViewToExternalTexture.hasDrawnToSurfaceTexture();
if (!hasDrawnToSurfaceTexture) {
return;
}

if (!isInitialized) {
getMaterial()
.setExternalTexture("viewTexture", renderViewToExternalTexture.getExternalTexture());
updateSuggestedCollisionShape();

isInitialized = true;
Expand Down
Binary file modified core/src/main/res/raw/sceneform_view_material.filamat
Binary file not shown.

0 comments on commit 0b07343

Please sign in to comment.