Texture Generation using Random NoiseIntroduction In nature, everything has a random look, while mathematical formulas typically don't generate random looking results, unless you use them well. Random noise, such asPerlin noise invented by Ken Perlin, uses random numbers to generate natural looking textures. Smooth Noise As source for the random noise we need an array of random values, called noise[x][y]. Since our interest is generating 2D textures, a 2D array is used. The function generateNoise will fill the array with noise, and the main function is programmed to show this noise array on the screen. The noise itself is generated with the rand() function from the <cstdlib> header file, this function returns a random integer value between 0 and 32768 (as defined in the header file). It's normalized to a random real number between 0 and 1 by dividing it through 32768.0 (make sure to use floating point division).
Here's the noise it generates: ![]() This noise doesn't look very natural however, especially if you zoom in. Zoom in by dividing the x and y used to call the noise array through 8, in the pixel loop of the main function. You get something blocky:
When zooming in, we want something smoother. For that, linear interpolation can be used. Currently the noise is an array and it's got only a discrete set of integer indices pointing to it's contents. By using bilinear interpolation on the fractional part, you can make it smoother. For that, a new function, smoothNoise, is introduced:
The returned value is the weighed average of 4 neighboring pixels of the array. In the main function, now use this instead of directly calling the noise array, and use real numbers for the division:
This is again the result zoomed in 8 times, but now with the bilinear interpolation. If you don't zoom in you won't be able to see the interpolation: ![]() This is quite useful for random noise, the smoothing method could be better maybe, bilinear interpolation is often used by 3D cards for smoothing textures in games as a cheap and fast technique. Let's call this image a "noise texture". Turbulence Turbulence is what creates natural looking features out of smoothed noise. The trick is to add multiple noise textures of different zooming scales together. An example of how this represents nature can be found in a mountain range: there are very large features (the main mountains), they are very deeply zoomed in noise.
To use the turbulence function, change the small part of the code in the loop that goes through every pixel by the following:
The size is set to 64 there, and the result looks like this: ![]() If you set the initial size to 256 instead, the result is much bigger and smoother: ![]() And here's a very small initial size of only 8: ![]() The textures here have some obvious horizontal and vertical lines because of the bilinear filter smooth function. The Clouds filter in Photoshop generates a texture similar to the ones above, but with a nicer smooth function. Nicer smooth functions are beyond the scope of this article though. If you use no smooth function at all, it looks like this: ![]() |
利用随机噪声生成纹理 Texture Generation using Random Noise A D V E R T I S E M E N T Introduction In n
最新推荐文章于 2023-07-24 22:06:53 发布