Skip to content

Fix depth occlusion functionality #611

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import io.github.sceneview.loaders.MaterialLoader
import io.github.sceneview.managers.safeDestroy
import io.github.sceneview.material.setExternalTexture
import io.github.sceneview.material.setParameter
import io.github.sceneview.material.setTexture
import io.github.sceneview.math.Transform
import io.github.sceneview.safeDestroyTexture
import io.github.sceneview.safeDestroyVertexBuffer
import io.github.sceneview.texture.TextureSampler2D
import io.github.sceneview.utils.OpenGL
import io.github.sceneview.utils.clone
import java.nio.ByteBuffer
Expand Down Expand Up @@ -86,9 +86,17 @@ open class ARCameraStream(
/**
* Extracted texture from the session depth image
*/
val depthTexture =
Texture.Builder().sampler(Texture.Sampler.SAMPLER_2D).format(Texture.InternalFormat.RG8)
.levels(1).build(engine)
var depthTexture: Texture? = null
set(value) {
if (field != value) {
field = value
value?.let {
depthOcclusionMaterial.setDefaultParameter(
kDepthTextureParameter, value, TextureSampler2D()
)
}
}
}

/**
* ### Flat camera material
Expand All @@ -107,7 +115,6 @@ open class ARCameraStream(
defaultInstance.apply {
setParameter(kUVTransformParameter, Transform())
setExternalTexture(kCameraTextureParameter, cameraTexture)
setTexture(kDepthTextureParameter, depthTexture)
}
}

Expand Down Expand Up @@ -238,6 +245,19 @@ open class ARCameraStream(
else -> null
}?.let { depthImage ->
// Recalculate Occlusion
val depthTexture = depthTexture?.takeIf {
it.getWidth(0) == depthImage.width && it.getHeight(0) == depthImage.height
} ?: Texture.Builder()
.width(depthImage.width)
.height(depthImage.height)
.sampler(Texture.Sampler.SAMPLER_2D)
.format(Texture.InternalFormat.RG8)
.levels(1)
.build(engine).also {
depthTexture?.let(engine::safeDestroyTexture)
depthTexture = it
}

// To solve a problem with a to early released DepthImage the ByteBuffer which holds
// all necessary data is cloned. The cloned ByteBuffer is unaffected of a released
// DepthImage and therefore produces not a flickering result.
Expand All @@ -261,7 +281,7 @@ open class ARCameraStream(
engine.safeDestroyVertexBuffer(vertexBuffer)
renderableManager.safeDestroy(entity)
cameraTextures.values.forEach { engine.safeDestroyTexture(it) }
engine.safeDestroyTexture(depthTexture)
depthTexture?.let(engine::safeDestroyTexture)
uvCoordinates.clear()
transformedUvCoordinates?.clear()
Log.d("Sceneview", "CameraStream destroyed")
Expand Down