Filament 3D模型动态替换纹理(图片)

场景:
加载一个基础模型,正面动态替换图片。

1. 加载固定模型:


    fun loadBase3DModel(bitmap: Bitmap?) {
        val buffer = readAsset(ActivityUtils.getTopActivity(), "base_3d_model.glb")
        modelViewer.loadModelGlb(buffer)
        modelViewer.transformToUnitCube()
        try {
            replaceCoverTexture(bitmap)
        } catch (e: Exception) {
            ToastUtils.showShort(e.localizedMessage)
            e.printStackTrace()
        }
    }

  1. 找到对应的图层,替换准备好的图片
 private fun replaceCoverTexture(cover: Bitmap?) {
        val activity = ActivityUtils.getTopActivity()
        val engine = modelViewer.engine
        val asset = modelViewer.asset!!
        val rm = modelViewer.engine.renderableManager
        for (entity in asset.entities) {
            val renderable = rm.getInstance(entity)
            if (renderable == 0) {
                continue
            }
            if (asset.getName(entity) == "xxx") { // ⬅︎ 这里是图层的名字
                val material = rm.getMaterialInstanceAt(renderable, 0)
                var bitmap = cover
                if (ObjectUtils.isEmpty(bitmap)) {
                    bitmap = BitmapFactory.decodeResource(
                        activity.resources,
                        R.mipmap.od_img_launcher
                    )
                }

                bitmap = BitmapUtils.adjustPhotoRotation(bitmap, 90)

                val width = bitmap.width
                val height = bitmap.height

                val bytes = bitmap.byteCount
                val buffer = ByteBuffer.allocate(bytes)
                bitmap.copyPixelsToBuffer(buffer)
                buffer.flip()

                val builder = Texture.Builder()
                val newTexture = builder.width(width)
                    .height(height)
                    .levels(0xff)
                    .format(Texture.InternalFormat.SRGB8_A8)
                    .sampler(Texture.Sampler.SAMPLER_2D)
                    .build(engine)

                TextureHelper.setBitmap(engine, newTexture, 0, bitmap)
//                newTexture.generateMipmaps(engine)

                val textureSampler = TextureSampler()
                textureSampler.wrapModeR = TextureSampler.WrapMode.REPEAT
                textureSampler.wrapModeS = TextureSampler.WrapMode.REPEAT
                textureSampler.wrapModeT = TextureSampler.WrapMode.REPEAT

                material.setParameter("baseColorIndex", 0)
                material.setParameter("baseColorMap", newTexture, textureSampler)
            }
        }
    }

    private fun readAsset(context: Context, assetName: String): ByteBuffer {
        val input = context.assets.open(assetName)
        val bytes = ByteArray(input.available())
        input.read(bytes)
        return ByteBuffer.wrap(bytes)
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Filament引擎是一种现代化的渲染引擎,用于实时渲染图形,特别是用于移动设备和虚拟现实平台。它是由Google公司开发的,旨在为开发者提供高质量、高性能的渲染功能。 Filament引擎的一个重要特点是其卓越的可视效果。它使用基于物理的材质系统,能够模拟光照、阴影、反射等视觉效果,使得渲染出来的图像更加逼真和细腻。同时,Filament引擎还支持更高级的图形功能,如屏幕空间反射、全局光照和泛光效果等,以提供更出色的视觉体验。 除了可视效果外,Filament引擎还注重性能。它采用了先进的渲染技术和算法,以提高渲染速度和效率。该引擎能够在移动设备和虚拟现实平台上运行,而不影响用户体验,并且可以自动适应不同硬件设备的性能和功能。 另外,Filament引擎还提供了灵活的工具和API,使开发者能够更轻松地创建和定制渲染效果。开发者可以使用Filament的材质编辑器和场景编辑器来调整和优化图形效果,也可以通过使用Filament的C++ API来实现更高级的自定义需求。 总之,Filament引擎是一款功能强大、易于使用且性能优越的渲染引擎。它将高质量的视觉效果和高性能相结合,为开发者提供了创建令人惊叹的实时渲染图形的能力。无论是用于移动游戏、虚拟现实应用还是其他图形渲染需求,Filament引擎都能够发挥重要作用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值