Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question Correctly clipping textures rendered by seperate cameras

Discussion in 'Shaders' started by JakeWHO, Aug 8, 2022.

  1. JakeWHO

    JakeWHO

    Joined:
    Jul 30, 2021
    Posts:
    4
    Hey,
    To preface this I'm pretty new into the shaders and general GPU side of development so not very versed in technicalities of this stuff.
    I've been looking to create a system that pixelates only certain objects within a scene. I've tried various ways of doing this but landed on having two cameras, one that renders pixelated objects via a layer/Culling Mask, and another camera that renders everything else. The pixelated camera runs a pixelating Compute Shader in its OnRenderImage function then sets this RenderTexture to the secondary texture of another shader which, Blit'd with the normal cameras Render Texture, renders the two cameras together.

    On a simple scene with no changing variables this gives the exact visual effect I'm looking for, however what might be evident already is that since the pixelated objects and everything else are under different lables, there is absolutely no clipping or culling happening between the two. I've tried various things including trying to use _CameraDepthTexture aswell as going down a rabbit hole of manually rendering seperate depth textures for each of my cameras so that I can perform some kind of manual clipping from within the shader but none of these have come to a satisfying conclusion.

    There's got to be something simple I'm missing here as there seems to be very little resources of people attempting similar techniques but as I see it at the moment I can either pixelate the whole screen with correct clipping or pixelate the objects I want with incorrect clipping.

    Image 1: These are my two Camera scripts
    cameras.png

    Image 2: The shader that handles combining the two views aswell as the simple compute shader for pixelizing the entire screen.
    scripts.png

    Image 3: The lack of clipping shown in unity
    clippingissue.png

    Image 4: Another example
    clipping2.png

    Again I'm sure theres something simple I'm missing here but all the resources I've looked at to try and fix this either don't achieve the same effect that I'm looking for or are so convoluted to the point it seems I've searched the wrong term (URP stack cameras and seperate shaders for each camera was a mess). If theres any way for example I can write depth to the alpha channel of my RenderTexture or something similar that I can work with from within the shader that would be perfect but I'm unsure.

    Image hosts incase embed doesnt work:
    Image 1: https://prnt.sc/JvwOAixX9Agi
    Image 2: https://prnt.sc/X-B0MtGWRbVl
    Image 3: https://prnt.sc/Epe6m3zoN_S2
    Image 4: https://prnt.sc/QQYQKpLlSueN

    Thanks,
    -Jake
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,402
  3. JakeWHO

    JakeWHO

    Joined:
    Jul 30, 2021
    Posts:
    4
    Thanks for the reply but I'll admit that I already found a solution which is slightly different so I'll post it anyway. Since I'm rendering my pixelated Camera at full resolution and using a compute shader to downscale it (rather than using a lower resolution Render Texture), the method I was looking for was
    ComputeShader.SetTextureFromGlobal
    .
    I could then sample from this in my compute shader and append it to the alpha channel of my outputted, downscaled texture. After computing the downscaled texture, I then set it to the secondary texture of the shader I use to blit the main camera view which will then compare to the alpha with the CameraDepthTexture from the main camera and draw the appropriate pixel. Might not have explained that the best but heres the accompanying code.
    answer.png

    And the result it gives
    answerresult.png
    This however does have the same issues as yours in the case of shadows and transparent objects but I'm willing to believe that since I have both cameras depth textures easily accessible that if I ran into that problem then I could probably deal with this is some way (perhaps writing to the ZBuffer during the combination shader) but it seems to be a lot more simple than a lot of similar techniques that I've seen on youtube and the like that insert methods directly into the pipeline which seemed a bit out of my skill range