Problem with uniform boolean var to turn lighting on/off


I guess this goes in the beginner section rather than the advanced section.

I’m developing a 3D viewer of OpenStreetMap data which also shows terrain generated from NASA SRTM height data. The current version (very much pre-pre-alpha grade…) is available at

I’d like to turn lighting on for the terrain, but off for the OpenStreetMap data (roads, footpaths, and other vector map data) because I don’t need lighting effects on the OSM data. I attempted to do this by declaring a boolean uniform variable and then using code in the vertex shader such as:

if (uDoCalculation==true)
// calculate lighting
vLightWeighting = vec3(1.0, 1.0, 1.0);

I render the SRTM data first, then the OpenStreetMap data. My plan was to set uDoCalculation to true when rendering the former, and to false when I render the latter. However this does not work (I get a blank screen), and even if I switch off the OpenStreetMap layer and just render the terrain (i.e uDoCalculation is always true) I just get a blank screen, with neither terrain nor OpenStreetMap data rendering.

However, if I change the if statement in the above code to if(true) it works, and if I change it to if(uDoCalculation==false) I get (as expected) a scene with no lighting effects.

I have checked that the vertex variable location for uDoCalculation is >=0 (rather than -1) and it is, so I’m really stumped as to why this is not working. It’s almost as if the shader variables are being corrupted in some way. Does anyone have any suggestions?


first of all: It is not a wise idea to program an universal shader. Don’t try to rebuild the fixed function! Your graphics hardware needs to do this decision for every vertex! If you do that in the fragment shader, this is done for EVERY fragment/pixel. It’s better to switch the shader program instead.

However, you implementation should work… :?
Maybe you set the uniform wrong?

// true
gl.uniform1i(gl.getUniformLocation("uDoCalculation"), 1);

// false
gl.uniform1i(gl.getUniformLocation("uDoCalculation"), 0);

Hi, thanks for that. Am now using a separate shader for the layer which doesn’t need lighting so works fine thanks :slight_smile: