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 | import {
abs,
clamp,
cos,
dot,
float,
Fn,
If,
Loop,
mat2,
mix,
normalize,
reflect,
sin,
smoothstep,
uniform,
vec3
} from 'three/tsl'
import * as THREE from 'three/webgpu'
import { atmosphericScattering } from './AtmosphericScattering'
import AmbientOcclusion from './AmbientOcclusion'
import ShadowMarcher from './ShadowMarcher'
import GUI from 'three/examples/jsm/libs/lil-gui.module.min.js'
export default class Land {
private static options = {
octaves: 10,
lacunarity: 2.15,
gain: 0.35,
factorA: 0.19,
factorB: 2.2,
colour1: new THREE.Color(0xdfff00).convertLinearToSRGB(),
colour2: new THREE.Color(0xffffff).convertLinearToSRGB()
}
private static octaves = uniform(this.options.octaves)
private static lacunarity = uniform(this.options.lacunarity)
private static gain = uniform(this.options.gain)
private static factorA = uniform(this.options.factorA)
private static factorB = uniform(this.options.factorB)
private static colour1 = uniform(this.options.colour1)
private static colour2 = uniform(this.options.colour2)
// @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.mulAssign(lacunarity)
p.mulAssign(this.lacunarity.mul(this.rotate(this.factorA)))
})
return accumulator
})
//@ts-ignore
static scene = Fn(([position]) => {
const p = position.mul(0.5)
const height = float(0).toVar()
If(abs(position.y).lessThan(5), () => {
height.assign(this.fbm(p).mul(this.factorB))
})
return p.y.sub(height)
})
//@ts-ignore
static render = Fn(([position, normal, rayDirection, lightPosition, lightDirection]) => {
const diffuse = clamp(dot(normal, lightDirection), 0, 1).toVar()
const colour = atmosphericScattering(reflect(rayDirection, normal), normalize(lightPosition)).mul(this.colour1)
const steepness = float(abs(normal.y).oneMinus()).toVar()
colour.assign(mix(colour, this.colour2, smoothstep(0.1, 0.7, steepness)))
colour.assign(mix(colour, this.colour2, smoothstep(0, 1.0, position.y.sub(2))))
diffuse.mulAssign(ShadowMarcher.render(position.add(normal.mul(0.001)), vec3(lightDirection), this.scene))
colour.mulAssign(AmbientOcclusion.render(position, normal, this.scene))
return colour.mul(diffuse)
})
static setGUI(gui: GUI) {
const folder = gui.addFolder('Land')
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, 'factorA', 0, Math.PI / 2, 0.01)
.name('Factor A')
.onChange((v) => {
this.factorA.value = v
})
folder
.add(this.options, 'factorB', -10, 10, 0.01)
.name('Factor B')
.onChange((v) => {
this.factorB.value = v
})
folder.addColor(this.options, 'colour1').name('Colour 1')
folder.addColor(this.options, 'colour2').name('Colour 2')
folder.close()
}
}
|