Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix light dir and calculation #2

Merged
merged 2 commits into from
Jun 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 13 additions & 24 deletions packages/lighting-control/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getPosition, getMoonPosition} from "suncalc";
import { getPosition, getMoonPosition, getTimes } from "suncalc";
import type Mapgl from "../project";
import { radToDeg } from "./utils";

Expand All @@ -14,9 +14,9 @@ interface AmbientLightSource {
color: [number, number, number];
}

const MAX_SUN_INTENSITY = 0.6;
const MAX_AMBIENT_INTENSITY = 0.7;
const MAX_MOON_INTENSITY = 0.2; // Пока что не учитываем фазы луны
const SUN_INTENSITY = 0.3;
const AMBIENT_INTENSITY = 0.75;
const MOON_INTENSITY = 0;

export class MapglLightingControl {
constructor(private map: Mapgl.Map) {}
Expand All @@ -27,33 +27,22 @@ export class MapglLightingControl {
const moonPosition = getMoonPosition(date, lat, lng);

const sunAltitude = radToDeg(sunPosition.altitude);
const sunAzimuth = radToDeg(sunPosition.azimuth);
const sunAzimuth = radToDeg(sunPosition.azimuth + Math.PI);
const moonAltitude = radToDeg(moonPosition.altitude);
const moonAzimuth = radToDeg(moonPosition.azimuth);
const moonAzimuth = radToDeg(moonPosition.azimuth + Math.PI);

const sunIntensityFactor =
sunAltitude > 0 ? Math.max(sunAltitude / 90, 0.85) : 0;
const moonIntensityFactor =
moonAltitude > 0 ? Math.max(moonAltitude / 90, 0.85) : 0;
const sunIntensity = Math.sin(sunPosition.altitude) < 0 ? 0 : SUN_INTENSITY;
const moonIntensity = MOON_INTENSITY;

const ambientIntensity = AMBIENT_INTENSITY - moonIntensity;

const sunIntensity = MAX_SUN_INTENSITY * sunIntensityFactor;
const moonIntensity = MAX_MOON_INTENSITY * moonIntensityFactor;
const ambientIntensity = Math.max(
0.5,
((sunIntensity + moonIntensity) /
(MAX_SUN_INTENSITY + MAX_MOON_INTENSITY)) *
MAX_AMBIENT_INTENSITY,
);

const hour = date.getHours() + date.getMinutes() / 60;
const colorFactor =
Math.max(0, 1 - Math.max(0, (hour < 10 ? 10 - hour : hour - 14) / 6));
const GBColorFactor = Math.round(255 * (0.9 + 0.1 * Math.max(0, Math.sin(sunPosition.altitude))));

this.setLighting({
sun: {
altitude: sunAltitude,
azimuth: sunAzimuth,
color: [255, Math.round(255 * (0.5 + colorFactor / 2)), Math.round(255 * colorFactor)],
color: [255, 255, 255],
intensity: sunIntensity,
},
moon: {
Expand All @@ -63,7 +52,7 @@ export class MapglLightingControl {
intensity: moonIntensity,
},
atmosphere: {
color: [255, 255, 255],
color: [255, GBColorFactor, GBColorFactor],
intensity: ambientIntensity,
},
shadowSource: "sun",
Expand Down
Loading