I want to use mouse to control my camera position. The idea is to have a global variable float3 pos that carry the current camera position so that the next update will be from the pos itself.
When I declare a global variable in Metal:
float3 pos;
and get this error:
Global variable must have a constant address space qualifier.
But if I add constant, it become read only thus I cannot update my current position.
An alternative I can think of is using struct. But that seems a bit over do.
What is the best way to do this?
Indeed, only read-only variables (constants) can be defined as globals in MSL. You could do this by sending the mouse coordinates from the host (API) code via a buffer, to your shader. In there update the value of these coordinates and then update the buffer so the host code sees your updated values in real time. Here is a playground that shows you the mouse coordinates in the console as you click inside the rendered area.
answered Nov 21 '25 09:11
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With