Forum rules - please read before posting.

Raycast mouse clicks use camera instead of screen?

Excuse the non-AC information, it's relevant towards the end. As you may know, I'm using Unity's own built-in pixel-perfect 2d camera package to keep my graphics pixel-perfect, and I'm also sending all my UI's into the camera using camera-space, rather than using overlay, to keep pixel-precision consistently on everything. I've been working with one of the developers to allow for 90's-style non-square pixels, which is one of the graphic hallmarks of these games, especially since the c64 font that all Lucasarts games used was designed for non-square pixel ratios. Here is the portion of the pixel-perfect camera script that stretches the viewport vertically so that each pixel appears at a height of 1.2:
`internal Rect CalculatePostRenderPixelRect(float cameraAspect, int screenWidth, int screenHeight)
{
// This VP is used when the internal temp RT is blitted back to screen.
Rect pixelRect = new Rect();

        if (useStretchFill)
        {
            // stretch (fit either width or height)
            float screenAspect = (float)screenWidth / screenHeight;
            if (screenAspect > cameraAspect)
            {
                pixelRect.height = screenHeight;
                pixelRect.width = screenHeight * cameraAspect * 0.8f;
                pixelRect.x = (screenWidth - (int)pixelRect.width) / 2;
                pixelRect.y = 0;
            }
            else
            {
                pixelRect.width = screenWidth;
                pixelRect.height = screenWidth / cameraAspect * 1.2f;
                pixelRect.y = (screenHeight - (int)pixelRect.height) / 2;
                pixelRect.x = 0;
            }
        }`

Obviously, this won't make a lot of sense out of context, but you can see the camera viewport has been modified so visually it fits within the screenspace, but is now slightly taller than it was wide, or slightly thinner.

Here is the camera code before my 1.2 stretch modification:
`internal Rect CalculatePostRenderPixelRect(float cameraAspect, int screenWidth, int screenHeight)
{
// This VP is used when the internal temp RT is blitted back to screen.
Rect pixelRect = new Rect();

        if (useStretchFill)
        {
            // stretch (fit either width or height)
            float screenAspect = (float)screenWidth / screenHeight;
            if (screenAspect > cameraAspect)
            {
                pixelRect.height = screenHeight;
                pixelRect.width = screenHeight * cameraAspect;
                pixelRect.x = (screenWidth - (int)pixelRect.width) / 2;
                pixelRect.y = 0;
            }
            else
            {
                pixelRect.width = screenWidth;
                pixelRect.height = screenWidth / cameraAspect;
                pixelRect.y = (screenHeight - (int)pixelRect.height) / 2;
                pixelRect.x = 0;
            }
        }`

Now, here is an example of how the game and the raycasting looks before I use the stretch mod:
https://gfycat.com/ScrawnyWebbedChevrotain

As you can see, the player walks exactly to the pixel position, and the verbs highlight exactly when the mouse overlaps them precisely.

Here is the same scene, this time with the camera stretch mod:
https://gfycat.com/BewitchedBaggyHammerheadbird

As you can see, the further away from the center of the screen I hover the verbs or click on the navmesh, the less precise the raycast is, since the game is still raycasting as if the screen hadn't been "slimmed down". The mouse clicks and hovers are in exactly the same position as the first clip, while the visuals have noticeably changed.

My question is can AC detect mouse clicks and hovers using the viewport after it has been adjusted? I'm not certain how AC detects clicks or hovers on UI's, but if it's using ScreenPointToRay or some such, would it be possible instead to use ViewportPointToRay so the new aspect ratio can be accounted for in the raycast positions? I'm not aware if it's possible to scale the transofrm of the entire screen raycast vertically by 0.8, but that's effectively what is needed for mouse input positions to match the new camera projection.

Comments

  • You can override AC's perceived mouse position through an override of the InputMousePositionDelegate - see the Manual's "Remapping inputs" chapter.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.