April 25, 2019, 11:52:52 AM

Author Topic: AGK HDR lightmapping  (Read 350 times)

Offline GaborD

  • Full Member
  • ***
  • Posts: 244
AGK HDR lightmapping
« on: January 18, 2019, 10:43:49 AM »
I have to make some demos and got tired of slow noisy Blender lightmaps and all the conversion steps to get the HDR data to AGK, so I dug up some old test code and started implementing a quick own lightmapping solution in AGK.

Supports HDRi / image based lighting (could light up the whole scene just with a sky HDRi without mixing in a realtime sun), up to 4 precalced realtime lights per surface, each with correct soft shadows and accordingly attentuated speculars, auto-padding, adjustable blurring of the result, an extreme range (from a billionth to about a million brightness, if we assume full LDR range to be 0 to 1), dual lightmapping and correct mixing of dynamic shadows with the soft precalced ones, a controllable light-bounce count, full support for emissive and translucent surfaces and contact hardening soft penumbras (light sizes can be set for each light).

The system can also produce directional lightmaps if needed so that normalmaps correctly work with the bouncelight (the direct light is already realtime, so that works anyway), it will also create all the corresponding PBR probes and I also want to implement an idea for reflections on planar and bigger surfaces (which are hard to get to look right with just probes) that doesn't rely on constant flipped cam renders or slowpoke pathtracing for SSR (That said, I will probably port my NB SSR solution as an option for surfaces where it's a good fit).

So basically all the hassle of creating a fully lit PBR scene with semi decent quality HDR lighting simplified and condensed down to a button press. Will make life so much easier haha. 
Will see how it goes. Quality is not too bad for a first rough implementation but can still be improved a lot.
It's a pretty bruteforce unbiased solution, should produce mostly reasonable results, but bounce and direct light strength can be also adjusted independently at runtime for a more artistic approach and effects.

Lightmap resolutions and also the sample counts are flexible on a per object or group basis, it's also possible to use different map resolutions for the shadows and the bouncelight (bounces usually don't need high rez), so there is full flexibility for speed vs quality and quick lighting tests.
Simple and directional lightmaps can also be mixed in the scene and will match up correctly.
Later I plan to add outdoor rendering focussed features too. But that's for a later day, don't need it for my planned demos.

The test-scene is admittedly very simple, but made to be a worst case scenario with big totally flat surfaces that have smooth lighting gradients and also some pretty saturated colors (always prone to banding and causing weird lighting).

Here are some test screens directly from AGK, just the diffuse lighting on the smooth untextured sufaces. (textured surfaces would ofcourse bounce correctly colored light based on their texture, same for emissive and translucent surfaces)
Click the thumbs to open them in a new tab and click again to enlarge, the images are pretty big.
AA could be better, didn't port my whole post chain over yet. Lazy.
So much to do...

   

 

Offline Madjack

  • Jr. Member
  • **
  • Posts: 43
Re: AGK HDR lightmapping
« Reply #1 on: January 18, 2019, 10:58:53 AM »
Aww yeh! Gabor's back, back in the shader hoouse!!!  :D

Offline Steve Elliott

  • Hero Member
  • *****
  • Posts: 1719
Re: AGK HDR lightmapping
« Reply #2 on: January 18, 2019, 11:29:48 AM »
Beautiful work, as always.
Windows 10, 64-bit, 16Gb RAM, CPU Intel i5, 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb).
MacOS Mojave, 64-bit, 8Gb RAM, CPU Intel i5, 2.3 Ghz, Intel Iris Plus Graphics 640 1536 MB.
Linux Mint 19.1, 64-bit, 16Gb RAM, CPU Intel i5, 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb).

Offline Rick Nasher

  • Hero Member
  • *****
  • Posts: 676
Re: AGK HDR lightmapping
« Reply #3 on: January 18, 2019, 04:36:34 PM »
Nice beginning. Can't wait to see the next development given your previous work.

I'm guessing you are using a high spec system?

_______________________________________
 B3D + physics + shaders + X-platform = AGK!
:D ..ALIENBREED *LIVES* (thanks to Qube).. :D
_______________________________________

Offline Qube

  • Administrator
  • Hero Member
  • *****
  • Posts: 1733
Re: AGK HDR lightmapping
« Reply #4 on: January 18, 2019, 06:40:48 PM »
Very nice :) - You should make your own AGK shader pack for sale.
Until the next time...

Offline GaborD

  • Full Member
  • ***
  • Posts: 244
Re: AGK HDR lightmapping
« Reply #5 on: January 18, 2019, 08:52:27 PM »
Thanks for the comments!
I am really having fun with this.
Once all the PBR shenanigans are in I'll make a vid with a better scene. Have some fun ideas for effects and whatnot.
I want to push this until I have a full openworld RPG system with modern graphics. 


Very nice :) - You should make your own AGK shader pack for sale.

I have some things planned. Last year was unfortunately a big mess and life got in the way, but this year will hopefully be better. :)
My idea is to keep working on the RPG system and maybe make packs and tutorials out of the stuff I develop on the way.
I used to have PBR tutorials on the NB and some other forums, didn't get much feedback (other than one guy flaming me because I put something he considered to be a "code snippet" into the "tutorials" section... thing is, there was no "code snippet" section, so yeah, whatever. I still remember that one lol) but I hope it helped some people none the less. Would love to do stuff like that again for AGK users, just need to find the time for it.
In my opinion AGK is very underrated because people just see the stock stuff. It can do much more with a bit of effort.


I'm guessing you are using a high spec system?

I'd call it upper midrange, it's a GTX1070.



Offline RemiD

  • Hero Member
  • *****
  • Posts: 801
Re: AGK HDR lightmapping
« Reply #6 on: January 19, 2019, 09:41:42 AM »
woaw ! nice  8)


so, with your shader wizardry, can you blend dynamic (real time) per pixel shadows (of animated characters) with static (precalculated) per texel shadows (of the environment) ? ( tricky question, that only the enlightened ones understand ;D )
DualCore AMD E-450, 1646 MHz - 6 Go DDR3 1333 SDRAM - AMD Radeon HD 6320 Graphics (384 Mo) - Windows 7 Home Premium - DirectX 11.0

Offline GaborD

  • Full Member
  • ***
  • Posts: 244
Re: AGK HDR lightmapping
« Reply #7 on: January 19, 2019, 10:32:18 AM »
Yes, exactly.
Besides the better static shadow quality this allows, it's also a really nice speed boost for dynamic shadows with only the dynamic objects needing to be rendered into the map.

Offline Rick Nasher

  • Hero Member
  • *****
  • Posts: 676
Re: AGK HDR lightmapping
« Reply #8 on: January 19, 2019, 11:19:45 AM »
Wow. RemiD, you just called me enlightened!  :P
_______________________________________
 B3D + physics + shaders + X-platform = AGK!
:D ..ALIENBREED *LIVES* (thanks to Qube).. :D
_______________________________________

Offline RemiD

  • Hero Member
  • *****
  • Posts: 801
Re: AGK HDR lightmapping
« Reply #9 on: January 19, 2019, 08:56:23 PM »
Quote
Wow. RemiD, you just called me enlightened!
we are that cool :P


@Gabor>> so how do you know when to add (or not) a dynamic shadow color to the (pixel) of an area of scene, since from my limited understanding of shader, you use per pixel data, and not per texel datas ? (a texel can have a dark color because of its material / texture, but not necessarily because of a (precalculated) (texel) shadow).
DualCore AMD E-450, 1646 MHz - 6 Go DDR3 1333 SDRAM - AMD Radeon HD 6320 Graphics (384 Mo) - Windows 7 Home Premium - DirectX 11.0

Offline GaborD

  • Full Member
  • ***
  • Posts: 244
Re: AGK HDR lightmapping
« Reply #10 on: January 19, 2019, 11:52:55 PM »
so how do you know when to add (or not) a dynamic shadow color to the (pixel) of an area of scene, since from my limited understanding of shader, you use per pixel data, and not per texel datas ? (a texel can have a dark color because of its material / texture, but not necessarily because of a (precalculated) (texel) shadow).

The two shadows are multiplicative, so you can just combine them to one shadow value and then use that to attentuate the sunlight.
The material color of the surface is not in play at this point.
You only multiply it in after you have the combined incoming diffuse lighting. (direct lighting + GI)

So basically (for non-metals): combinedDiffuseLight * surfaceAlbedo + reflections.
(reflections don't get influenced by the surface color. They just get added on top)

For metals it's much easier, because you don't have any diffuse lighting (their albedo is black), so for metals you can skip the entire diffuse stuff.
But, the metal's specular color tints the reflections, which non-metals as seen above don't do (their spec color is essentially white), so that has to be taken into account.

This is why in order to get somewhat realistic looking metals, you kinda have to move towards PBR. You need the reflections, because that's all they are.
The big difference is, diffuse light is static (if the object and the light are both static) no matter which direction you look at it from. Reflections on the other hand are ofcourse view dependent and will shift around if the camera moves.

edit: I went off on a tangent, sorry for that lol.

Offline RemiD

  • Hero Member
  • *****
  • Posts: 801
Re: AGK HDR lightmapping
« Reply #11 on: January 20, 2019, 09:17:10 AM »
@Gabor>>i am quite ignorant about how per pixel shadows are made, so my question was more : in your procedure, how do you determine if a pixel (on screen) of a shadow of a turning moving shape, must be colored in the shadow color (because the environment does not have a shaded area / shadow at this place (texel)) or not (because the environment already has a shaded area / shadow at this place (texel)) ?
Do you start from the surface -> triangle -> texel -> corresponding 3d world position, of the shadow / light map, then convert it to the pixel screen position, then decide, depending on the color of the pixel (determined by the "rendering" of the shadow / light map) if the pixel is already a shaded area / shadow or not, and if there is a need to color it with the shadow color or not (because of the "dynamic" shadow of a turning moving shape projected to this same area)

i hope this is clear ;D
DualCore AMD E-450, 1646 MHz - 6 Go DDR3 1333 SDRAM - AMD Radeon HD 6320 Graphics (384 Mo) - Windows 7 Home Premium - DirectX 11.0

Offline GaborD

  • Full Member
  • ***
  • Posts: 244
Re: AGK HDR lightmapping
« Reply #12 on: January 20, 2019, 04:08:45 PM »
Yes correct, the dynamic shadowmap lookup uv is calculated in the pixel shader from the 3D position (which is coming from the vertex shader instances of the 3 parent vertices.)
The static smooth shadow factor is just a texture lookup based on the second object UV set. (so just like the lightmap for GI)

I just fetch both shadow values and multiply them together to have the overall shadowing factor for the light they belong to. (will always be between 0 for full shadow and 1 for no shadow, so that we can just simply multiply the light color with it)
I don't think of it in terms of screen position or pixel/texel or "drawing shadows" at all. It's just an occlusion factor for a single light.

In a pixel shader, I generally don't think of screen positions.
The screen position can't be changed anyway, the pixel shader instance runs for some pixel position somewhere on the screen, I don't need to know which one. The texture lookups will be based on object UVs anyway. (even in Post while doing fullscreen effects, I'm just drawing an UV'd 3D object. It just happens to be a quad aligned to the cam, but that's irrelevant in the pixel shader.)
That said, you can look up the screen position with gl_FragCoord.xy, it's just almost never necessary. (there are exceptions ofcourse, like planar flipped cam reflections being drawn directly in non-screenquad object shaders, but those are barely used anymore these days, everyone loves SSR so much haha. I don't. :P Time to bring back oldschool!)