|
| 1 | +<script setup lang="ts"> |
| 2 | +import { Environment, OrbitControls } from '@tresjs/cientos' |
| 3 | +import { TresCanvas, useTexture } from '@tresjs/core' |
| 4 | +import { TresLeches, useControls } from '@tresjs/leches' |
| 5 | +import type { Mesh } from 'three' |
| 6 | +import { BackSide, NoToneMapping } from 'three' |
| 7 | +import { BlendFunction, KernelSize } from 'postprocessing' |
| 8 | +import { EffectComposerPmndrs, GodRaysPmndrs } from '@tresjs/post-processing' |
| 9 | +import { ref, watch } from 'vue' |
| 10 | +import { gsap } from 'gsap' |
| 11 | +
|
| 12 | +import '@tresjs/leches/styles' |
| 13 | +
|
| 14 | +const gl = { |
| 15 | + toneMapping: NoToneMapping, |
| 16 | + multisampling: 8, |
| 17 | +} |
| 18 | +
|
| 19 | +const sphereMeshRef = ref<Mesh | null>(null) |
| 20 | +
|
| 21 | +const pbrTexture = await useTexture({ |
| 22 | + map: '/lens-distortion/room-map.png', |
| 23 | +}) |
| 24 | +
|
| 25 | +const { blur, kernelSize, resolutionScale, opacity, blendFunction, density, decay, weight, exposure, samples, clampMax } = useControls({ |
| 26 | + blendFunction: { |
| 27 | + options: Object.keys(BlendFunction).map(key => ({ |
| 28 | + text: key, |
| 29 | + value: BlendFunction[key as keyof typeof BlendFunction], |
| 30 | + })), |
| 31 | + value: BlendFunction.SCREEN, |
| 32 | + }, |
| 33 | + kernelSize: { |
| 34 | + options: Object.keys(KernelSize).map(key => ({ |
| 35 | + text: key, |
| 36 | + value: KernelSize[key as keyof typeof KernelSize], |
| 37 | + })), |
| 38 | + value: KernelSize.SMALL, |
| 39 | + }, |
| 40 | + opacity: { value: 1, step: 0.01, min: 0, max: 1.0 }, |
| 41 | + density: { value: 0.96, step: 0.01, min: 0, max: 1.0 }, |
| 42 | + decay: { value: 0.93, step: 0.01, min: 0, max: 1.0 }, |
| 43 | + weight: { value: 0.4, step: 0.1, min: 0, max: 1.0 }, |
| 44 | + exposure: { value: 0.6, step: 0.1, min: 0, max: 1.0 }, |
| 45 | + samples: { value: 60, step: 1, min: 15, max: 200 }, |
| 46 | + clampMax: { value: 1.0, step: 0.1, max: 1.0 }, |
| 47 | + resolutionScale: { value: 0.5, step: 0.1, min: 0.1, max: 1.0 }, |
| 48 | + blur: true, |
| 49 | +}) |
| 50 | +
|
| 51 | +const torusMeshes = [ |
| 52 | + { rotationY: Math.PI / 2, position: [-10, 2, 0], roughness: 0.1, color: '#82DBC5' }, |
| 53 | + { rotationY: Math.PI / 2, position: [0, 2, 0], roughness: 0.3, color: '#505050' }, |
| 54 | + { rotationY: Math.PI / 2, position: [10, 2, 0], roughness: 0.65, color: '#FC7BAC' }, |
| 55 | +] |
| 56 | +
|
| 57 | +watch(sphereMeshRef, () => { |
| 58 | + if (!sphereMeshRef.value) { return } |
| 59 | +
|
| 60 | + gsap.to(sphereMeshRef.value.position, { |
| 61 | + x: 20, |
| 62 | + duration: 3, |
| 63 | + repeat: -1, |
| 64 | + yoyo: true, |
| 65 | + ease: 'sine.inOut', |
| 66 | + }) |
| 67 | +}) |
| 68 | +</script> |
| 69 | + |
| 70 | +<template> |
| 71 | + <div class="aspect-16/9"> |
| 72 | + <TresCanvas |
| 73 | + v-bind="gl" |
| 74 | + > |
| 75 | + <TresPerspectiveCamera |
| 76 | + :position="[0, 5, 35]" |
| 77 | + :look-at="[0, 0, 0]" |
| 78 | + /> |
| 79 | + <OrbitControls auto-rotate /> |
| 80 | + |
| 81 | + <TresMesh ref="sphereMeshRef" :position="[-20, 2, 0]"> |
| 82 | + <TresSphereGeometry :args="[2, 32, 32]" /> |
| 83 | + <TresMeshBasicMaterial color="#FFDDAA" :transparent="true" /> |
| 84 | + </TresMesh> |
| 85 | + |
| 86 | + <template v-for="(mesh) in torusMeshes" :key="`demo-god-rays-${mesh.color}`"> |
| 87 | + <TresMesh :rotation-y="mesh.rotationY" :position="mesh.position"> |
| 88 | + <TresTorusGeometry :args="[5, 2, 16, 100]" /> |
| 89 | + <TresMeshPhysicalMaterial :roughness="mesh.roughness" :color="`${mesh.color}`" /> |
| 90 | + </TresMesh> |
| 91 | + </template> |
| 92 | + |
| 93 | + <TresMesh :position="[0, 2, 0]"> |
| 94 | + <TresBoxGeometry :args="[50, 50, 50]" /> |
| 95 | + <TresMeshStandardMaterial :side="BackSide" :map="pbrTexture.map" /> |
| 96 | + </TresMesh> |
| 97 | + |
| 98 | + <Suspense> |
| 99 | + <Environment preset="shangai" /> |
| 100 | + </Suspense> |
| 101 | + |
| 102 | + <Suspense> |
| 103 | + <EffectComposerPmndrs> |
| 104 | + <GodRaysPmndrs |
| 105 | + :opacity="opacity" |
| 106 | + :lightSource="sphereMeshRef" |
| 107 | + :blendFunction="Number(blendFunction)" |
| 108 | + :density="density" |
| 109 | + :decay="decay" |
| 110 | + :weight="weight" |
| 111 | + :exposure="exposure" |
| 112 | + :samples="samples" |
| 113 | + :clampMax="clampMax" |
| 114 | + :resolutionScale="resolutionScale" |
| 115 | + :kernelSize="kernelSize" |
| 116 | + :blur="blur" |
| 117 | + /> |
| 118 | + </EffectComposerPmndrs> |
| 119 | + </Suspense> |
| 120 | + </TresCanvas> |
| 121 | + </div> |
| 122 | + |
| 123 | + <TresLeches :float="false" /> |
| 124 | +</template> |
0 commit comments