DynamicObjects.vue 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <script setup lang="ts">
  2. import { Box, OrbitControls, Sphere } from '@tresjs/cientos'
  3. import { TresCanvas, type TresPointerEvent } from '@tresjs/core'
  4. import { reactive } from 'vue'
  5. const hotspots = reactive([
  6. {
  7. position: [-2, 0, -2],
  8. },
  9. {
  10. position: [0, 0, -2],
  11. },
  12. {
  13. position: [2, 0, -2],
  14. },
  15. ])
  16. const addHotspot = () => {
  17. const newHotspot = reactive({
  18. position: [-2, 0, 0],
  19. })
  20. hotspots.push(newHotspot)
  21. }
  22. const removeHotspot = () => {
  23. hotspots.pop()
  24. }
  25. const grow = (event: TresPointerEvent) => {
  26. event.object.scale.set(1.5, 1.5, 1.5)
  27. }
  28. const shrink = (event: TresPointerEvent) => {
  29. event.object.scale.set(1, 1, 1)
  30. }
  31. </script>
  32. <template>
  33. <TresCanvas>
  34. <Suspense>
  35. <StatsGl />
  36. </Suspense>
  37. <OrbitControls />
  38. <TresPerspectiveCamera />
  39. <TresAmbientLight :args="['white', 0.5]" />
  40. <Box :position="[0, 0, 0]" :scale="[1, 1, 1]" @click="addHotspot" @context-menu="removeHotspot">
  41. <TresMeshNormalMaterial />
  42. </Box>
  43. <Sphere
  44. v-for="(hotspot, index) in hotspots"
  45. :key="index"
  46. :args="[0.5, 16, 16]"
  47. :position="hotspot.position"
  48. @click="console.log('click', index)"
  49. @pointerenter="grow"
  50. @pointerleave="shrink"
  51. >
  52. <TresMeshNormalMaterial />
  53. </Sphere>
  54. </TresCanvas>
  55. </template>