Replies: 1 comment 1 reply
-
you can get the root store via const store = useStore() daishi removed the selector, you can quickly avoid that warning by just checking old against new let oldSize = store.getState().size
store.subscribe(() => {
const { size } = store.getState()
if (size !== oldSize) {
...
oldSize = size
}
}) though again i would love to fix the root issue for you. as it stands r3f calls gl.setSize. it seems to me like your solution works, but at best doubles the work and at worst is a race condition. i could define "manual" as literally do nothing at all, no resize. this is the current subscribe that handles resize: // Resize camera and renderer on changes to size and pixelratio
let oldSize = state.size
let oldDpr = state.viewport.dpr
rootState.subscribe(() => {
const { camera, size, viewport, internal } = rootState.getState()
if (size !== oldSize || viewport.dpr !== oldDpr) {
// https://github.com/pmndrs/react-three-fiber/issues/92
// Do not mess with the camera if it belongs to the user
if (!camera.manual && !(internal.lastProps.camera instanceof THREE.Camera)) {
if (isOrthographicCamera(camera)) {
camera.left = size.width / -2
camera.right = size.width / 2
camera.top = size.height / 2
camera.bottom = size.height / -2
} else {
camera.aspect = size.width / size.height
}
camera.updateProjectionMatrix()
// https://github.com/pmndrs/react-three-fiber/issues/178
// Update matrix world since the renderer is a frame late
camera.updateMatrixWorld()
}
// Update renderer
gl.setPixelRatio(viewport.dpr)
gl.setSize(size.width, size.height)
oldSize = size
oldDpr = viewport.dpr
}
}) so how about rootState.subscribe(() => {
const { camera, size, viewport, internal } = rootState.getState()
if (size !== oldSize || viewport.dpr !== oldDpr) {
if (!camera.manual && !(internal.lastProps.camera instanceof THREE.Camera)) {
...
// Set renderer size
gl.setSize(size.width, size.height)
}
// Update renderer
gl.setPixelRatio(viewport.dpr) if you set the default cam to manual you'd be responsible for sizing the renderer. would this work for you? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Following @drcmda's suggestion here I can get access to the root store inside a component:
Note that I'm doing this in a normal component inside the canvas so maybe there's a simpler way of accessing it, but in any case, this works.
Next, I want to subscribe to
size
for manually handling resizing (I'm using the newcamera.manual
prop)Over in the camera component I now do this:
Resizing the canvas causes this to fire and the size is logged to the console, so great!
However, because of the recent changes to Zustand it also logs this deprecation warning:
Is there any plan to add the
subscribeWithSelector
middleware? Or if not, is there some other way I can avoid this warning? Wrap the store in another Zustand store that does use the middleware, perhaps?Beta Was this translation helpful? Give feedback.
All reactions