Search Unity

  1. 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

problem with extrapolating parts of a texture

Discussion in 'General Graphics' started by MaeL0000, Oct 9, 2018.

  1. MaeL0000

    MaeL0000

    Joined:
    Aug 8, 2015
    Posts:
    35
    I have a plugin that returns me a Texture that contains two images one above the other like so:

    the top image is the right camera output of the Vive Pro and the bottom is the left camera.
    I need to put these images side by side, with the left camera output on the left like so:

    Is there a way to do this that isn't CPU intensive?

    Blitting won't suffice since there's no way of specifying which part of the rendertexture you want to blit into, right?
    I've looked into shaders, but even if I manage to separate the images into two separate textures I can't figure out how to write the shader so that I can put those two textures side by side.
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    6,245
    you could have single plane mesh (or 2 quads), with suitable UV mapping.
    so that the top texture is drawn on left half and bottom texture to right side.
     
  3. brownboot67

    brownboot67

    Joined:
    Jan 5, 2013
    Posts:
    365
    It's possible to make an inefficient shader to do everything you need, but mgear's solution is the best.
     
  4. MaeL0000

    MaeL0000

    Joined:
    Aug 8, 2015
    Posts:
    35
    @mgear unfortunately I don't think I can do that! If I understood correctly, you're saying to create a mesh and tailor the UV mapping right? If so, that won't work because I'm supplying the texture to an OpenVR Overlay, which just takes a texture in input and uses it's own mesh internally I'm guessing.
     
  5. mbowen89

    mbowen89

    Joined:
    Jan 21, 2013
    Posts:
    639
    Maybe I am misunderstanding, but why can't you just make a Texture2D and do a getpixel from the source and setpixel on the new texture you are making?
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    9,290
    Because that is really slow. However, it's along the right idea.
    https://docs.unity3d.com/ScriptReference/Graphics.CopyTexture.html
    CopyTexture supports copying texture regions from one texture to another with offsets into the destination.
     
  7. MaeL0000

    MaeL0000

    Joined:
    Aug 8, 2015
    Posts:
    35
    I completely missed that!
    Unfortunately it's not working correctly.
    This is the source texture:

    and this is the destination texture:

    this is simply using
    Code (CSharp):
    1. Graphics.CopyTexture(texture, 0, 0, combinedTexture, 0, 0);
    while using
    Code (CSharp):
    1. Graphics.CopyTexture(texture, 0, 0, 0, 0, 1224, 920, combinedTexture, 0, 0, 0, 0);
    gives me this:
    (the intent would be to take just the bottom image)

    As you can see, the source texture is 0 bytes, because it's actually an external texture (created through Texture2D.CreateExternalTexture()), could that have to do with it?

    EDIT: never mind, apparently texture.width/height is unreliable on externally created textures, you should be getting width & height from the plugin that is creating the external texture.

    P:S: Also, is CopyTexture faster than Blit for simplying copying a texture to a rendertexture?
     
    Last edited: Oct 11, 2018
  8. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    9,290
    It should be much faster, or at least no slower.
     
unityunity