@threlte/gltf 1.0.0-next.0 → 1.0.0-next.2

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 CHANGED
@@ -1,5 +1,17 @@
1
1
  # @threlte/gltf
2
2
 
3
+ ## 1.0.0-next.2
4
+
5
+ ### Patch Changes
6
+
7
+ - 124eabab: Fixed preload method to return a Promise
8
+
9
+ ## 1.0.0-next.1
10
+
11
+ ### Minor Changes
12
+
13
+ - 72c71414: Added option to add a preload method that makes preloading an asset easy. Also, added the option to make an isolated component (so no \$\$restProps usage)
14
+
3
15
  ## 1.0.0-next.0
4
16
 
5
17
  ### Major Changes
package/cli.js CHANGED
@@ -12,19 +12,21 @@ const __dirname = dirname(__filename)
12
12
 
13
13
  const cli = meow(
14
14
  `
15
- Usage
16
- $ npx @threlte/gltf [Model.glb] [options]
15
+ Usage
16
+ $ npx @threlte/gltf [Model.glb] [options]
17
17
 
18
- Options
18
+ Options
19
19
  --output, -o Output file name/path
20
20
  --types, -t Add Typescript definitions
21
21
  --keepnames, -k Keep original names
22
22
  --keepgroups, -K Keep (empty) groups, disable pruning
23
23
  --meta, -m Include metadata (as userData)
24
- --shadows, s Let meshes cast and receive shadows
25
- --printwidth, w Prettier printWidth (default: 120)
24
+ --shadows, -s Let meshes cast and receive shadows
25
+ --printwidth, -w Prettier printWidth (default: 120)
26
26
  --precision, -p Number of fractional digits (default: 2)
27
27
  --draco, -d Draco binary path
28
+ --preload -P Add preload method to module script
29
+ --isolated, -i Output as isolated module (No $$restProps usage)
28
30
  --root, -r Sets directory from which .gltf file is served
29
31
  --transform, -T Transform the asset for the web (draco, prune, resize)
30
32
  --resolution, -R Transform resolution for texture resizing (default: 1024)
@@ -45,6 +47,8 @@ const cli = meow(
45
47
  printwidth: { type: 'number', alias: 'p', default: 120 },
46
48
  meta: { type: 'boolean', alias: 'm' },
47
49
  precision: { type: 'number', alias: 'p', default: 2 },
50
+ isolated: { type: 'boolean', alias: 'i', default: false },
51
+ preload: { type: 'boolean', alias: 'P', default: false },
48
52
  draco: { type: 'string', alias: 'd' },
49
53
  root: { type: 'string', alias: 'r' },
50
54
  transform: { type: 'boolean', alias: 'T' },
@@ -60,6 +64,15 @@ const cli = meow(
60
64
 
61
65
  const { packageJson } = readPackageUpSync({ cwd: __dirname, normalize: false })
62
66
 
67
+ function toPascalCase(string) {
68
+ return `${string}`
69
+ .toLowerCase()
70
+ .replace(new RegExp(/[-_]+/, 'g'), ' ')
71
+ .replace(new RegExp(/[^\w\s]/, 'g'), '')
72
+ .replace(new RegExp(/\s+(.)(\w*)/, 'g'), ($1, $2, $3) => `${$2.toUpperCase() + $3}`)
73
+ .replace(new RegExp(/\w/), (s) => s.toUpperCase())
74
+ }
75
+
63
76
  if (cli.input.length === 0) {
64
77
  console.log(cli.help)
65
78
  } else {
@@ -69,16 +82,20 @@ if (cli.input.length === 0) {
69
82
  Command: npx @threlte/gltf@${packageJson.version} ${process.argv.slice(2).join(' ')}`
70
83
  }
71
84
  const file = cli.input[0]
72
- const filePath = path.resolve(__dirname, file)
73
85
  let nameExt = file.match(/[-_\w]+[.][\w]+$/i)[0]
74
86
  let name = nameExt.split('.').slice(0, -1).join('.')
75
- const baseName = name.charAt(0).toUpperCase() + name.slice(1)
87
+ const baseName = toPascalCase(name.charAt(0).toUpperCase() + name.slice(1))
76
88
  const output = baseName + '.svelte'
77
89
  const showLog = (log) => {
78
90
  console.info('log:', log)
79
91
  }
80
92
  try {
81
- const response = await gltf(file, output, { ...config, showLog, timeout: 0, delay: 1 })
93
+ const response = await gltf(file, output, baseName, {
94
+ ...config,
95
+ showLog,
96
+ timeout: 0,
97
+ delay: 1
98
+ })
82
99
  } catch (e) {
83
100
  console.error(e)
84
101
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@threlte/gltf",
3
- "version": "1.0.0-next.0",
3
+ "version": "1.0.0-next.2",
4
4
  "description": "GLTF to Threlte converter",
5
5
  "type": "module",
6
6
  "keywords": [
package/src/index.js CHANGED
@@ -22,7 +22,7 @@ function toArrayBuffer(buf) {
22
22
  return ab
23
23
  }
24
24
 
25
- export default function (file, output, options) {
25
+ export default function (file, output, baseName, options) {
26
26
  function getRelativeFilePath(file) {
27
27
  const filePath = path.resolve(file)
28
28
  const rootPath = options.root ? path.resolve(options.root) : path.dirname(file)
@@ -53,7 +53,7 @@ export default function (file, output, options) {
53
53
  arrayBuffer,
54
54
  '',
55
55
  (gltf) => {
56
- const raw = parse(filePath, gltf, options)
56
+ const raw = parse(filePath, baseName, gltf, options)
57
57
  try {
58
58
  const prettiered = prettier.format(raw, {
59
59
  semi: false,
@@ -1,7 +1,7 @@
1
1
  import THREE from 'three'
2
2
  import isVarName from './isVarName.js'
3
3
 
4
- function parse(fileName, gltf, options = {}) {
4
+ function parse(fileName, baseName, gltf, options = {}) {
5
5
  const url = (fileName.toLowerCase().startsWith('http') ? '' : '/') + fileName
6
6
  const animations = gltf.animations
7
7
  const hasAnimations = animations.length > 0
@@ -467,6 +467,22 @@ function parse(fileName, gltf, options = {}) {
467
467
  }
468
468
  : undefined
469
469
 
470
+ const imports = `
471
+ ${options.types ? `\nimport type * as THREE from 'three'` : ''}
472
+ import { Group } from 'three'
473
+ import { ${[
474
+ 'T',
475
+ options.types && !options.isolated ? 'type Props, type Events, type Slots' : ''
476
+ ].join(', ')} } from '@threlte/core'
477
+ import { ${['useGltf', hasAnimations ? 'useGltfAnimations' : ''].join(
478
+ ', '
479
+ )} } from '@threlte/extras'
480
+ `
481
+
482
+ const useGltf = `useGltf${options.types ? '<GLTFResult>' : ''}('${url}'${
483
+ useGltfOptions ? `, ${JSON.stringify(useGltfOptions)}` : ''
484
+ })`
485
+
470
486
  // Output
471
487
  return `
472
488
  <!--
@@ -477,29 +493,41 @@ ${
477
493
  }
478
494
  ${parseExtras(gltf.parser.json.asset && gltf.parser.json.asset.extras)}-->
479
495
 
480
- <script${options.types ? ' lang="ts"' : ''}>
496
+ ${
497
+ options.preload
498
+ ? `
481
499
 
500
+ <script context="module"${options.types ? ' lang="ts"' : ''}>
501
+ ${imports}
482
502
 
483
- ${options.types ? `\nimport type * as THREE from 'three'` : ''}
484
- import { Group } from 'three'
485
- import { ${['T', options.types ? 'type Props, type Events, type Slots' : ''].join(
486
- ', '
487
- )} } from '@threlte/core'
488
- import { ${['useGltf', hasAnimations ? 'useGltfAnimations' : ''].join(
489
- ', '
490
- )} } from '@threlte/extras'
503
+ ${options.types ? printThrelteTypes(objects, animations) : ''}
504
+
505
+ const load = () => {
506
+ return ${useGltf}
507
+ }
508
+
509
+ export const preload${baseName} = async () => {
510
+ await load()
511
+ }
512
+ </script>
513
+ `
514
+ : ''
515
+ }
491
516
 
492
- ${options.types ? 'type $$Props = Props<THREE.Group>' : ''}
493
- ${options.types ? 'type $$Events = Events<THREE.Group>' : ''}
494
- ${options.types ? 'type $$Slots = Slots<THREE.Group>' : ''}
517
+
518
+ <script${options.types ? ' lang="ts"' : ''}>
519
+
520
+ ${!options.preload ? imports : ''}
521
+
522
+ ${options.types && !options.isolated ? 'type $$Props = Props<THREE.Group>' : ''}
523
+ ${options.types && !options.isolated ? 'type $$Events = Events<THREE.Group>' : ''}
524
+ ${options.types && !options.isolated ? 'type $$Slots = Slots<THREE.Group>' : ''}
495
525
 
496
526
  export const ref = new Group()
497
527
 
498
- ${options.types ? printThrelteTypes(objects, animations) : ''}
528
+ ${options.types && !options.preload ? printThrelteTypes(objects, animations) : ''}
499
529
 
500
- const gltf = useGltf${options.types ? '<GLTFResult>' : ''}('${url}'${
501
- useGltfOptions ? `, ${JSON.stringify(useGltfOptions)}` : ''
502
- })
530
+ ${!options.preload ? `const gltf = ${useGltf}` : 'const gltf = load()'}
503
531
  ${
504
532
  hasAnimations
505
533
  ? `export const { actions, mixer } = useGltfAnimations${
@@ -511,7 +539,7 @@ ${parseExtras(gltf.parser.json.asset && gltf.parser.json.asset.extras)}-->
511
539
  </script>
512
540
 
513
541
  {#if $gltf}
514
- <T is={ref} {...$$restProps}>
542
+ <T is={ref} dispose={false} ${!options.isolated ? '{...$$restProps}' : ''}>
515
543
  ${scene}
516
544
 
517
545
  <slot {ref} />