-
-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathTresCanvas.vue
More file actions
302 lines (261 loc) · 8.13 KB
/
TresCanvas.vue
File metadata and controls
302 lines (261 loc) · 8.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<script setup lang="ts">
import {
ACESFilmicToneMapping,
PCFSoftShadowMap,
PerspectiveCamera,
Scene,
WebGLRenderer,
} from 'three'
import type { App, Ref } from 'vue'
import type { TresCamera, TresContextWithClock, TresObject, TresScene } from '../types/'
import type { PointerEvent } from '@pmndrs/pointer-events'
import * as THREE from 'three'
import {
createRenderer,
defineComponent,
Fragment,
getCurrentInstance,
h,
onMounted,
onUnmounted,
provide,
ref,
shallowRef,
toValue,
watch,
watchEffect,
} from 'vue'
import pkg from '../../package.json'
import type { RendererOptions, TresContext } from '../composables'
import { useTresContextProvider } from '../composables'
import { INJECTION_KEY as CONTEXT_INJECTION_KEY } from '../composables/useTresContextProvider'
import { extend } from '../core/catalogue'
import { nodeOps } from '../core/nodeOps'
import { disposeObject3D } from '../utils/'
import { registerTresDevtools } from '../devtools'
import type { TresPointerEventName } from '../utils/pointerEvents'
import { promiseTimeout } from '@vueuse/core'
const props = withDefaults(defineProps<TresCanvasProps>(), {
alpha: undefined,
depth: undefined,
shadows: undefined,
stencil: undefined,
antialias: true,
windowSize: undefined,
useLegacyLights: undefined,
preserveDrawingBuffer: undefined,
logarithmicDepthBuffer: undefined,
failIfMajorPerformanceCaveat: undefined,
renderMode: 'always',
clearColor: '#000000',
clearAlpha: 1,
enableProvideBridge: true,
toneMapping: ACESFilmicToneMapping,
shadowMapType: PCFSoftShadowMap,
})
const emit = defineEmits<TresCanvasEmits>()
const slots = defineSlots<{
default: () => any
}>()
const canvasRef = ref<HTMLCanvasElement>()
/*
`scene` is defined here and not in `useTresContextProvider` because the custom
renderer uses it to mount the app nodes. This happens before `useTresContextProvider` is called.
The custom renderer requires `scene` to be editable (not readonly).
*/
const scene = shallowRef<TresScene | Scene>(new Scene())
const instance = getCurrentInstance()
extend(THREE)
const createInternalComponent = (context: TresContext, empty = false) =>
defineComponent({
setup() {
const ctx = getCurrentInstance()?.appContext
if (ctx) { ctx.app = instance?.appContext.app as App }
const provides: { [key: string | symbol]: unknown } = {}
// Helper function to recursively merge provides from parents
function mergeProvides(currentInstance: any) {
if (!currentInstance) { return }
// Recursively process the parent instance
if (currentInstance.parent) {
mergeProvides(currentInstance.parent)
}
// Extract provides from the current instance and merge them
if (currentInstance.provides) {
Object.assign(provides, currentInstance.provides)
}
}
// Start the recursion from the initial instance
if (instance?.parent && props.enableProvideBridge) {
mergeProvides(instance.parent)
Reflect.ownKeys(provides)
.forEach((key) => {
provide(key, provides[key])
})
}
provide(CONTEXT_INJECTION_KEY, context)
provide('extend', extend)
if (typeof window !== 'undefined' && ctx?.app) {
registerTresDevtools(ctx?.app, context)
}
return () => h(Fragment, null, !empty ? slots.default() : [])
},
})
const mountCustomRenderer = (context: TresContext, empty = false) => {
const InternalComponent = createInternalComponent(context, empty)
const { render } = createRenderer(nodeOps(context))
render(h(InternalComponent), scene.value as unknown as TresObject)
}
const dispose = (context: TresContext, force = false) => {
disposeObject3D(context.scene.value as unknown as TresObject)
if (force) {
context.renderer.instance.dispose()
if (context.renderer.instance instanceof WebGLRenderer) {
context.renderer.instance.renderLists.dispose()
context.renderer.instance.forceContextLoss()
}
}
(scene.value as TresScene).__tres = {
root: context,
}
}
const context = shallowRef<TresContext | null>(null)
defineExpose({ context, dispose: () => dispose(context.value as TresContext, true) })
const handleHMR = (context: TresContext) => {
dispose(context)
mountCustomRenderer(context)
}
const unmountCanvas = () => {
dispose(context.value as TresContext)
mountCustomRenderer(context.value as TresContext, true)
}
onMounted(() => {
const existingCanvas = canvasRef as Ref<HTMLCanvasElement>
context.value = useTresContextProvider({
scene: scene.value as TresScene,
canvas: existingCanvas,
windowSize: props.windowSize ?? false,
rendererOptions: props,
})
const { camera, renderer } = context.value
const { registerCamera, cameras, activeCamera, deregisterCamera } = camera
mountCustomRenderer(context.value)
const addDefaultCamera = () => {
const camera = new PerspectiveCamera(
45,
window.innerWidth / window.innerHeight,
0.1,
1000,
)
camera.position.set(3, 3, 3)
camera.lookAt(0, 0, 0)
registerCamera(camera)
const unwatch = watchEffect(() => {
if (cameras.value.length >= 2) {
camera.removeFromParent()
deregisterCamera(camera)
unwatch?.()
}
})
}
context.value.events.onPointerMissed((event) => {
emit('pointermissed', event)
})
watch(
() => props.camera,
(newCamera, oldCamera) => {
if (newCamera) {
registerCamera(toValue(newCamera), true)
}
if (oldCamera) {
toValue(oldCamera).removeFromParent()
deregisterCamera(toValue(oldCamera))
}
},
{
immediate: true,
},
)
if (!activeCamera.value) {
addDefaultCamera()
}
renderer.onRender(() => {
if (context.value) {
emit('render', context.value)
}
})
renderer.loop.onLoop((loopContext) => {
if (context.value) {
emit('loop', { ...context.value, ...loopContext })
}
})
renderer.loop.onBeforeLoop((loopContext) => {
if (context.value) {
emit('beforeLoop', { ...context.value, ...loopContext })
}
})
renderer.onReady(() => {
emit('ready', context.value!)
})
// HMR support
if (import.meta.hot && context.value) { import.meta.hot.on('vite:afterUpdate', () => handleHMR(context.value as TresContext)) }
})
// warn if the canvas has no area
onMounted(async () => {
await promiseTimeout(3000)
if (context.value && (!context.value.sizes.width || !context.value.sizes.height.value)) {
const windowSizePropName: keyof Pick<TresCanvasProps, 'windowSize'> = 'windowSize'
console.warn(`TresCanvas: The canvas has no area, so nothing can be rendered. Set it manually on the parent element or use the prop ${windowSizePropName}.`)
}
})
onUnmounted(unmountCanvas)
</script>
<script lang="ts">
export interface TresCanvasProps extends RendererOptions {
/**
* Custom camera instance to use as main camera
* If not provided, a default PerspectiveCamera will be created
*/
camera?: TresCamera
/**
* Whether the canvas should be sized to the window
* When true, canvas will be fixed positioned and full viewport size
* @default false
*/
windowSize?: boolean
/**
* Whether to enable the provide/inject bridge between Vue and TresJS
* When true, Vue's provide/inject will work across the TresJS boundary
* @default true
*/
enableProvideBridge?: boolean
}
export type TresCanvasEmits = {
ready: [context: TresContext]
pointermissed: [event: PointerEvent<MouseEvent>]
render: [context: TresContext]
beforeLoop: [context: TresContextWithClock]
loop: [context: TresContextWithClock]
} & {
// all pointer events are supported because they bubble up
[key in TresPointerEventName]: [event: PointerEvent<MouseEvent>]
}
</script>
<template>
<canvas
ref="canvasRef"
:data-scene="scene.uuid"
:class="$attrs.class"
:data-tres="`tresjs ${pkg.version}`"
:style="{
display: 'block',
width: '100%',
height: '100%',
position: windowSize ? 'fixed' : 'relative',
top: 0,
left: 0,
pointerEvents: 'auto',
touchAction: 'none',
...$attrs.style as Object,
}"
></canvas>
</template>