Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Get pixel world position

Discussion in 'Shaders' started by IlisanVlad, Mar 2, 2020.

  1. IlisanVlad

    IlisanVlad

    Joined:
    Dec 3, 2017
    Posts:
    30
    Hi. I am trying to get the world position of a pixel inside a fragmented shader.

    This is for calculating the distance between the pixels world position and the mouse click position and then passed the RaycastHit to the shader. With this I want to modify the pixels that are in a certain distance from the click.

    I don't think it matters but I am using Graphics.Blit at runtime to pass the texture to a material with default shader.

    Thank you!
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    10,090
    This does matter. It also doesn’t make complete sense.

    When an object renders using the material & shader assigned to it, you can trivially get the world position for each pixel.

    If you’re calling Blit() then you’re rendering a full screen quad aligned to the screen to a render texture or the current render target with no connection to the meshes in the scene. It’s not even using the camera’s transform & projection matrices when rendering that quad, so you can’t even calculate that mesh’s “world position” as you don’t have access to the information you’d need to be able to calculate that. That can be worked around by using the camera's depth texture and passing the current inverse view projection matrix to the material to reconstruct the world position of opaque objects in the scene.

    However I'm super confused by that original statement of using "blit at runtime to pass the texture to a material with default shader". As mentioned above, Blit is used to render a full screen quad. If by "default shader" you mean you're calling
    Graphics.Blit(myTexture, someRenderTexture);
    that means you're using the default blit shader to render
    myTexture
    into
    someRenderTexture
    , which is useful if you need to copy an existing texture into another where the two textures have different resolutions or formats. But if the goal is to eventually hand that texture to a material, why not just use the original texture?
     
  3. IlisanVlad

    IlisanVlad

    Joined:
    Dec 3, 2017
    Posts:
    30
unityunity