mns-mini-zabor/components/model/smoothCamera.vue

57 lines
1.5 KiB
Vue

<script setup lang="ts">
import { Vector3 } from 'three';
const { controls, camera } = useTresContext();
const goto_camera = use_goto_camera();
const goto_target = use_goto_target();
const COUNT = 60
type smooth = {
value: Vector3 | undefined,
count: number
}
const smooth_target = reactive({}) as smooth
const smooth_move = reactive({}) as smooth
const set_moveto = (obj: smooth, value: smooth["value"]) => {
obj.value = value
obj.count = COUNT
}
watch(goto_target, () => {
smooth_target.value = goto_target.value
smooth_target.count = COUNT
})
watch(goto_camera, () => {
smooth_move.value = goto_camera.value
smooth_move.count = COUNT
})
const koef = (1 / COUNT) * 3
const { onBeforeLoop } = useRenderLoop()
onBeforeLoop(() => {
if (smooth_target.value || smooth_move.value) {
if (smooth_target.value) {
(controls.value as any).target.lerp(smooth_target.value as Vector3, koef);
(controls.value as any).update();
smooth_target.count -= 1;
if (smooth_target.count == 1) {
set_moveto(smooth_target, undefined);
}
}
if (smooth_move.value) {
camera.value?.position.lerp(smooth_move.value as Vector3, koef);
camera.value?.updateMatrixWorld();
(controls.value as any).update();
smooth_move.count -= 1;
if (smooth_move.count == 1) {
set_moveto(smooth_move, undefined);
}
}
}
})
</script>
<template></template>