1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Racer and glass

Discussion in 'Racer Physics and Technical' started by Mr Whippy, Jun 28, 2012.

  1. I'm still not happy with the shaders we have, everything feels like a compromise.

    For some older cars then we have problems any way, trying to combine the glossy black outer strip with the transparent middle, but Stereo's shader kinda fixed that a bit. I've not used it properly yet...

    ...mainly because newer cars we just define that bit as separate polygons. Also stuff like headlight lenses don't need that control either.

    Right now I'm trying to set up glass to be like glass, and I just can't.

    The diffuse map is black, since there is no diffuse colour I want in this glass. It's almost totally transparent too like headlight glass is.

    But as you drop the alpha towards black the glass gets very transparent but also it's reflection goes right down too.

    Ideally we want a shader that is straight glass. Diffuse is just that, and alpha is transparency. Reflection can be controlled by reflect = X parameter... that way we avoid trying to do reflect AND transparency off one channel.

    I'm just messing with it now and it looks so much better having rgb do diffuse colouring (if needed), transparency do transparency, and reflect just be the reflection mix amount (almost always going to be 1 on almost all glass any way!)

    Just tinkering with a Forza 4 conversion and there is no nice way to do glass. I struggled all those years back with my M3 too (headlights especially), not being able to control transparency and reflection properly and always feeling compromised.

    Since most car glass is glassy (ie, top surface is dead smooth and usually underlaid with glass), then reflect=1 seems the 99% most used property irrespective of the diffuse/transparency amount (ie, why compromise the glass shader so we have reflect control when it's hardly ever needed, and if it is then we can use a better shader with another texture imo)

    As said, I'll post some samples soon :D

  2. Stereo

    Premium Member

    The problem with transparency to 0 is in the way reflections work with the regular blendfunc.

    I don't really have the tools to measure how glass actually works, but when I implemented it the way 1st year physics taught optics to me, it looked 'wrong'. So maybe 1st year physics doesn't have all the answers.

    What that says is eye = fresnel*R + (1-fresnel)*T - the viewed brightness is the sum of transmitted and reflected light, proportional to the percent of the beam going to each source (which fresnel approximates).

    (well, more strictly, it's an infinitely thin layer of glass - the actual model is http://i.imgur.com/1VbV8.png, where the reflections continue to cascade along inside the glass, becoming fainter each time )

    The way to implement this - in the glass shader - is to set col.a = tex.alpha + (1-tex.alpha)* fresnel, which then lets the blendfunc do its thing*. But, despite this sounding okay, it 'looks wrong' when I've posted screenshots of it.

    Another thing of note - 100% reflectivity is reached before 90 degrees - as soon as the IoR of the glass bends the incoming up too far. So really what you want on glass is a curve from 20% at perpendicular, up to 100% at about 60 degrees. In practice this means bias+scale > 1, which then gets clamped for fresnel.

    The function I had the best response with, ftr, is eye = fresnel*R + T. I had a post a while ago explaining why, I'll go look for it. This one requires changing the blendfunc in the car.shd.
    http://www.racedepartment.com/forum/threads/the-shader-thread.14571/ - should sift through there. That's where gtpdzbiz was posting various screenspace stuff.

    * except for the sky, and decals, which may interact badly if you change the blendfunc.
  3. Is it not just an add blend then, with appropriate fresnel coeffs?

    Do just do the usual transmit of elements behind layer depending on alpha transparency, then add fresnel envmap reflection?

    Should be a pretty simple shader?!

    That is where I've been going with it any way. The biggest simple flaw with the current shader is as you go more transparent it loses reflection so it's just not based in any kind of real response.

    Pretty sure we can also implement this technique on older cars too, might just need a relatively costly 2nd control map to do reflect strength separate to transparency... Also good for rally cars or race cars with screen decals etc I guess.

    Old system worked ok in old days but IMO it's struggling to match the realism of the rest of our new shaders these days :)

    Thanks for the fresnel data, I'll start with that now :)


  4. Stereo

    Premium Member

    dyn_standard_reflect_transparency_window_f.cg is actually using the first method I mentioned - as you can see by
    outColor.a = lerp(1.0,reflCol.g + fresnel - fresnel*reflCol.g,IN.extinction); // refl.g is transparency
    (the lerp is so that it fades with fog correctly)

    It's using a second control map, so you have
    layer0 baseMap : rgb color
    layer1 reflMap : r reflectivity, g transparency, b specular
    layer2 envMap : as normal
    The Aronde uses this as vf_glass, if you need an example. (fresnel bias .04 scale .96 power 4 -)

    An important note is that reflectivity is divorced from transparency - reflectivity of 100% will be the same brightness added to the scene at a given angle regardless of the transparency of the material.

    Magenta (r 1, g 0, b 1) is the stereotypical glass control map - completely reflective, transparent, and specular shining as normal. It multiplies reflectivity and specularity with Kr and Ks so those variables still tune it globally for the shader.

    If you want to make it match reality, controlled lighting and photographs of automotive glass near 90 degree angles reflecting black & white would be helpful :p
  5. :D

    Hmmm, so the glass is a bit more developed than I expected if using the window_f.cg...

    I've been using the dyn_standard_reflect window shader... that is probably why it's not working so well haha.

    These are certainly reasons why we could do with a newer technique default car, or I could do with upgrading the Murcielago again a bit so it's accurately using the best current shaders/techniques!

    Let me try this other shader working properly then :D

    I can't really see the need for the old one. Assuming all content will be updated to the new system for v0.9 proper it makes sense to go with glass that at least responds realistically?!

  6. OK, quick tinker in maths land.

    20% at 0deg, 100% at 60deg ~ 0.2 @ 0 > 1 @ 0.67

    Bias ~ 0.2
    Scale ~ 2.35
    Power ~ 3


    Scale ~ 4
    Power ~ 4

    Hmm, need to re-arrange it so we get a y=1 intersect at x = 0.67 then we can just get a scale/power number generator :D

    Trying that shader generally looks much better. Much more natural looking and as you would expect with given input values.

    It is however causing the auto-exposure to fly around quite a lot here at certain angles. Hmmm.


  7. Hmmm, is the fresnel wrong there. A quick check and it seems ok for glass to air (ie, looking out from inside glass to air), but air to glass is more classical shaped, 0.04 > 1 from 0 > 90deg... so the usual 0.04/0.96/4 is probably good for air > glass (ie, looking at car glass)

    Not the most user friendly tool but used this

    in this

    And this

    And it seems 0.04/0.96/4.5 are good for glass hehe...

    I guess the main thing is that many different glasses may have different IOR or other properties like coatings etc, many are now plastics on lights too... so lots of variance possible :D


  8. Well that shader looks to work ok, but I'm fairly sure the transparency map info isn't being used from the green channel properly

    I can put black or white in there and it makes only the slightest difference... ie, the glass doesn't really appreciably change in transparency.

    In theory it should be working having looked at the shader but I can do weird things in there like change outcol.a to = refCol.g and there is no visual change... so it's feeling like the alpha isn't even being done properly?!

  9. Stereo

    Premium Member

    That is odd... it works fine for me? I see distinct differences using black or white and by changing outcol.a.

    I haven't played with it in a while so I don't know which of these are important.
      specular=0 0 0 1
        blendfunc=dst_alpha one_minus_src_alpha
    Must have been annoyed with specular at some point to set it to zero...
  10. Ah probably your blendfunc on the 1st layer is different. The Simca has it set to "one one" which had a distinct impact on the glass transparency, so no doubt it's wrong.

    I'm still confused why we need to set anything there, isn't the full behaviour handled in the proper shader we reference?

    Hmmm, blendfunc=dst_alpha one_minus_dst_alpha on the 1st layer is referencing the diffuse colour map alpha channel but there isn't one.

    I thought it was two rgb texture maps for this technique. So what data is on the diffuses alpha map? Transparency on there as well as the 2nd maps green channel?

    Getting really confused how this shader is meant to work now haha.

    rgb tex0 = colour
    r tex1 = reflect
    g tex1 = transparency
    b tex1 = specular (monotone)

    Should tex1 mode=linear because of the nature of the map? Setting linear on seems to make the glass totally opaque which is surprising considering when it's off it's totally transparent (gamma adjustment shouldn't make that much difference)

    I'm getting a result here but it's not nice and the tex1 green channel still seems to be a bit of a non-entity wrt how the glass appears.
    Through the glass I'm getting an odd black band too that cuts across elements in the background behind the car that wasn't there with blendfunc=one one.

    I'd be very tempted to say the shader is broken or buggy in and around the transparency on tex1 green channel.

    Stereo, can you explain what the layer0 blendfunc is doing? How come we don't hide that away in the shader itself so the author doesn't have to worry about it... surely it's as fundamental to right looking glass as anything else and shouldn't be adjusted?


  11. Stereo

    Premium Member

    Oh you may be right that it's using the diffuse map alpha. I didn't delete it when I added the second texture. I'd been assuming blendfunc is going to be 'current shader output blended with whatever's behind the transparency', not do something with the actual texture. It's possible layer0.a and layer1.g need to be identical with the way the shader's written. Should probably push transparency back into layer0.a entirely to avoid potential confusion.

    The only explanation for it that I can give is that it looked worse somehow with blendfunc=blend (which is the standard method to induce transparency), but I haven't done anything with it in a while.

    edit: after playing with it a bit, I think blendfunc=blend is the right call, and uses outColor.a from the fragment shader. I must have been expecting a more opaque glass or something.

    (there are 2 shaders in the Aronde using vf_glass - one uses blendfunc=one one, the other uses blendfunc=blend now. The first is with textures tuned to match the 'one one' method)

    I was confused about which way the ray bent on air->glass, you're right that it shouldn't get to total reflection here. At the same time I'm not sure why it would go outside the maximum reflectiveness. Fresnel.cg caps the fresnel value at 0.95.
  12. OK getting closer.

    It still *feels* that the more see-through you make the glass, the less reflective it is too.

    OK, changing blendfunc=blend to blendfunc=add

    Currently running:

      specular=0 0 0 0
    2 x 24bit texture lookups (no alpha on each)

    and imo, that looks pretty right to me. Just driven on my track with lots of trees on it over the road and the glass looked natural to me passing under with add vs blend.

    I'm not sure if that is right or wrong but it felt right :D

    Note I'm using a linear control map, seems to give nicer more intuitive results when dealing with the transparency values I think.

    A quick test with the add blendfunc:


  13. Hmmmm, the transparency doesn't seem to work with blendfunc=add though.

    Add seems the right way to put reflections over the resultant glass diffuse/ambient response though...

    Surely we can just dump the blendfunc bit altogether.

    Isn't it just a case of rendering the glass purely as diffuse/ambient term, with illumination/shadowing (just like standard_f.cg), all done on the 1st texture lookup.

    Then apply transparency from the 2nd texture map green channel to the result.

    Then 'add' the reflection result over the top of that using the 2nd texture map red channel as an 'add' amount.

  14. Hmmm,

    Now just tried blendfunc=one one_minus_src_alpha which seems to keep the same response in reflection terms but dims the transmission of intensity of stuff behind the glass (ie, looking through the sky via different tints of glass) obviously one src_alpha is probably better on GPU load and we just invert the green channel?? (haha, Photoshop doesn't do a linear invert though, only a gamma'd invert :D , can do it in After Effects linearly though ;) )

    As I said though Stereo, how come we have control of blendfunc like this? Surely this stuff is absolute rather than optional?
    Why is it something we need to set?

    Ie, we don't set the blend method for reflect.cg shaders to say how we want the envmap applying for example.


  15. Dave, maybe try
    blendfunc=src_color one
    then you're only adding the reflections based on their brightness to the framebuffer contents?
    Factor Explanation
    zero 0
    one 1
    dst_color the destination color (what's in the framebuffer)
    one_minus_dst_color 1-dst_color
    src_color Source color
    one_minus_src_color 1-src_color
    src_alpha the texture's alpha channel value
    one_minus_src_alpha 1-src_alpha
    dst_alpha destination alpha in the framebuffer (may NOT be present!)
    one_minus_dst_alpha 1-dst_alpha
    src_alpha_saturate never lowers the alpha value
  16. Thanks for that David...

    I'll give that a try.

    BUT, I don't get why we are adding this to layer0 when the reflections are way off up on layer2...?

    Shouldn't all this really be an absolute definition set in the shader file?!

    Anyhow, I'm still getting the best results now with one src_alpha and gamma on the control texture now (makes life easier)

    I get reflections that stay intense despite high opacity or low opacity which is the behaviour I wanted, and the opacity control map is intuitive to control. Win win, but who knows if it's actually working right 'physically' etc... feels a bit like cheating having preference over things like blending mode when it's an absolute that should be fixed.

    One thing I have noticed though, though I'm not massively concerned about it, is that when you look through the front screen to a tinted back screen it's obviously not tinted.
    Cull=none causes issues, so I guess the fix if you wanted to is to put a 2nd plane up with reversed normals and offset back a bit... hmmm...

  17. Also has anyone noticed their auto-exposure flashes around while using this shader?

    On the RS3 it flickers a lot with it, and then reverting to the old glass shader it doesn't flicker around so much.

    It's either the shader or a combo of the car an shader.


  18. @Mr Whippy, is that track on the movie with RS3 private or public?
  19. Hi there,

    The track is WIP, it's really WIP and has been for a long time. I could do a bit more work and then release it. It's not drivable for now unless you know the road so you don't fall off :D

    It also doesn't look so great. I might post up a WIP thread so you can see it though. There is a near full video of the course on my youtube, lambo murcielago driving along it.

    The primary thing in there is the road driving surface is very close to a real life road, also the terrain is very accurate too.

    Apart from a few hills/bends the road surface is almost signed off then I can start building the verges etc off it. I have lots of data and some of it conflicts so it's a matter of trying to figure out the actual shape of the road (heights/cambers mainly)
    It'll all come together it's just such a long course (8km ish)

    I'd like to finish it up soon but I have another track I'm working on which is easier to finish and also a bit smaller/further on in the development process. I'll get that done first :D

  20. Anyone else using this shader still?

    Do you get weird exposure settings now and again (generally the scene brightness goes up by a few stops so it looks blown out)...

    You can be panning around the car and it looks normal, and then suddenly you hit a certain angle and it gets really bright. Move the camera a bit again and it returns to normal...?

    Just checking it's not something at my end! It only happens when this glass shader setup is used for me at least.