2
votes

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?

1

1 Answers

2
votes

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.