threepipe
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

script.ts 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import {
  2. _testFinish,
  3. _testStart,
  4. downloadBlob,
  5. HemisphereLight,
  6. IObject3D,
  7. LoadingScreenPlugin,
  8. ThreeViewer,
  9. } from 'threepipe'
  10. import {AssimpJsPlugin} from '@threepipe/plugin-assimpjs'
  11. const viewer = new ThreeViewer({
  12. canvas: document.getElementById('mcanvas') as HTMLCanvasElement,
  13. msaa: true,
  14. plugins: [LoadingScreenPlugin],
  15. })
  16. // Export to fbx is done by first exporting to glb and converting that to fbx using AssimpJsPlugin
  17. async function init() {
  18. const assimp = viewer.addPluginSync(AssimpJsPlugin)
  19. await assimp.init()
  20. viewer.setEnvironmentMap('https://threejs.org/examples/textures/equirectangular/venice_sunset_1k.hdr')
  21. // load a file
  22. const result = await viewer.load<IObject3D>('https://threejs.org/examples/models/gltf/DamagedHelmet/glTF/DamagedHelmet.gltf', {
  23. autoCenter: true,
  24. autoScale: true,
  25. })
  26. let fbxBlob: Blob | undefined = undefined
  27. let converting = false
  28. async function exportFbx() {
  29. if (fbxBlob) return fbxBlob
  30. if (converting) {
  31. console.warn('Already converting, please wait...')
  32. return
  33. }
  34. converting = true
  35. // export to glb
  36. const blob = await viewer.export(result, {
  37. embedUrlImages: true,
  38. })
  39. // const blob = await viewer.exportScene(); // its possible to export the whole scene also
  40. if (!blob || blob.ext !== 'glb') {
  41. alert('Unable to export scene')
  42. converting = false
  43. return
  44. }
  45. fbxBlob = assimp.convertFiles({['file.glb']: await blob.arrayBuffer()}, 'fbx')
  46. if (!fbxBlob) {
  47. alert('Failed to convert glb to fbx')
  48. converting = false
  49. return
  50. }
  51. // clear scene
  52. viewer.scene.disposeSceneModels()
  53. // load a light and fbx file
  54. const hemiLight = viewer.scene.addObject(new HemisphereLight(0xffffff, 0x444444, 5), {addToRoot: true})
  55. hemiLight.name = 'Hemisphere Light'
  56. await viewer.load({
  57. path: 'file.fbx',
  58. file: fbxBlob,
  59. }, {
  60. autoCenter: true,
  61. autoScale: true,
  62. })
  63. converting = false
  64. return fbxBlob
  65. }
  66. // add download button
  67. const convertButton = document.createElement('button')
  68. convertButton.innerText = 'Convert to fbx'
  69. convertButton.style.position = 'absolute'
  70. convertButton.style.bottom = '6rem'
  71. convertButton.style.right = '3rem'
  72. convertButton.style.zIndex = '10000'
  73. convertButton.onclick = async() => {
  74. await exportFbx()
  75. }
  76. document.body.appendChild(convertButton)
  77. const downloadButton = document.createElement('button')
  78. downloadButton.innerText = 'Download as .fbx'
  79. downloadButton.style.position = 'absolute'
  80. downloadButton.style.bottom = '3rem'
  81. downloadButton.style.right = '3rem'
  82. downloadButton.style.zIndex = '10000'
  83. downloadButton.onclick = async() => {
  84. await exportFbx()
  85. if (fbxBlob) downloadBlob(fbxBlob, 'file.fbx')
  86. }
  87. document.body.appendChild(downloadButton)
  88. }
  89. _testStart()
  90. init().finally(_testFinish)