build.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { writeFileSync } from 'fs-extra'
  2. import path from 'node:path'
  3. const rootDir = path.resolve(__dirname, '..')
  4. export type FunctionListingsType = (string | [string, string])[]
  5. export interface FunctionList {
  6. cameras: FunctionListingsType
  7. lights: FunctionListingsType
  8. materials: FunctionListingsType
  9. geometries: FunctionListingsType
  10. textures: FunctionListingsType
  11. renderers: FunctionListingsType
  12. scenes: FunctionListingsType
  13. objects: FunctionListingsType
  14. helpers: FunctionListingsType
  15. effects: FunctionListingsType
  16. postprocessing: FunctionListingsType
  17. controls: FunctionListingsType
  18. animations: FunctionListingsType
  19. physics: FunctionListingsType
  20. audio: FunctionListingsType
  21. xr: FunctionListingsType
  22. ui: FunctionListingsType
  23. misc: FunctionListingsType
  24. }
  25. async function buildComponentDefinitions(functions: FunctionList) {
  26. const buildMaterialDefinition = (name: string | [string, string]) => {
  27. const [importName, componentName] = Array.isArray(name) ? [`${name[0]}Parameters`, name[1]] : [`${name}Parameters`, `Tres${name}`]
  28. return {
  29. componentName,
  30. importName,
  31. definition: `${componentName}: DefineComponent<Partial<${importName}>>`
  32. }
  33. }
  34. const buildGeometryDefinition = (name: string | [string, string]) => {
  35. const [importName, componentName] = Array.isArray(name) ? name : [name, `Tres${name}`]
  36. return {
  37. componentName,
  38. importName,
  39. definition: `${componentName}: DefineComponent<Partial<${importName}['parameters']>>`
  40. }
  41. }
  42. const materialDefinitions = functions.materials.map(buildMaterialDefinition)
  43. const geometryDefinitions = functions.geometries.map(buildGeometryDefinition)
  44. const _imports = [... new Set([...materialDefinitions, ...geometryDefinitions].map(({ importName }) => importName))].join(',\n')
  45. const _definitions = [...materialDefinitions, ...geometryDefinitions].map(({ definition }) => definition).join('\n ')
  46. const componentDefinitions = `
  47. import type { DefineComponent } from 'vue'
  48. import type {
  49. ${_imports} } from 'three'
  50. declare module '@vue/runtime-core' {
  51. export interface GlobalComponents {
  52. ${_definitions}
  53. }
  54. }
  55. `.trim()
  56. writeFileSync(path.resolve(rootDir, 'components.d.ts'), componentDefinitions)
  57. }
  58. buildComponentDefinitions({
  59. cameras: [],
  60. lights: [],
  61. materials: [
  62. 'LineBasicMaterial',
  63. 'LineDashedMaterial',
  64. 'MeshBasicMaterial',
  65. 'MeshDepthMaterial',
  66. 'MeshDistanceMaterial',
  67. 'MeshLambertMaterial',
  68. 'MeshMatcapMaterial',
  69. 'MeshNormalMaterial',
  70. 'MeshPhongMaterial',
  71. 'MeshPhysicalMaterial',
  72. 'MeshStandardMaterial',
  73. 'MeshToonMaterial',
  74. 'PointsMaterial',
  75. ['ShaderMaterial', 'TresRawShaderMaterial'], // RawShaderMaterial uses ShaderMaterial's parameters
  76. 'ShaderMaterial',
  77. 'ShadowMaterial',
  78. 'SpriteMaterial',
  79. 'Material',
  80. ],
  81. geometries: [
  82. 'BoxGeometry',
  83. 'CapsuleGeometry',
  84. 'CircleGeometry',
  85. 'ConeGeometry',
  86. 'CylinderGeometry',
  87. 'DodecahedronGeometry',
  88. 'EdgesGeometry',
  89. // 'ExtrudeGeometry', - Not yet supported via this method
  90. 'IcosahedronGeometry',
  91. 'LatheGeometry',
  92. 'OctahedronGeometry',
  93. 'PlaneGeometry',
  94. 'PolyhedronGeometry',
  95. 'RingGeometry',
  96. // 'ShapeGeometry', - Not yet supported via this method
  97. 'SphereGeometry',
  98. 'TetrahedronGeometry',
  99. 'TorusGeometry',
  100. 'TorusKnotGeometry',
  101. 'TubeGeometry',
  102. 'WireframeGeometry',
  103. ],
  104. textures: [],
  105. renderers: [],
  106. scenes: [],
  107. objects: [],
  108. helpers: [],
  109. effects: [],
  110. postprocessing: [],
  111. controls: [],
  112. animations: [],
  113. physics: [],
  114. audio: [],
  115. xr: [],
  116. ui: [],
  117. misc: [],
  118. })