<script setup lang="ts">
import { reactive } from 'vue';
import { Vector3 } from 'three';
import { TresCanvas } from '@tresjs/core';
import { vLightHelper } from '@tresjs/core'
import { CameraControls, useGLTF, useProgress } from '@tresjs/cientos'

import sunset from '../../assets/promo/models/sunset.hdr'

const minPan = new Vector3(-10, 1, -5);
const maxPan = new Vector3(5, 1, 5);
const _v = new Vector3();

const onChange = (e: any) => {
    _v.copy(e._target);
    e._target.clamp(minPan, maxPan);
    _v.sub(e._target);
    e._camera.position.sub(_v);
}

const cameraPosition = [-6, 4, 25] as unknown as Vector3
const lightPosition = [3, 3, 3] as unknown as Vector3

const controlsState = reactive({
    minDistance: 1,
    maxDistance: 30,
    maxPolarAngle: (Math.PI / 2) - 0.02,
    maxZoom: 1,
    minZoom: 0.2,
})


const { hasFinishLoading, progress } = await useProgress()
const { scene: spot_light } = await useGLTF('/Spot_light.glb')
const spot_light_item = spot_light.children[0]
</script>
<template>
    <div :class="[{ 'invisible': !!hasFinishLoading }, 'loader']">
        Загрузка {{ progress }}%
    </div>
    <div :class="[{ 'invisible': !hasFinishLoading }]">
        <TresCanvas window-size alpha shadows clear-color="#87ceeb">
            <TresPerspectiveCamera :position="cameraPosition" />
            <CameraControls v-bind="controlsState" @change="onChange" make-default />

            <!-- <TresGridHelper :args="[50, 50]" /> -->
            <Suspense>
                <Models />
            </Suspense>
            <TresSpotLight :position="spot_light_item.position"
                :color="spot_light_item.color" :angle="spot_light_item.angle" :penumbra="spot_light_item.penumbra"
                :intensity="spot_light_item.intensity" cast-shadow v-light-helper />
            <!-- <TresDirectionalLight :position="lightPosition" :intensity="10" cast-shadow /> -->
            <!-- <TresAmbientLight /> -->
        </TresCanvas>
    </div>
</template>
<style scoped>
.invisible {
    visibility: hidden;
}

.loader {
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 2rem;
    min-height: 100vh;
}
</style>