@threepipe/plugin-gltf-transform 0.2.2 → 0.3.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/CHANGELOG.md +22 -3
- package/README.md +2 -2
- package/dist/GLTFDracoExportPlugin.d.ts +2 -44
- package/dist/GLTFDracoExportPlugin.d.ts.map +1 -1
- package/dist/GLTFDracoExporter.d.ts +5 -40
- package/dist/GLTFDracoExporter.d.ts.map +1 -1
- package/dist/GLTFDracoExporterBase.d.ts +49 -0
- package/dist/GLTFDracoExporterBase.d.ts.map +1 -0
- package/dist/GLTFSpecGlossinessConverterPlugin.d.ts +10 -31
- package/dist/GLTFSpecGlossinessConverterPlugin.d.ts.map +1 -1
- package/dist/GLTFSpecGlossinessConverterPluginBase.d.ts +45 -0
- package/dist/GLTFSpecGlossinessConverterPluginBase.d.ts.map +1 -0
- package/dist/dependencies.txt +12 -12
- package/dist/index.d.ts +5 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +119 -136
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +117 -134
- package/dist/index.mjs.map +1 -1
- package/docs/assets/hierarchy.js +1 -1
- package/docs/assets/icons.js +1 -1
- package/docs/assets/icons.svg +1 -1
- package/docs/assets/main.js +2 -2
- package/docs/assets/navigation.js +1 -1
- package/docs/assets/search.js +1 -1
- package/docs/assets/style.css +3 -3
- package/docs/classes/GLTFDracoExportPlugin.html +7 -6
- package/docs/classes/GLTFDracoExporter.html +15 -14
- package/docs/classes/GLTFDracoExporterBase.html +37 -0
- package/docs/classes/GLTFSpecGlossinessConverterPlugin.html +16 -14
- package/docs/classes/GLTFSpecGlossinessConverterPluginBase.html +37 -0
- package/docs/classes/GLTFViewerConfigExtensionGP.html +5 -0
- package/docs/enums/EncoderMethod.html +2 -2
- package/docs/functions/createGenericExtensionClass.html +1 -1
- package/docs/functions/gltfKhrPbrSpecularGlossinessConverter.html +2 -1
- package/docs/functions/gltfKhrPbrSpecularGlossinessConverterBase.html +1 -0
- package/docs/hierarchy.html +1 -1
- package/docs/index.html +1 -1
- package/package.json +24 -2
- package/src/GLTFDracoExportPlugin.ts +2 -2
- package/src/GLTFDracoExporter.ts +10 -417
- package/src/GLTFDracoExporterBase.ts +427 -0
- package/src/GLTFSpecGlossinessConverterPlugin.ts +14 -115
- package/src/GLTFSpecGlossinessConverterPluginBase.ts +129 -0
- package/src/index.ts +3 -1
- package/tsconfig.json +1 -0
- package/docs/.nojekyll +0 -1
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import {AViewerPluginSync, DRACOLoader2, GLTFBinaryExtension, GLTFLoader2, GLTFParser, ThreeViewer} from 'threepipe'
|
|
2
|
+
import {Extension, PlatformIO} 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
|
+
* GLTFSpecGlossinessConverterPluginBase
|
|
9
|
+
*
|
|
10
|
+
* Base plugin that adds a gltf loader extension that automatically converts GLTF files with specular glossiness materials
|
|
11
|
+
* ([KHR_materials_pbrSpecularGlossiness](https://kcoley.github.io/glTF/extensions/2.0/Khronos/KHR_materials_pbrSpecularGlossiness/))
|
|
12
|
+
* to metallic roughness during import.
|
|
13
|
+
*
|
|
14
|
+
* Takes an IO factory function to create PlatformIO instances, allowing use with both WebIO (browser) and NodeIO (Node.js).
|
|
15
|
+
*/
|
|
16
|
+
export abstract class GLTFSpecGlossinessConverterPluginBase extends AViewerPluginSync {
|
|
17
|
+
enabled = true
|
|
18
|
+
|
|
19
|
+
static readonly PluginType = 'GLTFSpecGlossinessConverterPlugin'
|
|
20
|
+
toJSON: any = undefined
|
|
21
|
+
|
|
22
|
+
// dependencies = [GLTFDracoExportPlugin]
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Whether to show the confirmation dialog when loading a GLTF file with specular glossiness materials.
|
|
26
|
+
* If set to false, the conversion will be done automatically without confirmation.
|
|
27
|
+
* To disable the conversion entirely, disable the plugin.
|
|
28
|
+
*/
|
|
29
|
+
confirm = true
|
|
30
|
+
/**
|
|
31
|
+
* The message to show in the confirmation dialog when loading a GLTF file with specular glossiness materials.
|
|
32
|
+
*/
|
|
33
|
+
confirmMessage = 'GLTF Load: This file includes specular glossiness materials, do you want to convert it to metallic roughness before load?'
|
|
34
|
+
|
|
35
|
+
protected abstract _createIO(): PlatformIO
|
|
36
|
+
|
|
37
|
+
private _loaderCreate({loader}: {loader: GLTFLoader2}) {
|
|
38
|
+
if (!loader.isGLTFLoader2) return
|
|
39
|
+
|
|
40
|
+
const ep = this._viewer?.getPlugin(GLTFDracoExportPlugin)
|
|
41
|
+
if (!ep) {
|
|
42
|
+
console.error('GLTFSpecGlossinessConverterPlugin requires GLTFDracoExportPlugin in the plugin for extra extensions')
|
|
43
|
+
}
|
|
44
|
+
loader.register(gltfKhrPbrSpecularGlossinessConverterBase(() => this._createIO(), async(m)=>!this.enabled ? false : !this.confirm ? true : this._viewer?.dialog.confirm(this.confirmMessage || m) ?? true, ep?.gltfTransformExtensions))
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
constructor() {
|
|
48
|
+
super()
|
|
49
|
+
this._loaderCreate = this._loaderCreate.bind(this)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
onAdded(v: ThreeViewer) {
|
|
53
|
+
super.onAdded(v)
|
|
54
|
+
v.assetManager.importer.addEventListener('loaderCreate', this._loaderCreate as any)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
onRemove(v: ThreeViewer) {
|
|
58
|
+
v.assetManager.importer.removeEventListener('loaderCreate', this._loaderCreate as any)
|
|
59
|
+
return super.onRemove(v)
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
export const gltfKhrPbrSpecularGlossinessConverterBase = (ioFactory: () => PlatformIO, confirm?: (s: string)=>Promise<boolean>, extensions?: (typeof Extension)[])=>(parser: GLTFParser)=>({
|
|
65
|
+
name: 'KHR_materials_pbrSpecularGlossiness',
|
|
66
|
+
beforeRoot: async() => {
|
|
67
|
+
try {
|
|
68
|
+
if (!parser.json.extensionsUsed || !parser.json.extensionsUsed.includes('KHR_materials_pbrSpecularGlossiness')) return
|
|
69
|
+
const doConfirm = parser.importOptions?.confirmSpecGlossConversion ?? true
|
|
70
|
+
if (doConfirm && confirm && !await confirm('Convert KHR_materials_pbrSpecularGlossiness to KHR_materials_pbrMetallicRoughness?')) return
|
|
71
|
+
|
|
72
|
+
const json = parser.json
|
|
73
|
+
const io = ioFactory().registerExtensions(ALL_EXTENSIONS).registerExtensions(extensions ?? [])
|
|
74
|
+
|
|
75
|
+
if (parser.extensions.KHR_draco_mesh_compression) {
|
|
76
|
+
const dracoLoader = parser.extensions.KHR_draco_mesh_compression.dracoLoader as DRACOLoader2
|
|
77
|
+
if (!dracoLoader.isDRACOLoader2) {
|
|
78
|
+
console.error('gltfKhrPbrSpecularGlossinessConverter: DRACOLoader2 required')
|
|
79
|
+
return
|
|
80
|
+
}
|
|
81
|
+
const decoder = await dracoLoader.initDecoder()
|
|
82
|
+
io.registerDependencies({
|
|
83
|
+
// 'draco3d.encoder': libs[0],
|
|
84
|
+
['draco3d.decoder']: decoder,
|
|
85
|
+
})
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const document = await io.readJSON({
|
|
89
|
+
json, resources: {
|
|
90
|
+
['@glb.bin']: new Uint8Array(parser.extensions.KHR_binary_glTF?.body),
|
|
91
|
+
},
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
// Convert materials.
|
|
96
|
+
await document.transform(metalRough())
|
|
97
|
+
|
|
98
|
+
// no need to compress
|
|
99
|
+
if (parser.extensions.KHR_draco_mesh_compression) {
|
|
100
|
+
document.getRoot().listExtensionsUsed().find(e => e.extensionName === 'KHR_draco_mesh_compression')?.dispose()
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Write back to GLB.
|
|
104
|
+
const res = await io.writeBinary(document)
|
|
105
|
+
const binaryExtension = new GLTFBinaryExtension(res.buffer as any)
|
|
106
|
+
parser.extensions.KHR_binary_glTF = binaryExtension
|
|
107
|
+
parser.json = JSON.parse(binaryExtension.content)
|
|
108
|
+
|
|
109
|
+
// this doesn't work for some reason
|
|
110
|
+
// const res = await io.writeJSON(document, {format: Format.GLB})
|
|
111
|
+
// parser.extensions.KHR_binary_glTF.data = res.resources['@glb.bin'].buffer as any
|
|
112
|
+
// parser.extensions.KHR_binary_glTF.content = res.json
|
|
113
|
+
// parser.json = res.json
|
|
114
|
+
} catch (e) {
|
|
115
|
+
console.error(e)
|
|
116
|
+
return
|
|
117
|
+
}
|
|
118
|
+
},
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
declare module 'threepipe'{
|
|
122
|
+
interface ImportAddOptions{
|
|
123
|
+
/**
|
|
124
|
+
* Whether to confirm the conversion of specular glossiness materials to metallic roughness.
|
|
125
|
+
* @default true
|
|
126
|
+
*/
|
|
127
|
+
confirmSpecGlossConversion?: boolean
|
|
128
|
+
}
|
|
129
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export {GLTFDracoExporterBase, GLTFViewerConfigExtensionGP, createGenericExtensionClass} from './GLTFDracoExporterBase'
|
|
2
|
+
export {GLTFDracoExporter} from './GLTFDracoExporter'
|
|
2
3
|
export {GLTFDracoExportPlugin, EncoderMethod} from './GLTFDracoExportPlugin'
|
|
4
|
+
export {GLTFSpecGlossinessConverterPluginBase, gltfKhrPbrSpecularGlossinessConverterBase} from './GLTFSpecGlossinessConverterPluginBase'
|
|
3
5
|
export {GLTFSpecGlossinessConverterPlugin, gltfKhrPbrSpecularGlossinessConverter} from './GLTFSpecGlossinessConverterPlugin'
|
package/tsconfig.json
CHANGED
package/docs/.nojekyll
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
TypeDoc added this file to prevent GitHub Pages from using Jekyll. You can turn off this behavior by setting the `githubPages` option to false.
|