Moderators: Despellanion, Dr. Best

Flashlight Example

Rockin
Advanced Member
Rockin
Advanced Member
Joined: September 22nd, 2009, 4:12 pm

June 16th, 2013, 11:55 pm #1

Hello everyone.
I just need an example about Flashlight. I did try whatever i can but i haven't get any good result yet. So asking you the best way i guess.

I'm trying to do something like this
http://www.visualwalkthroughs.com/halfl ... life/6.jpg
If you can make an example about that or at least teach me how i can, that would be so good !
thanks everyone anyway..
Luftwaffe Ground Forces
Quote
Like
Share

Gandalf20000
Forum God
Joined: March 4th, 2008, 9:19 pm

June 17th, 2013, 1:18 am #2

That's a somewhat tricky one to pull off. One way is to use the projective texture shader from the Tech Demo. Unfortunately, the real master with projective textures is Skarik, who isn't on much.

The plus side is that your lighting will almost certainly be per-pixel. The downside is that it's very tricky to use and requires you reimplement any shader effect to also allow projective lightmapping. Using a projective lighting/shadow mapping shader, project the flashlight texture onto the scene. In the shader, use traditional lighting methods (multiply the dot product of the surface normal and the light direction by the light color, or the texture in this case) to calculate lighting for the flashlight and for the scene. Add them together, then multiply the final lighting by the texture and color of the model. It's a pain, especially fiddling with the projection matrix to get the projective texture coordinates.

If you want to try it this way, some research on shadow mapping might be helpful.

The alternative is easier, but it's not as pretty. It is similar to deferred shading (which U3D users can cheat somewhat), and it requires two passes and a post screen shader:
  • Render the scene to a separate camera. This should contain the scene with just the lighting.
  • Render the scene to a separate camera again. This should contain the scene with just the textures.
  • The post screen shader should take both scene renders and a flashlight texture. Add the flashlight texture to the lighting pass, then multiply the texture pass by the lighting pass. Voila, you have a cheap flashlight.
There are numerous disadvantages to trying this, though:
  • Shaders of any kind become a pain, especially accurate reflections.
  • Alpha blending can often become an issue.
  • It doesn't look as good as a light mapped approach would.
EDIT: I could try to make you an example, but I quit GM a while ago. I was never good at understanding the projective texturing, anyway.
Quote
Like
Share

Monty
Forum Leader
Monty
Forum Leader
Joined: September 13th, 2009, 3:58 pm

June 17th, 2013, 1:27 am #3

The easiest way by far:

1. Put a simple point light at the camera position to be your flashlight light source.
2. Make a flashlight overlay 2D texture. The color should be all black, but the alpha should have a transparent circle in the center.
3. Use DrawTex or whatever it's called to overlay it on top of the screen.

There you have it. No shaders at all. You could even make the DrawTex bigger than the screen and animate it so that the flashlight lags behind the camera a little for a realistic effect. I can't think of any drawbacks at the moment...

Edit: Found one. Your gun will have to be drawn on with DrawTex as well. This may mean drawing simply a 2D gun image or it may mean creating a separate render pass with a gun.
Quote
Like
Share

Gandalf20000
Forum God
Joined: March 4th, 2008, 9:19 pm

June 17th, 2013, 2:54 am #4

Monty wrote:The easiest way by far:

1. Put a simple point light at the camera position to be your flashlight light source.
2. Make a flashlight overlay 2D texture. The color should be all black, but the alpha should have a transparent circle in the center.
3. Use DrawTex or whatever it's called to overlay it on top of the screen.

There you have it. No shaders at all. You could even make the DrawTex bigger than the screen and animate it so that the flashlight lags behind the camera a little for a realistic effect. I can't think of any drawbacks at the moment...

Edit: Found one. Your gun will have to be drawn on with DrawTex as well. This may mean drawing simply a 2D gun image or it may mean creating a separate render pass with a gun.
I don't know if that would work as well as one would hope, considering I tried something like it once. Wouldn't the flashlight texture just obstruct the scene? I don't think you can change blend modes on screen textures without using a post screen shader, can you?

Here's another idea that would use a (simple) post screen shader, but it requires that the area is completely dark to be good.

1. Use a point light in a completely dark room.
2. Using a post screen shader, multiply the screen by the flashlight texture.

Post screen shaders are really easy to program compared to some other shaders.

I'm rusty, but I think the shader would end up looking like this (please correct me if I'm wrong, since I haven't done ASM shader language in a while):

Code: Select all

//Vertex shader
vs.1.1

mov oPos, v0
mov oT0, v7
mov oT1, v8

Code: Select all

//Pixel shader
ps.1.4

//Get the scene
texld r0, t0
//Get the flashlight texture
texld r1, t1

//Multiply them together
mul r0, r0, r1
Of course, then you could always tweak this to use a pseudo-deferred shading method to combine this with other lighting... :whistle:
Quote
Like
Share

Monty
Forum Leader
Monty
Forum Leader
Joined: September 13th, 2009, 3:58 pm

June 17th, 2013, 3:26 am #5

Hmmm maybe I'm thinking wrong but isn't there no difference between
a. multiplying by a grayscale map and
b. overlaying a black, pseudo-transparent texture?

Those pixel shaders look good to me. And you are of course quite right about the deferred lighting. If you go for this approach and if Gandalf's shaders don't work, use the shaders from this: http://s9.zetaboards.com/Ultimate3D_com ... 7322033/1/. This is a deferred lighting example which does exactly what we're talking about.

Anyways do one of these two and then we'll figure out the gun.
Quote
Like
Share

Gandalf20000
Forum God
Joined: March 4th, 2008, 9:19 pm

June 17th, 2013, 2:18 pm #6

Monty wrote:Hmmm maybe I'm thinking wrong but isn't there no difference between
a. multiplying by a grayscale map and
b. overlaying a black, pseudo-transparent texture?

Those pixel shaders look good to me. And you are of course quite right about the deferred lighting. If you go for this approach and if Gandalf's shaders don't work, use the shaders from this: http://s9.zetaboards.com/Ultimate3D_com ... 7322033/1/. This is a deferred lighting example which does exactly what we're talking about.

Anyways do one of these two and then we'll figure out the gun.
a. A pure white leaves the scene untouched. A pure black is black. Everything in between darkens the scene gradually. Assuming the default scene is everything at 100% light, this creates a lighting effect.
b. Overlaying a simple alpha texture will use the following formula to get the final color: color=as * src + (1-as) * dest = as * (src - dest) + dest
This formula just composites an alpha texture on top of another. Using black as transparency requires additive or subtractive blending.
Quote
Like
Share

Rockin
Advanced Member
Rockin
Advanced Member
Joined: September 22nd, 2009, 4:12 pm

June 17th, 2013, 3:52 pm #7

Hello,
thanks for you. I understood Monthy's way but it would be a problem when you turn yourself to a corner of a wall(or an object)
I am total noob and Gandalf's way is too hard for me(But i think it's the only way)
example : http://www.visualwalkthroughs.com/halfl ... ife/57.jpg

I just stuck at this problem :)
An example would so great. I made models, maps.. everything. I need just something to finish it up.
Credits goes you.
Luftwaffe Ground Forces
Quote
Like
Share

Gandalf20000
Forum God
Joined: March 4th, 2008, 9:19 pm

June 17th, 2013, 5:09 pm #8

I'd help you out, but like I said, I no longer have GM. I'll try to be descriptive enough that even if you don't have enough experience yet to understand what to do, hopefully somebody else will:
For projective texture mapping, start by jacking the projection texture from the Tech Demo. I believe this is the one used in the portal room that allows you to see the other side. It will require some modifications. From here on there are two routes.
The "easy" route:
Use the flashlight's position and perspective to project the flashlight texture onto the model. Calculate lighting data as if it was a point light. Multiply the point light information by the flashlight texture. Add to the rest of the lighting data (you might be able to squeeze out one or two lights after this, but including any more than this will require deferred shading or a static lightmap), then multiply the lighting data by the color map. You can also implement bump mapping in the process.
The hard route:
Do complete shadow mapping (which I won't explain, because there are plenty of instructions online, and it's highly impractical), then all of the above.

The "easy" route should just require modifying the projection shader to include the light projection matrix (you shouldn't need a full camera or a separate pass), lighting information for any other lights, lighting calculations, and making sure the texture being passed to the shader for projection is the flashlight texture, not a separate scene render.

EDIT: Monty, this is your cue. You've made nifty examples before. Now do it again. :P
Quote
Like
Share

Monty
Forum Leader
Monty
Forum Leader
Joined: September 13th, 2009, 3:58 pm

June 17th, 2013, 7:51 pm #9

I am all Mac and XCode now unfortunately. As you can see by my thinking that overlaying black and multiplying are the same thing, my pixel shader knowledge is quite rusty :P .

I think Rockin should go the deferred shading route though. If I understand it correctly, he would otherwise have to apply a custom shader to all of the lighted objects in his game, which would be a pain.
Quote
Like
Share

Gandalf20000
Forum God
Joined: March 4th, 2008, 9:19 pm

June 17th, 2013, 7:54 pm #10

Monty wrote:I am all Mac and XCode now unfortunately. As you can see by my thinking that overlaying black and multiplying are the same thing, my pixel shader knowledge is quite rusty :P .

I think Rockin should go the deferred shading route though. If I understand it correctly, he would otherwise have to apply a custom shader to all of the lighted objects in his game, which would be a pain.
You turned to the dark side? Shame on you. :(

I guess I'm your alter ego in this case; I'm busy with Java (shudders) and Android right now.
Quote
Like
Share