I have a texture in my assets, it has the “lightmap” checkbox marked in the texture importer. I use it to replace a scene lightmap with the following command:
But when I use this new generated texture as lightmap as I did with the asset it gets too bright, just like happened with the asset without the “lightmap” checked.
So my question is: What does Texture importer “lightmap” checkbox and how can I simulate it on the generated texture?
WHat I do is copy the lightmap texture to a new texture, using something similar to what you do. Then I encode it as a PNG and write it to file. Then since this is an ordinary PNG Unity imports it as normal which means we get a texture importer that we can set the lightmap property on. Another thing to note, because the lightmap looks like its encoded between 0 and 0.5 you will need to loop through each texel and double the value.
Something like:
Texture2D texture = LightmapSettings.lightmaps[lightmapIndex].lightmapFar;
// Do lightmap changes here to texture
texture.Apply();
Texture2D newTexture = new Texture2D(texture.width, texture.height,texture.format, true);
for (int x = 0; x < texture.width; x++)
{
for (int y = 0; y < texture.height; y++)
{
Color newColor = texture.GetPixel(x,y);
newColor = new Color(newColor.r * 2, newColor.g * 2, newColor.b * 2, newColor.a * 2);
newTexture.SetPixel(x,y,newColor);
}
}
string path = AssetDatabase.GetAssetPath(texture);
string extension = Path.GetExtension(path);
path = path.Remove(path.Length - extension.Length);
path += ".png";
byte[] bytes = newTexture.EncodeToPNG();
string fullpath = Application.dataPath + "/../" + path;
File.WriteAllBytes(fullpath, bytes);