- Home /
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
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.
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.
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?
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.
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?
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.
Can I call that script between draw calls on the cameras? As both viewports are visible at the same time?
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.
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
Follow this Question
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