<p>As you are taking the mean of a number of sine waves, your colour values will range from -1 to 1. From your example image, it looks like only the top half of this range of values (from 0 to 1) is resulting in colour, with everywhere else remaining black.</p>

<p>If whatever you are using to display the result can only handle positive values, then you will need to convert the result to the correct range of values. For example, to convert from the range [-1 to 1] into the range [0 to 1], you would add 1 and divide by 2.</p>

<p>In the context of your example code, this could be by following the line</p>

<pre><code>color /= (float)_iterations;
</code></pre>

<p>with</p>

<pre><code>color += 1;
color /= 2;
</code></pre>

CC BY-SA 3.0
2016-04-07T23:10:28.537Z
2016-04-07T23:10:28.537Z
trichoplax is on Codidact now
<p>I am trying to implement this formula to generate bump but I am facing some issue. The result doesn't look the same it's much darker.</p>

<p><a href="https://i.sstatic.net/kUWt5.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/kUWt5.png" alt="enter image description here"></a></p>

<p>Here is my result (without same parameters) but it is much darker and I don't get why.</p>

<p><a href="https://i.sstatic.net/9GUF2.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/9GUF2.png" alt="enter image description here"></a></p>

<p>And here is the associated code.</p>

<pre><code>// randx, randy and frequencies are array with some random values for each sin wave.
for (int x = 0; x &lt; _width; ++x)
{
    for (int y = 0; y &lt; _height; ++y)
    {
        float color = 0.0f;
        for (int i = 0; i &lt; _iterations; ++i)
        {
            val += Mathf.Sin(Mathf.Sqrt(Mathf.Pow(x - randx[i], 2.0f) + Mathf.Pow(y - randy[i], 2.0f)) * 1.0f / (2.08f + 5.0f * frequencies[i]));
        }
        color /= (float)_iterations;
    }
}
</code></pre>

<p>Any idea why I am getting this result ?<br>
Thanks a lot !</p>

<p><strong>EDIT</strong>: Thanks to @trichoplax it works by doing this.</p>

<pre><code>float tmp = Mathf.Sin(Mathf.Sqrt(Mathf.Pow(x - randx[i], 2.0f) + Mathf.Pow(y - randy[i], 2.0f)) * 1.0f / (2.08f + 5.0f * frequencies[i]));
tmp = tmp * 0.5f + 0.5f;
val += tmp;
</code></pre>

CC BY-SA 3.0
2016-04-08T02:02:55.560Z
Pro tips, unrelated to your question: you can expect `(foo * foo)` to be a lot faster than `pow(foo, 2.0f)`; if your data is organized row by row (as opposed to column by column), you should swap the for loops to traverse data in a more coherent order.
Julien Guertault
<texture><shader><mathematics><procedural-generation>
Sine-based Tiled Procedural Bump
MaT
# Sine-based Tiled Procedural Bump

(Asked by: username_0 on 2016-04-07)

I am trying to implement this formula to generate bump but I am facing some issue. The result doesn't look the same it's much darker.

[![enter image description here](https://i.sstatic.net/kUWt5.png)](https://i.sstatic.net/kUWt5.png)

Here is my result (without same parameters) but it is much darker and I don't get why.

[![enter image description here](https://i.sstatic.net/9GUF2.png)](https://i.sstatic.net/9GUF2.png)

And here is the associated code.

```
// randx, randy and frequencies are array with some random values for each sin wave.
for (int x = 0; x < _width; ++x)
{
    for (int y = 0; y < _height; ++y)
    {
        float color = 0.0f;
        for (int i = 0; i < _iterations; ++i)
        {
            val += Mathf.Sin(Mathf.Sqrt(Mathf.Pow(x - randx[i], 2.0f) + Mathf.Pow(y - randy[i], 2.0f)) * 1.0f / (2.08f + 5.0f * frequencies[i]));
        }
        color /= (float)_iterations;
    }
}

```

Any idea why I am getting this result ?  
Thanks a lot !

**EDIT**: Thanks to @username_2 it works by doing this.

```
float tmp = Mathf.Sin(Mathf.Sqrt(Mathf.Pow(x - randx[i], 2.0f) + Mathf.Pow(y - randy[i], 2.0f)) * 1.0f / (2.08f + 5.0f * frequencies[i]));
tmp = tmp * 0.5f + 0.5f;
val += tmp;

```

**Tags:** texture, shader, mathematics, procedural-generation

> **Comments:**
> **username_1**: Pro tips, unrelated to your question: you can expect `(foo * foo)` to be a lot faster than `pow(foo, 2.0f)`; if your data is organized row by row (as opposed to column by column), you should swap the for loops to traverse data in a more coherent order.  

---

## 1 answers

### Answer by username_2 on 2016-04-07. Score: 8 | Accepted answer

As you are taking the mean of a number of sine waves, your colour values will range from -1 to 1. From your example image, it looks like only the top half of this range of values (from 0 to 1) is resulting in colour, with everywhere else remaining black.

If whatever you are using to display the result can only handle positive values, then you will need to convert the result to the correct range of values. For example, to convert from the range [-1 to 1] into the range [0 to 1], you would add 1 and divide by 2.

In the context of your example code, this could be by following the line

```
color /= (float)_iterations;

```

with

```
color += 1;
color /= 2;

```


---

