- Home /
How I can change lightmaps at runtime?
I have an interior scene of a house, and i need to remove furniture with their generated lightmaps at runtime, and replace it with a set of lightmaps baked previously without furniture?
Thanks in advance.
Is there a simpler way to do this for objects that are either lit or not lit? Once a lightmap is baked onto an object, how can it be accessed and disabled? (For instance, by a button or axis input?)
I am trying to achieve a spotlight effect (saved in lightmap data) that is only active while an object is selected, otherwise it is unlit.
First of all, you shouldn't post questions as Answers. I do not want to sound like a jerk, but there are some protocols that we should follow to keep this awesome tool working as it should:
Video Tutorial for Answers in Unity3d
Second, to achieve what you are ai$$anonymous$$g to do, you have to be aware that every static object in your scene will have a set of Atlas values, you can see those values in your Lightmap window, under Objects/Render tab.
If you do not want to go too much into details, I could suggest you to change the lightmap index value of your desired Render mesh:
Renderer.lightmapIndex
Oops, sorry. I did not realize this would create a second answer. I see the lightmap renderer for what I'm dealing with now. Also the Atlas (listing this as Lightmap Index 0).
The code that ended up working was something like:
void On$$anonymous$$ouseOver{ renderer.lightmapIndex = 0; }
void On$$anonymous$$ouseExit{ renderer.lightmapIndex = 255; }
Sorry again and thanks for the help!
Answer by liszto · Dec 05, 2012 at 12:20 PM
There is another way to do this. You can change lightmapData at runtime and it's an interesting way that I use in one of my project.
You can do something like this : http://pastebin.com/AqwmSCNE
The lightmapData array is the array which stack all the lightmaps used in your level and you can change them when you want.
For example if you need to change the sun orientation and you want that your lightmap change to you can reset this array with your new lightmaps. All your lightmaps must be baked before btw.
When I want do this, I bake all the lightmaps that I need and I bake them with the same number of lightmaps to avoid all problems with offset and tilling on your meshes.
Indeed, if you baked your lightmaps and if the final lightmaps number is not the same you can have different offset and tilling for every mesh in your scene. So one lightmap will be correctly displayed whereas another one maybe not.
If you need more explanations, don't hesitate I can give you more details ;)
Edit : little script to do this simply ==> http://pastebin.com/23FdBnRU
Thanks liszto
Can you give me a two lines of script which I can use for my project two just swapping only two lightmaps. I dont want to use array
Oops! Google Chrome could not find pastebin.com RIP pastebin.com...
Anyone with some examples or suggestions?
$$anonymous$$y pastebin are limited in time. I will paste it again soon if you need them ;).
It's weird cause on my computer the previous links works :
http://pastebin.com/GayDUvQU (try again with this)
There is an example here too : http://answers.unity3d.com/questions/359301/access-use-lightmap-property-using-script.html
Answer by efge · Feb 23, 2011 at 10:13 PM
You could define several materials with different lightmaps and change them at runtime. Look at the example here: Renderer.material
Or you could change the texture for the shader. (You have to find the texture name inside the shader, e.g. "_LightMap".)
Here is the example: SetTexture
Does that work on the shaders other than Legacy Shaders /Lightmapped in Unity3d 3.xx ?? I can not get that to work.
Answer by SuperUltraHyper · Mar 15, 2016 at 01:50 PM
Try this:
using UnityEngine;
public class TransferLightmap : MonoBehaviour {
public Renderer sourceObject;
public Renderer targetObject;
void Start () {
targetObject.lightmapIndex = sourceObject.lightmapIndex;
targetObject.lightmapScaleOffset = sourceObject.lightmapScaleOffset;
targetObject.material.SetTexture("_LightMap", sourceObject.material.GetTexture("_LightMap"));
}
}