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 DavidMcCl · Aug 09, 2013 at 03:05 PM · raycastlightdirectional light

How Can I Detect If An Object Is Lit By A Specific Light?

I have a scene with a number of objects in it. I want to detect if objects are lit by a specific directional light. I don't want to detect any objects that are within a shadow it casts.

One solution I was looking at was to raycast from relevant objects at an angle equal to the angle the light would be arriving from. However, this seems a little inaccurate, as it only checks if the centre of the object is lit or not. Ideally, I would detect whether any part of the object was lit by the directional light.

Is there an obvious way to improve the accuracy of my check or to straightforwardly check in C# which light is illuminating an object?

Comment
Add comment · Show 7
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 whydoidoit · Aug 09, 2013 at 03:06 PM 0
Share

I guess you could use more than one ray cast to check a few points on the object. There's possibly a complex way using shaders I'd imagine.

avatar image IgorAherne · Aug 09, 2013 at 05:08 PM 0
Share

subscribed

avatar image robertbu · Aug 09, 2013 at 06:22 PM 0
Share

You could improve your algorithm by converting the eight points of the mesh.bounds into world space and using each for your raycast. This will produce some false positives since the bounds is larger than your object.

avatar image Jamora · Aug 09, 2013 at 07:23 PM 0
Share

For future visitors, and hopefully give you a few ideas, I'll link the other questions I found with a similar problem.
Detect light/shadow falling on object?
How to detect if a GameObject is hit by a light

avatar image whydoidoit · Aug 09, 2013 at 07:28 PM 0
Share

I can't quite figure out what you'd have to do with the camera settings to simulate a directional light rather than a spot light - but the shader approach would be:

  • Place a camera at the light's no$$anonymous$$al position, oriented along its forward.

  • Create a render texture (say 256x256) and have the camera render to that

  • Code the vertex colours of the meshes of interest to encode an index for the item that needs to be seen. If there's only one then just have it set to 1. All the vertex colours need to be the same (you could also use UV2 or another vertex property if you are using vertex colouring for something else).

  • Render the camera with a replacement shader that writes the vertex colour of objects

  • Read back the render texture and scan it for any values that aren't 0 - these will be the indices of the items that are seen.

I think that for the case of a directional light you would need to position the camera a distance away from each object of interest in the opposite direction to the light's forward and do it once for each.

Clearly you need to be able to decode the vertex colour from a float to an int - so just remembering that each index would be 1f/255 per colour channel should do it.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by gardian06 · Aug 09, 2013 at 07:18 PM

give the light a sphere collider (marked trigger) place the trigger in the interested area (where the light illuminates the ground hopefully ignoring collisions with the ground), and then give it an OnTriggerXXX() then you can determine what is colliding with that trigger, and grant you the ability to see what is inside the light.

if you want to have more accuracy you could use a capsule collider (sized to the radius of the light), and then do a Vector3.Angle() test, but I am not the most sure on this.

pre EDIT note: a Directional light (defies logic as a directional light illuminates every face with normal vector less then 90 degrees of angle from the direction of the light vector for all intents and purposes everything in the scene) instead of a SpotLight. so I wrote with regards to a SpotLight.

EDIT: as a directional Light effectively illuminates everything in the scene. instead of determining what is illuminated it would be easier to determine what is not illuminated for this I am seeing:

  • have a manager responsible for tracking each detectable object.

  • have the manager hold the negative vector of the directional light

  • foreach of those detectable objects take about 8-12+ points of interest (based on level of accuracy/anal-retention)

  • and fire a ray from those points of interest in the direction of the negative vector of the direction light, and if the ray does not hit anything within distanceX the object is visible.

Comment
Add comment · Show 2 · 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 whydoidoit · Aug 09, 2013 at 07:22 PM 0
Share

That wouldn't cope with the shadows (which is what the OP is asking for).

avatar image gardian06 · Aug 09, 2013 at 08:11 PM 0
Share

@whydoidoit sorry about that Edited to answer question.

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

20 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to detect if an object is under a shadow casted by a directional light 1 Answer

What am i doing wrong? I want to make a raycast acces panel with lights 2 Answers

Directional light between scenes ( .LoadLevel) 3 Answers

using occlusion maps to reduce direct light 0 Answers

Accessing a shadowmap and using it in C# script to turn off colliders? 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