|
| 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 { Vector3 } from 'three' |
| 6 | +import { NoToneMapping } from 'three' |
| 7 | +import { BlendFunction } from 'postprocessing' |
| 8 | +import { ColorDepthPmndrs, EffectComposerPmndrs } from '@tresjs/post-processing' |
| 9 | +import { onUnmounted, toRaw, useTemplateRef, watch } from 'vue' |
| 10 | +import { gsap } from 'gsap' |
| 11 | +
|
| 12 | +import '@tresjs/leches/styles' |
| 13 | +
|
| 14 | +const gl = { |
| 15 | + clearColor: '#ffffff', |
| 16 | + toneMapping: NoToneMapping, |
| 17 | + multisampling: 8, |
| 18 | +} |
| 19 | +
|
| 20 | +const ctx = gsap.context(() => {}) |
| 21 | +
|
| 22 | +const meshsArray = useTemplateRef('meshRefs') |
| 23 | +
|
| 24 | +const { blendFunction, bits, opacity } = useControls({ |
| 25 | + blendFunction: { |
| 26 | + options: Object.keys(BlendFunction).map(key => ({ |
| 27 | + text: key, |
| 28 | + value: BlendFunction[key as keyof typeof BlendFunction], |
| 29 | + })), |
| 30 | + value: BlendFunction.NORMAL, |
| 31 | + }, |
| 32 | + bits: { value: 8, step: 1, min: 1, max: 16 }, |
| 33 | + opacity: { value: 1, step: 0.1, min: 0, max: 1 }, |
| 34 | +}) |
| 35 | +
|
| 36 | +const meshes: { position: [number, number, number], color: string }[] = [ |
| 37 | + { position: [0, 0.5, 0], color: 'white' }, |
| 38 | + { position: [0, 0.5, -2], color: 'red' }, |
| 39 | + { position: [0, 0.5, 2], color: 'yellow' }, |
| 40 | + { position: [-2, 0.5, 0], color: 'blue' }, |
| 41 | + { position: [2, 0.5, 0], color: 'purple' }, |
| 42 | +] |
| 43 | +
|
| 44 | +watch(meshsArray, () => { |
| 45 | + ctx.add(() => { |
| 46 | + meshsArray.value?.forEach((el) => { |
| 47 | + const position = toRaw(el.position) as Vector3 |
| 48 | +
|
| 49 | + if (position) { |
| 50 | + gsap.to(position as Vector3, { |
| 51 | + x: position.x === 0 ? '+=0' : position.x < 0 ? '-=3' : '+=3', |
| 52 | + z: position.z === 0 ? '+=0' : position.z < 0 ? '-=3' : '+=3', |
| 53 | + repeat: -1, |
| 54 | + yoyo: true, |
| 55 | + }) |
| 56 | + } |
| 57 | + }) |
| 58 | + }) |
| 59 | +}) |
| 60 | +
|
| 61 | +onUnmounted(() => { |
| 62 | + ctx.revert() |
| 63 | +}) |
| 64 | +</script> |
| 65 | + |
| 66 | +<template> |
| 67 | + <div class="aspect-16/9"> |
| 68 | + <TresCanvas |
| 69 | + v-bind="gl" |
| 70 | + > |
| 71 | + <TresPerspectiveCamera :position="[8, 5, 5]" /> |
| 72 | + <OrbitControls auto-rotate /> |
| 73 | + |
| 74 | + <TresMesh |
| 75 | + v-for="(mesh, index) in meshes" |
| 76 | + :key="`color-depth-demo-box-${index}`" |
| 77 | + ref="meshRefs" |
| 78 | + :position="mesh.position" |
| 79 | + > |
| 80 | + <TresBoxGeometry :args="[2, 2, 2]" /> |
| 81 | + <TresMeshPhysicalMaterial :color="mesh.color" :roughness="0.25" /> |
| 82 | + </TresMesh> |
| 83 | + |
| 84 | + <Suspense> |
| 85 | + <Environment background preset="umbrellas" /> |
| 86 | + </Suspense> |
| 87 | + |
| 88 | + <Suspense> |
| 89 | + <EffectComposerPmndrs> |
| 90 | + <ColorDepthPmndrs :blendFunction="Number(blendFunction)" :bits="bits" :opacity="opacity" /> |
| 91 | + </EffectComposerPmndrs> |
| 92 | + </Suspense> |
| 93 | + </TresCanvas> |
| 94 | + </div> |
| 95 | + <TresLeches :float="false" /> |
| 96 | +</template> |
0 commit comments