threepipe
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MeshOptSimplifyModifierPlugin.ts 3.9KB

1 jaar geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import {ThreeViewer} from '../../viewer'
  2. import {BufferAttribute, BufferGeometry} from 'three'
  3. import {IGeometry, iGeometryCommons} from '../../core'
  4. import {toIndexedGeometry} from '../../three'
  5. import {SimplifyModifierPlugin} from './SimplifyModifierPlugin'
  6. import {uiFolderContainer, uiNumber, uiToggle} from 'uiconfig.js'
  7. /**
  8. * Simplify modifier using [meshoptimizer](https://github.com/zeux/meshoptimizer) library.
  9. * Loads the library at runtime from a customisable cdn url.
  10. */
  11. @uiFolderContainer('Simplify Modifier (meshopt)')
  12. export class MeshOptSimplifyModifierPlugin extends SimplifyModifierPlugin {
  13. public static readonly PluginType = 'MeshOptSimplifyModifierPlugin'
  14. constructor(initialize = true, public readonly rootNode = document.head) {
  15. super()
  16. // todo: check if compatible?
  17. if (initialize) this.initialize()
  18. }
  19. get initialized() {
  20. return !!window.MeshoptSimplifier
  21. }
  22. // static SIMPLIFIER_URL = 'https://cdn.jsdelivr.net/gh/zeux/meshoptimizer@master/js/meshopt_simplifier.module.js'
  23. static SIMPLIFIER_URL = 'https://unpkg.com/meshoptimizer@0.20.0/meshopt_simplifier.module.js'
  24. onAdded(viewer: ThreeViewer) {
  25. super.onAdded(viewer)
  26. }
  27. protected _initializing?: Promise<void> = undefined
  28. protected _script?: HTMLScriptElement
  29. async initialize() {
  30. if (this.initialized) return
  31. if (this._initializing) return await this._initializing
  32. const s = document.createElement('script')
  33. s.type = 'module'
  34. const ev = Math.random().toString(36).substring(7)
  35. s.innerHTML = `
  36. import { MeshoptSimplifier } from '${MeshOptSimplifyModifierPlugin.SIMPLIFIER_URL}';
  37. MeshoptSimplifier.ready.then(() => {
  38. window.MeshoptSimplifier = MeshoptSimplifier;
  39. window.dispatchEvent(new CustomEvent('${ev}'))
  40. });
  41. `
  42. this._initializing = new Promise<void>((res) => {
  43. window.addEventListener(ev, ()=>res(), {once: true})
  44. this.rootNode.appendChild(s)
  45. this._script = s
  46. })
  47. return await this._initializing
  48. }
  49. dispose() {
  50. if (this._script) {
  51. this._script.remove()
  52. delete window.MeshoptSimplifier
  53. }
  54. this._script = undefined
  55. }
  56. @uiNumber()
  57. errorThreshold = 0.5
  58. @uiToggle()
  59. lockBorder = false
  60. protected _simplify(geometry: BufferGeometry, count: number): IGeometry {
  61. if (!this.initialized) throw new Error('MeshOptSimplifyModifierPlugin not initialized')
  62. if (!geometry.index) {
  63. geometry = toIndexedGeometry(geometry)
  64. } else {
  65. geometry = geometry.clone()
  66. }
  67. const srcIndexArray = geometry.index!.array
  68. const srcPositionArray = geometry.attributes.position.array
  69. const factor = count / geometry.attributes.position.count
  70. // console.log(factor)
  71. // const targetCount = count * 3
  72. const targetCount = 3 * Math.floor(factor * srcIndexArray.length / 3)
  73. // console.log('srcCount', srcIndexArray.length / 3, 'targetCount', targetCount / 3)
  74. // const errorThresh = 1e-2
  75. const [dstIndexArray, error] = window.MeshoptSimplifier.simplify(
  76. srcIndexArray,
  77. srcPositionArray,
  78. 3,
  79. targetCount,
  80. this.errorThreshold,
  81. this.lockBorder ? ['LockBorder'] : [],
  82. )
  83. console.log('srcCount', srcIndexArray.length / 3, 'destCount', dstIndexArray.length / 3)
  84. if (error) {
  85. console.warn('Simplify error', error)
  86. // return geometry // todo
  87. }
  88. // (geometry.index!.array as Uint32Array).set(dstIndexArray)
  89. geometry.setIndex(new BufferAttribute(new Uint32Array(dstIndexArray), 1))
  90. // geometry.index!.needsUpdate = true
  91. // geometry.setDrawRange(0, dstIndexArray.length)
  92. return iGeometryCommons.upgradeGeometry.call(geometry.toNonIndexed())
  93. }
  94. }
  95. declare global{
  96. interface Window{
  97. MeshoptSimplifier?: any
  98. }
  99. }