Impossible to add light at runtime?
HI,
I'm having some difficulty while trying to add a light after some time to the scene. If the light is created in the init() function there is no issue, but as soon as you create it later it makes everything look black.
Here is a sample script showing this issue: http://jsfiddle.net/GKCx6/1/
there is a variable "dynamic_light" that you can set to false to show a working version (light created in init). If dynamic_light is set to true you will have to click "add light" to see the issue.
I hope i'm not missing something obvious.
I've found a workaround. Just after the light is added the material(s) need to be rebuild. renderer.initMaterial(material, scene.lights, false, mesh);
I feel this should be handled by three.js with minimal performance impact (not recreating the whole shaders if possible).
I agree. But we haven't come up with a nice way of dealing with lights in the WebGLRenderer yet...
I've made a basic modification to the WebGLRenderer to handle this case. It's not ready but I wanted to ask for advice.
- SHA: 1ab47a5ef5b21469f43d728ea6cdc10c691cf7bd
I see two issue with this one.
- It checks the scene.lights.length. This will not handle cases where a pointLight is switched to directionnalLight and maybe other cases. I was thinking about adding an id attribute to the lights to check for differences. Since the lights number is restricted this will not create much overhead.
- The obvious one is that with this modification all shaders are rebuild when a light is added or removed. I have no clear idea about this one. At first I thought about changing max_dir_lights, max_points_lights and max_shadows to uniforms (or attributes). But after looking at the WebGLShaders.js code, well, it doesn't look that easy.
To explain a little, I'm trying to build some kind of "vvvv" clone in js, that's why I wanted this kind of functionality. It look like the complete shader rebuild will be required for many shader variables. I have some ideas for the automatic rebuild, but none for the optimization.
Someone managed to do this? I tried using the workaround by @idflood but it didn't worked nicely.
I'm trying to do something with websocket and when a new users comes around it create a new light on the scene and when he leaves the light dies, any idea?
I've been researching on this problem, and these are the results:
- With ShaderModel 4 one solution is to compile for's structures with a uniform in the break condition, allowing us to set MAX_WHATEVER_LIGHTS to a higher number without a lot of wasted GPU/CPU/Memory. Sadly this solution don't work on older models (my notebook is an example =]), because branching (if, break, etc) is "emulated" on these models.
- A lot of people recommend recompiling the shader whenever light count changes. I've been thinking if it is possible to recompile on a 10 (or something configurable) step basis, up or down, like a buffer, and control the granularity with a uniform. Example:
Context starts compiling the shader with MAX_POINT_LIGHTS = 10 and pointLightCounter = 0. Here we have a maximum of 10 cycles wasted per for structure on older hardware).
User code adds 3 lights, the context sets pointLightCounter uniform equals 3. Still 7 cycles to the stars.
Code adds 8 lights, so the context recompiles the shader with MAX_POINT_LIGHTS = 20 and set pointLightCounter equals 11. Now we have 9 cycles doing nothing.
Code removes 2 lights, so the context recompiles the shader with MAX_POINT_LIGHTS = 10 and sets pointLightCounter equals 9. Only 1 cycle is discarded on this scenario.
Does worth a try? I'm a newbie on GLSL, WebGL and Three.js, at least this could help me learn something.
Is there any way to work around this manually? In my code, I can almost guarantee that there will be fewer than ten lights at any given time, and I have a list of all of the objects in the scene that would need to be updated along with their materials. Is it just a matter of having some ugly for loops?
I've searched a little more and found a related question on stackechange: http://gamedev.stackexchange.com/questions/19575/is-there-a-way-to-use-an-arbitrary-number-of-lights-in-a-fragment-shader
So one possible solution would be to do some multipass rendering (one for each lights) Another possibility I've seen is to define the number of lights to the max and fill the array with black lights. But this sounds not really optimal.
Another finding is that recompiling the shaders is clearly not recommended: http://developer.apple.com/library/ios/#documentation/3DDrawing/Conceptual/OpenGLES_ProgrammingGuide/BestPracticesforShaders/BestPracticesforShaders.html#//apple_ref/doc/uid/TP40008793-CH7-SW3
edit: deffered rendering is maybe another option
Well, now we have also material.needsUpdate flag (set it to true to force shader rebuilding). Use with caution.
In general, having many lights is tough. Most common solution nowadays seem to be deferred rendering. However this comes with own set of problems and limitations and it's not very well suited for WebGL (due to lack of multiple render targets support). Still may be ok, at least for some particular use cases, Illyriad folks used it:
http://www.illyriad.co.uk/blog/index.php/2011/11/webgl-experiments-illyriads-3d-town/
Oh, the needsUpdate flag is a great addition (i was simply setting the .program to false).
Do you think that three.js should handle the rebuild of the shaders when the light count change? This may not be ideal but it should be easy to implement.
Not sure about that. Shaders compilation is potentially very costly, for the best user experience it's probably better to let the application decide when it's the right time to rebuild what.
For example if you deal with lights in GUI, if some GUI action would result in shader rebuild, it could feel pretty bad as everything may freeze for a while (that's kinda what's going on with GLSL Sandbox, when shaders are more complex it feels unresponsive, and this would be worse with many shader getting rebuilt at the same time).
Now if you let the application layer decide about rebuilds, you can mitigate this "lag" at least to some extent (for example you could only trigger rebuild when the user stops interacting with GUI, or spread rebuilding over several frames, or give some progress indication, etc).
Not sure about that. Shaders compilation is potentially very costly, for the best user experience it's probably better to let the application decide when it's the right time to rebuild what.
Agreed!
I don't see this as an issue I like triggering the update in demand, I just do after creating a light material.needsUpdate = true;
that's on a function that checks every node in the scene
and that works
That you @varomix, I misunderstood how to use Three.js. My apologies.
Thanks a lot! needsUpdate did the trick :+1: