Page MenuHomeWildfire Games

Water-Patch
Needs ReviewPublic

Authored by DanW58 on Feb 24 2021, 9:03 PM.

Details

Reviewers
vladislavbelov
Summary

I replaced the original Fresnel formula with Schlick's Approximation.
Had to increase brightness of sky, as the water reflected less.
Had to increase the normal bending of the waves for them to be noticeable.
With those changes, the water looked more natural.
Found a couple of optical mistakes:

  • Fresnel was being modified by shadows.
  • Specularity was being modified by shadows.

One final unresolved problem is the reflections of terrain and objects are much fainter than they used to be.

Another feature I wanted to implement was blurring of refracted ocean bottom by means of LOD biasing the texture read, but it seemed to have no effect.

Test Plan

None. This contribution is for whatever it's worth; not intended for adoption.

Diff Detail

Lint
Lint Skipped
Unit
Unit Tests Skipped

Event Timeline

DanW58 requested review of this revision.Feb 24 2021, 9:03 PM
DanW58 created this revision.
DanW58 edited the summary of this revision. (Show Details)Feb 24 2021, 9:06 PM
Freagarach edited the summary of this revision. (Show Details)Feb 24 2021, 9:22 PM
Freagarach set the repository for this revision to rP 0 A.D. Public Repository.

@DanW58 I believe these problems can be solved, and I support you.

DanW58 updated this revision to Diff 16094.EditedFeb 27 2021, 11:00 AM

Solved the problem of reflections being too faint.
Solved a problem with artifacts on land; it yielded to a number changed in the Schlick approximation from 1.0 to 1.15.
Found another mistake: The refracted bottom had no incident lighting computed; it was essentially an emitter.
Added a hack to make coasts exhibit a darker band resembling wetness. It was at the cost of draining some of the water (lowering the level, optically).
Added crossing waves at 90 degrees with different scaling, to try to break or hide the tiled look of waves.
EDIT: Thanks, Gameboy; missed your comment.
For a screenshot, see this post:
https://wildfiregames.com/forum/topic/36330-a-psychic-shader-mod-development-begins/?do=findComment&comment=418155

DanW58 updated this revision to Diff 16153.Mar 1 2021, 2:51 AM

Got rid of Schlick's Approximation and implemented true Fresnel from Wikipedia.
Obtained RGB refractive indexes for seawater and freshwater; presently sea-water is used.
Found another mistake: Refracted light was not multiplied by (1-reflection_factor); now it is.
Commented out my previous attempts to mix scalings and orientations of the wave animation; they were
causing the appearance of fine ripples, not sure why.
Made the wet coasts feature more subtle.
Fixed problems with there being tabs instead of spaces.
For screenshots, check this forum post:
https://wildfiregames.com/forum/topic/36330-a-psychic-shader-mod-development-begins/?do=findComment&comment=418372

Come on, my friend, solve your problems, I believe you can do better.

DanW58 updated this revision to Diff 16154.Mar 1 2021, 5:50 AM

Improved the hack for wet-looks of coasts.
Fixed the refractive factors in and out of the water: Now sunlight penetration is modulated by fresnel refractive factor based on ndotl, while light from the sky is not modulated going in. Upon coming out of the water, light is modulated again by fresnel refractive factor based on ndotv.
Screenshots in this post:
https://wildfiregames.com/forum/topic/36330-a-psychic-shader-mod-development-begins/?do=findComment&comment=418383

Hey, thanks for the patch!

However it seems to contain a lot of different changes which makes it harder to review it without letting bugs slip. Could you split it into several parts?

  • One for wet shores change
  • One for Fresnel adjustments change
  • One for blur adjustments change
  • One for style changes at the end

Style, it's important you follow the coding conventions, I know it's a bit annoying at first, but until we have a good way to enforce them, we need to
make sure the style stays consistent (e.g we don't use snake case in function names, but one can have a_ or v_ prefixes).

binaries/data/mods/public/shaders/glsl/water_high.fs
2

Space.

15–19

I'd like to avoid the alignment at all, since every change breaks blame.

123–124

Unused code.

311–314

Variables should be declared in a place where they're used. Also their names should correspond somehow with their usages.

DanW58 added a comment.Mar 1 2021, 6:43 PM

I'm sorry; I thought that converting tabs to spaces was going to help, but it seems the original file had tabs. Should I convert double-spaces back to tabs? Let me know.
In the meantime, I have a suggestion without any preconceived idea who would be implementing it, if at all; of course my willingness is absolute.
The idea is to expand the water shader to include stuff that floats on water, such as water-lilies --as are found in Belgian Bog.
I think the engine could programmatically separate floaters and send them to the water shader as a separate texture.
The benefits of doing so are that the water shader could then,
a) rock the floaters as per the water waves' normal ((they look odd being still when the water is waving)), and
b) move them around slightly, by using texture offsets on the floaters texture, to represent wind.
The texture offsets could be related back to the numbers controlling trees' motions representing wind.

DanW58 updated this revision to Diff 16198.Mar 2 2021, 1:16 AM

The water_high.fs shader was starting to slow framerate on my old videocard, so I looked for ways to optimize the code.
This first step is to move the sun-dimming factor for sunlight refracting into the water; namely the ndotl calculation, where n is 0,1,0, and the fresnel calculation for refraction. The product of fresnel refraction and ndotl is written to a varying called "sun_dimming_factor", such that water_high.fs now only has to compute Fresnel once.

DanW58 added a comment.EditedMar 2 2021, 1:33 AM

@vladislavbelov Sorry, never saw your comment until now. I'll see what I can do. The problem with shaders is that often the order that operations can be applied is not as originally envisioned. Case in point the dimming of sunlight entering the water is done at the end of the pipeline, which is neither here nor there since the order of multiplicands is invariant, but makes isolating concerns more difficult. Off the bat,

  • One for wet shores change *** It will be difficult to isolate, but I'll try.
  • One for Fresnel adjustments change *** The sunlight fresnel just moved to the vertex shader, so the only fresnel adjustment is in line 346: vec3 color = mix(refrLight, specLight, F_refl_RGB );
  • One for blur adjustments change *** I didn't change it, as you told me there's no LOD's.
  • One for style changes at the end *** Style changes?

Another change is, near the top of the file, I took the guts of getNormal() and split them into getNormalSub(), so as to have getNormal() call it several times with different parameters for wave movie scalings and rotations, so as to mix it into hopefully a less noticeable tiling. But this never worked, so there's only a single call of getNormalSub(), with the original parameters.

I just had another idea; maybe the engine could pre-blur the bottom texture with radius = depth / 16. EDIT: Either that, or it could add LOD's to the generated texture. It would be much cheaper, performance-wise, than blurring manually in the shader. But the LOD's have to be blurred; can't just be like scalings; it has to be like 2 texel radius gaussian blur; but probably faster to code AND faster to compute than a larger radius pre-blur of a non-LOD texture.

There is a problem I have no idea how to solve; and it looks awful... Shadows falling on surface underwater, don't get distorted, since the distortions are done by texture offsets prior to shadowing. This is particularly bad because shadows should be distorted by waves TWICE: going in, and coming out. The shadow of a palm tree falling into the water, right now, is so shockingly sharp and still you don't know how it's possible. I faded the shadows in line 344 for this reason; it's not correct optically to fade them; but they look so bad it's not even a choice. There must be a way to fix this problem ...

Wait! Can't we use offsets with the shadow map?
I mean, we have getShadowOnLandscape(); we could have a
getShadowOnLandscape( float OffsetU, float OffsetV) maybe?

EDIT: Another problem is that shadows don't refract, --which is now their fourth weirdness, to wit,

1 they don't blur
2 going into the water they are not modulated by waves 
3 coming out of the water they are not modulated by waves 
4 they don't even refract statically
nani awarded a token.Apr 2 2021, 1:12 AM
nani added a subscriber: nani.

@DanW58 what happened? This diff looked so promising would be a pity to be left to accumulate dust.

What do you plan to do next, and what will you surprise us with?

DanW58 added a comment.EditedApr 5 2021, 9:52 PM

@nani and @gameboy Friends, I totally agree it's a pity to have done so much work on these shaders and let it all go to dust; but it's really pointless to try to continue with it, with all the hate I got, and the monkey-wrenching. The fact is my contributions are not wanted. Period. If you look at the comments on this patch, back to the beginning, note what I'm being asked for: separate patches for each feature... !!! Like, how much time was I supposed to spend on this? It would take me weeks of work to separate this into 3 separate shaders, individually debugged and diffed. It's insane.
Notice also how many thoughts, reports and ideas I presented here, about blurring, about shadows; and I got no reply. Silent treatment is what it is.
So, if my work is never going to be accepted, there's no point in continuing. I stopped the C++ work I was doing too, for the same reason.
And I also stopped looking at the wildfire engine for my own purposes, since I would probably not get any help with modding it.
And so I also stopped playing 0ad, for good measure.
Divorce.

I've been looking at other engines to work with, unity, godot, and a couple others; frankly I didn't like any of them; all too limiting and specialized; I'm working on coding my own engine now; mostly working at low level code while collecting info, libraries and papers.
The only thing I've coded AND debugged so far is a quad-tree, and it worked; but since then I had ideas on how to speed it up by orders of magnitude, as well as to make it a rolling quadtree, that moves about a much larger plane, following the camera; haven't tried the new idea in code yet. Also studying the VCL library, which abstracts SIMD assembler; it seems pretty amazing.
But yeah, once I get to the OpenGL stage I'll resume work on the new shader. I found my old shaders from 20 years ago, BTW; been looking at them; some of the stuff I don't understand, though :-)
Also found my 20yr old work on per-thread array-allocators, and found I was using the /thread and /pool boost libraries. The two folders are still in the boost libraries today, so I probably just need to change a few things and it will work again.
What I could NOT find was my old 3DNow! optimized vector and matrix libraries; but 3DNow! doesn't exist anymore, anyways; and there are vector, matrix and quaternion libraries that come with VCL.
But I don't want to use this medium as a chat forum; you can email me if you want to discuss. One of my emails is "danw.danw{[(AT)]}G-maiI{[(DOT)]}COM", which hopefully spammers' email mining bots won't be able to parse...

Stan added a subscriber: Stan.Apr 6 2021, 11:10 AM

@nani and @gameboy Friends, I totally agree it's a pity to have done so much work on these shaders and let it all go to dust; but it's really pointless to try to continue with it, with all the hate I got, and the monkey-wrenching. The fact is my contributions are not wanted. Period. If you look at the comments on this patch, back to the beginning, note what I'm being asked for: separate patches for each feature... !!! Like, how much time was I supposed to spend on this? It would take me weeks of work to separate this into 3 separate shaders, individually debugged and diffed. It's insane.

Right, cause All I have done is hate, be unhelpful, treat you like dust, and leave you hanging. Thank you very much.

Maybe @vladislavbelov will find something interesting to take out of this patch, and we'll make sure you're credited as due if he does.

Notice also how many thoughts, reports and ideas I presented here, about blurring, about shadows; and I got no reply. Silent treatment is what it is.
So, if my work is never going to be accepted, there's no point in continuing. I stopped the C++ work I was doing too, for the same reason.
And I also stopped looking at the wildfire engine for my own purposes, since I would probably not get any help with modding it.
And so I also stopped playing 0ad, for good measure.
Divorce.

I hope you find solace in that.

I've been looking at other engines to work with, unity, godot, and a couple others; frankly I didn't like any of them; all too limiting and specialized; I'm working on coding my own engine now; mostly working at low level code while collecting info, libraries and papers.
The only thing I've coded AND debugged so far is a quad-tree, and it worked; but since then I had ideas on how to speed it up by orders of magnitude, as well as to make it a rolling quadtree, that moves about a much larger plane, following the camera; haven't tried the new idea in code yet. Also studying the VCL library, which abstracts SIMD assembler; it seems pretty amazing.
But yeah, once I get to the OpenGL stage I'll resume work on the new shader. I found my old shaders from 20 years ago, BTW; been looking at them; some of the stuff I don't understand, though :-)
Also found my 20yr old work on per-thread array-allocators, and found I was using the /thread and /pool boost libraries. The two folders are still in the boost libraries today, so I probably just need to change a few things and it will work again.
What I could NOT find was my old 3DNow! optimized vector and matrix libraries; but 3DNow! doesn't exist anymore, anyways; and there are vector, matrix and quaternion libraries that come with VCL.
But I don't want to use this medium as a chat forum; you can email me if you want to discuss. One of my emails is "danw.danw{[(AT)]}G-maiI{[(DOT)]}COM", which hopefully spammers' email mining bots won't be able to parse...

Might want to look at the GLM library https://glm.g-truc.net/0.9.9/index.html

@DanW58 I hope you'll be back again, and I hope the 0AD team values your contribution to 0AD. They need you and you may need to be able to communicate with them better. Welcome back.