定制设计【Three.js】导出.GLTF和.GLB格式模型

(GL传输格式)定制设计是一种用于有效传输和加载3D定制设计内容的开放格式规范。可以以JSON (.gltf)或二进制(.glb)格式提供。定制设计外部文件存储纹理(.jpg, .png)定制设计和额外的二进制数据(.bin)。一个glTF定制设计可以保存一个或多个场景,包括网格、材质、纹理、皮肤、骨骼、变形目标、动画、灯光和/或相机。

说人话:定制设计就是导出后节省资源和定制设计加载速度快,比如需要加载人员模型或者大型场景模型,在Three中加载gltf和glb的模型确实快很多,比如加载一个人物模型4M Fbx 明显感觉卡顿一下,而用gltf和glb就不卡顿。

 这里Three用的是r142

制作思路:

 

1.加载模型。我用的是OBJ模型 官网API:

2.给模型贴上透明材质。连同材质会一起导出,如果不想导出材质就不设置材质

4.使用GLTFExporter生成GLTF或GLB格式数据。官网API:

5.使用HTML5 Blob进行下载。

直接上代码:

  1. createOBJ() {
  2. const that = this;
  3. // 建筑面材质
  4. let buildMaterial = new THREE.MeshBasicMaterial({
  5. color: "#009EFF", // 颜色
  6. transparent: true, // 是否开启使用透明度
  7. opacity: 0.25, // 透明度
  8. depthWrite: false, // 关闭深度写入 透视效果
  9. side: THREE.DoubleSide, // 双面显示
  10. });
  11. // 建筑线材质
  12. let lineMaterial = new THREE.LineBasicMaterial({
  13. color: "#36BCFF",
  14. transparent: true,
  15. opacity: 0.4,
  16. depthWrite: false,
  17. side: THREE.DoubleSide,
  18. });
  19. const loader = new OBJLoader(); //引用加载obj模型组件
  20. loader.load(
  21. 'data/models/light.obj',
  22. function (object) {
  23. object.scale.set(0.1, 0.1, 0.1); // 设置模型缩放
  24. object.traverse((e) => { // 遍历模型中所有mesh
  25. e.material = buildMaterial; // 赋模型材质
  26. if (e.geometry) {
  27. const edges = new THREE.EdgesGeometry(
  28. e.geometry
  29. );
  30. const line = new THREE.LineSegments(
  31. edges,
  32. lineMaterial // 赋线条材质
  33. );
  34. object.add(line); // 把每一个mesh生成的线条添加到场景中
  35. }
  36. });
  37. that.scene.add(object); // 添加到场景中
  38. // 导出
  39. that.exporter({
  40. input:object,
  41. })
  42. },
  43. function (xhr) {
  44. console.log((xhr.loaded / xhr.total * 100) + '% 加载进度');
  45. },
  46. function (error) {
  47. console.log("错误,检查你的模型路径是否准确,模型是否存在!", error);
  48. }
  49. );
  50. },
  51. /**
  52. * gltf和glb导出器
  53. * @param option
  54. */
  55. exporter(option) {
  56. const gltfExporter = new GLTFExporter();
  57. const options = {
  58. trs: option.trs == null ? false : option.trs, //导出位置、旋转和缩放,而不是每个节点的矩阵 默认是false
  59. onlyVisible: option.onlyVisible == null ? true : option.onlyVisible, //只导出可见的对象 默认是true
  60. truncateDrawRange: option.truncateDrawRange == null ? true : option.truncateDrawRange,
  61. binary: option.binary == null ? false : option.binary, //binary==true 导出glb | binary==false 导出gltf
  62. forceindices: option.forceindices == null ? false : option.forceindices, //为非索引几何图形生成索引并导出它们 默认false
  63. maxTextureSize: 4096, //限制图像的最大尺寸(宽度、高度)为给定值。默认是无限
  64. };
  65. gltfExporter.parse(
  66. [option.input], //这是个数组
  67. function (result) {
  68. if (result instanceof ArrayBuffer) {
  69. save(new Blob([result], {type: 'application/octet-stream'}), 'scene.glb');
  70. } else {
  71. const output = JSON.stringify(result, null, 2);
  72. save(new Blob([output], {type: 'text/plain'}), 'scene.gltf');
  73. }
  74. },
  75. function (error) {
  76. console.log('An error happened during parsing', error);
  77. },
  78. options
  79. );
  80. const link = document.createElement('a');
  81. link.style.display = 'none';
  82. document.body.appendChild(link); // Firefox workaround, see #6594
  83. function save(blob, filename) {
  84. link.href = URL.createObjectURL(blob);
  85. link.download = filename;
  86. link.click();
  87. }
  88. },

ps:

1.需要引用THREE、GLTFExporter、OBJLoader。没人不会引用这个吧?应该没人吧!!

这是我本地引用的:

  1. import THREE from "three";
  2. import {GLTFExporter} from "@/plugins/three.js-r142/examples/jsm/exporters/GLTFExporter";
  3. import {OBJLoader} from "@/plugins/three.js-r142/examples/jsm/loaders/OBJLoader";

2.运行之后自动下载 

 3.加载GTTF和GLB使用,可自行查看官网API

  1. const gltfLoader = new GLTFLoader()
  2. const dracoLoader = new DRACOLoader()
  3. dracoLoader.setDecoderPath(option.dracoUrl)
  4. gltfLoader.setDRACOLoader(dracoLoader)
  5. gltfLoader.load(option.url, option.onLoad, option.onProgress, option.onError)

 

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发