lunchboxjs 0.1.4001 → 0.1.4005

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/extras/Gltf.vue CHANGED
@@ -7,7 +7,7 @@
7
7
  <script lang="ts" setup>
8
8
  import { ref } from 'vue'
9
9
  import { GLTF } from 'three/examples/jsm/loaders/GLTFLoader'
10
- import { Lunchbox } from '../src'
10
+ import { Lunch } from '../src'
11
11
 
12
12
  const props = defineProps<{
13
13
  preventAdd?: boolean
@@ -20,7 +20,7 @@ const emit = defineEmits<{
20
20
  (e: 'error', evt: ErrorEvent): void
21
21
  }>()
22
22
 
23
- const container = ref<Lunchbox.LunchboxComponent<THREE.Group>>()
23
+ const container = ref<Lunch.LunchboxComponent<THREE.Group>>()
24
24
  const onLoad = (gltf: GLTF) => {
25
25
  if (!props.preventAdd) {
26
26
  container.value?.$el?.instance?.add(gltf.scene)
@@ -11,8 +11,8 @@
11
11
  </template>
12
12
 
13
13
  <script lang="ts" setup>
14
- import { computed, ref } from 'vue'
15
- import { onBeforeRender, globals, Lunchbox } from '../src'
14
+ import { computed, ref, watch } from 'vue'
15
+ import { onBeforeRender, globals, Lunch, camera, renderer } from '../src'
16
16
 
17
17
  // props
18
18
  const props = defineProps<{
@@ -20,18 +20,17 @@ const props = defineProps<{
20
20
  }>()
21
21
 
22
22
  // computed
23
- const camera = globals.camera
24
- const renderer = globals.renderer
23
+ // const camera = globals.camera
24
+ // const renderer = globals.renderer
25
25
  const ready = computed(() => {
26
- return camera.value !== null && renderer.value?.instance?.domElement
26
+ return camera.value !== null && renderer.value?.domElement
27
27
  })
28
- const orbitArgs = computed(() => [
29
- camera.value?.instance,
30
- renderer.value?.instance?.domElement,
31
- ])
28
+ const orbitArgs = computed(() => [camera.value, renderer.value?.domElement])
29
+ // watch(() => orbitArgs.value, console.log, { immediate: true })
30
+ // console.log(renderer)
32
31
 
33
32
  // update
34
- const controls = ref<Lunchbox.LunchboxComponent>()
33
+ const controls = ref<Lunch.LunchboxComponent>()
35
34
  const update = () => {
36
35
  const instance = controls.value?.$el.instance as any
37
36
  if (instance && ready.value) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lunchboxjs",
3
- "version": "0.1.4001",
3
+ "version": "0.1.4005",
4
4
  "scripts": {
5
5
  "dev": "vite -c utils/vite.config.ts",
6
6
  "build": "vue-tsc --noEmit && vite build -c utils/vite.config.ts",
@@ -8,7 +8,10 @@
8
8
  "build:rollup": "rollup -c ./utils/lib-rollup.ts",
9
9
  "build:dts": "cp utils/lib-dts.d.ts dist/lunchboxjs.es.d.ts && cp utils/lib-dts.d.ts dist/lunchboxjs.umd.d.ts",
10
10
  "build:lib": "npm run build:tsc && npm run build:rollup && npm run build:dts",
11
- "prepare": "npm run build:lib"
11
+ "prepare": "npm run build:lib",
12
+ "docs:dev": "vitepress dev docs",
13
+ "docs:build": "vitepress build docs",
14
+ "docs:serve": "vitepress serve docs"
12
15
  },
13
16
  "dependencies": {
14
17
  "lodash": "4.17.21",
@@ -22,11 +25,14 @@
22
25
  "@types/three": "0.133.0",
23
26
  "@types/uuid": "8.3.1",
24
27
  "@vitejs/plugin-vue": "^1.9.3",
28
+ "chroma-js": "2.1.2",
29
+ "nice-color-palettes": "3.0.0",
25
30
  "rollup-plugin-delete": "2.0.0",
26
31
  "rollup-plugin-terser": "7.0.2",
27
32
  "typescript": "^4.4.3",
28
33
  "vite": "^2.6.4",
29
34
  "vite-plugin-glsl": "0.0.5",
35
+ "vitepress": "0.20.1",
30
36
  "vue-tsc": "^0.3.0"
31
37
  },
32
38
  "peerDependencies": {
@@ -42,5 +48,20 @@
42
48
  ],
43
49
  "main": "./dist/lunchboxjs.js",
44
50
  "module": "./dist/lunchboxjs.module.js",
45
- "types": "dist/lunchboxjs.es.d.ts"
51
+ "types": "dist/lunchboxjs.es.d.ts",
52
+ "directories": {
53
+ "doc": "docs"
54
+ },
55
+ "repository": {
56
+ "type": "git",
57
+ "url": "git+https://github.com/breakfast-studio/lunchboxjs.git"
58
+ },
59
+ "keywords": [],
60
+ "author": "",
61
+ "license": "ISC",
62
+ "bugs": {
63
+ "url": "https://github.com/breakfast-studio/lunchboxjs/issues"
64
+ },
65
+ "homepage": "https://github.com/breakfast-studio/lunchboxjs#readme",
66
+ "description": ""
46
67
  }
@@ -5,19 +5,25 @@ import {
5
5
  onBeforeUnmount,
6
6
  onMounted,
7
7
  ref,
8
+ WritableComputedRef,
8
9
  } from 'vue'
9
10
  import {
11
+ cameraReady,
10
12
  cancelUpdate,
11
- ensureCamera,
13
+ createNode,
14
+ ensuredCamera,
12
15
  ensureRenderer,
13
- ensureScene,
16
+ ensuredScene,
17
+ fallbackCameraUuid,
14
18
  fallbackRendererUuid,
15
19
  MiniDom,
20
+ rendererReady,
21
+ tryGetNodeWithInstanceType,
16
22
  update,
17
23
  } from '../../core'
18
24
  import { set } from 'lodash'
19
25
  import { globals, Lunch } from '../..'
20
- import { Color } from 'three'
26
+ import { Color, Vector2 } from 'three'
21
27
  import { prepCanvas } from './prepCanvas'
22
28
 
23
29
  /** fixed & fill styling for container */
@@ -38,8 +44,13 @@ export const LunchboxWrapper: ComponentOptions = {
38
44
  props: {
39
45
  // These should match the Lunchbox.WrapperProps interface
40
46
  background: String,
47
+ cameraArgs: Array,
48
+ cameraLook: Array,
49
+ cameraLookAt: Array,
41
50
  cameraPosition: Array,
42
51
  dpr: Number,
52
+ ortho: Boolean,
53
+ orthographic: Boolean,
43
54
  rendererProperties: Object,
44
55
  shadow: [Boolean, Object],
45
56
  transparent: Boolean,
@@ -49,7 +60,8 @@ export const LunchboxWrapper: ComponentOptions = {
49
60
  const useFallbackRenderer = ref(true)
50
61
  const dpr = ref(props.dpr ?? -1)
51
62
  const container = ref<MiniDom.RendererDomNode>()
52
- let renderer: MiniDom.RendererStandardNode<THREE.WebGLRenderer>
63
+ let renderer: Lunch.Node<THREE.WebGLRenderer> | null
64
+ let camera: Lunch.Node<THREE.Camera> | null
53
65
  let scene: MiniDom.RendererStandardNode<THREE.Scene>
54
66
 
55
67
  // MOUNT
@@ -58,42 +70,121 @@ export const LunchboxWrapper: ComponentOptions = {
58
70
  // canvas needs to exist
59
71
  if (!canvas.value) throw new Error('missing canvas')
60
72
 
61
- // ensure camera
62
- const camera = ensureCamera().instance
63
- // move camera if needed
64
- if (camera && props.cameraPosition) {
65
- camera.position.set(...props.cameraPosition)
66
- }
67
-
68
73
  // RENDERER
69
74
  // ====================
70
- // build renderer args
71
- const rendererArgs: THREE.WebGLRendererParameters = {
72
- antialias: true,
73
- canvas: canvas.value.domElement,
74
- }
75
- if (props.transparent) {
76
- rendererArgs.alpha = true
77
- }
78
- const sugar = {
79
- shadow: props.shadow,
80
- }
81
- // ensure renderer
82
- renderer = ensureRenderer([rendererArgs], sugar)
83
- // set renderer props if needed
84
- if (props.rendererProperties) {
85
- Object.keys(props.rendererProperties).forEach((key) => {
86
- set(renderer, key, (props.rendererProperties as any)[key])
75
+ // is there already a renderer?
76
+ // TODO: allow other renderer types
77
+ renderer = tryGetNodeWithInstanceType([
78
+ 'WebGLRenderer',
79
+ ]) as unknown as Lunch.Node<THREE.WebGLRenderer> | null
80
+
81
+ // if renderer is missing, initialize with options
82
+ if (!renderer) {
83
+ // build renderer args
84
+ const rendererArgs: THREE.WebGLRendererParameters = {
85
+ antialias: true,
86
+ canvas: canvas.value.domElement,
87
+ }
88
+ if (props.transparent) {
89
+ rendererArgs.alpha = true
90
+ }
91
+
92
+ // create new renderer
93
+ ensureRenderer.value = createNode<THREE.WebGLRenderer>({
94
+ type: 'WebGLRenderer',
95
+ uuid: fallbackRendererUuid,
96
+ props: {
97
+ args: [rendererArgs],
98
+ },
87
99
  })
88
- }
89
- if (renderer.uuid !== fallbackRendererUuid) {
100
+
101
+ // we've initialized the renderer, so anything depending on it can execute now
102
+ rendererReady.value = true
103
+
104
+ const rendererAsWebGlRenderer =
105
+ ensureRenderer as WritableComputedRef<
106
+ Lunch.Node<THREE.WebGLRenderer>
107
+ >
108
+
109
+ // update render sugar
110
+ const sugar = {
111
+ shadow: props.shadow,
112
+ }
113
+ if (rendererAsWebGlRenderer.value.instance && sugar?.shadow) {
114
+ rendererAsWebGlRenderer.value.instance.shadowMap.enabled =
115
+ true
116
+ if (typeof sugar.shadow === 'object') {
117
+ rendererAsWebGlRenderer.value.instance.shadowMap.type =
118
+ sugar.shadow.type
119
+ }
120
+ }
121
+
122
+ // set renderer props if needed
123
+ if (props.rendererProperties) {
124
+ Object.keys(props.rendererProperties).forEach((key) => {
125
+ set(
126
+ rendererAsWebGlRenderer.value,
127
+ key,
128
+ (props.rendererProperties as any)[key]
129
+ )
130
+ })
131
+ }
132
+
133
+ // update using created renderer
134
+ renderer = rendererAsWebGlRenderer.value
135
+ } else {
90
136
  useFallbackRenderer.value = false
137
+ // the user has initialized the renderer, so anything depending
138
+ // on the renderer can execute
139
+ rendererReady.value = true
91
140
  return
92
141
  }
93
142
 
143
+ // CAMERA
144
+ // ====================
145
+ // is there already a camera?
146
+ camera = tryGetNodeWithInstanceType([
147
+ 'PerspectiveCamera',
148
+ 'OrthographicCamera',
149
+ ])
150
+ // if not, let's create one
151
+ if (!camera) {
152
+ // create ortho camera
153
+ if (props.ortho || props.orthographic) {
154
+ ensuredCamera.value = createNode<THREE.OrthographicCamera>({
155
+ props: { args: props.cameraArgs ?? [] },
156
+ type: 'OrthographicCamera',
157
+ uuid: fallbackCameraUuid,
158
+ })
159
+ } else {
160
+ ensuredCamera.value = createNode<THREE.PerspectiveCamera>({
161
+ props: {
162
+ args: props.cameraArgs ?? [45, 0.5625, 1, 1000],
163
+ },
164
+ type: 'PerspectiveCamera',
165
+ uuid: fallbackCameraUuid,
166
+ })
167
+ }
168
+
169
+ cameraReady.value = true
170
+
171
+ camera = ensuredCamera.value
172
+ } else {
173
+ cameraReady.value = true
174
+ }
175
+ // move camera if needed
176
+ if (camera && props.cameraPosition) {
177
+ camera.instance?.position.set(...props.cameraPosition)
178
+ }
179
+ // angle camera if needed
180
+ if (camera && (props.cameraLookAt || props.cameraLook)) {
181
+ const source = (props.cameraLookAt || props.cameraLook)!
182
+ camera.instance?.lookAt(...source)
183
+ }
184
+
94
185
  // SCENE
95
186
  // ====================
96
- scene = ensureScene()
187
+ scene = ensuredScene.value
97
188
  // set background color
98
189
  if (scene && scene.instance && props.background) {
99
190
  scene.instance.background = new Color(props.background)
@@ -105,7 +196,7 @@ export const LunchboxWrapper: ComponentOptions = {
105
196
  dpr.value = window.devicePixelRatio
106
197
  }
107
198
 
108
- if (renderer.instance) {
199
+ if (renderer?.instance) {
109
200
  renderer.instance.setPixelRatio(dpr.value)
110
201
  globals.dpr.value = dpr.value
111
202
  // prep canvas (sizing, observe, unmount, etc)
@@ -120,9 +211,10 @@ export const LunchboxWrapper: ComponentOptions = {
120
211
 
121
212
  // KICK UPDATE
122
213
  // ====================
214
+ // console.log(scene)
123
215
  update({
124
216
  app: getCurrentInstance()!.appContext.app as Lunch.App,
125
- camera,
217
+ camera: camera.instance,
126
218
  renderer: renderer.instance,
127
219
  scene: scene.instance,
128
220
  })
@@ -1,30 +1,40 @@
1
- import { ensureCamera, ensureRenderer, ensureScene } from '../../core'
1
+ import { ensuredCamera, ensureRenderer, ensuredScene } from '../../core'
2
+ import { toRaw } from 'vue'
2
3
 
3
4
  export const resizeCanvas = (width?: number, height?: number) => {
4
- const renderer = ensureRenderer().instance
5
- const scene = ensureScene().instance
5
+ const renderer = ensureRenderer.value?.instance
6
+ const scene = ensuredScene.value.instance
7
+ const camera = ensuredCamera.value
6
8
 
7
9
  // ignore if no element
8
- if (!renderer?.domElement || !scene) return
10
+ if (!renderer?.domElement || !scene || !camera) return
9
11
 
10
12
  width = width ?? window.innerWidth
11
13
  height = height ?? window.innerHeight
12
14
 
13
15
  // update camera
14
16
  const aspect = width / height
15
- const camera = ensureCamera()
16
17
  if (camera.type?.toLowerCase() === 'perspectivecamera') {
17
18
  const perspectiveCamera = camera.instance as THREE.PerspectiveCamera
18
19
  perspectiveCamera.aspect = aspect
19
20
  perspectiveCamera.updateProjectionMatrix()
21
+ } else if (camera.type?.toLowerCase() === 'orthographiccamera') {
22
+ // console.log('TODO: ortho camera update')
23
+ const orthoCamera = camera.instance as THREE.OrthographicCamera
24
+ const heightInTermsOfWidth = height / width
25
+ orthoCamera.top = heightInTermsOfWidth * 10
26
+ orthoCamera.bottom = -heightInTermsOfWidth * 10
27
+ orthoCamera.right = 10
28
+ orthoCamera.left = -10
29
+ orthoCamera.updateProjectionMatrix()
20
30
  } else {
21
- console.log('TODO: ortho camera update')
31
+ console.log('TODO: non-ortho or perspective camera')
22
32
  }
23
33
 
24
34
  // update canvas
25
35
  renderer.setSize(width, height)
26
36
  // render immediately so there's no flicker
27
37
  if (scene && camera.instance) {
28
- renderer.render(scene, camera.instance)
38
+ renderer.render(toRaw(scene), toRaw(camera.instance))
29
39
  }
30
40
  }
@@ -0,0 +1,175 @@
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
+
110
+ // helpers
111
+ 'cameraHelper',
112
+
113
+ // cameras
114
+ 'camera',
115
+ 'perspectiveCamera',
116
+ 'orthographicCamera',
117
+ 'cubeCamera',
118
+ 'arrayCamera',
119
+
120
+ // renderers
121
+ 'webGLRenderer',
122
+
123
+ /*
124
+ // List copied from r3f:
125
+ // https://github.com/pmndrs/react-three-fiber/blob/master/packages/fiber/src/three-types.ts
126
+
127
+ // NOT IMPLEMENTED (can be added via Extend - docs.lunchboxjs.com/components/extend/):
128
+ audioListener: AudioListenerProps
129
+ positionalAudio: PositionalAudioProps
130
+
131
+ lOD: LODProps
132
+ skinnedMesh: SkinnedMeshProps
133
+ skeleton: SkeletonProps
134
+ bone: BoneProps
135
+ lineSegments: LineSegmentsProps
136
+ lineLoop: LineLoopProps
137
+ // see `audio`
138
+ // line: LineProps
139
+ immediateRenderObject: ImmediateRenderObjectProps
140
+
141
+ // primitive
142
+ primitive: PrimitiveProps
143
+
144
+ // helpers
145
+ spotLightHelper: SpotLightHelperProps
146
+ skeletonHelper: SkeletonHelperProps
147
+ pointLightHelper: PointLightHelperProps
148
+ hemisphereLightHelper: HemisphereLightHelperProps
149
+ gridHelper: GridHelperProps
150
+ polarGridHelper: PolarGridHelperProps
151
+ directionalLightHelper: DirectionalLightHelperProps
152
+ boxHelper: BoxHelperProps
153
+ box3Helper: Box3HelperProps
154
+ planeHelper: PlaneHelperProps
155
+ arrowHelper: ArrowHelperProps
156
+ axesHelper: AxesHelperProps
157
+
158
+
159
+ // misc
160
+ raycaster: RaycasterProps
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
+ ]