threepipe
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

GLTFMaterialsAlphaMapExtension.ts 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import type {GLTFLoaderPlugin, GLTFParser} from 'three/examples/jsm/loaders/GLTFLoader'
  2. import type {MeshStandardMaterial} from 'three'
  3. import type {GLTFExporterPlugin, GLTFWriter} from 'three/examples/jsm/exporters/GLTFExporter'
  4. /**
  5. * Alpha Map Extension
  6. *
  7. * alphaTexture is added to the material
  8. * This is separate from the alpha in base color texture. This is used when that is not supported in the viewer
  9. *
  10. * Specification: https://webgi.xyz/docs/gltf-extensions/WEBGI_materials_alphamap.html
  11. */
  12. export class GLTFMaterialsAlphaMapExtension {
  13. static readonly WebGiMaterialsAlphaMapExtension = 'WEBGI_materials_alphamap'
  14. static Import = (parser: GLTFParser): GLTFLoaderPlugin=> new GLTFMaterialsAlphaMapExtensionImport(parser)
  15. static Export = (writer: GLTFWriter): GLTFExporterPlugin => new GLTFMaterialsAlphaMapExtensionExport(writer)
  16. // see GLTFDracoExportPlugin
  17. static Textures: Record<string, string|number> = {
  18. alphaTexture: 'G',
  19. }
  20. }
  21. class GLTFMaterialsAlphaMapExtensionImport {
  22. public name: string
  23. constructor(public parser: GLTFParser) {
  24. this.name = GLTFMaterialsAlphaMapExtension.WebGiMaterialsAlphaMapExtension
  25. }
  26. // getMaterialType(materialIndex: number) { // todo: required?
  27. //
  28. // const parser = this.parser
  29. // const materialDef = parser.json.materials[ materialIndex ]
  30. //
  31. // if (!materialDef.extensions || !materialDef.extensions[ this.name ]) return null
  32. //
  33. // return MeshPhysicalMaterial
  34. //
  35. // }
  36. async extendMaterialParams(materialIndex: number, materialParams: any) {
  37. const parser = this.parser
  38. const materialDef = parser.json.materials[ materialIndex ]
  39. if (!materialDef.extensions || !materialDef.extensions[ this.name ]) {
  40. return Promise.resolve()
  41. }
  42. const pending = []
  43. const extension = materialDef.extensions[ this.name ]
  44. if (extension.alphaTexture !== undefined) {
  45. pending.push(parser.assignTexture(materialParams, 'alphaMap', extension.alphaTexture))
  46. }
  47. return Promise.all(pending)
  48. }
  49. }
  50. export type {GLTFMaterialsAlphaMapExtensionImport}
  51. class GLTFMaterialsAlphaMapExtensionExport {
  52. public name: string
  53. constructor(public writer: GLTFWriter) {
  54. this.name = GLTFMaterialsAlphaMapExtension.WebGiMaterialsAlphaMapExtension
  55. }
  56. writeMaterial(material: MeshStandardMaterial, materialDef: any) {
  57. if (!material.isMeshStandardMaterial || !material.alphaMap) return
  58. const writer = this.writer
  59. const extensionsUsed = writer.extensionsUsed
  60. const extensionDef: any = {}
  61. if (material.alphaMap) {
  62. const alphaMapDef = {index: writer.processTexture(material.alphaMap)}
  63. writer.applyTextureTransform(alphaMapDef, material.alphaMap)
  64. extensionDef.alphaTexture = alphaMapDef
  65. }
  66. materialDef.extensions = materialDef.extensions || {}
  67. materialDef.extensions[ this.name ] = extensionDef
  68. extensionsUsed[ this.name ] = true
  69. }
  70. }
  71. export type {GLTFMaterialsAlphaMapExtensionExport}