Lightmap Shader

Posted By: A.Russell

Lightmap Shader - 08/06/06 12:21

This has been talked about before, but I ws wondering if anyone has made (or could make) a shader for applying a light map to model geometry. From what I understand, the UV map would have to be the same as for the texture. Nevertheless, has anyone tried it?
Posted By: mk_1

Re: Lightmap Shader - 08/06/06 12:54

UV map must be different and this isn't really done via a shader. Of course you need a shader (or ffp effect) to render the lightmap on the model but what you really need is a lighting precalculation.
Posted By: A.Russell

Re: Lightmap Shader - 08/06/06 14:46

You can use the same UV map (if we are on the same page here), because I can do it in Lightwave. I also have GILE[s], which creates a separate UV map for the lighting information (often several models for the same map).






Just the lights, this is probably what you mean by a "pre-lighting calculation" (with the same UV map as the texture) -actually, this is a bad example because I have mapped both the left and right side to the same part of the image, but you get the idea


Lighting baked in. This is what the shader should achieve by mixing the lightng information(above) with the base texture.
Posted By: mk_1

Re: Lightmap Shader - 08/06/06 15:01

This is no problem I think but a waste of texture memory.
Posted By: A.Russell

Re: Lightmap Shader - 08/06/06 15:08

Actually, if you are going to reuse the same model, but with different lighting, it could save you a heck of a lot of texture memory.

For example, with the same level item repeated multiple times across your level, they all share the same base texture and only need a different, low res 16 bit (or even 8 bit?) image for their lighting. If you were to bake the lighting into each instance of the model you would use much more memory.

So what about it, anyone have a solution.
Posted By: A.Russell

Re: Lightmap Shader - 08/09/06 09:35

Obviously no-one was interested, so I tried it myself. However, I don't know anything about shader programming, and this was a stab in the dark.

Effectively, I have got the lightmap to show, but it is too strong. How do can I adjust this to dim the lighting down?:

Code:

material lightmap
{

effect
"
texture entSkin1; // model texture
texture entSkin2; // light texture


technique test
{
pass p0
{
Texture[0] = <entSkin1>; // block
Texture[1] = <entSkin2>; // shadow


TexCoordIndex[0] = 1; //
TexCoordIndex[1] = 1; //

pixelshader = asm
{
ps.1.0
tex t0
tex t1
add r0, t0, t1
};

}
}
";
}



I thought it might work with multiplication, but I couldn't get it to do anything.
Posted By: mk_1

Re: Lightmap Shader - 08/09/06 11:05

Quote:

add r0, t0, t1



This line just adds texture1 (shadowmap) to texture0 (diffuse map).
What you need to do is to multiply with the shadowmap:
Code:
mul r0, t0, t1;


Anyway wiki features a little pixelshader tutorial using assembler.

Your code is missing albedo and specular though. Maybe you should try hlsl
Posted By: A.Russell

Re: Lightmap Shader - 08/09/06 11:33

I tried multiplying, but it made no change to the base texture.
Posted By: TWO

Re: Lightmap Shader - 08/09/06 13:47

This may work:
Code:

material mtl_shadowmap
{
effect=
"
texture entSkin1;
texture entSkin2;

technique one_pass_shadow
{
pass p0
{
Texture[0]=<entSkin1>;
Texture[1]=<entSkin2>;

ColorArg1[0]=Texture;
ColorOp[0]=Modulate2x;
ColorArg2[0]=Diffuse;
TexCoordIndex[0]=0;
TextureTransformFlags[0]=Count2;
TextureTransform[0]={1.0,0.0,0.0,0.0, // color map u scale
0.0,1.0,0.0,0.0, // color map v scale
0.0,0.0,0.0,0.0,
0.0,0.0,0.0,0.0};

ColorArg1[1]=Texture;
ColorOp[1]=Modulate;
ColorArg2[1]=Current;
TexCoordIndex[1]=0;
TextureTransformFlags[1]=Count2;
TextureTransform[1]={1.0,0.0,0.0,0.0,
0.0,1.0,0.0,0.0,
0.0,0.0,0.0,0.0,
0.0,0.0,0.0,0.0};
}
}
";
}


Posted By: A.Russell

Re: Lightmap Shader - 08/09/06 17:05

Thanks, but that doesn't work. I think the reason that multiplication doesn't work either is because I want to apply a light map, not a shadow map (ie, I want to add light, not shadows).
Posted By: Matt_Aufderheide

Re: Lightmap Shader - 08/09/06 17:22

Well in reality you need to think of all light maps as shadow maps.
Posted By: TWO

Re: Lightmap Shader - 08/10/06 20:06

This?

Code:


material mtl_lightmap
{
effect=
"

texture entSkin1;
texture entSkin2;

technique lightmap_mapping
{
pass p0
{
texture[2]=<entSkin1>;
texture[3]=<entSkin2>;


colorarg1[0]=texture; // shadow map
colorarg2[0]=diffuse; // vertex lighting
colorop[0]=add; // add vertex lighting to shadow map


colorarg1[1]=texture; // color map
colorarg2[1]=current; // (shadow map + vertex lighting)
colorop[1]=modulate2x; // modulate color map with (shadow map + vertex lighting)
resultarg[1]=temp; // output to temp register


colorarg1[2]=texture; // color map
colorop[2]=selectarg1;

magfilter[2]=linear;
minfilter[2]=linear;
mipfilter[2]=linear;
texcoordindex[2]=1;


colorarg1[3]=current; // unshaded color map
colorarg2[3]=temp; // shaded color map
colorarg0[3]=texture; // luminosity map
colorop[3]=lerp; // interpolate between shaded color map and unshaded color map depending on luminosity map

magfilter[3]=linear;
minfilter[3]=linear;
mipfilter[3]=linear;
texcoordindex[3]=1;
}
}
";
}



(The code was for worldblocks and i changed the skinsources, should work)
Posted By: A.Russell

Re: Lightmap Shader - 08/10/06 22:22

Thanks, but it throws an error on this line:

colorarg1[0]=texture; // shadow map

"colorarg accepts only dwords and ids"
Posted By: TWO

Re: Lightmap Shader - 08/11/06 07:29

Now it works, FF effects are case-sensitive forgott it
Code:

material mtl_lightmap
{
effect=
"
texture entSkin1;
texture entSkin2;

technique lightmap_mapping
{
pass p0
{
texture[0]=<entSkin1>; //Try texture[2] if it donīt do itīs job
texture[1]=<entSkin2>; //Try texture[3]


colorArg1[0]=Texture;
colorArg2[0]=Diffuse;
colorop[0]=add; // add vertex lighting to shadow map


colorArg1[1]=Texture;
colorArg2[1]=Current; // (shadow map + vertex lighting)
colorop[1]=Modulate2x; // modulate color map with (shadow map + vertex lighting) (Test Modulate4x / Modulate2x / Modulate)
resultarg[1]=Temp; // output to temp register


colorArg1[2]=Texture;
colorOp[2]=selectArg1;

magfilter[2]=linear;
minfilter[2]=linear;
mipfilter[2]=linear;
texcoordindex[2]=1;


colorarg1[3]=Current; // unshaded color map
colorarg2[3]=Temp; // shaded color map
colorArg0[3]=Texture; // luminosity map
colorop[3]=Lerp; // interpolate between shaded color map and unshaded color map depending on luminosity map

magfilter[3]=Linear;
minfilter[3]=Linear;
mipfilter[3]=Linear;
texcoordindex[3]=1;
}
}
";
}


Posted By: JetpackMonkey

Re: Lightmap Shader - 08/11/06 14:38

Could someone here explain how to apply and use this process this in a 'for-dummies' step by step manner? (Assume I've already got the lightmap files...)

How is this different from what is being discussed here?
[urlhttp://www.coniserver.net/ubbthreads/showflat.php?Cat=0&Board=UBB5&Number=679845&Searchpage=1&Main=674842&Words=lightmap&topic=&Search=true#Post679845[/url]
Posted By: A.Russell

Re: Lightmap Shader - 08/11/06 18:23

The shader runs now, but it doesn't add the lightmap. I could upload a test model if you want to try again.
Posted By: TWO

Re: Lightmap Shader - 08/12/06 15:57

Did you trie my comments? Like renaming texture[0] to texture[2] etc?

@Jetpack_Monkey:
-Create a modell with one normal skin
-Create the lightmap somehow
-Add the lightmap the the modell as second skin
-Copy&Paste the code iīve postet
-Copy&Paste this too:

Code:

action FX_lightmapped
{
my.material=mtl_lightmap;
}



Good luck
Posted By: A.Russell

Re: Lightmap Shader - 08/14/06 12:19

Yes, I tried that (and I'm not quite that green), but it came out a bit strange. I could post a screen shot, but I don't think that would help much, so I have uploaded a model with a base texture and a lightmap applied for you to see for yourself: Download
Posted By: TWO

Re: Lightmap Shader - 08/14/06 12:37

If I add the material it behaves as I want it to do, so whats the problem?
The shader catches the normal texture and adds the light ^^
Posted By: A.Russell

Re: Lightmap Shader - 08/14/06 18:06

My bad, yes it does work. It looked different because it was a fair bit darker than I expected. Is there a parameter that can be adjusted to brighten or darken the effect to get it to look as it was lit in another application?
Posted By: TWO

Re: Lightmap Shader - 08/14/06 20:19

change
Code:

colorop[1]=Modulate2x; // modulate color map with (shadow map + vertex lighting) (Test Modulate4x / Modulate2x / Modulate)


to
Code:

colorop[1]=Modulate4x;



Nice that I could help you
Posted By: A.Russell

Re: Lightmap Shader - 08/15/06 04:29

Thanks. Changing to 4x doesn't really make it much different, though. Is there anything else that can be adjusted? Otherwise I'll brighten the map up in Photoshop.

Thanks for all your help.
Posted By: A.Russell

Re: Lightmap Shader - 08/15/06 08:37

I wrote a short tutorial about how to use this shader, because I know other people like Frank and Jetpack were interested in this. I posted it in the Tutorial's forum.
Posted By: TWO

Re: Lightmap Shader - 08/15/06 08:56

Nice to hear, Iīll take a look at it; Hmm, you could bright the 3rd or 4th pass I think... *testing*
Posted By: Lion_Ts

Re: Lightmap Shader - 08/15/06 21:01

Thank You, A.Russell!
Quote:


I know other people like Frank and Jetpack...




and me
© 2024 lite-C Forums