Primitives.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <!-- eslint-disable no-console -->
  2. <script setup lang="ts">
  3. import { ref, watchEffect } from 'vue'
  4. import {
  5. BasicShadowMap,
  6. Group,
  7. Mesh,
  8. MeshToonMaterial,
  9. NoToneMapping,
  10. SRGBColorSpace,
  11. SphereGeometry,
  12. TorusGeometry,
  13. TorusKnotGeometry,
  14. } from 'three'
  15. import { TresCanvas, useRenderLoop } from '@tresjs/core'
  16. import { OrbitControls } from '@tresjs/cientos'
  17. import { TresLeches, useControls } from '@tresjs/leches'
  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 } = useControls({
  30. knot: true,
  31. })
  32. const { isVisible } = useControls({
  33. isVisible: true,
  34. })
  35. watchEffect(() => {
  36. if (meshRef.value) {
  37. console.log(meshRef.value)
  38. }
  39. })
  40. const torus = new Mesh(
  41. new TorusGeometry(1, 0.5, 16, 100),
  42. new MeshToonMaterial({
  43. color: '#82DBC5',
  44. }),
  45. )
  46. const torusKnot = new Mesh(
  47. new TorusKnotGeometry(1, 0.5, 100, 16),
  48. new MeshToonMaterial({
  49. color: '#ff00ff',
  50. }),
  51. )
  52. const sphere = new Mesh(
  53. new SphereGeometry(1, 32, 32),
  54. new MeshToonMaterial({
  55. color: '#82DBC5',
  56. }),
  57. )
  58. sphere.position.set(2, -2, 0)
  59. const firstGroup = new Group()
  60. firstGroup.add(torus)
  61. firstGroup.add(torusKnot)
  62. const secondGroup = new Group()
  63. secondGroup.add(sphere)
  64. const primitiveRef = ref()
  65. useRenderLoop().onLoop(() => {
  66. if (primitiveRef.value) {
  67. // This doesn't work
  68. /* torusKnot.rotation.x += 0.01 */
  69. // This does
  70. primitiveRef.value.rotation.x += 0.01
  71. primitiveRef.value.rotation.y += 0.01
  72. }
  73. })
  74. watchEffect(() => {
  75. console.log('primitiveRef.value', primitiveRef.value)
  76. })
  77. /* const reactivePrimitiveRef = ref(new Mesh(
  78. new TorusKnotGeometry(1, 0.5, 100, 16),
  79. new MeshToonMaterial({
  80. color: 'orange',
  81. }),
  82. ))
  83. const modelArray = ref([torus, torusKnot, sphere]) */
  84. </script>
  85. <template>
  86. <TresLeches />
  87. <TresCanvas
  88. v-bind="gl"
  89. ref="canvas"
  90. window-size
  91. class="awiwi"
  92. :style="{ background: '#008080' }"
  93. >
  94. <TresPerspectiveCamera
  95. :position="[7, 7, 7]"
  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>