Primitives.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <!-- eslint-disable no-console -->
  2. <script setup lang="ts">
  3. import { OrbitControls } from '@tresjs/cientos'
  4. import { TresCanvas } from '@tresjs/core'
  5. import { TresLeches, useControls } from '@tresjs/leches'
  6. import {
  7. BasicShadowMap,
  8. Group,
  9. Mesh,
  10. MeshToonMaterial,
  11. NoToneMapping,
  12. SphereGeometry,
  13. SRGBColorSpace,
  14. TorusGeometry,
  15. TorusKnotGeometry,
  16. } from 'three'
  17. import { ref, watchEffect } from 'vue'
  18. import '@tresjs/leches/styles'
  19. const gl = {
  20. clearColor: '#82DBC5',
  21. shadows: true,
  22. alpha: false,
  23. shadowMapType: BasicShadowMap,
  24. outputColorSpace: SRGBColorSpace,
  25. toneMapping: NoToneMapping,
  26. }
  27. const canvas = ref()
  28. const meshRef = ref()
  29. const { knot, isVisible } = useControls({
  30. knot: true,
  31. isVisible: true,
  32. })
  33. watchEffect(() => {
  34. if (meshRef.value) {
  35. console.log(meshRef.value)
  36. }
  37. })
  38. const torus = new Mesh(
  39. new TorusGeometry(1, 0.5, 16, 100),
  40. new MeshToonMaterial({
  41. color: '#82DBC5',
  42. }),
  43. )
  44. const torusKnot = new Mesh(
  45. new TorusKnotGeometry(1, 0.5, 100, 16),
  46. new MeshToonMaterial({
  47. color: '#ff00ff',
  48. }),
  49. )
  50. const sphere = new Mesh(
  51. new SphereGeometry(1, 32, 32),
  52. new MeshToonMaterial({
  53. color: '#82DBC5',
  54. }),
  55. )
  56. sphere.position.set(2, -2, 0)
  57. const firstGroup = new Group()
  58. firstGroup.add(torus)
  59. firstGroup.add(torusKnot)
  60. const secondGroup = new Group()
  61. secondGroup.add(sphere)
  62. const primitiveRef = ref()
  63. const rotate = () => {
  64. if (primitiveRef.value) {
  65. // This doesn't work
  66. /* torusKnot.rotation.x += 0.01 */
  67. // This does
  68. primitiveRef.value.rotation.x += 0.01
  69. primitiveRef.value.rotation.y += 0.01
  70. }
  71. }
  72. watchEffect(() => {
  73. console.log('primitiveRef.value', primitiveRef.value)
  74. })
  75. /* const reactivePrimitiveRef = ref(new Mesh(
  76. new TorusKnotGeometry(1, 0.5, 100, 16),
  77. new MeshToonMaterial({
  78. color: 'orange',
  79. }),
  80. ))
  81. const modelArray = ref([torus, torusKnot, sphere]) */
  82. </script>
  83. <template>
  84. <TresLeches />
  85. <TresCanvas
  86. v-bind="gl"
  87. ref="canvas"
  88. window-size
  89. class="awiwi"
  90. :style="{ background: '#008080' }"
  91. @render="rotate"
  92. >
  93. <TresPerspectiveCamera
  94. :position="[7, 7, 7]"
  95. :look-at="[0, 0, 0]"
  96. />
  97. <OrbitControls />
  98. <!-- <primitive
  99. :object="reactivePrimitiveRef"
  100. /> -->
  101. <!-- <primitive
  102. v-for="(model, index) of modelArray"
  103. :key="index"
  104. :object="model"
  105. :position="[index * 2, index * 2, 0]"
  106. /> -->
  107. <primitive
  108. v-if="isVisible"
  109. ref="primitiveRef"
  110. :object="knot ? torusKnot : torus"
  111. />
  112. <!-- <Suspense>
  113. <DynamicModel />
  114. </Suspense> -->
  115. <TresAxesHelper :args="[1]" />
  116. <TresDirectionalLight
  117. :position="[0, 2, 4]"
  118. :intensity="2"
  119. cast-shadow
  120. />
  121. </TresCanvas>
  122. </template>