gltf模型浏览器_腾讯硬核干货!如何在页面极速渲染3D模型

朋友们,还记得 QQ 20 周年 H5 中可可爱爱的太空鹅吗?

为了实现旋转和换肤功能,在 H5 中我们随机展示了5种类型的 3D 太空鹅模型,如下图所示:

但是在 H5 中引入 3D 模型往往存在资源太大、性能损耗严重、还原不真实的问题,这也让许多 3D 创意止步于开发阶段。

如何更好地在 H5 中还原模型呢?本文将从模型网格和贴图文件两方面分析,介绍几种通过技术角度优化加载速度和提高渲染性能的途径,在保证 3D 模型不减面,贴图不缩小的情况下,将模型精致地还原在 H5 或其他应用程序中。

模型网格压缩

首先是分析模型网格,对于大型 3D 资源,我们一般会通过在模型设计时进行“减面”来减少模型几何体的大小,但也会带来模型精致度的缺失。如下图所示:

而通过 glTF 配合 Draco 压缩的方式,可以在视觉效果近乎一致的情况下,让3D模型文件成倍缩小。下面具体介绍 glTF 格式及 Draco 压缩工具。

1. 将模型导出为 glTF 格式

glTF 介绍

glTF 称为“ 3D 界的 JPEG”,使用了更优的数据结构,为应用程序实时渲染而生。glTF 有以下几大特点:

- 由现有 OpenGL 的维护组织 Khronos 推出,目的就是为了统一用于应用程序渲染的 3D 格式,更适用于基于 OpenGL 的引擎;

- 减少了 3D 格式中除了与渲染无关的冗余信息,最小化 3D 文件资源;

- 优化了应用程序读取效率和和减少渲染模型的运行时间;

- 支持 3D 模型几何体、材质、动画及场景、摄影机等信息。

glTF 导出格式有两种后缀格式可供选择:.gltf 和 .glb:

- .gltf 文件导出时一般会输出两种文件类型,一是 .bin 文件,以二进制流的方式存储顶点坐标、顶点法线坐标和贴图纹理坐标、贴图信息等模型基本数据信息;二是 .gltf 文件,本质是 json 文件,记录对bin文件中模型顶点基本数据的索引、材质索引等信息,方便编辑,可读性较好;

- .glb 文件格式只导出一个 .glb 文件,将所有数据都输出为二进制流,通常来说会更小一点,若不关心模型内的具体数据可直接选择此类型。

glTF 转换

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"uniform float gltf_u_dec_texcoord_0_normConstant; uniform vec2 gltf_u_dec_texcoord_0_min; vec2 gltf_a_dec_texcoord_0; uniform float gltf_u_dec_position_normConstant; uniform vec3 gltf_u_dec_position_min; vec3 gltf_a_dec_position; precision highp float; uniform mat4 u_modelViewMatrix; uniform mat4 u_projectionMatrix; #ifdef APPLY_FLATTEN uniform sampler2D gltf_flattenTexture; uniform vec4 gltf_flattenBounds; uniform mat4 gltf_flattenRenderMatrix; uniform mat4 gltf_flattenInverseRenderMatrix; uniform float gltf_flattenHeight; #endif attribute vec3 a_position; attribute vec2 a_texcoord_0; varying vec2 v_texcoord_0; void gltf_decoded_POSITION() { vec3 weightedPosition = gltf_a_dec_position; vec4 position = vec4(weightedPosition, 1.0); position = u_modelViewMatrix * position; gl_Position = u_projectionMatrix * position; #ifdef PICK_VERTEX gl_PointSize = 1.0; #endif #ifdef APPLY_FLATTEN vec4 positionRelative = gltf_flattenInverseRenderMatrix * position; vec2 flattenBoundsDimension = gltf_flattenBounds.zw - gltf_flattenBounds.xy; vec2 texCoord = (positionRelative.xy - gltf_flattenBounds.xy) / flattenBoundsDimension; bool outOfBounds = texCoord.x > 1.0 || texCoord.x < 0.0 || texCoord.y > 1.0 || texCoord.y < 0.0; vec4 color = texture2D(gltf_flattenTexture, texCoord); if(!outOfBounds && abs(color.r - 1.0) < 0.1) { positionRelative.z = gltf_flattenHeight + sin(positionRelative.z) * 0.1; gl_Position = u_projectionMatrix * gltf_flattenRenderMatrix * positionRelative; } #endif v_texcoord_0 = gltf_a_dec_texcoord_0; } void gltf_decoded_TEXCOORD_0() { gltf_a_dec_position = gltf_u_dec_position_min + a_position * gltf_u_dec_position_normConstant; gltf_decoded_POSITION(); } void main() { gltf_a_dec_texcoord_0 = gltf_u_dec_texcoord_0_min + a_texcoord_0 * gltf_u_dec_texcoord_0_normConstant; gltf_decoded_TEXCOORD_0(); } "
02-07

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值