|
| 1 | +// 3D match view: the light a match is played in. Each map gets a time of day from its seed (a clear |
| 2 | +// morning, high noon, golden hour, or a gray day), which sets the sun, the sky, the haze and the color |
| 3 | +// grade together, so the four looks are each consistent and the same map always looks the same. |
| 4 | +import * as THREE from 'three'; |
| 5 | +import { Sky } from 'three/addons/objects/Sky.js'; |
| 6 | + |
| 7 | +const sunFrom = (elevation, azimuth) => new THREE.Vector3().setFromSphericalCoords(1, |
| 8 | + THREE.MathUtils.degToRad(90 - elevation), THREE.MathUtils.degToRad(azimuth)); |
| 9 | + |
| 10 | +/** |
| 11 | + * The looks. sky: three.js Sky settings; grade: warm (highlight tint), cool (shadow tint), split (how much |
| 12 | + * of each), contrast, saturation, vignette. haze tints the far ground toward the horizon. |
| 13 | + */ |
| 14 | +export const MOODS = { |
| 15 | + morning: { |
| 16 | + sun: sunFrom(24, 125), sunColor: '#ffd9b0', sunI: 2.5, shadow: 0.9, |
| 17 | + sky: { turbidity: 4, rayleigh: 1.9, mieCoefficient: 0.004, mieDirectionalG: 0.8 }, |
| 18 | + hemi: ['#bfd6ff', '#5d5442', 0.6], env: 0.6, exposure: 1.0, fog: '#c8d5e2', fogFar: 4.2, |
| 19 | + grade: { warm: [1.06, 1.0, 0.9], cool: [0.9, 0.97, 1.1], split: 0.55, contrast: 1.06, saturation: 1.0, vignette: 0.5 }, |
| 20 | + }, |
| 21 | + noon: { |
| 22 | + sun: sunFrom(52, 205), sunColor: '#fff0da', sunI: 2.9, shadow: 1, |
| 23 | + sky: { turbidity: 3.2, rayleigh: 1.1, mieCoefficient: 0.003, mieDirectionalG: 0.82 }, |
| 24 | + hemi: ['#b9d3ff', '#6a5a40', 0.55], env: 0.55, exposure: 0.95, fog: '#bccbd8', fogFar: 4.6, |
| 25 | + grade: { warm: [1.04, 1.0, 0.94], cool: [0.95, 0.99, 1.05], split: 0.35, contrast: 1.06, saturation: 1.02, vignette: 0.45 }, |
| 26 | + }, |
| 27 | + golden: { |
| 28 | + sun: sunFrom(13, 250), sunColor: '#ffb27a', sunI: 2.7, shadow: 0.95, |
| 29 | + sky: { turbidity: 7.5, rayleigh: 2.6, mieCoefficient: 0.006, mieDirectionalG: 0.86 }, |
| 30 | + hemi: ['#c4bdf0', '#6b4a30', 0.5], env: 0.5, exposure: 1.05, fog: '#e0c6a8', fogFar: 3.8, |
| 31 | + grade: { warm: [1.12, 1.0, 0.82], cool: [0.86, 0.96, 1.12], split: 0.8, contrast: 1.08, saturation: 1.04, vignette: 0.65 }, |
| 32 | + }, |
| 33 | + overcast: { |
| 34 | + sun: sunFrom(42, 190), sunColor: '#e9eef4', sunI: 1.25, shadow: 0.45, |
| 35 | + sky: { turbidity: 12, rayleigh: 0.5, mieCoefficient: 0.02, mieDirectionalG: 0.6 }, |
| 36 | + hemi: ['#d2dbe4', '#5e5a50', 1.0], env: 0.95, exposure: 1.05, fog: '#b9c1c9', fogFar: 3.4, |
| 37 | + grade: { warm: [1.0, 1.0, 0.98], cool: [0.94, 0.98, 1.04], split: 0.3, contrast: 1.04, saturation: 0.9, vignette: 0.45 }, |
| 38 | + }, |
| 39 | +}; |
| 40 | + |
| 41 | +/** Which look a map gets: mostly fair weather, now and then a gray day. */ |
| 42 | +export function moodFor(seed) { |
| 43 | + const order = ['noon', 'morning', 'golden', 'noon', 'morning', 'golden', 'overcast']; |
| 44 | + return order[((seed % order.length) + order.length) % order.length]; |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Light the view's scene for `name`: the sky dome and the image-based light taken from it, the sun and |
| 49 | + * its shadows, the fill light, the haze and the exposure. The grade pass picks the mood up from |
| 50 | + * view.mood (setGrade). |
| 51 | + */ |
| 52 | +export function applyMood(view, name) { |
| 53 | + const mood = MOODS[name] ?? MOODS.noon; |
| 54 | + view.mood = mood; |
| 55 | + view.moodName = MOODS[name] ? name : 'noon'; |
| 56 | + view.sunDir = mood.sun.clone(); |
| 57 | + const u = view.sky.material.uniforms; |
| 58 | + for (const [k, v] of Object.entries(mood.sky)) u[k].value = v; |
| 59 | + u.sunPosition.value.copy(mood.sun); |
| 60 | + |
| 61 | + // Image-based lighting from the same sky, so every surface picks up its color. |
| 62 | + const pmrem = new THREE.PMREMGenerator(view.renderer); |
| 63 | + const envScene = new THREE.Scene(); |
| 64 | + const envSky = new Sky(); |
| 65 | + envSky.scale.setScalar(100); |
| 66 | + Object.entries(u).forEach(([k, v]) => { envSky.material.uniforms[k].value = v.value?.clone ? v.value.clone() : v.value; }); |
| 67 | + envScene.add(envSky); |
| 68 | + view.envRT?.dispose(); |
| 69 | + view.envRT = pmrem.fromScene(envScene, 0, 0.1, 200); |
| 70 | + view.scene.environment = view.envRT.texture; |
| 71 | + view.scene.environmentIntensity = mood.env; |
| 72 | + pmrem.dispose(); |
| 73 | + envSky.material.dispose(); |
| 74 | + |
| 75 | + view.sun.color.set(mood.sunColor); |
| 76 | + view.sun.intensity = mood.sunI; |
| 77 | + view.sun.shadow.intensity = mood.shadow; |
| 78 | + view.hemi.color.set(mood.hemi[0]); |
| 79 | + view.hemi.groundColor.set(mood.hemi[1]); |
| 80 | + view.hemi.intensity = mood.hemi[2]; |
| 81 | + view.scene.fog.color.set(mood.fog); |
| 82 | + view.renderer.toneMappingExposure = mood.exposure; |
| 83 | + if (view.gradePass) setGrade(view.gradePass, mood); |
| 84 | +} |
| 85 | + |
| 86 | +/** Copy a mood's grade into the grade pass's uniforms. */ |
| 87 | +export function setGrade(pass, mood) { |
| 88 | + const g = mood.grade, u = pass.uniforms; |
| 89 | + u.uWarm.value.set(...g.warm); |
| 90 | + u.uCool.value.set(...g.cool); |
| 91 | + u.uSplit.value = g.split; |
| 92 | + u.uContrast.value = g.contrast; |
| 93 | + u.uSaturation.value = g.saturation; |
| 94 | + u.uVignette.value = g.vignette; |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * The color grade, after bloom and before tone mapping's output pass: split toning (cool shadows, warm |
| 99 | + * highlights), a gentle S-curve for contrast, saturation, and a soft vignette. |
| 100 | + */ |
| 101 | +export function gradeShader() { |
| 102 | + return { |
| 103 | + uniforms: { |
| 104 | + tDiffuse: { value: null }, |
| 105 | + uWarm: { value: new THREE.Vector3(1.04, 1, 0.94) }, uCool: { value: new THREE.Vector3(0.95, 0.99, 1.05) }, |
| 106 | + uSplit: { value: 0.35 }, uContrast: { value: 1.08 }, uSaturation: { value: 1.08 }, uVignette: { value: 0.55 }, |
| 107 | + }, |
| 108 | + vertexShader: 'varying vec2 vUv; void main() { vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); }', |
| 109 | + fragmentShader: ` |
| 110 | + uniform sampler2D tDiffuse; uniform vec3 uWarm; uniform vec3 uCool; |
| 111 | + uniform float uSplit; uniform float uContrast; uniform float uSaturation; uniform float uVignette; |
| 112 | + varying vec2 vUv; |
| 113 | + void main() { |
| 114 | + vec4 c = texture2D(tDiffuse, vUv); |
| 115 | + float l = dot(c.rgb, vec3(0.2126, 0.7152, 0.0722)); |
| 116 | + // Split toning: shadows lean cool, highlights warm. |
| 117 | + vec3 tint = mix(uCool, uWarm, smoothstep(0.05, 0.9, l)); |
| 118 | + c.rgb = mix(c.rgb, c.rgb * tint, uSplit); |
| 119 | + // Contrast about mid-gray in a log-ish space, so HDR highlights aren't crushed. |
| 120 | + vec3 lc = log2(max(c.rgb, vec3(1e-4)) / 0.18); |
| 121 | + c.rgb = 0.18 * exp2(lc * uContrast); |
| 122 | + l = dot(c.rgb, vec3(0.2126, 0.7152, 0.0722)); |
| 123 | + c.rgb = max(mix(vec3(l), c.rgb, uSaturation), 0.0); |
| 124 | + vec2 d = vUv - 0.5; |
| 125 | + c.rgb *= 1.0 - dot(d, d) * uVignette; |
| 126 | + gl_FragColor = c; |
| 127 | + }`, |
| 128 | + }; |
| 129 | +} |
0 commit comments