Home > database >  Raycast not capturing all Vector coordinates
Raycast not capturing all Vector coordinates

Time:09-19

I have a gameobject that occupies the whole screen just for testing purposes. I'm drawing a line btw. What I'm trying to achieve is if the mouse position hits a gameobject it will store the vector2 coordinates in a list. But raycast is not storing all the coordinates. Below is my code

private void Update()
{
    if (Input.GetMouseButton(0))
    {
        Vector2 mousePos = Input.mousePosition;

            Vector2 Pos = _camera.ScreenToWorldPoint(mousePos);

            if(!mousePositions.Contains(Pos))
                mousePositions.Add(Pos);


            if (Physics.Raycast(Camera.main.ScreenPointToRay(mousePos), out RaycastHit hit))
            {
                Vector2 textureCoord = hit.textureCoord;

                int pixelX = (int)(textureCoord.x * _templateDirtMask.width);
                int pixelY = (int)(textureCoord.y * _templateDirtMask.height);

                Vector2Int paintPixelPosition = new Vector2Int(pixelX, pixelY);

                if (!linePositions.Contains(paintPixelPosition))
                    linePositions.Add(paintPixelPosition);

                foreach (Vector2Int pos in linePositions)
                {
                    int pixelXOffset = pos.x - (_brush.width / 2);
                    int pixelYOffset = pos.y - (_brush.height / 2);

                    for (int x = 0; x < _brush.width; x  )
                    {
                        for (int y = 0; y < _brush.height; y  )
                        {
                            _templateDirtMask.SetPixel(
                                pixelXOffset   x,
                                pixelYOffset   y,
                                Color.black
                            );
                        }
                    }
                }

                _templateDirtMask.Apply();
            }
    }
}

Everytime I checked the element count mousePositions are always greater than linePositions. I don't know what's causing this

CodePudding user response:

It happens because there is really could be a case, when several elements from mousePositions are associated with one elment from linePositions.

Rough example: your texture resolution is only 1x1px. In this case you linePositons will contain only one element. And this element will be associated with all elements from mosePositions.

So, relation of the number of elements in these lists depends on relation of your texture and screen resolutions.

CodePudding user response:

the element count mousePositions are always greater than linePosition

well it is quite simple: In

 int pixelX = (int)(textureCoord.x * _templateDirtMask.width);
 int pixelY = (int)(textureCoord.y * _templateDirtMask.height);

you are casting to int and cut off any decimals after the comma (basically like doing Mathf.FloorToInt).

So you can totally have multiple mouse positions which result in float pixel positions like e.g.

1.2, 1.2
1.4, 1.7
1.02, 1.93
...

all these will map to

Vector2Int paintPixelPosition = new Vector2Int(1, 1);

Besides, you might want to look at some better line drawing algorithms like e.g. this simple one

And then note that calling SetPixel repeatedly is quite expensive. You want to do a single SetPixels call like e.g.

var pixels = _templateDirtMask.GetPixels();

foreach (Vector2Int pos in linePositions)
{
    int pixelXOffset = pos.x - (_brush.width / 2);
    int pixelYOffset = pos.y - (_brush.height / 2);

    for (int x = 0; x < _brush.width; x  )
    {
        for (int y = 0; y < _brush.height; y  )
        {
            pixels[(pixelXOffset   x)   (pixelYOffset   y) * _templateDirtMask.width] = Color.black;
        }
    }
}

_templateDirtMask.SetPixels(pixels);
_templateDirtMask.Apply();
  • Related