- Home /
Beast lightmap ignores light culling mask and/or layers
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?
I have the exact same issue. Seems very strange. $$anonymous$$ust be a bug in unity?
Answer by David · May 06, 2011 at 01:04 PM
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.
Thanks for the suggestion. Yeah, that would work for the simple cube example. But in a more complex scene there is a problem because Unity/Beast requires you to bake everything that you want to be lightmapped at once. Lets say I have two cubes side-by-side and two lights: one red light, and one green light. How do I lightmap just the green light on cube1 and just the red light on cube2 ? I thought the answer would be culling mask, but Beast ignores that?
Yeah, i understand what you mean. The only way to do it is photoshop. You should do a lightmap for each light working over all the objects and merge all on photoshop. I upload a video to youtube to explain it. That would be the only option right now. Video: http://www.youtube.com/watch?v=DrxQ-ZVFVHc
Answer by ricardo_arango · Jul 19, 2013 at 05:47 PM
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);
}
}
Would there be a way to adapt your algorithm to light probe baking? Inside a scene, I have many groups of lightprobes, and I would like each group to receive light from a different directional.