|
| 1 | +<script setup lang="ts"> |
| 2 | +import { Environment, OrbitControls } from '@tresjs/cientos' |
| 3 | +import { TresCanvas } from '@tresjs/core' |
| 4 | +import { TresLeches, useControls } from '@tresjs/leches' |
| 5 | +import type { Mesh } from 'three' |
| 6 | +import { NoToneMapping } from 'three' |
| 7 | +import { BlendFunction } from 'postprocessing' |
| 8 | +import { ColorAveragePmndrs, EffectComposerPmndrs } from '@tresjs/post-processing' |
| 9 | +import { gsap } from 'gsap' |
| 10 | +import { onUnmounted, ref, watch } from 'vue' |
| 11 | +
|
| 12 | +import '@tresjs/leches/styles' |
| 13 | +
|
| 14 | +const gl = { |
| 15 | + clearColor: '#ffffff', |
| 16 | + toneMapping: NoToneMapping, |
| 17 | + multisampling: 8, |
| 18 | + envMapIntensity: 10, |
| 19 | +} |
| 20 | +
|
| 21 | +const ctx = gsap.context(() => {}) |
| 22 | +
|
| 23 | +const meshRef = ref<Mesh | null>(null) |
| 24 | +
|
| 25 | +const { blendFunction, opacity } = useControls({ |
| 26 | + blendFunction: { |
| 27 | + options: Object.keys(BlendFunction).map(key => ({ |
| 28 | + text: key, |
| 29 | + value: BlendFunction[key], |
| 30 | + })), |
| 31 | + value: BlendFunction.NORMAL, |
| 32 | + }, |
| 33 | + opacity: { |
| 34 | + value: 1, |
| 35 | + min: 0, |
| 36 | + max: 1, |
| 37 | + }, |
| 38 | +}) |
| 39 | +
|
| 40 | +function onUpdateTimeline(e) { |
| 41 | + const progress = 1 - e.progress() |
| 42 | + opacity.value.value = progress |
| 43 | +} |
| 44 | +
|
| 45 | +watch(meshRef, () => { |
| 46 | + if (!meshRef.value) { return } |
| 47 | +
|
| 48 | + ctx.add(() => { |
| 49 | + gsap.timeline({ |
| 50 | + repeat: -1, |
| 51 | + yoyo: true, |
| 52 | + onUpdate() { |
| 53 | + onUpdateTimeline(this) |
| 54 | + }, |
| 55 | + }) |
| 56 | + .to(meshRef.value.position, { y: -3.5, duration: 2 }) |
| 57 | + }) |
| 58 | +}) |
| 59 | +
|
| 60 | +onUnmounted(() => { |
| 61 | + ctx.revert() |
| 62 | +}) |
| 63 | +</script> |
| 64 | + |
| 65 | +<template> |
| 66 | + <TresLeches style="left: initial;right:10px; top:10px;" /> |
| 67 | + |
| 68 | + <TresCanvas |
| 69 | + v-bind="gl" |
| 70 | + > |
| 71 | + <TresPerspectiveCamera |
| 72 | + :position="[5, 2, 15]" |
| 73 | + :look-at="[0, 0, 0]" |
| 74 | + /> |
| 75 | + <OrbitControls auto-rotate /> |
| 76 | + |
| 77 | + <TresMesh ref="meshRef" :position="[0, 3.5, 0]"> |
| 78 | + <TresBoxGeometry :args="[2, 2, 2]" /> |
| 79 | + <TresMeshPhysicalMaterial color="#8B0000" :roughness=".25" /> |
| 80 | + </TresMesh> |
| 81 | + |
| 82 | + <Suspense> |
| 83 | + <Environment background preset="shangai" /> |
| 84 | + </Suspense> |
| 85 | + |
| 86 | + <Suspense> |
| 87 | + <EffectComposerPmndrs> |
| 88 | + <ColorAveragePmndrs :blendFunction="Number(blendFunction.value)" :opacity="opacity.value" /> |
| 89 | + </EffectComposerPmndrs> |
| 90 | + </Suspense> |
| 91 | + </TresCanvas> |
| 92 | +</template> |
0 commit comments