Meaning and usage of viewport, size, aspect, and mouse #857
-
Any time I have to work with conversions between screen and world space, I get a little lost looking at the r3f docs. The API file states that
What is the intuition for what width, height, factor, and distance represent? For instance, if I have a perspective camera, the "width" that's on screen in world space will vary depending on the distance of an object. It seems like there is a fundamental relationship between these variables that's not obvious to me. It would be helpful to see some examples of common use cases for these variables. Singularly, I see there are other variables relating to screen measurements:
The meaning of these variables is more apparent to me, but again it would be helpful to see examples of common use cases for them which might overlap with the viewport stuff. Edit: for reference, here's an example of how I currently do screen->world conversion. It feels like I should be doing it a different way with the variables above. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
viewport is screen width/height in three units. the following would fill the entire screen: const { viewport } the = useThree()
return (
<mesh scale={[viewport.width, viewport.height, 1]}>
<planeBufferGeometry /> size is the size in pixels of the dom container- aspect is just some thing that's necessary for the internal cameras, if you had your own camera you would need to calculate it yourself. i don't know what it is for but i take it some people do - after all they did use it in all of their projects before r3f. mouse are centered, normalized coordinates. -1 is left/top side, 0 middle, 1 is right/bottom side distance how far the camera is away from center point. you can edit the docs if you feel it's not entirely clear. i expect people to also add their own stuff if they need it. these were just the most relevant that i could think of. |
Beta Was this translation helpful? Give feedback.
-
as for your useworldcoords hook function Ball(props) {
const ref = useRef()
useFrame((state) => {
const { mouse, viewport } = state
const { width, height } = viewport()
ref.current.position.set((mouse.x * width) / 2, (mouse.y * height) / 2, 0)
})
return (
<mesh ref={ref} {...props}>
<sphereBufferGeometry args={[0.1, 16, 16]} />
<meshPhysicalMaterial color="hotpink" />
</mesh>
)
} it is limited though. that will only work as long as the camera is pointing from z down. and i think you know now why it works. you take the normalized mouse and just multiply it with width or half divided by two. -1 * width / 2 is left, 1 * width / 2 is right, 0 is center. https://codesandbox.io/s/r3f-mouse-screen-to-world-forked-ybfz7?file=/src/index.js |
Beta Was this translation helpful? Give feedback.
viewport is screen width/height in three units. the following would fill the entire screen:
size is the size in pixels of the dom container-
aspect is just some thing that's necessary for the internal cameras, if you had your own camera you would need to calculate it yourself. i don't know what it is for but i take it some people do - after all they did use it in all of their projects before r3f.
mouse are centered, normalized coordinates. -1 is left/top side, 0 middle, 1 is right/bottom side
distance how far the camera is away from center point.
you can edit the…