Google has not helped me in this area.

Here is the GLSL shader:

vec3 globalIllumination(vec3 p, vec3 n) {
vec3 g = vec3(0.0);
float dist;
for (float i = 1.0; i <= samples; i++) {
    dist = stepDistance * i;
float d =  vec3(dist - map(p + n * dist, returnColor));
    g += returnColor*d/ i;
return g;

I know how to convert this to HLSL, except for that map function. I could use some help.

  • \$\begingroup\$ I have not seen documentation for a 2-argument map as shown here, only for a 5-argument version (mapping the first argument from an input low-high range to an output low-high range). The math for the latter version is covered here — if that's not what you need, can you please include a definition or link explaining the expected behaviour of the version you're using? \$\endgroup\$
    – DMGregory
    Sep 1 '17 at 15:47
  • \$\begingroup\$ That's the thing, I don't have any idea what the expected behavior is, which is probably why it provided weird results when I just leave out the map and let the parenthesis just take over :P I got this shader from this link: pouet.net/topic.php?which=9086CachedSimilarJust \$\endgroup\$
    – user90214
    Sep 1 '17 at 16:13
  • \$\begingroup\$ This looks like it's approximating global illumination in a raymarching shader, possibly using a signed distance field to represent the scene. In that case, it looks like "map" is a custom method that samples the signed distance field formula at the input point (and sets the return color as an out parameter?) — does this match what you're trying to do? Try showing us how you're trying to use this shader code in the context of your scene. \$\endgroup\$
    – DMGregory
    Sep 1 '17 at 16:32
  • \$\begingroup\$ @DMGregory The way I'm using it is much the same way I use AO in my mod (its not my game engine, Im modifying shaders for Stalker): ao += globalillumination(P,N,color.rgb (added this)); hdiffuse (hemisphere diffuse term) *= ao; \$\endgroup\$
    – user90214
    Sep 1 '17 at 20:29

Answer with best guess of what map could be

Based on the link you posted, map should be provided by you and it should return the distance of the provided 3d position (first argument) and set the second argument to the color of the nearest object. If you don't know how to write such a function, I advise you to read this article.

Answer with the traditional meaning of map

map doesn't exist in neither glsl nor hlsl, but it's very easy to implement it.

The five arguments to a usual map function are

float map(float value, float min1, float max1, float min2, float max2)

it takes the value and converts it from the range defined by min1 and max1 to the equivalent in the range defined by min2 and max2. The implementation would look like this:

// Convert the current value to a percentage
// 0% - min1, 100% - max1
float perc = (value - min1) / (max1 - min1);

// Do the same operation backwards with min2 and max2
float value = perc * (max2 - min2) + min2;
  • \$\begingroup\$ Thanks but please read over my question more carefully: it does not use 5 values. \$\endgroup\$
    – user90214
    Sep 1 '17 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.