SceneView / sceneview-android

SceneView is a 3D and AR Android Composable and View with Google Filament and ARCore. This is a Sceneform replacement in Kotlin
Apache License 2.0
756 stars 151 forks source link

Crash when modifying texture #529

Closed shahimclt closed 2 weeks ago

shahimclt commented 2 weeks ago

I am trying to replace the texture of a model with an image using the following code:

fun createTextureFromBitmap(engine: Engine, bitmap: Bitmap): Texture {
        val texture = Texture.Builder()
            .width(bitmap.width)
            .height(bitmap.height)
            .levels(1)
            .sampler(Texture.Sampler.SAMPLER_2D)
            .format(Texture.InternalFormat.SRGB8_A8)
            .build(engine)

        val buffer = ByteBuffer.allocateDirect(bitmap.byteCount)
        bitmap.copyPixelsToBuffer(buffer)
        buffer.flip()
        texture.setImage(
            engine, 0, Texture.PixelBufferDescriptor(
                buffer,
                Texture.Format.RGBA,
                Texture.Type.UBYTE
            )
        )
        buffer.clear()
        return texture
    }
val inputStream = requireContext().resources.openRawResource(R.raw.kicker_texture_blue)
var bitmap = BitmapFactory.decodeStream(inputStream)
val texture = createTextureFromBitmap(engine, bitmap)
objectNode.renderableNodes.find { it.name == target }?.apply {
    Log.d("3DT", "SceneHolder: found $target node")
    Log.d("3DT", "SceneHolder: ${materialInstances}")
    materialInstance.setExternalTexture(texture)
    // This works, but I lose the reflectance and metallic properties of the original texture
    //  setMaterialInstances(materialLoader.createTextureInstance(texture))
}

but I get a crash:

Panic
    in getSamplerInfo:97
    reason: sampler named "texture" not found

What could be the reason for this? setMaterialInstances(materialLoader.createTextureInstance(texture)) works fine.

shahimclt commented 2 weeks ago

solved my just swapping the base color map. See https://github.com/SceneView/sceneview-android/issues/433

import io.github.sceneview.material.setBaseColorMap

materialInstance.setBaseColorMap(texture)