﻿ Section 15.5.  Turbulence

Free JavaScript Editor     Ajax Editor ﻿

Main Page

### 15.5. Turbulence

We can obtain some additional interesting effects by taking the absolute value of the noise function. This technique introduces a discontinuity of the derivative because the function folds on itself when it reaches 0 (see Figure 5.2 for an illustration of the absolute value function). When this folding is done to noise functions at several frequencies and the results are summed, the result is cusps or creases in the texture at various scales. Perlin started referring to this type of noise as TURBULENCE because it is reminiscent of turbulent flow. It shows up in a variety of places in nature, so this type of noise can be used to simulate various things like flames or lava. The two-dimensional appearance of this type of noise is shown in Figure 15.7.

#### 15.5.1. Sun Surface Shader

We can achieve an effect that looks like a pit of hot molten lava or the surface of the sun by using the same vertex shader as the cloud shader and a slightly different fragment shader. The main difference is that we scale each noise value and shift it over so that it is centered at 0; then we take its absolute value. After summing the values, we scale the result again to occupy nearly the full range of [0,1]. We clamp this value and use it to mix between yellow and red to get the result shown in Color Plate 24 (see Listing 15.5). (In Chapter 16, we examine some ways of animating these textures to make them more interesting.)

##### Listing 15.5. Sun surface fragment shader

 ```varying float LightIntensity; varying vec3 MCposition; uniform sampler3D Noise; uniform vec3 Color1; // (0.8, 0.7, 0.0) uniform vec3 Color2; // (0.6, 0.1, 0.0) uniform float NoiseScale; // 1.2 void main() { vec4 noisevec = texture3D(Noise, MCposition * NoiseScale); float intensity = abs(noisevec[0] - 0.25) + abs(noisevec[1] - 0.125) + abs(noisevec[2] - 0.0625) + abs(noisevec[3] - 0.03125); intensity = clamp(intensity * 6.0, 0.0, 1.0); vec3 color = mix(Color1, Color2, intensity) * LightIntensity; gl_FragColor = vec4(color, 1.0); }```

#### 15.5.2. Marble

Yet another variation on the noise function is to use it as part of a periodic function such as sine. By adding noise to the input value for the sine function, we get a "noisy" oscillating function. We use this to create a look similar to the alternating color veins of some types of marble. Listing 15.6 shows the fragment shader to do it. Again, we use the same vertex shader. Results of this shader are also shown in Color Plate 24.

##### Listing 15.6. Fragment shader for marble

 ```varying float LightIntensity; varying vec3 MCposition; uniform sampler3D Noise; uniform vec3 MarbleColor; uniform vec3 VeinColor; void main() { vec4 noisevec = texture3D(Noise, MCposition); float intensity = abs(noisevec[0] - 0.25) + abs(noisevec[1] - 0.125) + abs(noisevec[2] - 0.0625) + abs(noisevec[3] - 0.03125); float sineval = sin(MCposition.y * 6.0 + intensity * 12.0) * 0.5 + 0.5; vec3 color = mix(VeinColor, MarbleColor, sineval) * LightIntensity; gl_FragColor = vec4(color, 1.0); }```

﻿

Ajax Editor     JavaScript Editor