@threepipe/plugin-gltf-transform 0.1.3 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/README.md +12 -0
  3. package/dist/GLTFDracoExportPlugin.d.ts +2 -0
  4. package/dist/GLTFDracoExportPlugin.d.ts.map +1 -1
  5. package/dist/GLTFDracoExporter.d.ts +12 -3
  6. package/dist/GLTFDracoExporter.d.ts.map +1 -1
  7. package/dist/GLTFSpecGlossinessConverterPlugin.d.ts +43 -0
  8. package/dist/GLTFSpecGlossinessConverterPlugin.d.ts.map +1 -0
  9. package/dist/dependencies.txt +288 -8
  10. package/dist/index.d.ts +2 -1
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +9575 -3027
  13. package/dist/index.js.map +1 -1
  14. package/dist/index.mjs +9577 -3029
  15. package/dist/index.mjs.map +1 -1
  16. package/docs/.nojekyll +1 -0
  17. package/docs/assets/hierarchy.js +1 -0
  18. package/docs/assets/highlight.css +43 -0
  19. package/docs/assets/icons.js +18 -0
  20. package/docs/assets/icons.svg +1 -0
  21. package/docs/assets/main.js +60 -0
  22. package/docs/assets/navigation.js +1 -0
  23. package/docs/assets/search.js +1 -0
  24. package/docs/assets/style.css +1633 -0
  25. package/docs/classes/GLTFDracoExportPlugin.html +38 -0
  26. package/docs/classes/GLTFDracoExporter.html +35 -0
  27. package/docs/classes/GLTFSpecGlossinessConverterPlugin.html +34 -0
  28. package/docs/enums/EncoderMethod.html +3 -0
  29. package/docs/functions/createGenericExtensionClass.html +1 -0
  30. package/docs/functions/gltfKhrPbrSpecularGlossinessConverter.html +1 -0
  31. package/docs/hierarchy.html +1 -0
  32. package/docs/index.html +1 -0
  33. package/package.json +8 -5
  34. package/src/GLTFDracoExportPlugin.ts +12 -5
  35. package/src/GLTFDracoExporter.ts +8 -8
  36. package/src/GLTFSpecGlossinessConverterPlugin.ts +126 -0
  37. package/src/index.ts +2 -1
@@ -0,0 +1,126 @@
1
+ import {AViewerPluginSync, DRACOLoader2, GLTFBinaryExtension, GLTFLoader2, GLTFParser, ThreeViewer} from 'threepipe'
2
+ import {Extension, WebIO} from '@gltf-transform/core'
3
+ import {ALL_EXTENSIONS} from '@gltf-transform/extensions'
4
+ import {GLTFDracoExportPlugin} from './GLTFDracoExportPlugin'
5
+ import {metalRough} from '@gltf-transform/functions'
6
+
7
+ /**
8
+ * GLTFSpecGlossinessConverterPlugin
9
+ *
10
+ * Plugin that adds a gltf loader extension that automatically converts GLTF files with specular glossiness materials ([KHR_materials_pbrSpecularGlossiness](https://kcoley.github.io/glTF/extensions/2.0/Khronos/KHR_materials_pbrSpecularGlossiness/)) to metallic roughness during import.
11
+ *
12
+ * To use this plugin, simply add it to the viewer and import a file with specular glossiness materials.
13
+ * If `confirm` is set to true, a confirmation dialog will be shown before the conversion.
14
+ */
15
+ export class GLTFSpecGlossinessConverterPlugin extends AViewerPluginSync {
16
+ enabled = true
17
+
18
+ static readonly PluginType = 'GLTFSpecGlossinessConverterPlugin'
19
+ toJSON: any = undefined
20
+
21
+ // dependencies = [GLTFDracoExportPlugin]
22
+
23
+ /**
24
+ * Whether to show the confirmation dialog when loading a GLTF file with specular glossiness materials.
25
+ * If set to false, the conversion will be done automatically without confirmation.
26
+ * To disable the conversion entirely, disable the plugin.
27
+ */
28
+ confirm = true
29
+ /**
30
+ * The message to show in the confirmation dialog when loading a GLTF file with specular glossiness materials.
31
+ */
32
+ confirmMessage = 'GLTF Load: This file includes specular glossiness materials, do you want to convert it to metallic roughness before load?'
33
+
34
+ private _loaderCreate({loader}: {loader: GLTFLoader2}) {
35
+ if (!loader.isGLTFLoader2) return
36
+
37
+ const ep = this._viewer?.getPlugin(GLTFDracoExportPlugin)
38
+ if (!ep) {
39
+ console.error('GLTFSpecGlossinessConverterPlugin requires GLTFDracoExportPlugin in the plugin for extra extensions')
40
+ }
41
+ loader.register(gltfKhrPbrSpecularGlossinessConverter(async(m)=>!this.enabled ? false : !this.confirm ? true : this._viewer?.dialog.confirm(this.confirmMessage || m) ?? true, ep?.gltfTransformExtensions))
42
+ }
43
+
44
+ constructor() {
45
+ super()
46
+ this._loaderCreate = this._loaderCreate.bind(this)
47
+ }
48
+
49
+ onAdded(v: ThreeViewer) {
50
+ super.onAdded(v)
51
+ v.assetManager.importer.addEventListener('loaderCreate', this._loaderCreate as any)
52
+ }
53
+
54
+ onRemove(v: ThreeViewer) {
55
+ v.assetManager.importer.removeEventListener('loaderCreate', this._loaderCreate as any)
56
+ return super.onRemove(v)
57
+ }
58
+ }
59
+
60
+
61
+ export const gltfKhrPbrSpecularGlossinessConverter = (confirm?: (s: string)=>Promise<boolean>, extensions?: (typeof Extension)[])=>(parser: GLTFParser)=>({
62
+ name: 'KHR_materials_pbrSpecularGlossiness',
63
+ beforeRoot: async() => {
64
+ try {
65
+ if (!parser.json.extensionsUsed || !parser.json.extensionsUsed.includes('KHR_materials_pbrSpecularGlossiness')) return
66
+ const doConfirm = parser.importOptions?.confirmSpecGlossConversion ?? true
67
+ if (doConfirm && confirm && !await confirm('Convert KHR_materials_pbrSpecularGlossiness to KHR_materials_pbrMetallicRoughness?')) return
68
+
69
+ const json = parser.json
70
+ const io = new WebIO().registerExtensions(ALL_EXTENSIONS).registerExtensions(extensions ?? [])
71
+
72
+ if (parser.extensions.KHR_draco_mesh_compression) {
73
+ const dracoLoader = parser.extensions.KHR_draco_mesh_compression.dracoLoader as DRACOLoader2
74
+ if (!dracoLoader.isDRACOLoader2) {
75
+ console.error('gltfKhrPbrSpecularGlossinessConverter: DRACOLoader2 required')
76
+ return
77
+ }
78
+ const decoder = await dracoLoader.initDecoder()
79
+ io.registerDependencies({
80
+ // 'draco3d.encoder': libs[0],
81
+ ['draco3d.decoder']: decoder,
82
+ })
83
+ }
84
+
85
+ const document = await io.readJSON({
86
+ json, resources: {
87
+ ['@glb.bin']: new Uint8Array(parser.extensions.KHR_binary_glTF?.body),
88
+ },
89
+ })
90
+
91
+
92
+ // Convert materials.
93
+ await document.transform(metalRough())
94
+
95
+ // no need to compress
96
+ if (parser.extensions.KHR_draco_mesh_compression) {
97
+ document.getRoot().listExtensionsUsed().find(e => e.extensionName === 'KHR_draco_mesh_compression')?.dispose()
98
+ }
99
+
100
+ // Write back to GLB.
101
+ const res = await io.writeBinary(document)
102
+ const binaryExtension = new GLTFBinaryExtension(res.buffer as any)
103
+ parser.extensions.KHR_binary_glTF = binaryExtension
104
+ parser.json = JSON.parse(binaryExtension.content)
105
+
106
+ // this doesn't work for some reason
107
+ // const res = await io.writeJSON(document, {format: Format.GLB})
108
+ // parser.extensions.KHR_binary_glTF.data = res.resources['@glb.bin'].buffer as any
109
+ // parser.extensions.KHR_binary_glTF.content = res.json
110
+ // parser.json = res.json
111
+ } catch (e) {
112
+ console.error(e)
113
+ return
114
+ }
115
+ },
116
+ })
117
+
118
+ declare module 'threepipe'{
119
+ interface ImportAddOptions{
120
+ /**
121
+ * Whether to confirm the conversion of specular glossiness materials to metallic roughness.
122
+ * @default true
123
+ */
124
+ confirmSpecGlossConversion?: boolean
125
+ }
126
+ }
package/src/index.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export {GLTFDracoExporter, createGenericExtensionClass} from './GLTFDracoExporter'
2
- export {GLTFDracoExportPlugin} from './GLTFDracoExportPlugin'
2
+ export {GLTFDracoExportPlugin, EncoderMethod} from './GLTFDracoExportPlugin'
3
+ export {GLTFSpecGlossinessConverterPlugin, gltfKhrPbrSpecularGlossinessConverter} from './GLTFSpecGlossinessConverterPlugin'