-
-
Notifications
You must be signed in to change notification settings - Fork 183
Expand file tree
/
Copy pathHTML.vue
More file actions
541 lines (455 loc) · 13.3 KB
/
HTML.vue
File metadata and controls
541 lines (455 loc) · 13.3 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
<script setup lang="ts">
import { useLoop, useTresContext } from '@tresjs/core'
import {
DoubleSide,
Matrix4,
OrthographicCamera,
PlaneGeometry,
Quaternion,
Raycaster,
ShaderMaterial,
Vector3,
} from 'three'
import {
computed,
createVNode,
onUnmounted,
ref,
render,
shallowRef,
toRefs,
useAttrs,
watch,
watchEffect,
} from 'vue'
import type { TresCamera, TresObject3D } from '@tresjs/core'
import type { Mutable } from '@vueuse/core'
import type { VNode } from 'vue'
import fragmentShader from './shaders/fragment.glsl'
import vertexShader from './shaders/vertex.glsl'
import passthroughVertex from './shaders/passthrough-vertex.glsl'
import {
calculatePosition as defaultCalculatePosition,
epsilon,
getCameraCSSMatrix,
getObjectCSSMatrix,
getViewportFactor,
isObjectBehindCamera,
isObjectVisible,
objectScale,
objectZIndex,
} from './utils'
type PointerEventsProperties =
| 'auto'
| 'none'
| 'visiblePainted'
| 'visibleFill'
| 'visibleStroke'
| 'visible'
| 'painted'
| 'fill'
| 'stroke'
| 'all'
| 'inherit'
export interface HTMLProps {
geometry?: any
material?: any
as?: string
transform?: boolean
prepend?: boolean
portal?: Mutable<HTMLElement>
wrapperClass?: string
eps?: number
distanceFactor?: number
fullscreen?: boolean
center?: boolean
pointerEvents?: PointerEventsProperties
sprite?: boolean
transparentMaterial?: boolean
zIndexRange?: Array<number>
calculatePosition?: typeof defaultCalculatePosition
// Occlusion based off work by Jerome Etienne and James Baicoianu
// https://www.youtube.com/watch?v=ScZcUEDGjJI
// as well as Joe Pea in CodePen: https://codepen.io/trusktr/pen/RjzKJx
occlude?: TresObject3D | null | (TresObject3D | null)[] | boolean | 'blending'
castShadow?: boolean // Enable shadow casting for the occlusion plane
receiveShadow?: boolean // Enable shadow receiving for the occlusion plane
}
const props = withDefaults(defineProps<HTMLProps>(), {
zIndexRange: () => [16777271, 0],
as: 'div',
transform: false,
eps: 0.0001,
pointerEvents: 'auto',
sprite: false,
prepend: false,
castShadow: false,
receiveShadow: false,
transparentMaterial: false,
calculatePosition: () => defaultCalculatePosition,
})
const emits = defineEmits(['onOcclude'])
const slots = defineSlots()
const {
geometry,
material,
as,
transform,
portal,
wrapperClass,
eps,
distanceFactor,
fullscreen,
center,
pointerEvents,
sprite,
prepend,
occlude,
zIndexRange,
castShadow,
receiveShadow,
transparentMaterial,
calculatePosition,
} = toRefs(props)
const attrs = useAttrs()
const groupRef = shallowRef<TresObject3D | null>(null)
const occlusionMeshRef = shallowRef<TresObject3D | null>(null)
const defaultPlaneGeometry = new PlaneGeometry()
const { renderer, scene, camera, sizes } = useTresContext()
const el = computed(() => document.createElement(as.value))
const raycaster = new Raycaster()
const tmpVec = new Vector3()
const tmpMatrix = new Matrix4()
const tmpQuat = new Quaternion()
const previousPosition = ref<[number, number, number]>([0, 0, 0])
const previousZoom = ref(0)
const vnode = ref<VNode | null>(null)
const isVisible = ref(true)
const isMeshSizeSet = ref(false)
const baseStyle = computed(() => ({
position: 'absolute',
top: '0',
left: '0',
willChange: 'transform',
pointerEvents: pointerEvents.value,
...(typeof attrs.style === 'object' ? attrs.style : {}),
}))
const styles = computed(() => {
const w = sizes.width.value
const h = sizes.height.value
if (transform.value) {
return {
...baseStyle.value,
transformStyle: 'preserve-3d',
pointerEvents: 'none',
width: `${w}px`,
height: `${h}px`,
}
}
return {
...baseStyle.value,
transform: center.value ? 'translate3d(-50%,-50%,0)' : 'none',
...(fullscreen.value && {
top: `-${h / 2}px`,
left: `-${w / 2}px`,
width: `${w}px`,
height: `${h}px`,
}),
pointerEvents: fullscreen.value ? 'none' : pointerEvents.value,
}
})
const transformInnerStyles = computed(() => ({
position: 'absolute',
pointerEvents: pointerEvents.value,
}))
const isRayCastOcclusion = computed(() => {
const o = occlude.value
return (
o
&& o !== 'blending'
&& (Array.isArray(o) ? o.length && typeof o[0] !== 'boolean' : true)
)
})
const effectiveMaterial = computed(() => {
if (material.value) { return material.value }
return new ShaderMaterial({
vertexShader: sprite.value
? (vertexShader as string)
: transform.value
? (passthroughVertex as string)
: (vertexShader as string),
fragmentShader: fragmentShader as string,
side: DoubleSide,
transparent: transparentMaterial.value,
uniforms: {
uWidth: { value: 1 },
uHeight: { value: 1 },
},
})
})
watchEffect(() => {
effectiveMaterial.value.transparent = transparentMaterial.value
})
watch(
[occlude, () => renderer.instance],
([occludeVal, r]) => {
if (!r || occludeVal !== 'blending') { return }
const target = r.domElement
target.style.zIndex = `${Math.floor(zIndexRange.value[0] / 2)}`
target.style.position = 'absolute'
},
{ immediate: true },
)
watch(
() => [
groupRef.value,
renderer.instance,
sizes.width.value,
sizes.height.value,
slots.default?.(),
camera.activeCamera.value,
],
([group, r]) => {
if (!group || !r || !camera.activeCamera.value) { return }
isMeshSizeSet.value = false
scene.value?.updateMatrixWorld()
const elStyle = el.value.style
elStyle.position = 'absolute'
elStyle.top = '0'
elStyle.left = '0'
if (transform.value) {
elStyle.pointerEvents = 'none'
elStyle.overflow = 'hidden'
elStyle.transformStyle = 'preserve-3d'
}
else {
elStyle.transformOrigin = '0 0'
elStyle.willChange = 'transform'
if (!occlude.value) {
elStyle.zIndex = `${zIndexRange.value[0]}`
}
}
const parent = portal.value || r.domElement?.parentNode
if (parent && !el.value.parentNode) {
prepend.value ? parent.prepend(el.value) : parent.appendChild(el.value)
}
vnode.value = transform.value
? createVNode('div', { style: styles.value }, [
createVNode('div', { style: transformInnerStyles.value }, [
createVNode(
'div',
{
key: groupRef.value?.uuid,
class: attrs.class,
style: attrs.style,
},
slots.default?.(),
),
]),
])
: createVNode(
'div',
{
key: groupRef.value?.uuid,
style: styles.value,
class: attrs.class,
},
slots.default?.(),
)
render(vnode.value, el.value)
},
)
watchEffect(() => {
if (wrapperClass.value) { el.value.className = wrapperClass.value }
})
const { onBeforeRender } = useLoop()
onBeforeRender(({ invalidate }) => {
const group = groupRef.value
const cam = camera.activeCamera.value
const rend = renderer.instance
if (!group || !cam || !rend) { return }
cam.updateMatrixWorld()
group.updateWorldMatrix(true, false)
const width = sizes.width.value
const height = sizes.height.value
const widthHalf = width * 0.5
const heightHalf = height * 0.5
const newPos = transform.value
? previousPosition.value
: calculatePosition.value(group, cam as TresCamera, {
width,
height,
})
const posChanged
= Math.abs(previousZoom.value - cam.zoom) > eps.value
|| Math.abs(previousPosition.value[0] - newPos[0]) > eps.value
|| Math.abs(previousPosition.value[1] - newPos[1]) > eps.value
|| Math.abs(previousPosition.value[2] - newPos[2]) > eps.value
let changed = transform.value || posChanged
let visible = true
const behind = isObjectBehindCamera(group, cam)
if (isRayCastOcclusion.value) {
let targets: TresObject3D[] | false = false
if (Array.isArray(occlude.value)) {
targets = occlude.value as TresObject3D[]
}
else if (occlude.value !== 'blending') {
targets = [scene.value as unknown as TresObject3D]
}
if (targets) {
visible = isObjectVisible(group, cam, raycaster, targets) && !behind
}
}
else {
visible = !behind
}
if (visible !== isVisible.value) {
isVisible.value = visible
el.value.style.display = visible ? 'block' : 'none'
emits('onOcclude', !visible)
changed = true
}
const halfRange = Math.floor(zIndexRange.value[0] / 2)
const zRange = occlude.value
? isRayCastOcclusion.value
? [zIndexRange.value[0], halfRange]
: [halfRange - 1, 0]
: zIndexRange.value
el.value.style.zIndex = `${objectZIndex(group, cam, zRange)}`
if (transform.value) {
const persp = cam.projectionMatrix.elements[5] * heightHalf
const camMatrix = getCameraCSSMatrix(cam.matrixWorldInverse)
const isOrtho = cam instanceof OrthographicCamera
const cameraTransform = isOrtho
? `scale(${persp})translate(${epsilon(-(cam.right + cam.left) / 2)}px,${epsilon(
(cam.top + cam.bottom) / 2,
)}px)`
: `translateZ(${persp}px)`
let finalMatrix = group.matrixWorld
if (sprite.value) {
tmpMatrix.copy(group.matrixWorld)
const invCamMat = cam.matrixWorldInverse.clone().transpose()
tmpQuat.setFromRotationMatrix(invCamMat)
tmpMatrix.makeRotationFromQuaternion(tmpQuat)
group.getWorldPosition(tmpVec)
tmpMatrix.setPosition(tmpVec)
tmpMatrix.scale(group.scale)
tmpMatrix.elements[3] = tmpMatrix.elements[7] = tmpMatrix.elements[11] = 0
tmpMatrix.elements[15] = 1
finalMatrix = tmpMatrix
}
const style = el.value.style
style.width = `${width}px`
style.height = `${height}px`
style.perspective = isOrtho ? '' : `${persp}px`
if (vnode.value?.el) {
vnode.value.el.style.transform = `${cameraTransform}${camMatrix}translate(${widthHalf}px,${heightHalf}px)`
let inner: VNode | undefined
if (Array.isArray(vnode.value.children)) {
inner = vnode.value.children[0] as VNode
}
if (inner?.el) {
inner.el.style.transform = getObjectCSSMatrix(
finalMatrix,
1 / ((distanceFactor.value || 10) / 400),
)
}
}
}
else {
const scale
= distanceFactor.value === undefined
? 1
: objectScale(group, cam) * distanceFactor.value
el.value.style.transform = `translate3d(${newPos[0]}px,${newPos[1]}px,0) scale(${scale})`
}
previousPosition.value = [newPos[0], newPos[1], newPos[2]]
previousZoom.value = cam.zoom
if (!isRayCastOcclusion.value && occlusionMeshRef.value && !isMeshSizeSet.value) {
const mesh = occlusionMeshRef.value
if (transform.value) {
const vnodeRoot = vnode.value
const children = vnodeRoot?.children
if (Array.isArray(children)) {
const childVNode = children[0] as VNode
const element = childVNode?.el as HTMLElement
if (element) {
const isOrtho = cam instanceof OrthographicCamera
if (isOrtho && geometry.value && attrs.scale) {
if (!Array.isArray(attrs.scale)) {
mesh.scale.setScalar(1 / (attrs.scale as number))
}
else if (attrs.scale instanceof Vector3) {
mesh.scale.copy(attrs.scale)
}
else {
mesh.scale.set(
1 / attrs.scale[0],
1 / attrs.scale[1],
1 / attrs.scale[2],
)
}
}
else if (!isOrtho && !geometry.value) {
const ratio = (distanceFactor.value || 10) / 400
if (sprite.value) {
effectiveMaterial.value.uniforms.uWidth.value = element.clientWidth * ratio
effectiveMaterial.value.uniforms.uHeight.value
= element.clientHeight * ratio
mesh.lookAt(cam.position)
mesh.scale.set(1, 1, 1)
}
else {
const w = element.clientWidth * ratio
const h = element.clientHeight * ratio
mesh.scale.set(w, h, 1)
}
}
isMeshSizeSet.value = true
}
}
}
else {
const element = el.value.children[0] as HTMLElement
if (element?.clientWidth && element?.clientHeight) {
group.getWorldPosition(tmpVec)
const factor = getViewportFactor(cam, tmpVec, { width, height })
const ratio = 1 / factor
effectiveMaterial.value.uniforms.uWidth.value
= element.clientWidth * ratio
effectiveMaterial.value.uniforms.uHeight.value
= element.clientHeight * ratio
mesh.scale.set(1, 1, 1)
mesh.lookAt(cam.position)
isMeshSizeSet.value = true
}
}
}
if (changed) { invalidate() }
})
onUnmounted(() => {
defaultPlaneGeometry?.dispose()
effectiveMaterial.value?.dispose()
if (el.value?.parentNode) {
el.value.parentNode.removeChild(el.value)
}
})
defineExpose({
instance: groupRef,
isVisible,
occlusionMesh: occlusionMeshRef,
})
</script>
<template>
<TresGroup ref="groupRef">
<template v-if="occlude && !isRayCastOcclusion">
<TresMesh
ref="occlusionMeshRef"
:material="effectiveMaterial"
:cast-shadow="castShadow"
:receive-shadow="receiveShadow"
:geometry="geometry || defaultPlaneGeometry"
/>
</template>
</TresGroup>
</template>