index.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <script setup lang="ts">
  2. import { BasicShadowMap, MeshToonMaterial, NoToneMapping, SRGBColorSpace } from 'three'
  3. import { reactive, ref } from 'vue'
  4. import { TresCanvas, useRenderLoop } from '@tresjs/core'
  5. import { OrbitControls } from '@tresjs/cientos'
  6. const state = reactive({
  7. clearColor: '#ffffff',
  8. shadows: true,
  9. alpha: false,
  10. shadowMapType: BasicShadowMap,
  11. outputColorSpace: SRGBColorSpace,
  12. toneMapping: NoToneMapping,
  13. })
  14. const canvasRef = ref()
  15. const sphereRef = ref()
  16. const { onLoop } = useRenderLoop()
  17. onLoop(({ elapsed }) => {
  18. if (!sphereRef.value) { return }
  19. sphereRef.value.position.y += Math.sin(elapsed) * 0.01
  20. // Update events without needing the mouse to move
  21. canvasRef.value?.context?.eventManager?.forceUpdate()
  22. })
  23. function onPointerEnter(ev) {
  24. if (ev) {
  25. ev.object.material.color.set('#DFFF45')
  26. }
  27. }
  28. function onPointerOut(ev) {
  29. ev.object.material.color.set('teal')
  30. }
  31. const sphereExists = ref(true)
  32. const toonTealMaterial = new MeshToonMaterial({
  33. color: 'teal',
  34. })
  35. </script>
  36. <template>
  37. <input
  38. v-model="sphereExists"
  39. type="checkbox"
  40. />
  41. <TresCanvas
  42. v-if="sphereExists"
  43. ref="canvasRef"
  44. v-bind="state"
  45. >
  46. <TresPerspectiveCamera
  47. :position="[0, 8, 8]"
  48. :fov="45"
  49. :near="0.1"
  50. :far="1000"
  51. :look-at="[0, 0, 0]"
  52. />
  53. <OrbitControls />
  54. <TresAmbientLight :intensity="0.5" />
  55. <TresGroup>
  56. <TresMesh
  57. ref="sphereRef"
  58. :visible="sphereExists"
  59. :user-data="{ debug: true }"
  60. :position="[0, 4, 0]"
  61. cast-shadow
  62. :material="toonTealMaterial"
  63. @pointer-enter="onPointerEnter"
  64. @pointer-out="onPointerOut"
  65. >
  66. <TresSphereGeometry :args="[2, 32, 32]" />
  67. <TresMeshStandardMaterial attach="attachedMaterial" />
  68. </TresMesh>
  69. </TresGroup>
  70. <TresMesh
  71. ref="sphereRef"
  72. :position="[-4, 4, 0]"
  73. cast-shadow
  74. :material="toonTealMaterial"
  75. @pointer-enter="onPointerEnter"
  76. @pointer-out="onPointerOut"
  77. >
  78. <TresSphereGeometry :args="[2, 32, 32]" />
  79. </TresMesh>
  80. <TresDirectionalLight
  81. :position="[0, 8, 4]"
  82. :intensity="0.7"
  83. cast-shadow
  84. />
  85. <TresMesh
  86. :rotation="[-Math.PI / 2, 0, 0]"
  87. receive-shadow
  88. >
  89. <TresPlaneGeometry :args="[10, 10, 10, 10]" />
  90. <TresMeshBasicMaterial />
  91. </TresMesh>
  92. <TresDirectionalLight
  93. :position="[0, 2, 4]"
  94. :intensity="1"
  95. cast-shadow
  96. />
  97. </TresCanvas>
  98. </template>