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 milali · Nov 14, 2011 at 02:29 AM · cameralightingscene

Is it possible to have 2 separate lighting solutions in 1 scene for 2 separate cameras?

Hi,

My question is, I have 2 viewports, one I want to be day view which is currently working.

The other is a night view, I am currently using a colour correction and noise filter to 'simulate' night vision on the 2nd viewport.

What I would really like to do is have one viewport in 'day' view and the other using the a lighting setup for 'night' view.

Is this possible with the layer system? Every time I tried to switch the lights to different layers, it didn't change anything in the scene. (both cameras were not affected)

Thanks

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

2 Replies

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

Answer by Mox.du · Nov 14, 2011 at 03:26 PM

Hi,

here is an update on your subject.

You can do this with layers and light.cullingMask property.

  • Go to Edit -> Project Settings -> Tags
  • Click on the right side of the User Layer 8, and type in "night".
  • Click on the right side of the User Layer 9, and type in "day".
  • Select all objects that you want to be lit by night light, and put them in Layer night (by clicking on Layer drop down in inspector and selecting night).
  • Select all objects that you want to be lit by day light, and put them in Layer day (by clicking on Layer dropdown in inspector and selecting day).
  • Make two new tags - nightLights and dayLights
  • Select all day lights and Tag them in inspector dayLights, all night lights shoud be tagged nightLights.
  • add this script to your day camera:

    function Start(){ dayLightsTagged = GameObject.FindGameObjectsWithTag("dayLights");

    for (var dayLight:GameObject in dayLightsTagged){
    dayLight.light.cullingMask =  1 <<9;
    }
    

    }

    add this one you your night camera:

    function Start(){ nightLightsTagged = GameObject.FindGameObjectsWithTag("nightLights");

    for (var nightLight:GameObject in nightLightsTagged){ nightLight.light.cullingMask = 1 <<8; } }

    As you can see in both scripts we are finding all object with appropriate tags and assigning it to a var. Then we iterate through all of the objects and affecting cullingMask for every light to lit only objects in particular layers.

    Regards.

    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 milali · Nov 14, 2011 at 10:35 PM 0
    Share

    Thanks, I actually did all the steps up until the culling flags I assume your shift of 9 and 8 are becuase the the tags you set where the 9th and 8th. I noticed I could manually set the culling mask on each camera, so its working as you have stated above but without needing the code. Would you see a reason for the code if the culling mask is already set correctly?

    avatar image Mox.du · Nov 15, 2011 at 01:26 AM 0
    Share

    camera culling is something else. You should use this if you want to RENDER ONLY objects on selected layers. What light culling does, as I did here, is LIGHTING ONLY objects on selected layers, so there is a big difference.

    Regards.

    avatar image milali · Nov 15, 2011 at 03:11 AM 0
    Share

    Thanks for following through on this $$anonymous$$ox! I will indeed flip the culling mask for each lights atm I have day models and night models, with your approach I should be able to only have 1 model and have each camera set the lights before it draws. Start though is that called every frame before the draw?

    avatar image
    1

    Answer by Mox.du · Nov 14, 2011 at 04:50 AM

    Hi,

    what you could do is make a script which will enable all "Day lights" if DayLightCamera is on, and disable all "Night lights" if NightLightCamera is off and vice verse like this:

     // attach this to DayLightCamera, and duplicate of the script script to NightLightCamera, only put NightLightNames
         function Start(){
         if(gameObject.camera.enabled){
             GameObject.Find("lightDay").active = true;//enabling light, add more day lights here
             }else GameObject.Find("lightDay").active = false;// disabling lights here
         }
    
    

    Regards.

    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 milali · Nov 14, 2011 at 08:21 AM 0
    Share

    Can I call that script between draw calls on the cameras? As both viewports are visible at the same time?

    avatar image CHPedersen · Nov 14, 2011 at 10:16 AM 0
    Share

    This is not a good solution - GameObject.active is meant for completely disabling and removing a gameobject from existence. Read this:

    http://unity3d.com/support/documentation/ScriptReference/GameObject.Find.html

    You'll notice in that documentation that GameObject.Find only returns active objects. So if you use that code to disable the gameobject in one script, then the gameobjects will stop responding to GameObject.Find in the other script, and you'll get a NullReferenceException because you'd be trying to set GameObject.active on the result of a Find-operation that started returning null, because the GameObject it's looking for just got its .active set to false.

    You need to set enabled = true/false on the light component of the gameobjects ins$$anonymous$$d of setting active = true/false on the entire gameobject. Use GetComponent("Light").enabled to get to the light component.

    avatar image Mox.du · Nov 14, 2011 at 02:40 PM 0
    Share

    Regarding Christian`s comment, he is right.

    I made the script for proof of concept but, as he wrote, you should use GameObject.Find("lightDay").light.enabled = false;

    ins$$anonymous$$d of

    GameObject.Find("lightDay").active = false;

    because next call to Find() will return null.

    As for your comment that both view ports are visible at all time, does this means that you have some sort of monitor which shows two DIFFERENT scene parts? For example: In one part of the scene you have an island where is a day. In another part of the scene you have forest where is night.

    And on split view you are showing both cameras, so both day and night.

    If this is the case I can only think of using spot and point lights with small light radius which will not affect another part of the scene.

    Regards.

    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

    4 People are following this question.

    avatar image avatar image avatar image avatar image

    Related Questions

    Lighting Disabled. Black screen in Game view not in Camera preview 0 Answers

    Original Scene Setup 0 Answers

    Scene view different with camera view/VR view 0 Answers

    Camera not showing in scene view 0 Answers

    Camera appearance black & white 1 Answer


    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