Skip to content

Commit

Permalink
feat(core): performance improvement concerning raycaster (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinoooo committed Mar 30, 2023
1 parent 5460d55 commit 597e917
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 23 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"dev": "vite",
"build": "vite build",
"preview": "vite preview",
"playground": "cd playground && nr dev",
"playground": "cd playground && npm run dev",
"test": "vitest",
"test:ci": "vitest run",
"test:ui": "vitest --ui",
Expand Down
2 changes: 1 addition & 1 deletion playground/src/components/TheEvents.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { BasicShadowMap, NoToneMapping, sRGBEncoding } from 'three'
import { reactive } from 'vue'
import { OrbitControls } from '@tresjs/cientos'
import { TresCanvas } from '/@/'
const state = reactive({
clearColor: '#201919',
Expand All @@ -19,7 +20,6 @@ function onClick(ev) {
}
function onPointerEnter(ev) {
console.log(ev)
if (ev) {
ev.object.material.color.set('#DFFF45')
}
Expand Down
10 changes: 4 additions & 6 deletions playground/src/pages/index.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<script setup lang="ts">
</script>
<script setup lang="ts"></script>
<template>
<router-link to="/shapes">Shapes</router-link>
<TheBasic />
</template>
<!-- <router-link to="/shapes">Shapes</router-link> -->
<TheEvents />
</template>
2 changes: 1 addition & 1 deletion playground/src/pages/shapes.vue
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const tubePath = new CubicBezierCurve3(
</script>

<template>
<TresCanvas v-bind="state" >
<TresCanvas v-bind="state">
<TresPerspectiveCamera :position="[5, 5, 5]" :fov="75" :aspect="1" :near="0.1" :far="1000" />

<OrbitControls />
Expand Down
21 changes: 11 additions & 10 deletions src/components/TresCanvas.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { App, defineComponent, h, onUnmounted, ref, watch } from 'vue'
import { App, defineComponent, h, onUnmounted, ref, watch, watchEffect } from 'vue'
import * as THREE from 'three'
import { ShadowMapType, TextureEncoding, ToneMapping } from 'three'
import { createTres } from '/@/core/renderer'
Expand Down Expand Up @@ -50,17 +50,19 @@ export const TresCanvas = defineComponent<TresCanvasProps>({
if (props.physicallyCorrectLights === true) {
logWarning('physicallyCorrectLights is deprecated, useLegacyLights is now false by default')
}

const container = ref<HTMLElement>()
const canvas = ref<HTMLCanvasElement>()
const scene = new THREE.Scene()
const { setState } = useTres()

setState('scene', scene)
setState('canvas', canvas)

onUnmounted(() => {
setState('renderer', null)
})

function initRenderer() {
const { renderer } = useRenderer(canvas, container, props)

Expand All @@ -70,14 +72,13 @@ export const TresCanvas = defineComponent<TresCanvasProps>({

const { raycaster, pointer } = useRaycaster()

onLoop(() => {
if (!activeCamera.value) return

raycaster.value.setFromCamera(pointer.value, activeCamera.value)
renderer.value?.render(scene, activeCamera.value)
watchEffect(() => {
if (activeCamera.value) raycaster.value.setFromCamera(pointer.value, activeCamera.value)
})


onLoop(() => {
if (activeCamera.value) renderer.value?.render(scene, activeCamera.value)
})
}

watch(canvas, initRenderer)
Expand All @@ -103,7 +104,7 @@ export const TresCanvas = defineComponent<TresCanvasProps>({
}

if (import.meta.hot) {
import.meta.hot.on('vite:afterUpdate',dispose)
import.meta.hot.on('vite:afterUpdate', dispose)
}

return () => {
Expand All @@ -113,7 +114,7 @@ export const TresCanvas = defineComponent<TresCanvasProps>({
{
ref: container,
'data-scene': scene.uuid,
key: scene.uuid,
key: scene.uuid,
style: {
position: 'relative',
width: '100%',
Expand Down
9 changes: 5 additions & 4 deletions src/composables/useRaycaster/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Raycaster, Vector2 } from 'three'
import { Ref, ref, ShallowRef, shallowRef } from 'vue'
import { useTres } from '/@/composables'
import { MaybeElementRef, unrefElement } from '@vueuse/core'
import { Raycaster, Vector2 } from 'three'
import { onUnmounted, Ref, ref, ShallowRef, shallowRef, watchEffect } from 'vue'

const raycaster = shallowRef(new Raycaster())
const pointer = ref(new Vector2())
Expand Down Expand Up @@ -47,9 +48,9 @@ export function useRaycaster(): UseRaycasterReturn {
pointer.value.y = -(event.clientY / window.innerHeight) * 2 + 1
}

window.addEventListener('pointermove', onPointerMove)
window.addEventListener('pointermove', onPointerMove) //TODO listener should be on canvas

/* onUnmounted(() => {
/* onUnmounted(() => { TODO
window.removeEventListener('pointermove', onPointerMove)
}) */
return {
Expand Down

0 comments on commit 597e917

Please sign in to comment.