Lightmaps and Dynamic Lights

I’ve been attempting to use a lightmap with a slightly modified Lighting shader (I am using the vertex color for the lightmap instead of an actual lightmap) and i’ve been running into a problem with multiple lights in the scene. Basically, since each pass of the lighting shader re-renders the screen it is multiplying my lightmap each time there is a new light. After looking at the code it looks like the normal lightmaps don’t handle this case either. Is this a known issue using lightmaps and lights together?



If it isn’t intended, the easiest way to handle it I suppose would be to pass in a variable with the number of technique passes that I could use to divide the lightmap value by. That is pretty cumbersome right now with my code but I suppose that would work. Any other thoughts or ideas?

Just a guess, but I think ambient is also only done on the first pass. It might be possible to use that to our advantage somehow.

The lightmap is applied to the diffuse and specular light components, not ambient. Is that the issue you’re having? In that case, you can just apply the lightmap to the ambient light component, which is applied on the first pass.

@Momoko_Fan said:
The lightmap is applied to the diffuse and specular light components, not ambient. Is that the issue you're having? In that case, you can just apply the lightmap to the ambient light component, which is applied on the first pass.


Yea, basically every extra light in the scene is doubling the diffuse and specular components that the light doesn't affect. My assumption was that lightmaps should be treated as a light, kinda like how jmonkey is currently handling ambient light, and not as just an addition to the diffuse or specular channel. Is this the intended functionality?

I can hack together a solution if I add an ambient light to the scene, but this affects all the other geometry in the scene as well so i'm not sure thats the best solution.
@glh3586 said:
Yea, basically every extra light in the scene is doubling the diffuse and specular components that the light doesn't affect.

That's false. The lightmap is multiplied by diffuse/specular, and then added together in each pass. E.g.:
[java]diffuse * lightmap * light_contrib1 + diffuse * lightmap * light_contrib2 + ... + diffuse * lightmap * light_contribN[/java]
If you factor out "diffuse * lightmap" you get the following:
[java] diffuse * lightmap * (light_contrib1 + light_contrib2 + ... + light_contribN)[/java]
Therefore, there's no "doubling" of lightmap contribution as you claim, its just that its applied to the diffuse/specular components.

@glh3586 said:
My assumption was that lightmaps should be treated as a light, kinda like how jmonkey is currently handling ambient light, and not as just an addition to the diffuse or specular channel. Is this the intended functionality?

It depends on what data that lightmap represents. In your case, you have baked static lights onto the lightmap and therefore you want to treat them as a separate light and then have dynamic lights on top of that. In other cases the lightmap might contain ambient occlusion data which does not depend on the lighting setup in the scene, in that case it makes no sense to have it work as a separate light.
@Momoko_Fan said:
Therefore, there's no "doubling" of lightmap contribution as you claim, its just that its applied to the diffuse/specular components.

I know in the current shader code it doesn't since it is multiplied by the light contributions. I meant that in my slightly modified version of the shader it gives the appearance of doubling since the diffuse component is added for every light in the scene. I wasn't clear enough in what I was referring to.

@Momoko_Fan said:It depends on what data that lightmap represents. In your case, you have baked static lights onto the lightmap and therefore you want to treat them as a separate light and then have dynamic lights on top of that. In other cases the lightmap might contain ambient occlusion data which does not depend on the lighting setup in the scene, in that case it makes no sense to have it work as a separate light.

In the case wouldn't having a light at a corner that is shadowed from ambient occlusion lite up that area more? I guess that makes sense for directional lights but it still works pretty differently than lightmaps work in any other engine I've used which is what has thrown me off. I'll probably end up just customizing the engine for my own local use for now since there doesn't appear to be a good way to work around this problem. That is the beauty of Jmonkey being open source :). Thanks for the insight into the design and the decisions you guys have made, its been very helpful.