File size: 9,296 Bytes
2b0432b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c0d0800
2b0432b
 
 
 
 
 
 
 
 
b02b26f
 
 
2b0432b
b02b26f
2b0432b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c0d0800
2b0432b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b02b26f
2b0432b
 
 
 
 
 
 
 
 
 
 
b02b26f
2b0432b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import * as THREE from 'three'
import { U } from './materials'
import { clamp, lerp, smoothstep } from './rng'
import { nightFactor, sunAzimuth, sunElevation } from './timeline'

// Palette keyed by sun elevation (degrees). Linear HDR colours.
type RGB = [number, number, number]
interface Key { e: number; zen: RGB; hor: RGB; horSun: RGB; sun: RGB; sunI: number; cloudLit: RGB; cloudDark: RGB }
const KEYS: Key[] = [
  { e: 10, zen: [0.11, 0.22, 0.46], hor: [0.562, 0.475, 0.396], horSun: [0.961, 0.608, 0.341], sun: [1.0, 0.76, 0.52], sunI: 3.4, cloudLit: [1.540, 1.015, 0.595], cloudDark: [0.45, 0.42, 0.5] },
  { e: 5, zen: [0.09, 0.17, 0.38], hor: [0.533, 0.389, 0.317], horSun: [1.085, 0.508, 0.223], sun: [1.0, 0.62, 0.36], sunI: 2.9, cloudLit: [1.680, 0.805, 0.385], cloudDark: [0.42, 0.33, 0.4] },
  { e: 2, zen: [0.07, 0.12, 0.3], hor: [0.490, 0.302, 0.274], horSun: [1.054, 0.372, 0.124], sun: [1.0, 0.48, 0.22], sunI: 2.0, cloudLit: [1.540, 0.560, 0.266], cloudDark: [0.36, 0.25, 0.33] },
  { e: 0, zen: [0.055, 0.09, 0.24], hor: [0.396, 0.238, 0.259], horSun: [0.837, 0.279, 0.099], sun: [1.0, 0.38, 0.14], sunI: 0.9, cloudLit: [1.260, 0.385, 0.252], cloudDark: [0.28, 0.2, 0.3] },
  { e: -2.5, zen: [0.035, 0.06, 0.18], hor: [0.259, 0.173, 0.230], horSun: [0.558, 0.198, 0.087], sun: [1.0, 0.3, 0.1], sunI: 0.0, cloudLit: [0.735, 0.266, 0.280], cloudDark: [0.2, 0.15, 0.26] },
  { e: -5, zen: [0.02, 0.038, 0.12], hor: [0.17, 0.15, 0.26], horSun: [0.45, 0.2, 0.13], sun: [1, 0.3, 0.1], sunI: 0, cloudLit: [0.42, 0.22, 0.3], cloudDark: [0.1, 0.09, 0.17] },
  { e: -8, zen: [0.009, 0.018, 0.06], hor: [0.065, 0.07, 0.13], horSun: [0.15, 0.085, 0.08], sun: [1, 0.3, 0.1], sunI: 0, cloudLit: [0.12, 0.09, 0.13], cloudDark: [0.04, 0.045, 0.08] },
  { e: -12, zen: [0.004, 0.008, 0.028], hor: [0.028, 0.034, 0.068], horSun: [0.045, 0.04, 0.05], sun: [1, 0.3, 0.1], sunI: 0, cloudLit: [0.04, 0.04, 0.06], cloudDark: [0.02, 0.025, 0.045] },
]

function mixRGB(a: RGB, b: RGB, f: number): RGB {
  return [lerp(a[0], b[0], f), lerp(a[1], b[1], f), lerp(a[2], b[2], f)]
}

export interface SkyState {
  elev: number; sunDir: THREE.Vector3; moonDir: THREE.Vector3; night: number
  zen: RGB; hor: RGB; horSun: RGB; sun: RGB; sunI: number; cloudLit: RGB; cloudDark: RGB
  exposure: number
}

export function skyState(t: number): SkyState {
  const elev = sunElevation(t)
  let i = 0
  while (i < KEYS.length - 2 && elev < KEYS[i + 1].e) i++
  const a = KEYS[i], b = KEYS[i + 1]
  const f = clamp((a.e - elev) / (a.e - b.e))
  const az = sunAzimuth(t)
  const er = (elev * Math.PI) / 180
  const sunDir = new THREE.Vector3(Math.cos(az) * Math.cos(er), Math.sin(er), Math.sin(az) * Math.cos(er))
  const maz = az + 0.42, mel = (24 * Math.PI) / 180
  const moonDir = new THREE.Vector3(Math.cos(maz) * Math.cos(mel), Math.sin(mel), Math.sin(maz) * Math.cos(mel))
  const night = nightFactor(t)
  return {
    elev, sunDir, moonDir, night,
    zen: mixRGB(a.zen, b.zen, f), hor: mixRGB(a.hor, b.hor, f), horSun: mixRGB(a.horSun, b.horSun, f),
    sun: mixRGB(a.sun, b.sun, f), sunI: lerp(a.sunI, b.sunI, f),
    cloudLit: mixRGB(a.cloudLit, b.cloudLit, f), cloudDark: mixRGB(a.cloudDark, b.cloudDark, f),
    // camera "eye adaptation": open up as the light fades
    exposure: lerp(0.72, 1.5, smoothstep(1, -9, elev)),
  }
}

const SKY_VS = /* glsl */ `
varying vec3 vDir;
void main() {
  vDir = normalize(position);
  vec4 p = modelViewMatrix * vec4(position, 1.0);
  gl_Position = projectionMatrix * p;
  #ifdef REVERSED_DEPTH
  gl_Position.z = gl_Position.w * 1e-6;
  #else
  gl_Position.z = gl_Position.w * 0.99999;
  #endif
}
`

const SKY_FS = /* glsl */ `
uniform vec3 uSunDir;
uniform vec3 uMoonDir;
uniform vec3 uZen;
uniform vec3 uHor;
uniform vec3 uHorSun;
uniform vec3 uSunCol;
uniform float uSunI;
uniform vec3 uCloudLit;
uniform vec3 uCloudDark;
uniform float uNight;
uniform float uTime;
uniform float uStarBoost;
varying vec3 vDir;

float h21(vec2 p) { p = fract(p * vec2(123.34, 456.21)); p += dot(p, p + 45.32); return fract(p.x * p.y); }
float h31(vec3 p) { p = fract(p * 0.3183099 + 0.1); p *= 17.0; return fract(p.x * p.y * p.z * (p.x + p.y + p.z)); }
float vnoise(vec2 p) {
  vec2 i = floor(p), f = fract(p);
  vec2 u = f * f * (3.0 - 2.0 * f);
  return mix(mix(h21(i), h21(i + vec2(1, 0)), u.x), mix(h21(i + vec2(0, 1)), h21(i + vec2(1, 1)), u.x), u.y);
}
float fbm(vec2 p) {
  float s = 0.0, a = 0.5;
  for (int i = 0; i < 6; i++) { s += a * vnoise(p); p = p * 2.03 + vec2(1.7, 9.2); a *= 0.5; }
  return s;
}

vec3 stars(vec3 d) {
  vec3 acc = vec3(0.0);
  for (int layer = 0; layer < 2; layer++) {
    float K = layer == 0 ? 180.0 : 420.0;
    vec3 p = d * K;
    vec3 c = floor(p);
    float r = h31(c + float(layer) * 31.0);
    if (r > (layer == 0 ? 0.86 : 0.9)) {
      vec3 jitter = vec3(h31(c + 1.3), h31(c + 7.1), h31(c + 3.7));
      vec3 sp = c + 0.2 + 0.6 * jitter;
      float dist = length(p - sp);
      float bright = pow((r - 0.86) / 0.14, 3.0) * (layer == 0 ? 3.0 : 1.2);
      float tw = 0.75 + 0.25 * sin(uTime * (2.0 + jitter.x * 4.0) + jitter.y * 40.0);
      vec3 tint = mix(vec3(0.8, 0.88, 1.0), vec3(1.0, 0.9, 0.75), jitter.z);
      acc += tint * bright * tw * exp(-dist * dist * (layer == 0 ? 18.0 : 30.0));
    }
  }
  return acc;
}

void main() {
  vec3 d = normalize(vDir);
  float h = d.y;
  vec2 sxz = normalize(uSunDir.xz + 1e-5);
  vec2 dxz = normalize(d.xz + 1e-5);
  float towardSun = dot(dxz, sxz) * 0.5 + 0.5;
  float sunDot = dot(d, uSunDir);

  vec3 hor = mix(uHor, uHorSun, pow(towardSun, 2.6));
  float hp = max(h, 0.0);
  float grad = pow(hp, 0.42);
  vec3 col = mix(hor, uZen, smoothstep(0.0, 1.0, grad));
  // low warm band hugging the horizon on the sun side
  col += uHorSun * 0.35 * exp(-hp * 18.0) * pow(towardSun, 4.0);
  // anti-twilight pink band (Belt of Venus) opposite the sun
  float anti = pow(1.0 - towardSun, 3.0);
  col += vec3(0.32, 0.17, 0.2) * anti * exp(-pow((hp - 0.08) * 9.0, 2.0)) * smoothstep(-0.12, 0.03, uSunDir.y) * 0.6;
  // Mie glow around the sun
  float g1 = pow(max(sunDot, 0.0), 6.0), g2 = pow(max(sunDot, 0.0), 80.0), g3 = pow(max(sunDot, 0.0), 900.0);
  float sunVis = smoothstep(-0.12, 0.02, uSunDir.y);
  col += uSunCol * (g1 * 0.12 + g2 * 0.5 + g3 * 2.2) * (0.3 + uSunI * 0.3) * sunVis;
  // sun disk (slightly oversized for cinematic effect), dimmed and reddened near the horizon
  float disk = smoothstep(0.99982, 0.99990, sunDot);
  col += uSunCol * disk * (7.0 + uSunI * 4.0) * smoothstep(-0.02, 0.01, uSunDir.y);

  // clouds: thin stratus / cirrus layer
  if (h > 0.0) {
    vec2 uv = d.xz / (h + 0.06) * 1.6;
    uv += vec2(uTime * 0.004, uTime * 0.0015);
    float n = fbm(uv * vec2(0.6, 1.8) + 3.0);
    float n2 = fbm(uv * 2.6 - 7.0);
    float cov = smoothstep(0.48, 0.78, n * 0.75 + n2 * 0.35) * smoothstep(0.0, 0.08, h) * (1.0 - smoothstep(0.35, 0.8, h) * 0.7);
    float lit = pow(towardSun, 3.0) * 0.8 + 0.2 + g1 * 2.0;
    vec3 cc = mix(uCloudDark, uCloudLit, clamp(lit * (0.55 + n2 * 0.6), 0.0, 1.6));
    col = mix(col, cc, cov * 0.85);
  }

  // night sky: stars + faint milky band
  float starVis = uNight * smoothstep(0.02, 0.2, h);
  if (starVis > 0.001) {
    col += stars(d) * starVis * uStarBoost;
    float band = exp(-pow(dot(d, normalize(vec3(0.3, 0.6, -0.74))) * 4.0, 2.0));
    col += vec3(0.05, 0.055, 0.08) * band * fbm(d.xz * 8.0 / (h + 0.3)) * starVis;
  }
  // crescent moon
  float md = dot(d, uMoonDir);
  float moonDisk = smoothstep(0.99994, 0.99997, md);
  if (moonDisk > 0.0) {
    vec3 side = normalize(uSunDir - uMoonDir * dot(uSunDir, uMoonDir));
    vec3 off = normalize(d - uMoonDir * md);
    float lit = smoothstep(-0.1, 0.5, dot(off, side) + 0.35);
    col += vec3(1.6, 1.5, 1.3) * moonDisk * lit * (0.3 + uNight * 2.2);
  }
  col += vec3(0.6, 0.65, 0.8) * pow(max(md, 0.0), 2000.0) * 0.4 * uNight;

  // below horizon: dark ground haze (hidden by terrain anyway)
  if (h < 0.0) col = mix(hor * 0.75, uZen * 0.5, smoothstep(0.0, -0.3, h));
  gl_FragColor = vec4(col, 1.0);
}
`

export function createSky(reversed = false) {
  const uniforms = {
    uSunDir: U.uSunDir,
    uMoonDir: { value: new THREE.Vector3(0, 0.4, 1).normalize() },
    uZen: { value: new THREE.Color() }, uHor: { value: new THREE.Color() }, uHorSun: { value: new THREE.Color() },
    uSunCol: { value: new THREE.Color() }, uSunI: { value: 1 },
    uCloudLit: { value: new THREE.Color() }, uCloudDark: { value: new THREE.Color() },
    uNight: U.uNight, uTime: U.uTime, uStarBoost: { value: 1 },
  }
  const mat = new THREE.ShaderMaterial({
    vertexShader: SKY_VS, fragmentShader: SKY_FS, uniforms,
    side: THREE.BackSide, depthWrite: false, fog: false,
    defines: reversed ? { REVERSED_DEPTH: 1 } : {},
  })
  const mesh = new THREE.Mesh(new THREE.SphereGeometry(5000, 64, 32), mat)
  mesh.frustumCulled = false
  mesh.renderOrder = -10
  const update = (s: SkyState) => {
    uniforms.uMoonDir.value.copy(s.moonDir)
    uniforms.uZen.value.setRGB(...s.zen)
    uniforms.uHor.value.setRGB(...s.hor)
    uniforms.uHorSun.value.setRGB(...s.horSun)
    uniforms.uSunCol.value.setRGB(...s.sun)
    uniforms.uSunI.value = s.sunI
    uniforms.uCloudLit.value.setRGB(...s.cloudLit)
    uniforms.uCloudDark.value.setRGB(...s.cloudDark)
  }
  return { mesh, uniforms, update }
}