Skip to content

Commit 294bc28

Browse files
committed
Merge branch 'v4' into feature/remove-default-camera-warning
2 parents 12fd5dd + 83f0e06 commit 294bc28

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66
### Features
77

88
* 474 vue chrome devtools plugin ([#479](https://github.com/Tresjs/tres/issues/479)) ([224ab06](https://github.com/Tresjs/tres/commit/224ab06a4404e2ae5a0cbd2f43041961862b09fd))
9+
10+
11+
## [3.6.1](https://github.com/Tresjs/tres/compare/3.6.0...3.6.1) (2024-01-16)
912

1013

1114
### Bug Fixes
1215

1316
* correct minor typos ([#438](https://github.com/Tresjs/tres/issues/438)) ([341faac](https://github.com/Tresjs/tres/commit/341faacb93fd347aced7f1bc7e0484ecbc12e6ce)), closes [#452](https://github.com/Tresjs/tres/issues/452)
1417
* incorrect MathRepresentation type ([#456](https://github.com/Tresjs/tres/issues/456)) ([314b088](https://github.com/Tresjs/tres/commit/314b0883b78ded0cad2bdf9f2506bbeac4a0817e))
18+
* **usetrescontextprovider:** fixed rendering issues caused when resize is triggered ([#512](https://github.com/Tresjs/tres/issues/512)) ([a16b12b](https://github.com/Tresjs/tres/commit/a16b12b160098e97993b14a7bb054103c88b6263)), closes [#511](https://github.com/Tresjs/tres/issues/511)
1519

1620
## [3.6.0](https://github.com/Tresjs/tres/compare/3.5.2...3.6.0) (2023-12-12)
1721

src/components/TresCanvas.vue

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
h,
2222
getCurrentInstance,
2323
} from 'vue'
24+
import pkg from '../../package.json'
2425
import {
2526
useTresContextProvider,
2627
useLogger,
@@ -186,6 +187,7 @@ onMounted(() => {
186187
ref="canvas"
187188
:data-scene="scene.uuid"
188189
:class="$attrs.class"
190+
:data-tres="`tresjs ${pkg.version}`"
189191
:style="{
190192
display: 'block',
191193
width: '100%',

src/composables/useTresContextProvider/index.ts

+27-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { toValue, useElementSize, useFps, useMemory, useRafFn, useWindowSize } from '@vueuse/core'
2-
import { inject, provide, readonly, shallowRef, computed, ref, onUnmounted } from 'vue'
1+
import { toValue, useElementSize, useFps, useMemory, useRafFn, useWindowSize, refDebounced } from '@vueuse/core'
2+
import { inject, provide, readonly, shallowRef, computed, ref, onUnmounted, watchEffect } from 'vue'
33
import type { Camera, EventDispatcher, Scene, WebGLRenderer } from 'three'
44
import { Raycaster } from 'three'
55
import type { ComputedRef, DeepReadonly, MaybeRef, MaybeRefOrGetter, Ref, ShallowRef } from 'vue'
@@ -55,14 +55,23 @@ export function useTresContextProvider({
5555
: useElementSize(toValue(canvas).parentElement),
5656
)
5757

58-
const width = computed(() => elementSize.value.width.value)
59-
const height = computed(() => elementSize.value.height.value)
58+
const reactiveSize = shallowRef({
59+
width: 0,
60+
height: 0,
61+
})
62+
const debouncedReactiveSize = refDebounced(reactiveSize, 10)
63+
const unWatchSize = watchEffect(() => {
64+
reactiveSize.value = {
65+
width: elementSize.value.width.value,
66+
height: elementSize.value.height.value,
67+
}
68+
})
6069

61-
const aspectRatio = computed(() => width.value / height.value)
70+
const aspectRatio = computed(() => debouncedReactiveSize.value.width / debouncedReactiveSize.value.height)
6271

6372
const sizes = {
64-
height,
65-
width,
73+
height: computed(() => debouncedReactiveSize.value.height),
74+
width: computed(() => debouncedReactiveSize.value.width),
6675
aspectRatio,
6776
}
6877
const localScene = shallowRef<Scene>(scene)
@@ -113,7 +122,7 @@ export function useTresContextProvider({
113122

114123
// Performance
115124
const updateInterval = 100 // Update interval in milliseconds
116-
const fps = useFps({ every: updateInterval })
125+
const fps = useFps({ every: updateInterval })
117126
const { isSupported, memory } = useMemory({ interval: updateInterval })
118127
const maxFrames = 160
119128
let lastUpdateTime = performance.now()
@@ -125,7 +134,7 @@ export function useTresContextProvider({
125134
if (toProvide.scene.value) {
126135
toProvide.perf.memory.allocatedMem = calculateMemoryUsage(toProvide.scene.value as unknown as TresObject)
127136
}
128-
137+
129138
// Update memory usage
130139
if (timestamp - lastUpdateTime >= updateInterval) {
131140
lastUpdateTime = timestamp
@@ -147,35 +156,36 @@ export function useTresContextProvider({
147156
toProvide.perf.memory.accumulator.shift()
148157
}
149158

150-
toProvide.perf.memory.currentMem
159+
toProvide.perf.memory.currentMem
151160
= toProvide.perf.memory.accumulator.reduce((a, b) => a + b, 0) / toProvide.perf.memory.accumulator.length
152-
161+
153162
}
154163
}
155164
}
156165

157166
// Devtools
158167
let accumulatedTime = 0
159168
const interval = 1 // Interval in milliseconds, e.g., 1000 ms = 1 second
160-
169+
161170
const { pause, resume } = useRafFn(({ delta }) => {
162171
if (!window.__TRES__DEVTOOLS__) return
163172

164173
updatePerformanceData({ timestamp: performance.now() })
165-
174+
166175
// Accumulate the delta time
167176
accumulatedTime += delta
168-
177+
169178
// Check if the accumulated time is greater than or equal to the interval
170179
if (accumulatedTime >= interval) {
171180
window.__TRES__DEVTOOLS__.cb(toProvide)
172-
181+
173182
// Reset the accumulated time
174183
accumulatedTime = 0
175184
}
176-
}, { immediate: true })
177-
185+
}, { immediate: true })
186+
178187
onUnmounted(() => {
188+
unWatchSize()
179189
pause()
180190
})
181191

0 commit comments

Comments
 (0)