lunchboxjs 0.2.1019 → 2.0.0-beta.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.
Files changed (38) hide show
  1. package/dist/lunchboxjs.es.d.ts +1 -1
  2. package/dist/lunchboxjs.js +6 -5
  3. package/dist/lunchboxjs.min.js +1 -1
  4. package/dist/lunchboxjs.module.js +6 -5
  5. package/dist/lunchboxjs.umd.d.ts +1 -1
  6. package/package.json +37 -82
  7. package/LICENSE.md +0 -7
  8. package/README.md +0 -17
  9. package/src/components/LunchboxEventHandlers.tsx +0 -237
  10. package/src/components/LunchboxWrapper/LunchboxScene.tsx +0 -8
  11. package/src/components/LunchboxWrapper/LunchboxWrapper.tsx +0 -341
  12. package/src/components/LunchboxWrapper/prepCanvas.ts +0 -55
  13. package/src/components/LunchboxWrapper/resizeCanvas.ts +0 -41
  14. package/src/components/autoGeneratedComponents.ts +0 -175
  15. package/src/components/index.ts +0 -31
  16. package/src/core/createNode.ts +0 -71
  17. package/src/core/extend.ts +0 -25
  18. package/src/core/index.ts +0 -7
  19. package/src/core/instantiateThreeObject/index.ts +0 -37
  20. package/src/core/instantiateThreeObject/processProps.ts +0 -40
  21. package/src/core/interaction.ts +0 -55
  22. package/src/core/minidom.ts +0 -256
  23. package/src/core/update.ts +0 -149
  24. package/src/core/updateObjectProp.ts +0 -153
  25. package/src/index.ts +0 -400
  26. package/src/keys.ts +0 -31
  27. package/src/nodeOps/createElement.ts +0 -34
  28. package/src/nodeOps/index.ts +0 -83
  29. package/src/nodeOps/insert.ts +0 -165
  30. package/src/nodeOps/remove.ts +0 -32
  31. package/src/plugins/bridge/BridgeComponent.tsx +0 -60
  32. package/src/plugins/bridge/bridge.ts +0 -9
  33. package/src/types.ts +0 -186
  34. package/src/utils/find.ts +0 -24
  35. package/src/utils/get.ts +0 -18
  36. package/src/utils/index.ts +0 -60
  37. package/src/utils/isNumber.ts +0 -87
  38. package/src/utils/set.ts +0 -14
@@ -1,341 +0,0 @@
1
- import {
2
- computed,
3
- defineComponent,
4
- onBeforeUnmount,
5
- onMounted,
6
- PropType,
7
- reactive,
8
- ref,
9
- watch,
10
- WatchSource,
11
- } from 'vue'
12
- import { cancelUpdate, cancelUpdateSource, MiniDom, update } from '../../core'
13
- import { Lunch, useApp, useGlobals, useLunchboxInteractables } from '../..'
14
- import * as THREE from 'three'
15
- import { prepCanvas } from './prepCanvas'
16
- import { useUpdateGlobals, useStartCallbacks } from '../..'
17
- import { LunchboxScene } from './LunchboxScene'
18
- import { LunchboxEventHandlers } from '../LunchboxEventHandlers'
19
- import * as Keys from '../../keys'
20
- import { waitFor } from '../../utils'
21
-
22
- /** fixed & fill styling for container */
23
- const fillStyle = (position: string) => {
24
- return {
25
- position,
26
- top: 0,
27
- right: 0,
28
- bottom: 0,
29
- left: 0,
30
- width: '100%',
31
- height: '100%',
32
- display: 'block',
33
- }
34
- }
35
-
36
- export const LunchboxWrapper = defineComponent({
37
- name: 'Lunchbox',
38
- props: {
39
- // These should match the Lunchbox.WrapperProps interface
40
- background: String,
41
- cameraArgs: Array,
42
- cameraLook: Array as unknown as PropType<Lunch.Vector3AsArray>,
43
- cameraLookAt: Array as unknown as PropType<Lunch.Vector3AsArray>,
44
- cameraPosition: Array as unknown as PropType<Lunch.Vector3AsArray>,
45
- dpr: Number,
46
- ortho: Boolean,
47
- orthographic: Boolean,
48
- r3f: Boolean,
49
- rendererArguments: Object,
50
- rendererProperties: Object,
51
- sizePolicy: String as PropType<Lunch.SizePolicy>,
52
- shadow: [Boolean, Object],
53
- transparent: Boolean,
54
- zoom: Number,
55
- updateSource: Object as PropType<WatchSource>,
56
- },
57
- setup(props, context) {
58
- const canvas = ref<MiniDom.RendererDomNode>()
59
- let dpr = props.dpr ?? -1
60
- const container = ref<MiniDom.RendererDomNode>()
61
- const renderer = ref<Lunch.LunchboxComponent<THREE.Renderer>>()
62
- const camera = ref<Lunch.LunchboxComponent<THREE.Camera>>()
63
- const scene = ref<Lunch.LunchboxComponent<THREE.Scene>>()
64
- const globals = useGlobals()
65
- const updateGlobals = useUpdateGlobals()
66
- const app = useApp()
67
- const consolidatedCameraProperties: Record<string, any> = reactive({})
68
- const startCallbacks = useStartCallbacks()
69
-
70
- // https://threejs.org/docs/index.html#manual/en/introduction/Color-management
71
- if (props.r3f && (THREE as any)?.ColorManagement) {
72
- ;(THREE as any).ColorManagement.legacyMode = false
73
- }
74
-
75
- const interactables = useLunchboxInteractables()
76
-
77
- // MOUNT
78
- // ====================
79
- onMounted(async () => {
80
- // canvas needs to exist (or user needs to handle it on their own)
81
- if (!canvas.value && !context.slots?.renderer?.()?.length)
82
- throw new Error('missing canvas')
83
-
84
- // no camera provided, so let's create one
85
- if (!context.slots?.camera?.()?.length) {
86
- if (props.cameraPosition) {
87
- consolidatedCameraProperties.position = props.cameraPosition
88
- }
89
- if (props.cameraLook || props.cameraLookAt) {
90
- consolidatedCameraProperties.lookAt =
91
- props.cameraLook || props.cameraLookAt
92
- }
93
- if (props.zoom !== undefined) {
94
- consolidatedCameraProperties.zoom = props.zoom
95
- }
96
- }
97
-
98
- // SCENE
99
- // ====================
100
- // set background color
101
- if (scene.value?.$el?.instance && props.background) {
102
- scene.value.$el.instance.background = new THREE.Color(
103
- props.background
104
- )
105
- }
106
-
107
- // MISC PROPERTIES
108
- // ====================
109
- if (dpr === -1) {
110
- dpr = window.devicePixelRatio
111
- }
112
- updateGlobals?.({ dpr })
113
-
114
- while (
115
- !renderer.value?.$el?.instance &&
116
- // TODO: remove `as any`
117
- !(renderer.value as any)?.component?.ctx.$el?.instance
118
- ) {
119
- await new Promise((r) => requestAnimationFrame(r))
120
- }
121
-
122
- while (
123
- !scene.value?.$el?.instance &&
124
- // TODO: remove `as any`
125
- !(scene.value as any)?.component?.ctx.$el?.instance
126
- ) {
127
- await new Promise((r) => requestAnimationFrame(r))
128
- }
129
-
130
- const normalizedRenderer = (renderer.value?.$el?.instance ??
131
- (renderer.value as any)?.component?.ctx.$el
132
- ?.instance) as THREE.WebGLRenderer
133
-
134
- normalizedRenderer.setPixelRatio(globals.dpr)
135
-
136
- const normalizedScene = (scene.value?.$el?.instance ??
137
- (scene.value as any)?.component?.ctx.$el
138
- ?.instance) as THREE.Scene
139
-
140
- const normalizedCamera = (camera.value?.$el?.instance ??
141
- (camera.value as any)?.component?.ctx.$el
142
- ?.instance) as THREE.Camera
143
-
144
- // TODO: update DPR on monitor switch
145
- // prep canvas (sizing, observe, unmount, etc)
146
- // (only run if no custom renderer)
147
- if (!context.slots?.renderer?.()?.length) {
148
- // TODO: use dispose
149
- const { dispose } = prepCanvas(
150
- container,
151
- normalizedCamera,
152
- normalizedRenderer,
153
- normalizedScene,
154
- props.sizePolicy
155
- )
156
-
157
- if (props.r3f) {
158
- normalizedRenderer.outputEncoding = THREE.sRGBEncoding
159
- normalizedRenderer.toneMapping = THREE.ACESFilmicToneMapping
160
- }
161
-
162
- // update render sugar
163
- const sugar = {
164
- shadow: props.shadow,
165
- }
166
- if (sugar?.shadow) {
167
- normalizedRenderer.shadowMap.enabled = true
168
- if (typeof sugar.shadow === 'object') {
169
- normalizedRenderer.shadowMap.type = sugar.shadow.type
170
- }
171
- }
172
- }
173
-
174
- // START
175
- // ====================
176
- if (!app) {
177
- throw new Error('error creating app')
178
- }
179
-
180
- // save renderer, scene, camera
181
- app.config.globalProperties.lunchbox.camera = normalizedCamera
182
- app.config.globalProperties.lunchbox.renderer = normalizedRenderer
183
- app.config.globalProperties.lunchbox.scene = normalizedScene
184
-
185
- for (let startCallback of startCallbacks ?? []) {
186
- startCallback({
187
- app,
188
- camera: normalizedCamera,
189
- renderer: normalizedRenderer,
190
- scene: normalizedScene,
191
- })
192
- }
193
-
194
- // KICK UPDATE
195
- // ====================
196
- update({
197
- app,
198
- camera: normalizedCamera,
199
- renderer: normalizedRenderer,
200
- scene: normalizedScene,
201
- updateSource: props.updateSource,
202
- })
203
- })
204
-
205
- // UNMOUNT
206
- // ====================
207
- onBeforeUnmount(() => {
208
- cancelUpdate()
209
- cancelUpdateSource()
210
- })
211
-
212
- // RENDER FUNCTION
213
- // ====================
214
- const containerFillStyle =
215
- props.sizePolicy === 'container' ? 'static' : 'absolute'
216
- const canvasFillStyle =
217
- props.sizePolicy === 'container' ? 'static' : 'fixed'
218
-
219
- // REACTIVE CUSTOM CAMERAS
220
- // ====================
221
- // find first camera with `type.name` property
222
- // (which indicates a Lunch.Node)
223
- const activeCamera = computed(() => {
224
- const output = context.slots
225
- ?.camera?.()
226
- .find((c) => (c.type as any)?.name)
227
- if (output) {
228
- return output
229
- }
230
-
231
- return output
232
- })
233
-
234
- // TODO: make custom cameras reactive
235
- watch(
236
- activeCamera,
237
- async (newVal, oldVal) => {
238
- // console.log('got camera', newVal)
239
- if (newVal && newVal?.props?.key !== oldVal?.props?.key) {
240
- // TODO: remove cast
241
- camera.value = newVal as any
242
-
243
- // TODO: why isn't this updating app camera?
244
- // const el = await waitFor(() => newVal.el)
245
- // console.log(el)
246
- // camera.value = el
247
- // console.log(newVal.uuid)
248
- // updateGlobals?.({ camera: el })
249
- }
250
- },
251
- { immediate: true }
252
- )
253
-
254
- // RENDER FUNCTION
255
- // ====================
256
- return () => (
257
- <>
258
- {/* use renderer slot if provided... */}
259
- {context.slots?.renderer?.()?.length ? (
260
- // TODO: remove `as any` cast
261
- (renderer.value = context.slots?.renderer?.()[0] as any)
262
- ) : (
263
- // ...otherwise, add canvas...
264
- <>
265
- <div
266
- class="lunchbox-container"
267
- style={fillStyle(containerFillStyle) as any}
268
- ref={container}
269
- data-lunchbox="true"
270
- >
271
- <canvas
272
- ref={canvas}
273
- class="lunchbox-canvas"
274
- style={fillStyle(canvasFillStyle) as any}
275
- data-lunchbox="true"
276
- ></canvas>
277
- </div>
278
- {/* ...and create default renderer once canvas is present */}
279
- {canvas.value?.domElement && (
280
- <webGLRenderer
281
- {...(props.rendererProperties ?? {})}
282
- ref={renderer}
283
- args={[
284
- {
285
- alpha: props.transparent,
286
- antialias: true,
287
- canvas: canvas.value?.domElement,
288
- powerPreference: !!props.r3f
289
- ? 'high-performance'
290
- : 'default',
291
- ...(props.rendererArguments ?? {}),
292
- },
293
- ]}
294
- />
295
- )}
296
- </>
297
- )}
298
-
299
- {/* use scene slot if provided... */}
300
- {context.slots?.scene?.()?.length ? (
301
- // TODO: remove `as any` cast
302
- (scene.value = context.slots?.scene?.()[0] as any)
303
- ) : (
304
- // ...otherwise, add default scene
305
- // TODO: why does this need to be a separate component? <scene> throws an error
306
- <LunchboxScene ref={scene}>
307
- {context.slots?.default?.()}
308
- </LunchboxScene>
309
- )}
310
-
311
- {/* use camera slot if provided... */}
312
- {context.slots?.camera?.()?.length ? (
313
- // TODO: remove `any` cast
314
- camera.value
315
- ) : props.ortho || props.orthographic ? (
316
- <orthographicCamera
317
- ref={camera}
318
- args={props.cameraArgs ?? []}
319
- {...consolidatedCameraProperties}
320
- />
321
- ) : (
322
- <perspectiveCamera
323
- ref={camera}
324
- args={
325
- props.cameraArgs ?? [
326
- props.r3f ? 75 : 45,
327
- 0.5625,
328
- 1,
329
- 1000,
330
- ]
331
- }
332
- {...consolidatedCameraProperties}
333
- />
334
- )}
335
-
336
- {/* Lunchbox interaction handlers */}
337
- {interactables?.value.length && <LunchboxEventHandlers />}
338
- </>
339
- )
340
- },
341
- })
@@ -1,55 +0,0 @@
1
- import { MiniDom } from '../../core'
2
- import { Lunch } from '../../types'
3
- import { Ref } from 'vue'
4
- import { resizeCanvas } from './resizeCanvas'
5
-
6
- const getInnerDimensions = (node: Element) => {
7
- const computedStyle = getComputedStyle(node)
8
- const width =
9
- node.clientWidth -
10
- parseFloat(computedStyle.paddingLeft) -
11
- parseFloat(computedStyle.paddingRight)
12
- const height =
13
- node.clientHeight -
14
- parseFloat(computedStyle.paddingTop) -
15
- parseFloat(computedStyle.paddingBottom)
16
- return { width, height }
17
- }
18
-
19
- export const prepCanvas = (
20
- container: Ref<MiniDom.RendererDomNode | undefined>,
21
- camera: THREE.Camera,
22
- renderer: THREE.Renderer,
23
- scene: THREE.Scene,
24
- sizePolicy?: Lunch.SizePolicy
25
- ) => {
26
- const containerElement = container.value?.domElement
27
- if (!containerElement) throw new Error('missing container')
28
-
29
- // save and size element
30
- const resizeCanvasByPolicy = () => {
31
- if (sizePolicy === 'container') {
32
- const dims = getInnerDimensions(containerElement)
33
- resizeCanvas(camera, renderer, scene, dims.width, dims.height)
34
- } else resizeCanvas(camera, renderer, scene)
35
- }
36
- resizeCanvasByPolicy()
37
-
38
- // attach listeners
39
- let observer = new ResizeObserver(() => {
40
- resizeCanvasByPolicy()
41
- })
42
- // window.addEventListener('resize', resizeCanvas)
43
- if (containerElement) {
44
- observer.observe(containerElement)
45
- }
46
-
47
- // cleanup
48
- return {
49
- dispose() {
50
- if (containerElement) {
51
- observer.unobserve(containerElement)
52
- }
53
- },
54
- }
55
- }
@@ -1,41 +0,0 @@
1
- import { toRaw } from 'vue'
2
-
3
- export const resizeCanvas = (
4
- camera: THREE.Camera,
5
- renderer: THREE.Renderer,
6
- scene: THREE.Scene,
7
- width?: number,
8
- height?: number
9
- ) => {
10
- // ignore if no element
11
- if (!renderer?.domElement || !scene || !camera) return
12
-
13
- width = width ?? window.innerWidth
14
- height = height ?? window.innerHeight
15
-
16
- // update camera
17
- const aspect = width / height
18
- if (camera.type?.toLowerCase() === 'perspectivecamera') {
19
- const perspectiveCamera = camera as THREE.PerspectiveCamera
20
- perspectiveCamera.aspect = aspect
21
- perspectiveCamera.updateProjectionMatrix()
22
- } else if (camera.type?.toLowerCase() === 'orthographiccamera') {
23
- // TODO: ortho camera update
24
- const orthoCamera = camera as THREE.OrthographicCamera
25
- const heightInTermsOfWidth = height / width
26
- orthoCamera.top = heightInTermsOfWidth * 10
27
- orthoCamera.bottom = -heightInTermsOfWidth * 10
28
- orthoCamera.right = 10
29
- orthoCamera.left = -10
30
- orthoCamera.updateProjectionMatrix()
31
- } else {
32
- // TODO: non-ortho or perspective camera
33
- }
34
-
35
- // update canvas
36
- renderer.setSize(width, height)
37
- // render immediately so there's no flicker
38
- if (scene && camera) {
39
- renderer.render(toRaw(scene), toRaw(camera))
40
- }
41
- }
@@ -1,175 +0,0 @@
1
- // list of all components to register out of the box
2
- export const autoGeneratedComponents = [
3
- // ThreeJS basics
4
- 'mesh',
5
- 'instancedMesh',
6
- 'scene',
7
- 'sprite',
8
- 'object3D',
9
-
10
- // geometry
11
- 'instancedBufferGeometry',
12
- 'bufferGeometry',
13
- 'boxBufferGeometry',
14
- 'circleBufferGeometry',
15
- 'coneBufferGeometry',
16
- 'cylinderBufferGeometry',
17
- 'dodecahedronBufferGeometry',
18
- 'extrudeBufferGeometry',
19
- 'icosahedronBufferGeometry',
20
- 'latheBufferGeometry',
21
- 'octahedronBufferGeometry',
22
- 'parametricBufferGeometry',
23
- 'planeBufferGeometry',
24
- 'polyhedronBufferGeometry',
25
- 'ringBufferGeometry',
26
- 'shapeBufferGeometry',
27
- 'sphereBufferGeometry',
28
- 'tetrahedronBufferGeometry',
29
- 'textBufferGeometry',
30
- 'torusBufferGeometry',
31
- 'torusKnotBufferGeometry',
32
- 'tubeBufferGeometry',
33
- 'wireframeGeometry',
34
- 'parametricGeometry',
35
- 'tetrahedronGeometry',
36
- 'octahedronGeometry',
37
- 'icosahedronGeometry',
38
- 'dodecahedronGeometry',
39
- 'polyhedronGeometry',
40
- 'tubeGeometry',
41
- 'torusKnotGeometry',
42
- 'torusGeometry',
43
- // textgeometry has been moved to /examples/jsm/geometries/TextGeometry
44
- // 'textGeometry',
45
- 'sphereGeometry',
46
- 'ringGeometry',
47
- 'planeGeometry',
48
- 'latheGeometry',
49
- 'shapeGeometry',
50
- 'extrudeGeometry',
51
- 'edgesGeometry',
52
- 'coneGeometry',
53
- 'cylinderGeometry',
54
- 'circleGeometry',
55
- 'boxGeometry',
56
-
57
- // materials
58
- 'material',
59
- 'shadowMaterial',
60
- 'spriteMaterial',
61
- 'rawShaderMaterial',
62
- 'shaderMaterial',
63
- 'pointsMaterial',
64
- 'meshPhysicalMaterial',
65
- 'meshStandardMaterial',
66
- 'meshPhongMaterial',
67
- 'meshToonMaterial',
68
- 'meshNormalMaterial',
69
- 'meshLambertMaterial',
70
- 'meshDepthMaterial',
71
- 'meshDistanceMaterial',
72
- 'meshBasicMaterial',
73
- 'meshMatcapMaterial',
74
- 'lineDashedMaterial',
75
- 'lineBasicMaterial',
76
-
77
- // lights
78
- 'light',
79
- 'spotLightShadow',
80
- 'spotLight',
81
- 'pointLight',
82
- 'rectAreaLight',
83
- 'hemisphereLight',
84
- 'directionalLightShadow',
85
- 'directionalLight',
86
- 'ambientLight',
87
- 'lightShadow',
88
- 'ambientLightProbe',
89
- 'hemisphereLightProbe',
90
- 'lightProbe',
91
-
92
- // textures
93
- 'texture',
94
- 'videoTexture',
95
- 'dataTexture',
96
- 'dataTexture3D',
97
- 'compressedTexture',
98
- 'cubeTexture',
99
- 'canvasTexture',
100
- 'depthTexture',
101
-
102
- // Texture loaders
103
- 'textureLoader',
104
-
105
- // misc
106
- 'group',
107
- 'catmullRomCurve3',
108
- 'points',
109
- 'raycaster',
110
-
111
- // helpers
112
- 'cameraHelper',
113
-
114
- // cameras
115
- 'camera',
116
- 'perspectiveCamera',
117
- 'orthographicCamera',
118
- 'cubeCamera',
119
- 'arrayCamera',
120
-
121
- // renderers
122
- 'webGLRenderer',
123
-
124
- /*
125
- // List copied from r3f:
126
- // https://github.com/pmndrs/react-three-fiber/blob/master/packages/fiber/src/three-types.ts
127
-
128
- // NOT IMPLEMENTED (can be added via Extend - docs.lunchboxjs.com/components/extend/):
129
- audioListener: AudioListenerProps
130
- positionalAudio: PositionalAudioProps
131
-
132
- lOD: LODProps
133
- skinnedMesh: SkinnedMeshProps
134
- skeleton: SkeletonProps
135
- bone: BoneProps
136
- lineSegments: LineSegmentsProps
137
- lineLoop: LineLoopProps
138
- // see `audio`
139
- // line: LineProps
140
- immediateRenderObject: ImmediateRenderObjectProps
141
-
142
- // primitive
143
- primitive: PrimitiveProps
144
-
145
- // helpers
146
- spotLightHelper: SpotLightHelperProps
147
- skeletonHelper: SkeletonHelperProps
148
- pointLightHelper: PointLightHelperProps
149
- hemisphereLightHelper: HemisphereLightHelperProps
150
- gridHelper: GridHelperProps
151
- polarGridHelper: PolarGridHelperProps
152
- directionalLightHelper: DirectionalLightHelperProps
153
- boxHelper: BoxHelperProps
154
- box3Helper: Box3HelperProps
155
- planeHelper: PlaneHelperProps
156
- arrowHelper: ArrowHelperProps
157
- axesHelper: AxesHelperProps
158
-
159
-
160
- // misc
161
- vector2: Vector2Props
162
- vector3: Vector3Props
163
- vector4: Vector4Props
164
- euler: EulerProps
165
- matrix3: Matrix3Props
166
- matrix4: Matrix4Props
167
- quaternion: QuaternionProps
168
- bufferAttribute: BufferAttributeProps
169
- instancedBufferAttribute: InstancedBufferAttributeProps
170
- color: ColorProps
171
- fog: FogProps
172
- fogExp2: FogExp2Props
173
- shape: ShapeProps
174
- */
175
- ]
@@ -1,31 +0,0 @@
1
- import { h, defineComponent } from 'vue'
2
- import { LunchboxWrapper } from './LunchboxWrapper/LunchboxWrapper'
3
- import { autoGeneratedComponents } from './autoGeneratedComponents'
4
- import type { Lunch } from '../types'
5
-
6
- export const catalogue: Lunch.Catalogue = {}
7
-
8
- // component creation utility
9
- const createComponent = (tag: string) =>
10
- defineComponent({
11
- inheritAttrs: false,
12
- name: tag,
13
- setup(props, context) {
14
- return () => {
15
- return h(tag, context.attrs, context.slots?.default?.() || [])
16
- }
17
- },
18
- })
19
-
20
- // turn components into registered map
21
- const processed = autoGeneratedComponents
22
- .map(createComponent)
23
- .reduce((acc, curr) => {
24
- acc[curr.name] = curr
25
- return acc
26
- }, {} as Record<string, ReturnType<typeof defineComponent>>)
27
-
28
- export const components = {
29
- ...processed,
30
- Lunchbox: LunchboxWrapper,
31
- }