Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(usetrescontextprovider): fixed rendering issues caused when resize is triggered #512

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 27 additions & 17 deletions src/composables/useTresContextProvider/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { toValue, useElementSize, useFps, useMemory, useRafFn, useWindowSize } from '@vueuse/core'
import { inject, provide, readonly, shallowRef, computed, ref, onUnmounted } from 'vue'
import { toValue, useElementSize, useFps, useMemory, useRafFn, useWindowSize, refDebounced } from '@vueuse/core'
import { inject, provide, readonly, shallowRef, computed, ref, onUnmounted, watchEffect } from 'vue'
import type { Camera, EventDispatcher, Scene, WebGLRenderer } from 'three'
import { Raycaster } from 'three'
import type { ComputedRef, DeepReadonly, MaybeRef, MaybeRefOrGetter, Ref, ShallowRef } from 'vue'
Expand Down Expand Up @@ -55,14 +55,23 @@ export function useTresContextProvider({
: useElementSize(toValue(canvas).parentElement),
)

const width = computed(() => elementSize.value.width.value)
const height = computed(() => elementSize.value.height.value)
const reactiveSize = shallowRef({
width: 0,
height: 0,
})
const debouncedReactiveSize = refDebounced(reactiveSize, 10)
const unWatchSize = watchEffect(() => {
reactiveSize.value = {
width: elementSize.value.width.value,
height: elementSize.value.height.value,
}
})

const aspectRatio = computed(() => width.value / height.value)
const aspectRatio = computed(() => debouncedReactiveSize.value.width / debouncedReactiveSize.value.height)

const sizes = {
height,
width,
height: computed(() => debouncedReactiveSize.value.height),
width: computed(() => debouncedReactiveSize.value.width),
aspectRatio,
}
const localScene = shallowRef<Scene>(scene)
Expand Down Expand Up @@ -113,7 +122,7 @@ export function useTresContextProvider({

// Performance
const updateInterval = 100 // Update interval in milliseconds
const fps = useFps({ every: updateInterval })
const fps = useFps({ every: updateInterval })
const { isSupported, memory } = useMemory({ interval: updateInterval })
const maxFrames = 160
let lastUpdateTime = performance.now()
Expand All @@ -125,7 +134,7 @@ export function useTresContextProvider({
if (toProvide.scene.value) {
toProvide.perf.memory.allocatedMem = calculateMemoryUsage(toProvide.scene.value as unknown as TresObject)
}

// Update memory usage
if (timestamp - lastUpdateTime >= updateInterval) {
lastUpdateTime = timestamp
Expand All @@ -147,35 +156,36 @@ export function useTresContextProvider({
toProvide.perf.memory.accumulator.shift()
}

toProvide.perf.memory.currentMem
toProvide.perf.memory.currentMem
= toProvide.perf.memory.accumulator.reduce((a, b) => a + b, 0) / toProvide.perf.memory.accumulator.length

}
}
}

// Devtools
let accumulatedTime = 0
const interval = 1 // Interval in milliseconds, e.g., 1000 ms = 1 second

const { pause, resume } = useRafFn(({ delta }) => {
if (!window.__TRES__DEVTOOLS__) return

updatePerformanceData({ timestamp: performance.now() })

// Accumulate the delta time
accumulatedTime += delta

// Check if the accumulated time is greater than or equal to the interval
if (accumulatedTime >= interval) {
window.__TRES__DEVTOOLS__.cb(toProvide)

// Reset the accumulated time
accumulatedTime = 0
}
}, { immediate: true })
}, { immediate: true })

onUnmounted(() => {
unWatchSize()
pause()
})

Expand Down