I’m creating a plugin that has to let people select parts that are rendered inside a viewport frame. I’ve already created a script that finds where they clicked on the viewport frame and scales it down to the camera’s viewport size, but the ray it generates isn’t correct.
Below is a video of the behavior, where the part that turns white is supposed to be the part i click on.
I’ve already tried a lot of things to fix this, and currently the closest i’ve gotten to fixing the issue was by using the script below.
local x = mouse.X - viewportFrame.AbsolutePosition.X - 0.5 * viewportFrame.AbsoluteSize.X
local y = mouse.Y - viewportFrame.AbsolutePosition.Y - 0.5 * viewportFrame.AbsoluteSize.Y
local max = math.max(viewportFrame.AbsoluteSize.X, viewportFrame.AbsoluteSize.Y)
x /= max
y /= max
x += 0.5
y += 0.5
local ray = viewportCamera:ViewportPointToRay(x, y)
I divide by the maximum of the x and y sizes because the viewport frame doesn’t show the whole camera’s view, since the camera’s viewport size is 1x1, and therefore a square, unlike the viewport frame.
Does anyone have any ideas why this isn’t working?
The viewportframe is a rectangle in the UI, but the ViewportCameras projection is a perfect square. Unless you make the UI also a square, your running into issues where 2 sides are getting cropped off.
You might be dividing using the maximum of the x and y sides because of the non square frame, but your scaling BOTH by the max, which means one of the axis is correct, and one is wrong.
You should try a system where you find out where in the frame the player clicked, as a number from 0-1 on each axis (x,y). Then convert those values into pixel coordinates, in the cameras actual render space, taking into account the issue with of the aspect ratio mismatch, which then you put into ViewportPointToRay.
I don’t understand what you mean by converting it into pixel coordinates? 0-1 is pixel coordinates with cameras that aren’t used by the player because the viewport size is (1, 1), meaning that the entire view of the camera is within a single pixel, so I have to use subpixel measurements.
Dividing both axes by the maximum size is what keeps the correct aspect ratio. I’ve already tried dividing by the individual sizes, but it doesn’t work because it assumes that the aspect ratio of the camera is the same, but in reality on the viewport frame parts of the camera’s vision is cut off, in this case, the top and bottom.
No, when its a square it works correctly, but I’m pretty certain that what you suggested wouldn’t work because I had already tried it before making the post. If you want, I can test some code that would use your method if you can either give me the exact math I would have to do or just write it yourself.
What do you mean? I’m raycasting in workspace, but the issue i’m having is that the ray isn’t correct. The user’s camera in studio is different than their camera in the viewport frame, so using their normal camera wouldn’t work. I’d also like to keep it within a gui instead of using their actual camera.
I was unaware I could do that, thanks for telling me, it makes the raycasting slightly easier, but it still doesn’t solve my issue. The issue isn’t that the raycasting doesn’t work, because it does, the issue is that the ray that i’m using is facing in the wrong direction.
I’ve found the issue, I was dividing by the maximum of the sizes of the axes but I was supposed to divide by the y regardless of which was bigger or smaller. I suppose rather than fitting the view within the size of the camera, viewports just scale the y to be the same as the camera and just render wider than the camera’s view if it’s wider than it is tall