Search Unity

[Question] Rendering inside a square

Discussion in 'General Graphics' started by FreshDumb, Jun 20, 2019.

  1. FreshDumb

    FreshDumb

    Joined:
    Mar 7, 2018
    Posts:
    4
    Hello Everyone,

    I am currently working on a game using 3D Assets in a 2D game and have a problem, that seems pretty simple, but turns out it probably isn't?
    Searching the Forum i couldn't find anything fitting and i just need a pointer where to look, because there are probably a number of ways to achieve what i want.

    The player can activate a "field" device which creates a square around the player and then affects everything inside this square. Now i want to cut off everything at the edges of the square and only render whatever is inside the bounds of the square. Since i am not an artist i hope my incredibly art skills suffice to explain what i want. :D

    upload_2019-6-20_17-16-19.png

    So i want to render everything inside the Square, including parts of objects that are half in and half outside the square.
    The Camera is orthographic for a number of reasons, but i think that makes it easier anyway.

    I don't really know how to write shaders, but have a very basic grasp of how they work.
    Any help or input would be highly appreciated. :)

    Grandiose Greetings,
    FreshDumb
     
  2. dmennenoh

    dmennenoh

    Joined:
    Jul 1, 2015
    Posts:
    379
  3. FreshDumb

    FreshDumb

    Joined:
    Mar 7, 2018
    Posts:
    4

    Hm... I was considering Stencil Buffer. But doesn't that mean the cropping is on the Material of the Objects themselves?
    Couldn't a camera discard any pixel outside of the square? Like i said i am not very versed in Shaders, so not sure if those are stupid questions. :p

    Thanks for the Reply, nevertheless. :)
     
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,635
    Yes, but only because your area is square-shaped and you are using an orthographic view. You can have a second camera render your "inside the square" stuff to a render texture and apply the render texture to a quad that is attached to your main camera. Let the quad be your square.
     
  5. FreshDumb

    FreshDumb

    Joined:
    Mar 7, 2018
    Posts:
    4

    Hm... Also good Idea, i'll look into that as well. Thanks. :)
     
  6. BattleAngelAlita

    BattleAngelAlita

    Joined:
    Nov 20, 2016
    Posts:
    400
    You can make some dummy boxes that represents outside part of a window. Then apply to them shader that writes to depth, but don't write to color.
     
  7. FreshDumb

    FreshDumb

    Joined:
    Mar 7, 2018
    Posts:
    4
    I have delved into shaders a bit now and got a little closer to what i want, but i think i am still way to nooby to know whats going wrong. :D

    Code (CSharp):
    1.  
    2.                 fixed4 frag(v2f_img input) : COLOR
    3.                 {
    4.                     float alpha = 1;
    5.                     float4 base = tex2D(_MainTex, input.uv);
    6.  
    7.                     if (input.uv.x <= _LeftBound)
    8.                         alpha = 0;
    9.  
    10.                     if (input.uv.x >= _RightBound)
    11.                         alpha = 0;
    12.  
    13.                     if (input.uv.y <= _BottomBound)
    14.                         alpha = 0;
    15.  
    16.                     if (input.uv.y >= _TopBound)
    17.                         alpha = 0;
    18.  
    19.                     return base * alpha;
    20.                     }
    21.                     ENDCG
    22.                 }
    I wrote a fragment shader multiplying the pixels outside the bounds with 0 so they aren't rendered.
    This works pretty good and gives the desired effect, but there are still some bugs and i have no Idea what is happening.

    I have a Camera rendering everything else on a lower depth and am rendering the inside the square stuff on top.

    ShaderBugs.gif

    But the Stuff outside the Square isn't cleared properly? In the .gif you see the green cube being rendered where it was and not updating. It shouldn't be visible outside the square. And when i deactivate the Camera and reactivate it, this fixes itself.

    Anyone got any Ideas? :D
     

    Attached Files: