The shader thread?

I'm messing with shaders a bit right now.

Right now I am looking specifically at adding control for specular to the existing reflect shader... on Deyan's Murcielago the unified wheel/tyre texturing poses some problems.

I have to control reflection strength with an alpha map, so use the old trick of adding a syntax in the reflection shader so if we define reflection = -1, the baseCol.a is used to control reflection strength.

ambientColor=FresnelMix(ambientColor,envColor.rgb,(Kr>0?Kr:-Kr*baseCol.a),fresnel);


The problem now is that I have no spare texture slot for specular/shininess control.

So I have added this little fragment of code (from the bump speca shader)

Ks=baseCol*Ks;

This now allows the colouring of the outside of the tyre in the diffuse (rgb) channels, to do some work.



However, because the tyre is still dark in the diffuse texture (near black as it is a tyre), it doesn't have much effect on it's own.

I need to somehow increase the brightness of the texture so Racer pumps up the specular we see.

So I added a *5 multiplier to the Ks as you can see in the image attached.

shader_01.gif




Now what I don't understand is why the two images look different.

First case:
Ks in car.shd is 8, while Ks = baseCol * Ks * 5 essentially gives us Ks = baseCol * 40

Second case:
Ks in car.shd is 40, while Ks = baseCol * Ks essentially gives us Ks = baseCol * 40


Now, I don't get why they look different. Ks is meant to control the size of the specular too, but in either case they both calculate out to a peak shininess of 40, because the baseCol lookup isn't changing.
The specular settings remain the same.

It appears that multiplying by 5 in the shader means the specular level is pumped up 5x, while the shininess (size of the specular) is retained at 8 in the car.shd...

That is the effect I wanted to achieve, pumping up the actual output of the specular blob, while keeping it soft at a value of 8, but it seems un-intuitive/wrong because the value that really needs bumping up is the specular value, not shininess? (though the specular being multiplied by 5x (2.5 2.5 2.5) looked awful)

Am I missing something obvious here?




Ideally I'd like to control reflection amount, specular AND shininess using one shader here...

Probably using rgb.a for diffuse.reflection strength, and then another 3 channel texture for spec/shine and leaving another slot free for something else (perhaps transparrency?)

Specular/shininess maps add so much to these kinds of materials, so it is a shame that they haven't really been considered in the default shaders very much. I think they should almost be defaults in all materials that err away from being generally reflective (ie, using the envmap)


Bodging in the code I have for the Lambo works quite nicely in this case, but it's obviously not ideal to use the diffuse channel to control specular in this way.



Hmmmm

Dave
 
As per the GIMP issue, that is a tough one. Can you not just edit in redscale, bluescale, and greenscale, as it were?

Or better yet, keep each map as a greyscale, then copy/paste to the appropriate channel in the TGA for final use?

Can't find a way to directly paste into the channels unfortunately.
What I did was 'Colorify' to convert greyscale to red/green/blue layers, then set layer mix to 'additive' with a black layer behind them. Same result, but a bit more indirect than just being able to edit each channel. Once they're in red/green/blue, it's possible to just edit them with the normal tools limited to those pallettes. Eg darken/lighten, or flood fill in shades of red. I didn't play too much with colorify, but it seems like it's just removing all non-reds for example.
 
screenshot003iu.jpg

screenshot006iu.jpg



Mostly implemented as I was thinking through, and I edited the reflectivity map so the black frames match the rest of the car's gloss-black (0.3 reflectivity).

Just need to figure out how to handle skyColor and I'll post it up to fool around with. Saying again, I really need better test models than the Lamborghini :x It's hard to show off how the glass looks on it, but nothing else I can easily use as a testbed is much better.


EDIT: A couple bugs with using blendfunc=one one_minus_src_alpha, makes this currently not ideal
- semi-transparent objects can't be seen through it
- shadows don't cast through it
I'm not sure how either of these are handled but until they're fixed it's probably better to leave it as it is.


Anyway, candidate #4
Code:
//
// Standard
//

#include "atmosphere.cg"
#include "lighting.cg"
#include "fresnel.cg"
#include "shadowmapping.cg"

// Vertex to pixel shader structure
struct v2p
{
  float2 tc0            : TEXCOORD0;
  float3 Direction      : TEXCOORD1;
  float4 RayleighColor  : TEXCOORD2;
  float4 MieColor       : TEXCOORD3;
  float4 Position       : TEXCOORD4;    // For fragment shader
  float3 normal         : TEXCOORD5;
  float3 I              : TEXCOORD6;
  //float  fresnel        : TEXCOORD6;
  float3 R              : TEXCOORD7;    // Reflection vector
  float  extinction     : COLOR;
};

void main(
  // In
  in v2p IN,

  // Out
  out float4 outColor : COLOR0,
#ifdef CSM_MRT
  out float4 outShadow : COLOR1,
#endif

  // Constants
  uniform sampler2D   baseMap : TEXUNIT0,
  uniform sampler2D   reflMap : TEXUNIT1,
  uniform samplerCUBE envMap  : TEXUNIT2,
  uniform float3    lightDirection,
  uniform float3    lightColor,
  uniform float3    lightAmbient,
  uniform float3    eyePosW,
  uniform float     atmosRayleigh,
  uniform float     atmosMie,
  uniform float3    Ke,
  uniform float3    Ka,
  uniform float3    Kd,
  uniform float3    Ks,
  uniform float     Kr,
  uniform float     shininess,
  uniform float     sunny,
  uniform float     exposure,
#ifdef CSM
  // shadow mapping
	uniform sampler2D    shadowArray  : TEXUNIT7,
  uniform float4x4     smTexMatArray[SM_MAX_SPLITS],
  uniform float smSplits,
#endif
  uniform float    fresnelBias,
  uniform float    fresnelScale,
  uniform float    fresnelPower
)
{
  float3 skyColor;

#ifdef CSM
  float  shadow;

  // Output shadowing and normals
  shadow=GetShadowFactor(IN.Position, IN.normal,shadowArray, smTexMatArray, smSplits, lightDirection)*sunny;
  //outShadow.rgb = IN.normal;
#else
  const float shadow=1;
#endif

  // Get sky gradient color
  skyColor.rgb=GetSkyColor(lightDirection,IN.Direction,IN.RayleighColor,IN.MieColor,atmosRayleigh,atmosMie,lightColor,lightAmbient);

  // Get base texture color
  float4 baseCol=tex2D(baseMap,IN.tc0);
  float3 reflCol=tex2D(reflMap,IN.tc0);
  Kr *= reflCol.r; // second map red = reflectivity
  shininess *= reflCol.b; // second map blue = specular shininess

  // Reflection
  //const float Kr=0.5f;
  float4 envColor=texCUBE(envMap,IN.R);
//envColor*=3.0f;

  // Lighting
  float3 ambient,diffuse,specular;
  LightingSun(Ke,Ka,Kd,Ks,shininess,lightDirection,lightColor,lightAmbient,IN.Position,IN.normal,eyePosW,
    ambient,diffuse,specular);
  float3 litColor=(baseCol*diffuse+specular)*reflCol.g;
  float3 shadowColor=baseCol*(ambient+Ke)*reflCol.g;
  //baseCol.rgb=baseCol*(ambient+Ke)+baseCol*diffuse+(specular*((int)outShadow.a));

  // Add reflection
  float fresnel=Fresnel(fresnelBias,fresnelScale,fresnelPower,normalize(IN.I),normalize(IN.normal));
  outColor.a = lerp(1.0,reflCol.g + fresnel - fresnel*reflCol.g,IN.extinction); // refl.g is transparency
  shadowColor=FresnelMix(shadowColor,envColor.rgb,Kr,fresnel);
  // Reflection influences opaque-ness?
  //baseCol.a=max(baseCol.a,envColor.g);

#ifdef CSM_MRT
  // Mix sky with texture color based on atmospheric influence
  outColor.rgb=lerp(skyColor,shadowColor,IN.extinction);
  //outShadow.rgb=lerp(skyColor,litColor,IN.extinction);
  outShadow.rgb=litColor*IN.extinction;
//outColor.rgb=fresnel;

  // Blending
 // outColor.a=reflCol.g; // second map green = transparency
//outColor.a=1;
  // CSM
  outShadow.a=shadow;
#else
  outColor.rgb=lerp(skyColor,shadowColor+litColor*shadow,IN.extinction);
  //outColor.rgb = shadowColor.rgb + litColor.rgb*shadow; // hack
  //outColor.a=reflCol.g;
#endif
}
tex0 -> diffuse rgb
tex1 -> reflectivity, transparency, specular

And corresponding from car.shd
Code:
shader_glass~vf_glass
{
  sort_offset=1
  reflect=1.0
  layer0
  {
    map=glass.tga
    blendfunc=one one_minus_src_alpha
  }
  layer1
  {
    map=glass_r.tga
  }
  layer2
  {
    map=$trackenvmap
  }
  fresnel
  {
    bias=0.04
    scale=0.96
    power=4.0
  }
}
Lamborghini Murcielago tex1 -> http://img25.imageshack.us/img25/5955/glasstestr4.png (as in screenshots. Used with plain glass.tga as tex0 now.)

Other than the shadow/transparency problems, I'm very happy with how this looks using practical values (100% reflective, 15% transparency, 100% specular for glass; 30% reflective 100% opaque 100% specular for the surround)

EDIT - minor fix for specular/diffuse.
 
That looks good so far :D

I'd probably go for ~100% reflective on the surround though, since it's basically the same glass with black underneath it... again, that is probably a real value for it, so if it's not looking right with that value, then it's possibly the shader or other control maps not being right... hmmm...

So what colour are you using for glass? Light colours or dark colours? Diffuse colour for glass is a tough one... :D

Is a green tint and light mint green, or a dark green, for example... hmmmmm... :)


Also, for these kinds of glass, do we want specular, when the HDR envmap contains the suns effective specular component on materials already?

Dave
 
So what colour are you using for glass? Light colours or dark colours? Diffuse colour for glass is a tough one... :D

I think the best way to gauge a glasses tint (the colour the actual glass is, not window tinting) is by looking down its edge. the old old coke bottles while looking clear, looked greenish, my car windows are slightly blueish hue'd grey when viewed in bright sunlight.
 
I'd probably go for ~100% reflective on the surround though, since it's basically the same glass with black underneath it... again, that is probably a real value for it, so if it's not looking right with that value, then it's possibly the shader or other control maps not being right... hmmm...

Lack of border on the front windscreen mostly is what looks funny (see for example) - really the edge of the glass should not abut directly to the bodywork, it needs a small non-reflective gap. On the side windows it's modeled in, which is fine. On the front it's less noticeably missing if they're not so reflective. Partially a test of how close the surround could get to the other black on the car. I don't have the unwrap of the mesh so I can't tell where the edge of the front windscreen's mapping is to do it right, though.
 
I'm reworking the Lambo quite a bit, so much better materials splitting/logic.

I will find the windscreen outline and then we can draw that rubber line in around the glass in the texture :D

Still didn't try the shader, was going from 29 > 31 last night and fighting with the diffs spinning my wheels mad with MF5.2 tyres... hmmmm


Dave
 
Still didn't try the shader, was going from 29 > 31 last night and fighting with the diffs spinning my wheels mad with MF5.2 tyres... hmmmm


Dave

Should really work in any recent version although I haven't tested, don't think shaders had any major changes.

I noticed I can spin the wheels in neutral in .31, pretty cool! :D
 
Semi-random fiddling found a solution to the problems I was having with my glass shader... shadows and transparent objects work correctly, reflection strengths are right, all around I am very happy with it.

I'm going to write up a version to replace the default glass shader (using a single 4-channel texture as expected), and post it.

If you've been trying earlier ones, the change is in car.shd:
Code:
blendfunc=dst_alpha one_minus_src_alpha
I am still not 100% clear on how dst_alpha works, but it does, so. There you go.
 
idk if some of you have seen this issue with car lights, an illustrating picture :




Looks like a missing checking condition in the Z-buffer, which would check if projective lights are 'in front of' or 'after' the car itself rendered to window space.

The picture is from ForestClubCG, so go ahead & tell me if you encounter the same prob.

Grr, bestline shader does the same (racer.in gfx.show_bestline=1)....:frown:
 
idk exactly, seems to be a 'missing' computation of matrices in surface/tangent space, not that sure though.

Should be aligning exactly with the curvature of the road for both shaders & not to our ZX plane (terrain/road)...

Been checking Ruud/Mitch shader equations also, also gDebugged my whole scene to see what the Racer buffers doing, etc...
Anyway, we have a good basis really & most of formulas sitting good, still, the way the job was done is maybe discussable, quick & dirty !

Too much on my mind right now, been also checking cam link with the 'real water' shaders & reading more about all kind techniques for proper shadow mapping.

I think we shall fix the light shader props firstly, so it acts as in reality & from there , begin to think how we get best shadows in our open worlds. Maybe getting more dynamical with vertex/texels at run-time & help ourself (in Cg code) with what we already have or know...
 
IMO we really need to be making things more simple atm.
Honestly, racer is far too resource heavy atm...many other games look just as good and easily run at twice the FPS. Sure, it could be the difference between DirectX and OGL and how the GPU manufacturers have focussed their work, but I'm SURE there's optimisation to be had, it'll just take a bit of digging.
 
Try this:

goto \data\renderer\shaders and open shadowmapping.cg.

Search for "GetShadowFactor("
after the first following '{' put "return 1.0f;" , save, and open racer.
while racer runs, remove that line that you just entered and enter "reload gpushaders" in the console. put it back and enter the console command again .... are you seeing what happens ?
On my laptop the framerate goes from 55 to 150.

There is an unbalance in the GPU-pipeline
Racer's Pixel shaders are far to heavy atm, that is the biggest bottleneck as far as i know, and shadowmapping hurts the most.
 
Maybe racer.nl documentation should have some recommended settings for nVidia control panel, & some info on what to change to improve performance over image quality etc?

I tried what Mitch suggested, I go from 70fps to around 100fps. Thats with Mugello & Lambo.
 
Indeed, I did have a quick look a while ago to see what was causing the hold up and shadowmapping seemed to be the biggest drain.

Other than calculating a whole bunch of stuff it uses a LOT of texture memory. No way to avoid that really.

I should say, Racer runs great on my main machine. I just know a lot of other people have troubles.
 
Specifically, it is the blurring of the shadowmap that kills fps. Go into racer.ini, search for blur to get the shadowmap blur control, change it to zero & note fps in Racer, change it back to 1 & note the fps. shadowmapping itself only eats an fps or 3, but the blurring itself eats 10x that.
imho we need a more optimised and/or intelligent blur routine.
 
Haha this is where it gets tricky, as I've not noticed any discernible difference in FPS with the blur. I've even bumped up the samples.

This is where we really need to have profiles for different GPUs. Or quality level settings, with this community and the spread of different GPUs we have we should be able to come up with some pretty nice numbers for anything anyone throws at it.
 

Latest News

What's needed for simracing in 2024?

  • More games, period

  • Better graphics/visuals

  • Advanced physics and handling

  • More cars and tracks

  • AI improvements

  • AI engineering

  • Cross-platform play

  • New game Modes

  • Other, post your idea


Results are only viewable after voting.
Back
Top