<script setup lang="ts">
import { TresCanvas } from '@tresjs/core'
import { OrbitControls } from '@tresjs/cientos'

const controlsState = reactive({
    minDistance: 1,
    maxDistance: 10,
    enablePan: false,
    enableZoom: false,
    maxPolarAngle: (Math.PI / 2) - 0.2,
})
const container = ref()
const koef = ref()
const scrollBlock = (e: Event) => {
    const { top, bottom, height } = container.value.getBoundingClientRect();
    const { innerHeight } = window;
    const partiallyVisible = (top > 0 && top < innerHeight) ||
        (bottom > 0 && bottom < innerHeight)
    const visibleHeight = innerHeight - top
    const h = height * 1.5
    if (partiallyVisible && visibleHeight < h) {
        koef.value = visibleHeight / h
    }
}
onMounted(() => {
    document.addEventListener('scroll', scrollBlock)
})
onUnmounted(() => {
    document.removeEventListener('scroll', scrollBlock)
})
</script>
<template>
    <div class="container min-w-full outline outline-2" ref="container">
        <TresCanvas shadows>
            <TresPerspectiveCamera :position="[-7, 2, 4]" />
            <OrbitControls v-bind="controlsState" make-default />

            <ModelDiagram :koef="koef" />

            <TresDirectionalLight :position="[2, 2, 2]" :intensity="2" color="violet" cast-shadow />
            <TresDirectionalLight :position="[2, 2, -2]" :intensity="1" color="red" cast-shadow />
            <TresAmbientLight />
        </TresCanvas>
    </div>
</template>

<style scoped>
.container {
    height: 600px;
    display: block;
}
</style>