Search Unity

Get color of final blended material using raycasting

Discussion in 'Shaders' started by liamcroninmail, Dec 2, 2019.

  1. liamcroninmail

    liamcroninmail

    Joined:
    May 28, 2019
    Posts:
    2
    Hi,
    I am trying to return the color from a custom shader that uses three textures. Is it possible to access the final blended color by using raycasting on specific point? I can only access the individual textures which isn't much use. Using hit.textureCoord and Get pixelBilibear. I am not a graphics programmer, i'm not sure how I could do this.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    7,683
    It is not possible, no.

    The shader is running entirely on the GPU.
    Script raycasts are running entirely on the CPU.
    The two know nothing of each other. There's not even any guarantee that the mesh you're raycasting against matches the one the GPU is using, so the texture coordinates you get in either might not be the same, though if you're using a default mesh collider from an imported mesh you're probably fine there.

    There are two options. One is to use the raycast texture coordinate & GetPixelBilinear() for each of the textures and recreate all of the shader code math in c#. The other is to create a camera with a render texture set as its target texture, aim it at the point you want the color from, call Render(), and then use ReadPixels() to get the color value for the pixels at the center of the render texture. This second option is potentially very slow.
     
  3. liamcroninmail

    liamcroninmail

    Joined:
    May 28, 2019
    Posts:
    2
    Thanks for your reply!
    Very helpful! I kind of figured I was screwed here