Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by spraycanmansam · Aug 07, 2013 at 05:42 AM · cameraclippingfrustum

Oblique Frustum Clipping

Hi there,

I've been implementing oblique frustum clipping for our water to clip everything above water level. It works great :) For some reason, I can't for the life of me get it to work the other way around, ie: clip everything below water level. I can get it to work with the main camera at 0, but as soon as I try and incorporate the camera's position in any way, it screws up. The draw order reverses and it's almost like the frustum stays at 0 while the camera's moving.

Here's an example of the code I'm using for below, which works fine with clipping everything below the plane.

 public class Clipper : MonoBehaviour 
 {
     Matrix4x4 projection;
     Camera offscreenCam;
     
     void Start () 
     {
         projection               = camera.projectionMatrix;
     }
     
     void OnPreRender() 
     {
         camera.projectionMatrix      = projection;
         Matrix4x4 obliqueProjection  = projection;    
         
         Vector4 cameraSpaceClipPlane = CameraSpacePlane(camera, new Vector3(0.0f, 0.0f, 0.0f), Vector3.up, 1.0f);
         CalculateObliqueMatrix(ref obliqueProjection, cameraSpaceClipPlane);
         camera.projectionMatrix = obliqueProjection;
     }
     
     static Vector4 CameraSpacePlane(Camera cam, Vector3 pos, Vector3 normal, float sideSign)
     {
         Vector3 offsetPos = pos + normal * 0.07f;
         Matrix4x4 m = cam.worldToCameraMatrix;
         Vector3 cpos = m.MultiplyPoint(offsetPos);
         Vector3 point = m.inverse.MultiplyPoint(new Vector3(0.0f, 0.0f, 0.0f));
         cpos -= new Vector3(0.0f, point.y, 0.0f);
         Vector3 cnormal = m.MultiplyVector( normal ).normalized * sideSign;
         return new Vector4(cnormal.x, cnormal.y, cnormal.z, -Vector3.Dot(cpos,cnormal));
     }    
         
     static void CalculateObliqueMatrix(ref Matrix4x4 projection, Vector4 clipPlane)
     {
         Vector4 q = projection.inverse * new Vector4(
             sgn(clipPlane.x),
             sgn(clipPlane.y),
             1.0f,
             1.0f
         );
         Vector4 c = clipPlane * (2.0F / (Vector4.Dot (clipPlane, q)));
         // third row = clip plane - fourth row
         projection[2] = c.x - projection[3];
         projection[6] = c.y - projection[7];
         projection[10] = c.z - projection[11];
         projection[14] = c.w - projection[15];
     }
 }


Any help would be much appreciated!

Thanks! Sam

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why is part of object outside camera frustum visible? 1 Answer

Preventing camera clipping 2 Answers

How to make measurement markers show up, and at scale, in a large scene? 0 Answers

Minimum Near Clip Plane for the camera 2 Answers

Filling the Internal Portion of a Clipped Object 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges