Author Topic: Shaders and effects  (Read 34258 times)

Offline Johnnybravo

  • Hey there!
Shaders and effects
« on: August 23, 2011, 08:57:13 pm »
Well summer is not really friendly to development or work in general, but I realised some people would probably like to already try doing some stuff so I won't delay it any further due to less relevant technical details.

First off you will need to grab the effect (http://pastebin.ca/2077908), save this as arbitary plain text file in your client/effects directory ( I use 3D_SPecularMapping.fx )
You will also need to add following structure to your IOStructures.inc (this is text file as well)
Code: [Select]
struct VsToPs_3DNormal
{
float4 Position     : POSITION;
float2 TexCoord     : TEXCOORD0;
float3 Normal       : TEXCOORD1;
};
Note - by chance this is exactly the same as 3DTangent and I'll reuse it correctly when I'm done with that, so this is just the part of WIP version and might be changed eventually, but I'm aware this is duplicate in this form :d.

When you are done with this, you shouldn't need to worry about effects themself, but if you want to edit them, turn of your client, delete effects cache (D3D allows saving shaders as binary data, saving some time, however nobody checks for anything, so you have to empty it to have them recompiled). If you cannot notice the effect, or have no shadows, check out fonline log file to see the error during compilation.

Now you need to use this effect. I expect some external definitions from fo3d file in my WIP version, so you should add it to your file
Here's example how to use effect, as well as to pass required value to your file
Code: [Select]
Effect 3D_SpecularMapping.fx
EffDef Floats Specular_Power 16.0

Model Armor_APA.x

And now one last thing - you need to specify specular texture to actually see anything, since texture 0 is already used by default shaders for diffuse part, and texture 1 for normal map, this shader uses texture 2 for specular maps.
So just append
Code: [Select]
texture 2 <path> after texture 0 definitions and it'll correctly load.

Little tip to creating such texture: Transparency is calculated using normals on model, so you'r texture is supposed to cover whole model, and not just be diffuse texture with highlights painted on it. It's added on already calculated colors, so black parts will not actually darken anything, but just have no reflection appearing on it.
For metalic surface playing with levels should be enough, but for the best result you might want to actually paint the metal as if it was lit from every side.

Have fun with it, and post if you got any problem or questions, I'll add some effects and their variation later, and I'll answer questions about any effects, not just specular reflection, so if you got any idea feel free to ask.
"What is this, I don't even"
"This is your forum."

Offline Luther Blissett

  • Moderator
Re: Shaders and effects
« Reply #1 on: August 23, 2011, 09:19:34 pm »
Awesome! I'm excited like a child at Christmas :)

Going to try and test this tonight. If it works, expect a few screenshots later.

[edit] Totally works :)

Example text files stuff :

In "_FOHuman.fo3d" :

The old one I had attached was written like so :
Code: [Select]
# Body
Layer 3
[...]
Value 27 Root Attach Armor_%anim%_ncr_patrol.x Texture 0 Armor_male_ncr_patrol.png

That is roughly "make ~param 0 153 27 give me that model with that texture".

The shiny one is written like so :
Code: [Select]
# Body
Layer 3
[...]
Value 29 Root Attach Armor_male_ncr_patrol.fo3d

That is roughly "make ~param 0 153 29 give me whatever that fo3d file is".

Then the Armor_male_ncr_patrol.fo3d file contains :

Code: [Select]
Effect 3D_SpecularMapping.fx
EffDef Floats Specular_Power 16.0

Model Armor_male_ncr_patrol.x

Texture 0 Armor_male_ncr_patrol.png
Texture 2 Armor_male_ncr_patrol_spec.png

Texture 0 being the same as previous, texture 2 being the specular map. My example one is rubbish, so I won't post a screenshot, but trust me when I say it works.

If you comment out the "texture 2" line (or if the line just didn't exist) then there is no specular map, so it assumes "all non-shiny". The improved light and shadow still works in this case.

[edit 2] If you put the Effect and Effectdef lines at the top of the base human fo3d file, it applies the lighting setup to the base model. This is currently with no specular texture included but as mentioned, the light / shadow is there.

In my opinion it's beginning to look awesome already.
« Last Edit: August 23, 2011, 11:42:33 pm by Luther Blissett »

Offline Karpov

  • Moderator
  • Come Together
Re: Shaders and effects
« Reply #2 on: August 24, 2011, 12:04:23 am »
Johnny Bravissimo!
It is amazing.

Also, great job Luther, keep up your experiments  ;).

ok ok, I have to test this, bye.

Offline Johnnybravo

  • Hey there!
Re: Shaders and effects
« Reply #3 on: August 24, 2011, 03:50:02 am »
Quote
[edit 2] If you put the Effect and Effectdef lines at the top of the base human fo3d file, it applies the lighting setup to the base model. This is currently with no specular texture included but as mentioned, the light / shadow is there.
It's because I've changed some parameters to make it feel more like on sprites, and when there's no specular power defined, specular will be skipped at minimal loss.
Though it'll make better sense to just add extra effect for those who want to use it that way.

Oh and feel free to mod "eye" vector ( should be relative position of camera, normalized )
and "LightDir" vector (obvious from name), changing "eye" will result on different specular highlights, changing lightdir will change everything.
You might also want to try higher ambience as I might have lowered it way too much.
That is if you want to play around with simple things ( don't forget to clear your chache each time you change it, server don't need restart ).

By the way, thanks for bluesuit comparsion, seems like this will need better filtering and perhaps bigger resolution of textures.
Not sure how'd normal map improve it (it definetly would though, but would probably require quite high resolution to do more than just to form the chest/pelvis details), but the main problem now is that it looks blurry.

As for normal maps, not sure if it works because did not see it working, but no reason for it to not work. There's some statement for calculating tangent space and shader is already included in base repositary. It would be quite easier to work it out if there was enough [english] documentation. But considering the difficulty of creating this layer it shouldn't be priority at the moment.

BTW: check out VB female, it has quite awesome model job, and especialy smoothing is well done, when shaded per pixel it's pretty smooth even without any normal map.
"What is this, I don't even"
"This is your forum."

Offline Luther Blissett

  • Moderator
Re: Shaders and effects
« Reply #4 on: August 25, 2011, 03:23:16 pm »
It's because I've changed some parameters to make it feel more like on sprites, and when there's no specular power defined, specular will be skipped at minimal loss.
Though it'll make better sense to just add extra effect for those who want to use it that way.

Well ultimately, the base models need better light/shadow than their default - I believe the original settings will have been for the Van Buren models, and your settings seem to work much better on the base models, even without the shiny stuff. In bluesuit screenshot above, it's already much closer to the original sprites (I've tested a lot of the other base skins too, and though not yet perfect, it's starting to look great).

It may be possible to add a "universal normal map" to all of the base model textures, as they are all based off the same model - so if the effect was added with normal map into VbMaleStrong.fo3d, then all base models should use it - with the 0 layer diffuse map being selectable as it currently is... I think.

In this case though, there are only a couple of clothing layers which would use specular - the NCR longcoat trooper has metal chest plates for example. I'm not sure if there are any others. If there's only this one, then I may as well remake the NCR longcoat model, and make the metallic chest plate part of the armour model instead of a clothing texture, therefore leaving all clothing textures as non-metallic.

You mention "Though it'll make better sense to just add extra effect for those who want to use it that way." - does it use up processing power to calculate the specular stuff without a texture 2 layer? i.e. if we use this full effect on non-shiny materials, are we wasting processing? If so, would it be possible to adapt this to just use diffuse and normal maps, with the improved lighting and shadow you've set up?

Quite difficult to explain, but I mean "should we have two different 3D effects", like :

1) Default effect for all 3D models. Uses diffuse map (layer 0) and normal map (layer 1). Uses the improved lighting and shadow settings you've developed.

2) Everything as above, with the addition of specular map (layer 2) to be used on metallic armours.

If the unused specular layer doesn't use up extra processing, is there any reason you can think of to not just use Effect #2 on all the models?

Offline Johnnybravo

  • Hey there!
Re: Shaders and effects
« Reply #5 on: August 25, 2011, 09:47:49 pm »
Quote
does it use up processing power to calculate the specular stuff without a texture 2 layer
AFAIK "if(statement)" is not that costy on GPU, so it should be fine, but as I said, it'd probably make sense to just make it without this, to avoid some confusion and perhaps little FPS gain.

Quote
1) Default effect for all 3D models. Uses diffuse map (layer 0) and normal map (layer 1). Uses the improved lighting and shadow settings you've developed.
Normal map would not work for default because you need to specify game to calculate tangent space for given model. If you load just normal map effect by for example renaming files, you will get nasty artifacts instead of base lighting as one would expect.
But making default effect using per-pixel lighting calculation would probably work.

I cannot tell what is the best but should be easy to try different stuff. people might sound like overkill for small undetailed models - but also keep in mind they are still small so you have not much pixels to calculate anyway.

Well to be honest, best would be just to have 2 effects for 3D

Default and NormalMapped, where default will have techniques for vertex and per-pixel lighting (with specularity based on data from fo3d) and normal map would just add one technique ( because vertex lighting does not make any sense in this case :d).
Every other effect would be fancy stuff (glass, stealthboy,... whatever you can think of and might use in Fallout, like fancy glowing ghouls hehe :d).
But this will require some additions to game engine so script interface might ineract with techniques.

Normal maps for every model is not good idea because even such small objects will probably require about 512x512 to look good (compression decreases quality rapidly, and filtering is important factor as well).

Oh and in case people are interested in "pixelization", that would be better to have models rendered to texture and use that one as sprite, because artificialy adding it per fragment basis might not look that right.
"What is this, I don't even"
"This is your forum."

Offline Surf

  • Moderator
  • это моё.
Re: Shaders and effects
« Reply #6 on: August 25, 2011, 09:55:57 pm »

Oh and in case people are interested in "pixelization", that would be better to have models rendered to texture and use that one as sprite, because artificialy adding it per fragment basis might not look that right.

What do you mean with that?

Offline Johnnybravo

  • Hey there!
Re: Shaders and effects
« Reply #7 on: August 25, 2011, 10:47:01 pm »
It's simplier to have engine do it :d.
Since 2000s graphics hardware can just render directly into texture (think like dynamicly creating sprites), so you setup scene for your model, render it to texture in original dimensions ( I have no idea how big fallout sprites are, but you guys should know ), and then just resize it to fit game screen. This will have the same effect as having small sprites - eg. blurred out/pixelizated details like face on sprites.
It should be also quite cheap, because it wouldn't eat much GPU memory in that small dimensions, and you wouldn't stress GPU with complex fragment shader aproximating detail loss. Instead all you do is just map that texture on quad.

It is possible that some old hardware does not support render to texture, but that wouldn't support PS either.
And the best thing is that players will be able to chose their own settings that fit them the best.

That is this particular effect would look the best if it was done on software level using render targets, instead of rellying on post-processing. It needs engine change however.

Here is some example, though probably not the best one because of 512x512 scene on 640x480 screen looks sharp.
http://www.paulsprojects.net/opengl/rtotex/rtotex.html
"What is this, I don't even"
"This is your forum."

Offline Surf

  • Moderator
  • это моё.
Re: Shaders and effects
« Reply #8 on: August 25, 2011, 10:57:15 pm »
Ah, ok - I totally misunderstood you then. I thought you were suggesting to render all the 3d anims etc again to 2d sprites and pixelate them there then. ;)

Indeed, players being able to adjust the pixelisation (or disable) would be the best. If that is what you are aiming for, then all power to you and keep up the good work!

Offline Luther Blissett

  • Moderator
Re: Shaders and effects
« Reply #9 on: August 26, 2011, 09:59:43 pm »
I haven't managed to get anything out of the Normal maps yet - I've tried a lot of different things, but seem to be getting no results with any. Have you managed to get anything working with this?

Offline Johnnybravo

  • Hey there!
Re: Shaders and effects
« Reply #10 on: August 26, 2011, 11:18:06 pm »
Try to add  'CalculateTangentSpace' in your fo3d along with using normal map effect.
Here's example in documentation
Code: [Select]
Model    Box.3ds
CalculateTangentSpace
Subset 0 Texture 0 DED.tga
Subset 0 Texture 1 DEN.tga
Subset 1 Texture 0 DrD.tga
Subset 1 Texture 1 DIntrN.tga
Subset -1 Effect 3D_NormalMapping.fx
I did not check what format is normal map in, but expect it to be quite the same as in every other game.
Just find some random normal map on internet and slap it on box model, or any other surface where it would be apparent.
EDIT: and no sorry haven't tried it yet.
« Last Edit: August 26, 2011, 11:22:31 pm by Johnnybravo »
"What is this, I don't even"
"This is your forum."

Offline Karpov

  • Moderator
  • Come Together
Re: Shaders and effects
« Reply #11 on: August 27, 2011, 04:45:13 pm »
Here is a demo video for those who haven't seen this in action yet.

http://www.youtube.com/watch?v=_vCQhklJpvE

I've been tinkering with ambient color and light direction. I got some nice outdoor/sunlight shading. It needs to be set in every model you want it to appear, but if somehow it could be set globally, it could be a great feature, like different light setups during the day. Anyway, shaders are very good, thumbs up to the author.

Offline Luther Blissett

  • Moderator
Re: Shaders and effects
« Reply #12 on: August 27, 2011, 05:23:52 pm »
Johnnybravo - I will have a look at this, see if I can get something to work
Karpov - Very good - much clearer example with a video than screenshots. Regarding global effects, there must be some sort of default effect applied to the models, as they had a basic light and shadow working to begin with - I wonder if this could be replaced, or if you can find the file that "points to" the default shader, to "point it" to the path of the new one instead? Also agreed, thumbs up for this work - very important step forward.

Offline Johnnybravo

  • Hey there!
Re: Shaders and effects
« Reply #13 on: August 27, 2011, 06:12:48 pm »
API supports it, so that it shouldn't be problem to propagate some functions to script so that modders can play with it.
Unfortunately as far as I understand community behind engine is the russian part related to TLA server.
Might be a little harder to make related requests.

But it could be nice if script could chose techniques (I might want to have something else than a model + shadow, like ghost effect or lower detailed lighting to make game faster on ancient hardware (like stuff as old as GeForce 3/4 might have hard time calculating it all, anything older does not even have shaders to begin with :d)) and propagate variables.
There might be some more interesting things like lights (dunno how D3D deals with it, but GL has related structures available without need to propagate anything).

As the matter of fact it'd be probably the best to set light 0 structure for global illumination (sun and stuff - directional lighting that is already there) and have other lights used for dynamic (point) lighting.

However scripters should always be aware that scenery WILL be 2D, so changing global light direction will be highly contraproductive.

EDIT: Thanks for the vid Karpov, really nice showcase, it makes MA appear much better than even I expected.
"What is this, I don't even"
"This is your forum."

Offline Karpov

  • Moderator
  • Come Together
Re: Shaders and effects
« Reply #14 on: August 27, 2011, 09:05:08 pm »
You are right, the scenery and buildings would not change , it would look weird. Anyway, your shader seems to improve a lot the looks of non shiny surfaces too.
I changed the standard shader as suggested by Luther, so now all models use this one. Take a look.



In my opinion , the new one looks much better. What do you guys think?
Also the skin is mapped, just a bit of shine over the body, and some more on the head.

As for the global illumination, I used ambient color with a blueish tint. Of course, it's not the same as an additional light, but it softens the dark shadows.