Search Unity

How can I create a clipping plane for a camera in world space?

Discussion in 'Shaders' started by JoePatrick, Jul 13, 2017.

  1. JoePatrick

    JoePatrick

    Joined:
    Nov 3, 2013
    Posts:
    121
    Hi I want to have a clipping plane on my camera that is similar to the near-clipping plane but instead of being perpendicular to the camera, I want it to be a plane in world space.

    Here is a diagram to help explain, on the left is how the standard near clip plane works (yellow being what is rendered) but I want it to behave like on the right, with the yellow line being a plane in 3d space.

    Thanks :)
     
  2. aryes

    aryes

    Joined:
    Apr 3, 2013
    Posts:
    3
    Calculate world camera position and world vertex position distance by using only x and z using pitagora. Discard every pixel that wants to render before that
     
  3. JoePatrick

    JoePatrick

    Joined:
    Nov 3, 2013
    Posts:
    121
    So is this a shader sort of thing? I am pretty new to shaders so don't fully understand. Would you mind elaborating?
    Thanks
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
  5. JoePatrick

    JoePatrick

    Joined:
    Nov 3, 2013
    Posts:
    121
    Thanks
    The issue with this though is that this shader would have to be applied to every single gameobject in the scene in order to work, I'm looking more for something just on the camera
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    Yep, that's what you're going to have to do.

    Even if Unity exposed user clip planes, a feature of DirectX and OpenGL to allow for arbitrary clipping planes, you'd still need to use a custom shader to take advantage of them.
     
  7. JoePatrick

    JoePatrick

    Joined:
    Nov 3, 2013
    Posts:
    121
    But would it not be possible with a shader on the camera instead, otherwise I can't use any custom shaders at all which is a dealbreaker
     
  8. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    The camera doesn't have a shader, in fact as far as the GPU is concerned the camera isn't a thing that exists. The camera is effectively just a dummy object reference point used to calculate matrices which are to pass to the shaders which do the actual rendering. Everything looks consistent just by virtue of the fact every shader is passed the same view and projection matrices so they all line up in the end.

    Well, more specifically, all your shaders would have to be custom, so there's no problem!

    I suspect what you're actually complaining about is you can't use shaders you get from the asset store or downloaded from someplace. It's true you can't use them unmodified, to do this you would have to hand modify all of them.


    The only way I can think of to do what you want by purely modifying the camera component would be to use a skewed camera projection, which messes with a bunch of other stuff. The camera clipping plane will always be parallel to the view plane (ie: the "screen"), but if you asymetrically warp the projection matrix you can look "down" with out rotating the camera.

    Try the second script here to see what that looks like:
    https://docs.unity3d.com/ScriptReference/Camera-projectionMatrix.html

    You'll quickly see why this isn't a great option either.
     
  9. JoePatrick

    JoePatrick

    Joined:
    Nov 3, 2013
    Posts:
    121

    The issue is I am doing this a part of an Asset I will myself be publishing and it won't be very versatile if users have to edit all of their shaders,
    Perhaps is not called a shader for a camera but what I mean is sort of like post-image effects (e.g. colorisation, warp etc), I was wondering if I could clip the view there.


    here is an example what I mean by the post-image effects
     
  10. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    Image effects work on the image that was rendered. Basically anything you can do to a screenshot of a game in Gimp or Photoshop an image effect can do. You could certainly blank out pixels close to the camera along an arbitrary plane, but that won't make things visible behind those pixels as that data isn't available anymore, it'll just let you set it to a flat color or maybe another texture. Certainly the inverse of this is how a lot of image effect based fog works, it draws a gradient color, or perhaps a copy of the skybox texture, over things as they get further away to make it appear as if the sky is showing through it, when in fact the sky is just being painted on top.
     
  11. JoePatrick

    JoePatrick

    Joined:
    Nov 3, 2013
    Posts:
    121
    Right that makes sense, guess I'm stumped for now :(
     
  12. JoePatrick

    JoePatrick

    Joined:
    Nov 3, 2013
    Posts:
    121
    I did actually figure it out a few days ago, in case anyone else is here looking for a solution check this out
    Camera.CalculateObliqueMatrix

    You can also find more info on my other threads here and here
     
    fuzzy3d and bgolus like this.
  13. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Oblique projection is an option, but that also changes the rendered result (into 2-point projection.)
     
  14. nanotek1127

    nanotek1127

    Joined:
    Jan 10, 2015
    Posts:
    4
    You can actually construct your own projection matrix and set it to the camera. The built-in Matrix4x4 class has a helper function to do that as well. What you want to do is to face the camera perpendicular to the plane you want to clip, then construct the view frustum based on the "window" that you want to look through.
     
  15. JoePatrick

    JoePatrick

    Joined:
    Nov 3, 2013
    Posts:
    121
    I never knew about this so thanks