@threepipe/plugin-gltf-transform 0.1.3 → 0.2.0

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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@threepipe/plugin-gltf-transform",
3
3
  "description": "Utility plugins for threepipe using gltf-transform to optimize/compress glTF files.",
4
- "version": "0.1.3",
4
+ "version": "0.2.0",
5
5
  "dependencies": {},
6
6
  "exports": {
7
7
  ".": {
@@ -41,6 +41,6 @@
41
41
  "directory": "plugins/gltf-transform"
42
42
  },
43
43
  "peerDependencies": {
44
- "threepipe": "^0.0.39"
44
+ "threepipe": "^0.0.47"
45
45
  }
46
46
  }
@@ -17,9 +17,14 @@ import {
17
17
  noiseBumpMaterialGLTFExtension,
18
18
  fragmentClippingGLTFExtension,
19
19
  } from 'threepipe'
20
- import {GLTFDracoExporter} from './GLTFDracoExporter'
20
+ import {
21
+ createGenericExtensionClass,
22
+ GLTFDracoExporter,
23
+ GLTFViewerConfigExtensionGP,
24
+ } from './GLTFDracoExporter'
21
25
  import {UiObjectConfig} from 'uiconfig.js'
22
26
  import {EncoderOptions} from '@gltf-transform/extensions/dist/khr-draco-mesh-compression/encoder'
27
+ import {Extension} from '@gltf-transform/core'
23
28
 
24
29
  export enum EncoderMethod {
25
30
  EDGEBREAKER = 1,
@@ -42,7 +47,7 @@ export class GLTFDracoExportPlugin extends AViewerPluginSync {
42
47
  * These are added here, but also added as plugins. Added here by default so that the data is not lost if some plugin is not added in an app.
43
48
  * To explicitly remove the data, use `removeExtension` with the name of the extension
44
49
  */
45
- extraExtensions = [
50
+ extraExtensions = [ // its array because we want to keep the order of extensions
46
51
  [GLTFMaterialsBumpMapExtension.WebGiMaterialsBumpMapExtension, GLTFMaterialsBumpMapExtension.Textures],
47
52
  [GLTFMaterialsLightMapExtension.WebGiMaterialsLightMapExtension, GLTFMaterialsLightMapExtension.Textures],
48
53
  [GLTFMaterialsAlphaMapExtension.WebGiMaterialsAlphaMapExtension, GLTFMaterialsAlphaMapExtension.Textures],
@@ -69,6 +74,10 @@ export class GLTFDracoExportPlugin extends AViewerPluginSync {
69
74
  // SSBevelMaterialExtension
70
75
  ] as [string, Record<string, string|number>|undefined][]
71
76
 
77
+ get gltfTransformExtensions(): (typeof Extension)[] {
78
+ return [GLTFViewerConfigExtensionGP, ...this.extraExtensions.map(e => createGenericExtensionClass(e[0], e[1]))]
79
+ }
80
+
72
81
  addExtension(name: string, textures?: Record<string, string|number>) {
73
82
  const ext = this.extraExtensions.findIndex(e => e[0] === name)
74
83
  if (ext >= 0) this.extraExtensions[ext] = [name, textures]
@@ -95,9 +104,7 @@ export class GLTFDracoExportPlugin extends AViewerPluginSync {
95
104
  // todo unregister on dispose
96
105
  this._viewer.assetManager.importer.registerFile(tempFile) as DRACOLoader2)
97
106
  ex.setup(this._viewer, _exporter.extensions)
98
- for (const [ext, config] of this.extraExtensions) {
99
- ex.createAndAddExtension(ext, config)
100
- }
107
+ ex.addExtension(...this.gltfTransformExtensions)
101
108
  return ex
102
109
  }
103
110
 
@@ -14,14 +14,14 @@ import {
14
14
  } from '@gltf-transform/core'
15
15
  import {EncoderOptions} from '@gltf-transform/extensions/dist/khr-draco-mesh-compression/encoder'
16
16
  import {ALL_EXTENSIONS, KHRDracoMeshCompression} from '@gltf-transform/extensions'
17
- import {DRACOLoader2, GLTFExporter2, GLTFExporter2Options, GLTFViewerConfigExtension, IExportParser} from 'threepipe'
17
+ import {DRACOLoader2, GLTFExporter2, GLTFExporter2Options, GLTFViewerConfigExtension, IExportWriter} from 'threepipe'
18
18
 
19
19
  /**
20
20
  * GLTF Draco Exporter
21
21
  *
22
22
  * Extension of GLTFExporter2 that runs the output through gltf-transform for draco compression.
23
23
  */
24
- export class GLTFDracoExporter extends GLTFExporter2 implements IExportParser {
24
+ export class GLTFDracoExporter extends GLTFExporter2 implements IExportWriter {
25
25
  public loader?: DRACOLoader2 // required for loading draco libs.
26
26
  private _io: WebIO
27
27
  private _loadedLibs = false
@@ -34,9 +34,6 @@ export class GLTFDracoExporter extends GLTFExporter2 implements IExportParser {
34
34
  encodeSpeed: 5,
35
35
  }
36
36
  this._io = new WebIO().registerExtensions(ALL_EXTENSIONS)
37
- .registerExtensions([
38
- GLTFViewerConfigExtensionGP,
39
- ])
40
37
  this._encoderOptions = encoderOptions
41
38
 
42
39
  if (loader) {
@@ -131,13 +128,16 @@ export class GLTFDracoExporter extends GLTFExporter2 implements IExportParser {
131
128
  }
132
129
  }
133
130
 
134
- addExtension(extension: typeof Extension): this {
135
- this._io.registerExtensions([extension])
131
+ addExtension(...extension: (typeof Extension)[]): this {
132
+ this._io.registerExtensions(extension)
136
133
  return this
137
134
  }
138
135
  createAndAddExtension(name: string, textures?: Record<string, string|number>): this {
139
136
  return this.addExtension(createGenericExtensionClass(name, textures))
140
137
  }
138
+ createAndAddExtensions(extensions: [string, Record<string, string|number>|undefined][]): this {
139
+ return this.addExtension(...extensions.map(e=> createGenericExtensionClass(e[0], e[1])))
140
+ }
141
141
  }
142
142
 
143
143
  declare module 'threepipe'{
@@ -157,7 +157,7 @@ class ViewerJSONExtensionProperty extends ExtensionProperty {
157
157
  protected init(): void {return}
158
158
 
159
159
  }
160
- class GLTFViewerConfigExtensionGP extends Extension {
160
+ export class GLTFViewerConfigExtensionGP extends Extension {
161
161
  public readonly extensionName = GLTFViewerConfigExtension.ViewerConfigGLTFExtension
162
162
  public static readonly EXTENSION_NAME = GLTFViewerConfigExtension.ViewerConfigGLTFExtension
163
163
  private _viewerConfig: any = {}
@@ -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'