Similar to this question, which was never really answered, Beast seems to ignore the culling mask associated with lights.
As a test, I created the following project: a static cube with 2 different colored lights shining on it. One light has it's culling mask set to "Nothing". Without lightmapping, this works fine: only one light shows on the cube. However, when lightmapped, both lights show as if the culling mask has been ignored. This seems to happen regardless of whether the lights are point, directional, or spot. This also still happens when using user layers.
Am I doing something wrong here?
EDIT: Anyone? I can't be the only one with this problem... Is there an alternative way prevent certain lights from lighting a specific object in a lightmap?
Use this script to bake your lightmapping taking into account culling masks:
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class BakeLightmaps {
[MenuItem ("Lightmapping/Bake")]
static void Bake () {
// Get all lights which are active in the scene
Light[] lights = (from light in (Object.FindObjectsOfType (typeof(Light)) as Light[])
where (light.enabled == true && light.gameObject.activeInHierarchy == true) select light).ToArray();
// Get all the game objects which are active in the scene
GameObject[] gameObjects = (from go in (Object.FindObjectsOfType (typeof(GameObject)) as GameObject[])
where ( go.activeInHierarchy == true &&
(GameObjectUtility.GetStaticEditorFlags (go) & StaticEditorFlags.LightmapStatic) > 0 &&
(go.GetComponent<Renderer>() != null || go.GetComponent<Terrain>() != null ))
select go).ToArray();
ILookup<int, GameObject> gameObjectGroups = gameObjects.ToLookup(go => (1 << go.layer));
// Disable all the lights
SetActive (lights, false);
// For each group of gameObjects with a specific layer,
// bake them with the lights that have them in their culling mask
foreach (IGrouping<int, GameObject> gameObjectGroup in gameObjectGroups)
{
int layerForGroup = gameObjectGroup.Key;
GameObject[] gameObjectsForLayer = gameObjectGroup.ToArray();
Light[] lightsForLayer =
(from light in lights where ((light.cullingMask & layerForGroup) > 0) select light).ToArray();
if (lightsForLayer.Count() > 0) {
// Enable the lights for baking
SetActive (lightsForLayer, true);
// Select the GameObjects and do a "Bake Selected"
Selection.objects = gameObjectsForLayer;
UnityEditor.Lightmapping.BakeSelected ();
// Disable the objects
SetActive (lightsForLayer, false);
}
}
// Enable all the lights
SetActive (lights, true);
// Set all lights to lightmapped to avoid double lighting
foreach (Light light in lights){
SerializedObject serializedLight = new SerializedObject (light);
SerializedProperty actuallyLM = serializedLight.FindProperty ("m_ActuallyLightmapped");
actuallyLM.boolValue = true;
serializedLight.ApplyModifiedProperties ();
}
}
static void SetActive (Light[] lights, bool active){
foreach (Light light in lights)
light.gameObject.SetActive (active);
}
}
I think you have two options to do that. If you want just to bake some objects, select them and use Bake Selected.
If you want just to bake selected lights for an object you can disable the lights you don't want for that object and then bake the objects you want using Bake Selected.