distance to camera

This commit is contained in:
Kseninia Mikhaylova 2024-06-18 13:07:51 +03:00
parent fb44be6804
commit 1ff6de94e1
1 changed files with 14 additions and 4 deletions

View File

@ -5,10 +5,13 @@ import { OrbitControls, Environment, useGLTF } from '@tresjs/cientos'
const section_count = useState<number>('section_count') const section_count = useState<number>('section_count')
const fence_section = useState<number>('fence_section') const fence_section = useState<number>('fence_section')
const extra_section = useState<number>('extra_section')
const controlsState = reactive({ const controlsState = reactive({
minDistance: section_count.value * 5, distance: section_count.value,
maxDistance: 12, minDistance: 7,
maxDistance: 20,
position: { x: 0, y: 0, z: 0 },
enablePan: false, enablePan: false,
maxPolarAngle: (Math.PI / 2) - 0.2, maxPolarAngle: (Math.PI / 2) - 0.2,
}) })
@ -22,7 +25,7 @@ const pointLight = ref()
const groundMaterial = ref({ const groundMaterial = ref({
color: "#555", color: "#555",
roughness: 0.25, roughness: 0.2,
metalness: 0, metalness: 0,
side: FrontSide side: FrontSide
}) })
@ -60,17 +63,24 @@ const loadAll = async () => {
normalMap: pbrTexture.normalMap, normalMap: pbrTexture.normalMap,
}) })
} }
const camera = ref("camera")
onMounted(() => { onMounted(() => {
cameraStat.aspect = window.innerWidth / (window.innerHeight * 0.5) cameraStat.aspect = window.innerWidth / (window.innerHeight * 0.5)
loadAll() loadAll()
}) })
watch(section_count, () => {
let v = (section_count.value + ~~(!!extra_section.value))*1.5
if (v < controlsState.minDistance) v = controlsState.minDistance
if (v > controlsState.maxDistance) v = controlsState.maxDistance
camera.value.position.normalize().multiplyScalar(v)
})
</script> </script>
<template> <template>
<div class="container min-w-full relative h-[50vh]"> <div class="container min-w-full relative h-[50vh]">
<ClientOnly fallback-tag="div" fallback="Загрузка 3D модели"> <ClientOnly fallback-tag="div" fallback="Загрузка 3D модели">
<Loader /> <Loader />
<TresCanvas shadows> <TresCanvas shadows>
<TresPerspectiveCamera v-bind="cameraStat" /> <TresPerspectiveCamera v-bind="cameraStat" ref="camera" />
<OrbitControls v-bind="controlsState" make-default /> <OrbitControls v-bind="controlsState" make-default />
<Suspense v-if="false"> <Suspense v-if="false">
<Environment files='/hdrmaps/kloppenheim_06_4k.hdr' :background="true" /> <Environment files='/hdrmaps/kloppenheim_06_4k.hdr' :background="true" />