Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

How to change texture of 3d model at runtime #433

Closed
Somendra121 opened this issue Mar 6, 2024 · 1 comment
Closed

How to change texture of 3d model at runtime #433

Somendra121 opened this issue Mar 6, 2024 · 1 comment

Comments

@Somendra121
Copy link

Somendra121 commented Mar 6, 2024

Please let us know how to change texture of 3d model at runtime, in sceneform we use this code to change texture at runtime :

` public void LoadTexture(Context context, Bitmap colorImageBitmap) {
Log.d("LoadTexture", "LoadTexture : @@@@@@@1 renderableInstance.size() : " + renderableInstances.size());
for (RenderableInstance renderableInstance : renderableInstances) {
Texture.builder()
.setSource(colorImageBitmap)
.setSampler(
Texture.Sampler.builder()
.setWrapMode(Texture.Sampler.WrapMode.REPEAT)
.build()
)
.build()
.thenAccept(texture -> {
MaterialFactory.makeOpaqueWithTexture(context, texture)
.thenAccept(material -> {
material.setFloat("roughness", 0.5f); // Set roughness
material.setFloat("ao", 1.0f); // Set AO
Log.d("LoadTexture", "LoadTexture :@@@@@@2 renderableInstance.getMaterialsCount() : " + renderableInstance.getMaterialsCount());

                                renderableInstance.setMaterial(0, material);
                            });
                });
        Log.d("LoadTexture", "LoadTexture @@@@@@@3 : renderableInstance.getEntity() : " + renderableInstance.getEntity());
    }
}`
@EvergreenTree97
Copy link

EvergreenTree97 commented Apr 9, 2024

I solved it as follows

private 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
}

texture = createTextureFromBitmap(engine, bitmap)
(modelNodes[0] as ModelNode).materialInstances[0][0] = setBaseColorMap(texture)

@SceneView SceneView locked and limited conversation to collaborators May 27, 2024
@ThomasGorisse ThomasGorisse converted this issue into discussion #510 May 27, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants