Switchable lights tutorial


(MindLink) #1

I’ve seen this question quite often :
“How do I do switchable lights in ET?”
and the answer always has been : you don’t.
Well.
You could use dlights, but this would have a big impact on your FPS, which is something we want to avoid.

There is however another method to do switchable lights, and although it’s much work to do it it’s well worth the effort imho. You can do switchable lights using only lightmaps.

The downside of this is : the lightgrid won’t be affected by any switching, so if you turn off the lights in a room the player models will still be as bright as they were before. But if you don’t care about this I’ve got the method for you.

The weapon of choice is :
the script remapshader command

The trick :
use the remapshader command to switch shaders to other shaders with a different lightmap.

Ok, now on to the details.

All map compiles we will do afterwards will have to be done with -nocollapse in the light phase and best with pretty big lightmaps, 512x512 or even 1024x1024 if you can.

First you need to do a special shader for every shader that could be affected by switching the light. The more, the more complicated it will be.
Lets say we have a small room map consisting of only a simple concrete shader


textures/ml_light/wall_switch
{
	qer_editorimage textures/miltary_wall/concrete_m05c.jpg
	{
		map $lightmap
		rgbGen identity
	}
	{
		map textures/miltary_wall/concrete_m05c.jpg
		blendFunc GL_DST_COLOR GL_ZERO
		rgbGen identity
	}
}

This a simple standard lightmapped shader. Now the first thing we do is compile change the lights in our map to a new state, let’s say we turn all our lights from white to red.

We’ll compile our map, take the resulting lightmap, rename it and put it into our textures directory for our map, for example “textures/ml_light/red_light.tga”

Now we will compile the map again with white light. The thing we now need is a second shader :


textures/ml_light/wall_standard
{
	qer_editorimage textures/miltary_wall/concrete_m05c.jpg
	{
		map textures/ml_light/red_light.tga
		rgbGen identity
		tcgen lightmap
	}
	{
		map textures/miltary_wall/concrete_m05c.jpg
		blendFunc GL_DST_COLOR GL_ZERO
		rgbGen identity
	}
}

As you can see I switched the standard lightmap for the red one we did earlier.
Now we can set up a small trigger in our room which will change the lighting state which could look like the following :


	trigger switchlight
	{
		accum 1 trigger_if_equal 0 game_manager switch_red
		accum 1 trigger_if_equal 1 game_manager switch_white
	}

	trigger switch_red
	{
		remapshader textures/ml_light/wall_standard textures/ml_light/wall_switch
		remapshaderflush
		accum 1 set 1
		wait 50
	}

	trigger switch_white
	{
		remapshader textures/ml_light/wall_standard textures/ml_light/wall_standard
		remapshaderflush
		accum 1 set 0
		wait 50
	}

And voila, we’re actually done. Everytime we trigger this script the light will switch between white and red. I’ve made a small sample pk3 showing this effect here :

http://www.tectonicshift.de/maps/ml_light.zip

There’s a small box at the wall in front of you which can be used to switch the light color in the room.

Ok, this is only a simple box room consisting of no more than one shader, but it is actually possible to pull this off on more complex maps too under certain premises :

First of all : the lightmaps of all of our shaders we are going to switch must be contained in one lightmap, since we can’t switch to different lightmaps (which can be achieved by func_grouping our geometry mostly).

Each surface that could be affected by the light change must get one of those special “double shaders” we designed above, one with a standard lightmap and one with a special one.

Another thing is that it is absolutely crucial to compile with nocollapse since both lightmap passes must have the same lightmap coordinates for our geometry.

If we have multiple lightmaps we will have to find out which one is the correct one for our geometry by looking at them in an image editor (comparing the two compile passes, look where stuff changes, that’s the right texture).

It is even possible to have several different switches (or switching through more than one light color) using this way IF you do seperate shader pairs for each of those switches.

Also be sure to use the special shaders only in the area of the switchable light, else you’ll get funny artefacts all over your map.

Well, that’s it for now, have fun experimenting with this stuff, and be sure to let us see your results :wink:


(LaggingTom) #2

Just note if you use more than 32 remapshader commands in the entire script, you will get an error.