Search Unity

ReadPixels on Cubemap RenderTexture: How to read more than one side?

Discussion in 'Shaders' started by hadynlander, Jul 19, 2016.

  1. hadynlander

    hadynlander

    Joined:
    Jan 24, 2014
    Posts:
    41
    ReadPixels only seems to grab the first of the six sides of a Cubemap RenderTexture. I'm wondering if there's a neat way to grab the others?

    I know I could just use six standard RenderTextures, but it feels much neater if I can get away with a single CubeMap one...

    Any advice would be appreciated!
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Afaik there's no way to use ReadPixels to get all faces of a rendertexture set to be a cubemap, or really any way to get CPU side access to the pixels of a rendertexture beyond the first 2d plane.

    The questions I would have is why do you need ReadPixels, and is this something you could do on the GPU instead?
     
    hadynlander likes this.
  3. hadynlander

    hadynlander

    Joined:
    Jan 24, 2014
    Posts:
    41
    Thanks for the answer.

    To answer your question - it's a bit of a weird, experimental case. I want to find the average direction of the nearest surfaces around the player character in a zero-gravity game. I was curious whether I could get better accuracy/performance by using a low-res cubemap render of the depth/direction of the surrounding area (which I'm doing via shader replacement) and averaging that (thus the CPU processing) as opposed to the more common solution of using a spiky ball of ray-casts.

    My thinking is that it essentially shifts the load of raycasting to the GPU, which might offset the CPU cost of processing the captured textures... but I really have no idea. Mostly it just sounded like an interesting thing to try!
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Generally getting any data from the GPU to the CPU is really slow, usually slower than anything you can do on the CPU by itself. This isn't always the case, it's more a wrinkle of how Unity handles it as GPUs have a way to request data from it and let you wait until it shows up (maybe a frame or two later) but Unity implemented it in a way that the entire game stalls while waiting for the GPU to respond.

    You might want to look into using a compute buffer to iterate over the cubemap and spit out the data you need (pre-averaged) into a compute buffer which should have a more optimized path for getting data out.
     
    hadynlander likes this.
  5. hadynlander

    hadynlander

    Joined:
    Jan 24, 2014
    Posts:
    41
    Thanks for the tip - I've never used compute buffers before, but it sounds like a promising lead.