Skip to content

Commit d3b0fc9

Browse files
author
0x088730
committed
Implement $$Generic Material type and props
This is possible now - cool! see sveltejs/language-tools#442 (comment) 'mat' shorthand attribute will give us proper intellisense (props list) for the assigned 'material'!
1 parent 6324623 commit d3b0fc9

File tree

2 files changed

+43
-40
lines changed

2 files changed

+43
-40
lines changed

src/components/Mesh.svelte

+21-20
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ This is a **svelthree** _Mesh_ Component.
4242
SvelthreeInteractionVRHands
4343
} from "../components-internal"
4444
import { XRDefaults } from "../constants"
45-
import type { OnlyWritableNonFunctionPropsPlus, PropBlackList, SvelthreeAnimationFunction } from "../types-extra"
45+
import type {
46+
OnlyWritableNonFunctionPropsPlus,
47+
OnlyWritableNonFunctionProps,
48+
PropBlackList,
49+
SvelthreeAnimationFunction
50+
} from "../types-extra"
4651
import { svelthreeStores } from "../stores"
4752
import { PropUtils, StoreUtils, SvelthreeProps } from "../utils"
4853
import type { XrSessionVRInputType } from "../xr/types-svelthree"
@@ -112,7 +117,14 @@ This is a **svelthree** _Mesh_ Component.
112117
let generate = false
113118
export let mesh: Mesh = undefined
114119
115-
export let material: Material | Material[] = undefined
120+
// Generic Material type and props
121+
// COOL! This is possible now! see https://github.com/sveltejs/language-tools/issues/442#issuecomment-977803507
122+
// 'mat' shorthand attribute will give us proper intellisense (props list) for the assigned 'material'!
123+
// TODO MULTIPLE MATERIALS: this works only with single Material atm, multiple Materials are not implemented yet.
124+
type AnyMaterial = $$Generic<Material | Material[]>
125+
type AnyMaterialProps = OnlyWritableNonFunctionProps<Omit<AnyMaterial, PropBlackList>>
126+
127+
export let material: AnyMaterial = undefined
116128
export let geometry: BufferGeometry = undefined
117129
118130
if (mesh) {
@@ -130,7 +142,7 @@ This is a **svelthree** _Mesh_ Component.
130142
}
131143
132144
if (mesh.material) {
133-
material = mesh.material
145+
material = mesh.material as AnyMaterial
134146
} else {
135147
console.warn("SVELTHREE > Mesh : Mesh provided, but has no material!", { mesh })
136148
}
@@ -227,22 +239,6 @@ This is a **svelthree** _Mesh_ Component.
227239
}
228240
}
229241
230-
/*
231-
TODO `mat` shorthand attribute:
232-
233-
Recommended (workaround / see below): Assign a typed object for correct code completion / list of available properties, for example:
234-
235-
```javascript
236-
const meshStdMatProps: MeshStandardMaterialParameters = { ... }
237-
<Mesh mat={meshStdMatProps} />
238-
```
239-
It would be nice if we could get correct code completion of available properties based on the `material` attribute value / specified Material
240-
Unfortunately this doesn't seem to be possible atm due to [Svelte language-tools limitations](https://github.com/sveltejs/language-tools/issues/442). 🤔
241-
Any help / hints concerning this issue are very welcome! 👍
242-
*/
243-
244-
/** Shorthand attribute for specifying / mutating **Material properties**. */
245-
246242
export let mau: boolean = undefined
247243
248244
$: if (mesh) {
@@ -275,7 +271,12 @@ This is a **svelthree** _Mesh_ Component.
275271
276272
let sMat: SvelthreeProps
277273
$: !sMat && material ? (sMat = new SvelthreeProps(material)) : null
278-
export let mat: { [key: string]: any } = undefined
274+
275+
// Generic Material props
276+
// COOL! This works now! 'mat' shorthand attribute will give us proper intellisense (props list) for the assigned 'material'!
277+
// TODO MULTIPLE MATERIALS: this works only with single Material atm, multiple Materials are not implemented yet.
278+
export let mat: { [P in keyof AnyMaterialProps]: AnyMaterialProps[P] } = undefined
279+
279280
$: mat && sMat ? sMat.update(mat) : null
280281
281282
// reactive updating props

src/components/Points.svelte

+22-20
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ This is a **svelthree** _Points_ Component.
5353
SvelthreeInteractionVRHands
5454
} from "../components-internal"
5555
import { XRDefaults } from "../constants"
56-
import type { OnlyWritableNonFunctionPropsPlus, PropBlackList, SvelthreeAnimationFunction } from "../types-extra"
56+
import type {
57+
OnlyWritableNonFunctionProps,
58+
OnlyWritableNonFunctionPropsPlus,
59+
PropBlackList,
60+
SvelthreeAnimationFunction
61+
} from "../types-extra"
5762
import { svelthreeStores } from "../stores"
5863
import { PropUtils, StoreUtils, SvelthreeProps } from "../utils"
5964
import type { XrSessionVRInputType } from "../xr/types-svelthree"
@@ -123,7 +128,15 @@ This is a **svelthree** _Points_ Component.
123128
let generate = false
124129
export let points: Points = undefined
125130
126-
export let material: Material | Material[] | PointsMaterial = undefined
131+
// Generic Material type and props
132+
// COOL! This is possible now! see https://github.com/sveltejs/language-tools/issues/442#issuecomment-977803507
133+
// 'mat' shorthand attribute will give us proper intellisense (props list) for the assigned 'material'!
134+
// TODO MULTIPLE MATERIALS: this works only with single Material atm, multiple Materials are not implemented yet.
135+
// TODO Is the Material | Material[] usage correct / possible with Points? -> need to cleanup / test this component!
136+
type AnyMaterial = $$Generic<Material | Material[] | PointsMaterial>
137+
type AnyMaterialProps = OnlyWritableNonFunctionProps<Omit<AnyMaterial, PropBlackList>>
138+
139+
export let material: AnyMaterial = undefined
127140
export let geometry: BufferGeometry = undefined
128141
129142
if (points) {
@@ -141,7 +154,7 @@ This is a **svelthree** _Points_ Component.
141154
}
142155
143156
if (points.material) {
144-
material = points.material
157+
material = points.material as AnyMaterial
145158
} else {
146159
console.warn("SVELTHREE > Points : Points provided, but has no material!", { points })
147160
}
@@ -238,22 +251,6 @@ This is a **svelthree** _Points_ Component.
238251
}
239252
}
240253
241-
/*
242-
TODO `mat` shorthand attribute:
243-
244-
Recommended (workaround / see below): Assign a typed object for correct code completion / list of available properties, for example:
245-
246-
```javascript
247-
const pointsStdMatProps: PointsStandardMaterialParameters = { ... }
248-
<Points mat={pointsStdMatProps} />
249-
```
250-
It would be nice if we could get correct code completion of available properties based on the `material` attribute value / specified Material
251-
Unfortunately this doesn't seem to be possible atm due to [Svelte language-tools limitations](https://github.com/sveltejs/language-tools/issues/442). 🤔
252-
Any help / hints concerning this issue are very welcome! 👍
253-
*/
254-
255-
/** Shorthand attribute for specifying / mutating **Material properties**. */
256-
257254
export let mau: boolean = undefined
258255
259256
$: if (points) {
@@ -286,7 +283,12 @@ This is a **svelthree** _Points_ Component.
286283
287284
let sMat: SvelthreeProps
288285
$: !sMat && material ? (sMat = new SvelthreeProps(material)) : null
289-
export let mat: { [key: string]: any } = undefined
286+
287+
// Generic Material props
288+
// COOL! This works now! 'mat' shorthand attribute will give us proper intellisense (props list) for the assigned 'material'!
289+
// TODO MULTIPLE MATERIALS: this works only with single Material atm, multiple Materials are not implemented yet.
290+
export let mat: { [P in keyof AnyMaterialProps]: AnyMaterialProps[P] } = undefined
291+
290292
$: mat && sMat ? sMat.update(mat) : null
291293
292294
// reactive updating props

0 commit comments

Comments
 (0)