Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to get raycasthit.texturecoord from array of color32

Discussion in 'Editor & General Support' started by LevonRavel, Feb 12, 2020.

  1. LevonRavel

    LevonRavel

    Joined:
    Feb 26, 2014
    Posts:
    179
    I am trying to reference a pixel from the colors array from hit.textureCoord. Basically instead of setting the pixel every time I want to set the hit.textureCoord to a temporary array then assign the array back via SetPixels32.

    Code (CSharp):
    1. var colors = texture2d.GetPixels32();
    2. var textureCoord = hit.textureCoord;
    3. var hitColor = colors[?? hit.textureCoord];
    4. //how do I transform the hit.textureCoord to the array??
    actual code this is called from x y for loop to set the pixels, I figure to speed it up I can reference the pixels in an array then assign the hit.textureCoord to the referenced colors.

    Code (CSharp):
    1.     private void PaintPixel(Color color, Vector2 position)
    2.     {
    3.         if (color.a == 0) return;
    4.         if (color == Color.black) return;
    5.         ray = cam.ScreenPointToRay(position);
    6.         if (!Physics.Raycast(ray, out hit, 1)) return;
    7.         pCoord = hit.textureCoord;
    8.         pCoord.x *= SquaredImage.width;
    9.         pCoord.y *= SquaredImage.height;
    10.         SquaredImage.SetPixel((int)pCoord.x, (int)pCoord.y, color); <-- This has to be slow asf
    11.     }
    whole code body

    Code (CSharp):
    1.     public void Initiate(Texture2D raycastImage)
    2.     {
    3.         cam = Camera.main;
    4.         ModelTexture.material = CameraPreview.Instance.StandardShader;
    5.         modelTexture = ModelTexture.material.mainTexture as Texture2D;
    6.         SquaredImage = new Texture2D(modelTexture.width, modelTexture.height);
    7.         SquaredImage.SetPixels32(modelTexture.GetPixels32());
    8.         var startX = Screen.width / 6;
    9.         var endX = (Screen.width / 6) * 5;
    10.         var endY = (Screen.height / 8) * 6;
    11.         var startY = Screen.height / 8;
    12.         for(int x = startX; x < endX; x++)
    13.         {
    14.             for(int y = startY; y < endY; y++)
    15.             {
    16.                 var pos = new Vector3(x, y, 0);
    17.                 PaintPixel(raycastImage.GetPixel(x, y), pos);              
    18.             }
    19.         }
    20.         SquaredImage.Apply();
    21.         Model.GetComponent<MeshRenderer>().material.mainTexture = SquaredImage;
    22.     }
    23.     private void PaintPixel(Color color, Vector2 position)
    24.     {
    25.         if (color.a == 0) return;
    26.         if (color == Color.black) return;
    27.         ray = cam.ScreenPointToRay(position);
    28.         if (!Physics.Raycast(ray, out hit, 1)) return;
    29.         pCoord = hit.textureCoord;
    30.         pCoord.x *= SquaredImage.width;
    31.         pCoord.y *= SquaredImage.height;
    32.         SquaredImage.SetPixel((int)pCoord.x, (int)pCoord.y, color);
    33.     }
     
    Last edited: Feb 12, 2020