Skip to content

Clouds

Video Lecture

Section Video Links
Clouds Clouds Clouds

Description

We can create some clouds in our scene.

We will base it on the land FBM, so that the clouds appear above the mountain peaks.

Working Example

<>

Start Scripts

./src/Clouds.ts

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
import { clamp, cos, dot, float, Fn, If, Loop, mat2, max, negate, sin, time, uniform, vec3 } from 'three/tsl'
import GUI from 'three/examples/jsm/libs/lil-gui.module.min.js'

export default class Clouds {
  private static options = {
    octaves: 8,
    lacunarity: 2.15,
    gain: 0.35,
    bottom: 1.25
  }

  private static octaves = uniform(this.options.octaves)
  private static lacunarity = uniform(this.options.lacunarity)
  private static gain = uniform(this.options.gain)
  private static bottom = uniform(this.options.bottom)

  // @ts-ignore
  private static noise = Fn(([position]) => {
    return sin(position.x).add(sin(position.y))
  })

  // @ts-ignore
  private static rotate = Fn(([radians]) => {
    const a = sin(radians)
    const b = cos(radians)
    return mat2(b, a.negate(), a, b)
  })

  // @ts-ignore
  private static fbm = Fn(([position]) => {
    const p = position.xz.toVar()
    const accumulator = float(0.0).toVar()
    const amplitude = float(this.gain).toVar()

    Loop({ start: 0, end: this.octaves, condition: '<' }, () => {
      accumulator.addAssign(amplitude.mul(this.noise(p)))
      amplitude.mulAssign(this.gain)
      //p.x.addAssign(time)//.div(5))
      p.mulAssign(this.lacunarity.mul(this.rotate(0.19)))
      //p.mulAssign(this.lacunarity)
    })

    return accumulator
  })

  //@ts-ignore
  static scene = Fn(([position]) => {
    const p = position.mul(0.5)
    const height = float(0).toVar()

    //If(position.y.greaterThan(this.bottom), () => {
    height.assign(this.fbm(p))

    height.assign(height.mul(cos(p.x.mul(0.25))))
    height.assign(height.mul(cos(p.z.mul(0.25))))
    height.mulAssign(3.3)
    height.subAssign(0.1)

    //height.addAssign(1)
    //})

    return p.y.sub(height)

    //const bottom = p.y.sub(this.bottom)
    //return max(negate(bottom), p.y.sub(height))
  })

  //@ts-ignore
  static render = Fn(([baseColour, normal, lightDirection]) => {
    const diffuse = clamp(dot(normal, lightDirection), 0, 1).toVar()
    return baseColour.mul(diffuse)
    //return baseColour.mix(diffuse.mul(vec3(1)), 0.25)
  })

  static setGUI(gui: GUI) {
    const folder = gui.addFolder('Clouds')
    folder
      .add(this.options, 'octaves', 0, 10, 1)
      .name('Octaves')
      .onChange((v) => {
        this.octaves.value = v
      })
    folder
      .add(this.options, 'lacunarity', 1, 10, 0.01)
      .name('Lacunarity')
      .onChange((v) => {
        this.lacunarity.value = v
      })
    folder
      .add(this.options, 'gain', 0.01, 1.99, 0.01)
      .name('Gain')
      .onChange((v) => {
        this.gain.value = v
      })
    folder
      .add(this.options, 'bottom', 0, 5, 0.01)
      .name('Bottom')
      .onChange((v) => {
        this.bottom.value = v
      })

    folder.close()
  }
}

./src/SDFScene.ts

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import {
  Break,
  cos,
  float,
  If,
  Loop,
  normalize,
  sin,
  time,
  uniform,
  vec2,
  vec3,
  Fn,
  positionLocal,
  reflect,
  mix,
  exp,
  int,
  abs
} from 'three/tsl'
import { atmosphericScattering } from './AtmosphericScattering'
import Land from './Land'
import ShadowMarcher from './ShadowMarcher'
import AmbientOcclusion from './AmbientOcclusion'
import Fog from './Fog'
import GUI from 'three/examples/jsm/libs/lil-gui.module.min.js'
import Ocean from './Ocean'
import Fresnel from './Fresnel'
//import Clouds from './Clouds'

export default class SDFScene {
  private static options = {
    maxSteps: 384,
    surfaceDistance: 0.005,
    cameraNear: 0.01,
    cameraFar: 192.0,
    reflectivity: 0.5
  }

  private static maxSteps = uniform(this.options.maxSteps)
  private static surfaceDistance = uniform(this.options.surfaceDistance)
  private static cameraNear = uniform(this.options.cameraNear)
  private static cameraFar = uniform(this.options.cameraFar)
  private static reflectivity = uniform(this.options.reflectivity)

  // @ts-ignore
  private static scene = Fn(([position]) => {
    const land = Land.scene(position)

    const ocean = Ocean.scene(position)

    //const clouds = Clouds.scene(position)

    const distance = vec2(land, 0).toVar()
    If(distance.x.greaterThan(ocean), () => {
      distance.assign(vec2(ocean, 1))
    })
    // If(distance.x.greaterThan(clouds), () => {
    //   distance.assign(vec2(clouds, 2))
    // })

    return distance
  })

  // @ts-ignore
  private static getNormal = Fn(([position, distance]) => {
    const offset = vec2(0.0001, 0)

    return normalize(
      distance.sub(
        vec3(
          this.scene(position.sub(offset.xyy)).x,
          this.scene(position.sub(offset.yxy)).x,
          this.scene(position.sub(offset.yyx)).x
        )
      )
    )
  })

  // @ts-ignore
  static render = Fn(([rayOrigin_immutable]) => {
    const rayOrigin = rayOrigin_immutable.toVar()

    const p = positionLocal

    const rayDirection = normalize(p).toVar()

    const t = time.div(5)
    //const lightPosition = vec3(0, 50, this.cameraFar.negate())
    //const lightPosition = vec3(0, sin(t).mul(30).add(43), this.cameraFar.negate())
    const lightPosition = vec3(sin(t).mul(this.cameraFar), 50, cos(t).mul(this.cameraFar))
    //const lightPosition = vec3(sin(t).mul(this.cameraFar), sin(t).mul(40).add(50), cos(t).mul(this.cameraFar))
    const lightDirection = normalize(lightPosition.sub(p))

    const skyColour = atmosphericScattering(vec3(p.x, abs(p.y), p.z), normalize(lightPosition)).toVar()
    const finalColour = skyColour.toVar()

    const reflectivityFactor = float(1.0).toVar()

    const accumulatedDistance = float(this.cameraNear).toVar()
    const fogDistance = accumulatedDistance.toVar()

    const maxReflections = int(1).toVar()
    Loop({ start: 0, end: maxReflections, condition: '<=' }, () => {
      accumulatedDistance.assign(0)
      const distance = vec2(0).toVar()
      const position = vec3(0).toVar()

      // @ts-ignore
      Loop({ start: 0, end: this.maxSteps }, () => {
        position.assign(rayOrigin.add(rayDirection.mul(accumulatedDistance)))
        distance.assign(this.scene(position))

        If(abs(distance.x).lessThan(this.surfaceDistance).or(accumulatedDistance.greaterThan(this.cameraFar)), () => {
          Break()
        })

        accumulatedDistance.addAssign(distance.x)
        fogDistance.addAssign(distance.x)
      })

      const normal = this.getNormal(position, distance.x).toVar()

      If(accumulatedDistance.lessThan(this.cameraFar), () => {
        If(distance.y.equal(0), () => {
          // land
          finalColour.assign(
            mix(
              finalColour,
              Land.render(position, normal, rayDirection, lightPosition, lightDirection, this.scene),
              reflectivityFactor
            )
          )

          maxReflections.assign(0) //do not calc reflections on land layer
        }).Else(() => {
          //}).ElseIf(distance.y.equal(1), () => {
          // ocean
          normal.assign(mix(vec3(0, 1, 0), normal, exp(accumulatedDistance.pow(3).mul(-0.005))))

          finalColour.assign(
            Ocean.render(finalColour, position, normal, rayDirection, lightPosition, lightDirection, this.scene)
          )

          rayOrigin.assign(position.add(normal.mul(this.surfaceDistance)))
          rayDirection.assign(reflect(rayDirection, normal))
          reflectivityFactor.assign(this.reflectivity)
        })
        // .Else(() => {
        //   // clouds
        //   finalColour.assign(mix(finalColour, Clouds.render(finalColour, normal, lightDirection), reflectivityFactor))
        //   maxReflections.assign(0) //do not calc reflections on cloud layer
        // })

        // fog
        finalColour.assign(Fog.render(skyColour, finalColour, fogDistance))
      })
    })

    return finalColour
  })

  static setGUI(gui: GUI) {
    const folder = gui.addFolder('SDF Scene')
    folder
      .add(this.options, 'maxSteps', 1, 512, 1)
      .name('Raymarch Max Steps')
      .onChange((v) => {
        this.maxSteps.value = v
      })
    folder
      .add(this.options, 'surfaceDistance', 0, 0.01, 0.0001)
      .name('Surface Distance')
      .onChange((v) => {
        this.surfaceDistance.value = v
      })
    folder
      .add(this.options, 'cameraNear', 0.0001, 10, 0.1)
      .name('Camera Near')
      .onChange((v) => {
        this.cameraNear.value = v
      })
    folder
      .add(this.options, 'cameraFar', 1, 512, 0.1)
      .name('Camera Far')
      .onChange((v) => {
        this.cameraFar.value = v
      })
    folder
      .add(this.options, 'reflectivity', 0, 1, 0.01)
      .name('Reflectivity')
      .onChange((v) => {
        this.reflectivity.value = v
      })
    folder.close()

    ShadowMarcher.setGUI(gui)
    AmbientOcclusion.setGUI(gui)
    Land.setGUI(gui)
    Ocean.setGUI(gui)
    Fresnel.setGUI(gui)
    //Clouds.setGUI(gui)
    Fog.setGUI(gui)
  }
}

Final Scripts

./src/Clouds.ts

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
import { clamp, cos, dot, float, Fn, If, Loop, mat2, max, negate, sin, time, uniform, vec3 } from 'three/tsl'
import GUI from 'three/examples/jsm/libs/lil-gui.module.min.js'

export default class Clouds {
  private static options = {
    octaves: 3,
    lacunarity: 3.5,
    gain: 0.3,
    bottom: 1.25
  }

  private static octaves = uniform(this.options.octaves)
  private static lacunarity = uniform(this.options.lacunarity)
  private static gain = uniform(this.options.gain)
  private static bottom = uniform(this.options.bottom)

  // @ts-ignore
  private static noise = Fn(([position]) => {
    return sin(position.x).add(sin(position.y))
  })

  // @ts-ignore
  private static rotate = Fn(([radians]) => {
    const a = sin(radians)
    const b = cos(radians)
    return mat2(b, a.negate(), a, b)
  })

  // @ts-ignore
  private static fbm = Fn(([position]) => {
    const p = position.xz.toVar()
    const accumulator = float(0.0).toVar()
    const amplitude = float(this.gain).toVar()

    Loop({ start: 0, end: this.octaves, condition: '<' }, () => {
      accumulator.addAssign(amplitude.mul(this.noise(p)))
      amplitude.mulAssign(this.gain)
      p.x.addAssign(time.div(5))
      //p.mulAssign(this.lacunarity.mul(this.rotate(0.19)))
      p.mulAssign(this.lacunarity)
    })

    return accumulator
  })

  //@ts-ignore
  static scene = Fn(([position]) => {
    const p = position.mul(0.5)
    const height = float(0).toVar()

    If(position.y.greaterThan(this.bottom), () => {
      height.assign(this.fbm(p))

      height.assign(height.mul(cos(p.x.mul(0.25))))
      height.assign(height.mul(cos(p.z.mul(0.25))))
      height.mulAssign(3.3)
      height.subAssign(0.1)

      height.addAssign(1)
    })

    //return p.y.sub(height)

    const bottom = p.y.sub(this.bottom)
    return max(negate(bottom), p.y.sub(height))
  })

  //@ts-ignore
  static render = Fn(([baseColour, normal, lightDirection]) => {
    const diffuse = clamp(dot(normal, lightDirection), 0, 1).toVar()
    //return baseColour.mul(diffuse)
    return baseColour.mix(diffuse.mul(vec3(1)), 0.25)
  })

  static setGUI(gui: GUI) {
    const folder = gui.addFolder('Clouds')
    folder
      .add(this.options, 'octaves', 0, 10, 1)
      .name('Octaves')
      .onChange((v) => {
        this.octaves.value = v
      })
    folder
      .add(this.options, 'lacunarity', 1, 10, 0.01)
      .name('Lacunarity')
      .onChange((v) => {
        this.lacunarity.value = v
      })
    folder
      .add(this.options, 'gain', 0.01, 1.99, 0.01)
      .name('Gain')
      .onChange((v) => {
        this.gain.value = v
      })
    folder
      .add(this.options, 'bottom', 0, 5, 0.01)
      .name('Bottom')
      .onChange((v) => {
        this.bottom.value = v
      })

    folder.close()
  }
}

./src/SDFScene.ts

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import {
  Break,
  cos,
  float,
  If,
  Loop,
  normalize,
  sin,
  time,
  uniform,
  vec2,
  vec3,
  Fn,
  positionLocal,
  reflect,
  mix,
  exp,
  int,
  abs
} from 'three/tsl'
import { atmosphericScattering } from './AtmosphericScattering'
import Land from './Land'
import ShadowMarcher from './ShadowMarcher'
import AmbientOcclusion from './AmbientOcclusion'
import Fog from './Fog'
import GUI from 'three/examples/jsm/libs/lil-gui.module.min.js'
import Ocean from './Ocean'
import Fresnel from './Fresnel'
import Clouds from './Clouds'

export default class SDFScene {
  private static options = {
    maxSteps: 384,
    surfaceDistance: 0.005,
    cameraNear: 0.01,
    cameraFar: 192.0,
    reflectivity: 0.5
  }

  private static maxSteps = uniform(this.options.maxSteps)
  private static surfaceDistance = uniform(this.options.surfaceDistance)
  private static cameraNear = uniform(this.options.cameraNear)
  private static cameraFar = uniform(this.options.cameraFar)
  private static reflectivity = uniform(this.options.reflectivity)

  // @ts-ignore
  private static scene = Fn(([position]) => {
    const land = Land.scene(position)

    const ocean = Ocean.scene(position)

    const clouds = Clouds.scene(position)

    const distance = vec2(land, 0).toVar()
    If(distance.x.greaterThan(ocean), () => {
      distance.assign(vec2(ocean, 1))
    })
    If(distance.x.greaterThan(clouds), () => {
      distance.assign(vec2(clouds, 2))
    })

    return distance
  })

  // @ts-ignore
  private static getNormal = Fn(([position, distance]) => {
    const offset = vec2(0.0001, 0)

    return normalize(
      distance.sub(
        vec3(
          this.scene(position.sub(offset.xyy)).x,
          this.scene(position.sub(offset.yxy)).x,
          this.scene(position.sub(offset.yyx)).x
        )
      )
    )
  })

  // @ts-ignore
  static render = Fn(([rayOrigin_immutable]) => {
    const rayOrigin = rayOrigin_immutable.toVar()

    const p = positionLocal

    const rayDirection = normalize(p).toVar()

    const t = time.div(5)
    //const lightPosition = vec3(0, 50, this.cameraFar.negate())
    //const lightPosition = vec3(0, sin(t).mul(30).add(43), this.cameraFar.negate())
    const lightPosition = vec3(sin(t).mul(this.cameraFar), 50, cos(t).mul(this.cameraFar))
    //const lightPosition = vec3(sin(t).mul(this.cameraFar), sin(t).mul(40).add(50), cos(t).mul(this.cameraFar))
    const lightDirection = normalize(lightPosition.sub(p))

    const skyColour = atmosphericScattering(vec3(p.x, abs(p.y), p.z), normalize(lightPosition)).toVar()
    const finalColour = skyColour.toVar()

    const reflectivityFactor = float(1.0).toVar()

    const accumulatedDistance = float(this.cameraNear).toVar()
    const fogDistance = accumulatedDistance.toVar()

    const maxReflections = int(1).toVar()
    Loop({ start: 0, end: maxReflections, condition: '<=' }, () => {
      accumulatedDistance.assign(0)
      const distance = vec2(0).toVar()
      const position = vec3(0).toVar()

      // @ts-ignore
      Loop({ start: 0, end: this.maxSteps }, () => {
        position.assign(rayOrigin.add(rayDirection.mul(accumulatedDistance)))
        distance.assign(this.scene(position))

        If(abs(distance.x).lessThan(this.surfaceDistance).or(accumulatedDistance.greaterThan(this.cameraFar)), () => {
          Break()
        })

        accumulatedDistance.addAssign(distance.x)
        fogDistance.addAssign(distance.x)
      })

      const normal = this.getNormal(position, distance.x).toVar()

      If(accumulatedDistance.lessThan(this.cameraFar), () => {
        If(distance.y.equal(0), () => {
          // land
          finalColour.assign(
            mix(
              finalColour,
              Land.render(position, normal, rayDirection, lightPosition, lightDirection, this.scene),
              reflectivityFactor
            )
          )

          maxReflections.assign(0) //do not calc reflections on land layer
          //}).Else(() => {
        })
          .ElseIf(distance.y.equal(1), () => {
            // ocean
            normal.assign(mix(vec3(0, 1, 0), normal, exp(accumulatedDistance.pow(3).mul(-0.005))))

            finalColour.assign(
              Ocean.render(finalColour, position, normal, rayDirection, lightPosition, lightDirection, this.scene)
            )

            rayOrigin.assign(position.add(normal.mul(this.surfaceDistance)))
            rayDirection.assign(reflect(rayDirection, normal))
            reflectivityFactor.assign(this.reflectivity)
          })
          .Else(() => {
            // clouds
            finalColour.assign(mix(finalColour, Clouds.render(finalColour, normal, lightDirection), reflectivityFactor))
            maxReflections.assign(0) //do not calc reflections on cloud layer
          })

        // fog
        finalColour.assign(Fog.render(skyColour, finalColour, fogDistance))
      })
    })

    return finalColour
  })

  static setGUI(gui: GUI) {
    const folder = gui.addFolder('SDF Scene')
    folder
      .add(this.options, 'maxSteps', 1, 512, 1)
      .name('Raymarch Max Steps')
      .onChange((v) => {
        this.maxSteps.value = v
      })
    folder
      .add(this.options, 'surfaceDistance', 0, 0.01, 0.0001)
      .name('Surface Distance')
      .onChange((v) => {
        this.surfaceDistance.value = v
      })
    folder
      .add(this.options, 'cameraNear', 0.0001, 10, 0.1)
      .name('Camera Near')
      .onChange((v) => {
        this.cameraNear.value = v
      })
    folder
      .add(this.options, 'cameraFar', 1, 512, 0.1)
      .name('Camera Far')
      .onChange((v) => {
        this.cameraFar.value = v
      })
    folder
      .add(this.options, 'reflectivity', 0, 1, 0.01)
      .name('Reflectivity')
      .onChange((v) => {
        this.reflectivity.value = v
      })
    folder.close()

    ShadowMarcher.setGUI(gui)
    AmbientOcclusion.setGUI(gui)
    Land.setGUI(gui)
    Ocean.setGUI(gui)
    Fresnel.setGUI(gui)
    Clouds.setGUI(gui)
    Fog.setGUI(gui)
  }
}