Repository navigation
Hill shading improvements + new algorithm #2 - #1521
Conversation
- Enable sub-classing of hill shading algorithms, so users of the library can create theirs - Get rid of the memory mapping of DEM files, use a simple and more efficient `BufferedInputStream` instead (`InputStream `in general, instead of the `ByteBuffer`) - Create a new hill shading algorithm with better appearance and more customization
|
@Sublimis thanks!
|
|
This doesn't seem like an effect of the new algorithm (ClearAsymmetryShadingAlgorithm), but something else. Not sure, though, will check and get back to you. Can you compare slightly more zoomed in screenshots with those of other algorithms? If the effect is visible only over the sea, this should be indicative. Unfortunately there's no easy way for other algorithms (Simple and Diffuse), as they split the shading range in the middle, and use lower half (less shading) for one direction, and upper half (more shading) for other direction. Flat areas being in the middle always get 50% shading. When this is fixed, i.e. we put flat areas at zero shading, and distribute the shading range asymmetrically over two slope directions, we basically arrive at the new algorithm presented (ClearAsymmetryShadingAlgorithm). |
|
@Sublimis thanks for the extra info! I make the tests without permanent cache, so it is always new rendered tiles. The sea has different colors with all algorithms on desktop. |
This seems to be related to |
|
The solution seems to be very simple. Take a look at line 49 of the private static final java.awt.Color NEUTRAL_HILLS = new java.awt.Color(127, 127, 127);This is 50% gray shade. Instead, it should be: private static final java.awt.Color NEUTRAL_HILLS = new java.awt.Color(0, 0, 0, 0);(last extra zero is alpha component). Even better than "drawing" a 100% transparent bitmap would probably be to draw nothing at all in this case, lines 483 to 496: if (bitmap == null) {
// apply flat shading
if (tileRect != null) {
this.graphics2D.setClip(
(int) Math.round(tileRect.left), (int) Math.round(tileRect.top),
(int) Math.round(tileRect.right - (int) tileRect.left), (int) Math.round(tileRect.bottom) - (int) Math.round(tileRect.top) // subtract in after rounding to get same error as on neighbor tile
);
}
this.graphics2D.setColor(NEUTRAL_HILLS);
this.graphics2D.fillRect(0, 0, getWidth(), getHeight());
this.graphics2D.setComposite(oldComposite);
this.graphics2D.setClip(null);
return;
} |
|
@Sublimis thanks for the ideas. Have you tried them? Unfortunately none of them work. |
|
@Sublimis thanks for your nice work! 👍 |
Sorry, no, just looking at the code, should have mentioned that. |
|
@devemux86 |
|
Anyway, for better results on the desktop, more research may be needed. |
|
@devemux86 If we do this, is it better to mark them as |
|
There is still no plan for the next version, so we have plenty of time for improvements.
That would be great! 👍
If the old algorithms can be improved to work without gray shade in flat areas
Removing the gray shade in flat areas is something we want for all algorithms. |
|
Is there any news on the work to remove the gray shade of flat areas from the Simple and Diffuse algorithms? |
|
Yes -- sorry for the wait. This was the easy part, but we also created two new algorithms, both of which can look similar to the diffuse, but with more possible adjustments and optimizations, and clearer math. One of them is the "ultimate" high resolution algorithm (explanation comes with the code), and for the last week we've been trying to parallelize it to improve performance. This was a bit of a nightmare, but we succeeded yesterday. Multi-threading as an option will therefore be available in our other algorithms as well. Since more work is needed on the new algorithms, we can create a pull request just for the fixes to the existing algorithms to reduce the delay, and then another later for the new algorithms, is that okay? Here is an example of the original versus the corrected diffuse algorithm as it currently stands: |
|
@Sublimis Impressive results! 👍 Which algorithms will have a multi-threading option?
Let's do this, so we can test the changes in the old algorithms.
Do you use 1" for this? Aren't they slow to process? |
Only the new ones for now. But since the solution will be generic, in an abstract class, covering all aspects of file reading, buffer management, and everything else except converting a square of altitude points to shade pixel(s), basically any algorithm can be easily adapted to use multi threading as desired. However, multi threading is implemented in such a way that only one thread (per producer/calling thread) does the reading, with a custom number (can be zero) of other threads doing the processing. For simple algorithms that only do a few adds/muls per pixel (like e.g. the Simple one) there's probably no justification for increasing the complexity with multi threading.
All the images above use 1" data, but we use the term "high resolution algorithm" not for the data it uses (it can as well be 3"), but for the fact that it produces shading that has four times as many pixels than that of other ("normal") algorithms. Whether it is justified is for a user to decide, but it's useful to have in the arsenal. 1" data is not too much to process on modern hardware even with a high resolution algorithm, and especially on desktops, or at least not when performance is not of the utmost importance. BTW, the new algorithms are in the "code beautification" phase, basically almost finished! |
|
We ended up fighting on two fronts: While working on our shading algorithms, we found some inefficiencies in So we took care of this, and while there, we also implemented reading DEM data from ZIP archives containing multiple HGT files, so files downloaded from e.g. viewfinderpanoramas.org can be used as is, without repackaging. Should we make a separate pull request for these improvements (separate from hill shading algorithms)? |
Let's do it this way. 👍 |
|
One thing I have seen with the recent improvements, is that the hill shading is not as "strong" anymore. For example, in the past the "simple" algorithm had more "strong" hill shading, it looked better to the eyes. |
|
Unfortunately, there is no single "perfect" method of shading hills, it's all a matter of wishes and compromises. Let us explain. Previous versions of the algorithms had maximum contrast between NW and SE facing slopes: if you imagine a very steep hill with a triangle-like cross-section, spanning from SW to NE, then the NW-facing slope would have a shading value of 0, and the SE-facing slope would have 255 (in the range 0-255). At the top of such a hill there would be a "cut", an apparent line where two such opposite slopes meet, one with 0 and the other with 255 shading, so we can call it a "maximum contrast cut". Walking over the top of such a hill, the shading quickly changes from 0 to 127 (horizontal) to 255. However, for this to happen, the horizontal surface must be between the two extremes (unwanted horizontal shading), and the shading of NW slopes decreases as the slope is steeper. The current versions do not have such a maximum cut at the top of the hill, but a milder one. Horizontal surfaces are at 0 shading. NW slopes get shading proportional to the steepness, up to some maximum that can be less than 255 (and by default it is; 128 in the case of simple shading, which would be called 50% asymmetry in light of upcoming algorithms). The shading of SE slopes is similar, but always goes to 255. Walking over the top of such a hill, the shading quickly changes from 128 to 255 (going down to 0 and back up in an instant), which is less contrast. This can be alleviated somewhat (in upcoming algorithms, but perhaps also in the current ClearAsymmetry implementation) by adjusting the algorithm parameters, for example the maximum slope and the asymmetry factor. Also please see this: #1539 |




Enable sub-classing of hill shading algorithms, so users of the library can create theirs
Get rid of the memory mapping of DEM files, use a simple and more efficient
BufferedInputStreaminstead (InputStreamin general, instead of theByteBuffer)Create a new hill shading algorithm with better appearance and more customization