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
12
Question by Patrik · Jul 14, 2010 at 10:21 AM · camerapoints

How to check if a point is seeable by a camera

Hi. I need to know if a point can be seen by a certain camera or not. Now I'm converting the point world coordinates (as Vector3) to camera Viewport coordinates using Camera.WorldToViewportPoint, then I check if x and y values are outside the range [0..1] for both axis, but I suspect there is an easier way. Any ideas?

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

5 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by Mike 3 · Jul 14, 2010 at 10:35 AM

Your way is actually one of the easier ways actually - the alternative way I can think of is to check if the point lays in the camera's frustum, which is probably more code

Comment
Add comment · Show 3 · Share
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
avatar image Patrik · Jul 14, 2010 at 11:02 AM 1
Share

yes, but I noticed my way does not take in account the possibility that a point is inside the camera viewport but behind another object. How do i check camera's frustrum? doesn't unity provide a primitive to check if a point is visible by camera?

avatar image Mike 3 · Jul 14, 2010 at 11:16 AM 2
Share

No, calculating that in cpu is generally heavier than just drawing the entire scene (And not to mention that objects can be transparent, so generalization wouldnt be a good idea there). What you could do is, after doing your initial check, raycast from the camera to the point, and see if it doesn't hit anything - if it doesn't, the point is visible

avatar image Patrik · Jul 14, 2010 at 12:07 PM 0
Share

Well, it seems that project requirements dictate that I have only to check the view volume, eventual occlusions are irrelevant. tanks anyway

avatar image
8

Answer by atomicjoe · Aug 15, 2016 at 03:03 PM

use Camera.WorldToViewportPoint, check if X and Y are within 0..1 range AND check Z>0

(if Z is not greater than 0 it means the point is behind the camera.)

Comment
Add comment · Show 1 · Share
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
avatar image kamyflex · Mar 22, 2017 at 11:30 PM 0
Share

Thank you so much, its working :)

avatar image
1

Answer by UnityPlum · Oct 11, 2021 at 04:45 PM

@patrik Hello! I know you already have some answers but incase someone needs some code for this I will provide it. Also, these answers do not take into account the point being behind an object. Here is some tested and ran code that does it.

 public Camera camera;
 public bool PointInCameraView(Vector3 point) {
         Vector3 viewport = camera.WorldToViewportPoint(point);
         bool inCameraFrustum = Is01(viewport.x) && Is01(viewport.y);
         bool inFrontOfCamera = viewport.z > 0;
 
         RaycastHit depthCheck;
         bool objectBlockingPoint = false;
 
         Vector3 directionBetween = point - camera.transform.position;
         directionBetween = directionBetween.normalized;
 
         float distance = Vector3.Distance(camera.transform.position, point);
 
         if(Physics.Raycast(camera.transform.position, directionBetween, out depthCheck, distance + 0.05f)) {
             if(depthCheck.point != point) {
                 objectBlockingPoint = true;
             }
         }
 
         return inCameraFrustum && inFrontOfCamera && !objectBlockingPoint;
     }
 
 public bool Is01(float a) {
         return a > 0 && a < 1;
     }
Comment
Add comment · Share
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
avatar image
0

Answer by Bezzy · Mar 12, 2018 at 10:39 PM

(This thing stings people every time it's used, haha. Solidarity!).

Comment
Add comment · Share
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
avatar image
0

Answer by slake_it · Jul 23, 2019 at 09:12 AM

I think you need to ensure both x and y are within [0-1] ( not outside ) - if any value is outside 0-1 range then the point is not visible - also if z < 0 then I think object is behind camera

Comment
Add comment · Share
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

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

6 People are following this question.

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

Related Questions

Camera problem 2 Answers

How to make camera position relative to a specific target. 1 Answer

Camera spaced normals for a surface shader 1 Answer

How do I move in the Third Person? 0 Answers

Scale Vertices propotional to Camera Frustum 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