Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-10-30 21:59:14 -05:00
commit 75e86cd8c3
42 files changed
+7557

No files matched your search

+46
View File
@@ -0,0 +1,46 @@
#! Contains exposure: how a camera is set for a scene's light, as an exposure
#! value at ISO 100, of its settings or of what its meter reads; and what light
#! is multiplied by, so exposed, as a renderer records it.
#!
#! Exposed by a meter, a matte white surface lit as the meter read records as
#! about two thirds of the most, and an 18% grey card about an eighth, which an
#! sRGB display shows as middle grey.
import std::math;
module malunal::photometry;
#! The exposure value at ISO 100 of a camera's settings: its aperture's
#! f-number, its shutter's time open, in seconds, and its sensitivity, as ISO.
#! It's log₂(N²/t), a step less each time the ISO doubles past 100, so f/16 at a
#! hundredth of a second is about 15, a sunny day's, as the sunny 16 rule has
#! it.
pub def ev100_of_camera(aperture: f32, shutter: f32, iso: f32 = 100): f32 {
return math::log2(aperture * aperture / shutter) - math::log2(iso / 100);
}
#! The exposure value at ISO 100 a camera's own meter sets for a scene of an
#! average luminance, in nits, calibrated as such meters are, by 12.5:
#! log₂(nits × 100 / 12.5).
pub def ev100_of_nits(luminance: f32): f32 {
return math::log2(luminance * 8);
}
#! The exposure value at ISO 100 a meter held where a scene is lit sets for it,
#! of the lux it reads, calibrated as such meters are, by 250:
#! log₂(lux × 100 / 250). Direct sunlight's is about 15.3, an office's 7.3.
pub def ev100_of_lux(illuminance: f32): f32 {
return math::log2(illuminance * 0.4);
}
#! What light, in nits, is multiplied by, recorded by a camera at an exposure
#! value at ISO 100: the most it records, 1, over the luminance that fills its
#! sensor, 1.2 × 2^EV100 nits.
pub def exposure_of_ev100(ev100: f32): f32 {
return 1 / (1.2 * math::exp2(ev100));
}
#! The exposure value at ISO 100 at which light is multiplied by so much, as
#! `exposure_of_ev100` gives it.
pub def ev100_of_exposure(exposure: f32): f32 {
return -math::log2(1.2 * exposure);
}
+157
View File
@@ -0,0 +1,157 @@
#! Contains `photometry`: light as an eye sees it, in the units lights are named
#! by, and converted between them, so a game's lights are given as real ones
#! are: a bulb by its lumens, the sun by the lux it lights the ground with, the
#! sky by its luminance, and a camera by its exposure value.
#!
#! ```
#! lumens lm luminous flux: all the light a source gives, every way at once, as a bulb is sold by
#! candela cd, lm/sr luminous intensity: the light a source gives one way, in each steradian of it
#! lux lx, lm/m² illuminance: the light falling on a surface, as a meter on it reads
#! nits cd/m² luminance: the light a surface gives back toward an eye, or a sky gives
#! EV100 exposure value at ISO 100: how a camera is set for a scene's light, each step twice the light
#! ```
#!
#! Angles are radians, and a cone's angle is its half, from its axis to its
#! edge, as glTF's spot lights give theirs.
import std::math;
module malunal::photometry;
# π, as the `f32` nearest it.
const pi: f32 = 3.14159265;
#! The solid angle of a cone, in steradians: 2π(1 - cos angle), of its angle
#! from its axis to its edge. Of π, it's a sphere's, 4π.
pub def solid_angle_of(angle: f32): f32 {
return 2 * pi * (1 - math::cos(angle));
}
#! The candela of a light giving so many lumens within a cone, alike every way
#! within it: its lumens over the cone's solid angle. Of the angle left out, π,
#! it shines every way, as a bare bulb does, so it's its lumens over 4π; of a
#! narrower cone, its light is gathered into it, as a torch's reflector gathers
#! its bulb's, and it's brighter each way.
pub def candela_of_lumens(flux: f32, angle: f32 = pi): f32 {
return flux / solid_angle_of(angle);
}
#! The lumens of a light of so many candela within a cone, alike every way
#! within it, or every way, of the angle left out.
pub def lumens_of_candela(intensity: f32, angle: f32 = pi): f32 {
return intensity * solid_angle_of(angle);
}
#! The lumens of a lamp drawing so many watts, of how many lumens each watt of
#! its kind gives, as `efficacy` names them.
pub def lumens_of_watts(power: f32, lumens_per_watt: f32): f32 {
return power * lumens_per_watt;
}
#! The watts a lamp of so many lumens draws, of how many lumens each watt of
#! its kind gives.
pub def watts_of_lumens(flux: f32, lumens_per_watt: f32): f32 {
return flux / lumens_per_watt;
}
#! The lux a light of so many candela gives a surface so far from it, facing
#! it: its candela over the square of the distance. A surface turned from it is
#! given the cosine of the angle as much.
pub def lux_of_candela(intensity: f32, distance: f32): f32 {
return intensity / (distance * distance);
}
#! How far from a light of so many candela a surface facing it is given so many
#! lux: the square root of its candela over them, as far as it lights as much
#! as is seen.
pub def distance_of_lux(intensity: f32, illuminance: f32): f32 {
return math::sqrt(intensity / illuminance);
}
#! The nits a matte surface lit by so many lux gives back, every way alike, of
#! the fraction of what falls on it it gives back: what falls on it, by that
#! fraction, over π. White gives back all of it, and an 18% grey card, a
#! photograph's middle grey, 0.18.
pub def nits_of_lux(illuminance: f32, reflectance: f32 = 1): f32 {
return illuminance * reflectance / pi;
}
#! The lux lighting a matte surface that gives back so many nits, of the
#! fraction of what falls on it it gives back: π times its nits, over that
#! fraction. Of the fraction left out, it's the lux a sky of so many nits, alike
#! every way, lights the ground with.
pub def lux_of_nits(luminance: f32, reflectance: f32 = 1): f32 {
return luminance * pi / reflectance;
}
#! Illuminances, in lux, of the ground or a floor, about as each is lit.
pub struct lux {}
#! A moonless, overcast night, lit by starlight.
pub const lux::starlight: f32 = 0.0001;
#! A moonless clear night, lit by the sky's own glow.
pub const lux::moonless_night: f32 = 0.002;
#! A full moon on a clear night.
pub const lux::full_moon: f32 = 0.2;
#! The end of civil twilight, under a clear sky.
pub const lux::twilight: f32 = 3.4;
#! A family's living room.
pub const lux::living_room: f32 = 50;
#! An office building's hallway.
pub const lux::hallway: f32 = 80;
#! A very dark overcast day.
pub const lux::dark_overcast: f32 = 100;
#! An office's desks.
pub const lux::office: f32 = 400;
#! Sunrise or sunset on a clear day.
pub const lux::sunrise: f32 = 400;
#! An overcast day.
pub const lux::overcast: f32 = 1000;
#! Full daylight, out of the sun.
pub const lux::daylight: f32 = 20000;
#! Direct sunlight, at noon.
pub const lux::direct_sunlight: f32 = 100000;
#! Luminances, in nits, about as each is seen.
pub struct nits {}
#! The sky of a clear day, out of the sun, which lights the ground with about
#! 22,000 lux.
pub const nits::clear_sky: f32 = 7000;
#! Luminous fluxes, in lumens, about as each is sold or burns.
pub struct lumens {}
#! A candle, of a candela every way.
pub const lumens::candle: f32 = 12.566371;
#! A bulb as bright as a 40-watt incandescent bulb, as LED bulbs are sold.
pub const lumens::bulb_40w: f32 = 450;
#! As bright as a 60-watt incandescent bulb.
pub const lumens::bulb_60w: f32 = 800;
#! As bright as a 75-watt incandescent bulb.
pub const lumens::bulb_75w: f32 = 1100;
#! As bright as a 100-watt incandescent bulb.
pub const lumens::bulb_100w: f32 = 1600;
#! Luminous intensities, in candela.
pub struct candela {}
#! A candle's, every way, which the candela was first named for.
pub const candela::candle: f32 = 1;
#! How many lumens each watt a lamp draws gives, by its kind, about, as
#! `lumens_of_watts` takes them.
pub struct efficacy {}
#! The most a watt gives, as green light of 555 nm, by which the lumen is
#! defined.
pub const efficacy::most: f32 = 683;
#! An incandescent bulb's.
pub const efficacy::incandescent: f32 = 14;
#! A halogen bulb's.
pub const efficacy::halogen: f32 = 20;
#! A compact fluorescent bulb's.
pub const efficacy::fluorescent: f32 = 60;
#! A white LED's.
pub const efficacy::led: f32 = 100;
#! Sunlight's, of each watt of it falling on the ground.
pub const efficacy::sunlight: f32 = 93;
+34
View File
@@ -0,0 +1,34 @@
#! Contains color temperature: the color of a black body's light at a
#! temperature, as lamps' colors are named.
import std::math;
import malunal::linear;
module malunal::photometry;
#! The color of a black body's light at a temperature, in kelvin, as a lamp's
#! is named: a candle's about 1,900 K, a warm bulb's 2,700, daylight's 6,500,
#! and a blue sky's 10,000 and more. It's linear sRGB, its greatest channel 1,
#! as a light's color is, by its intensity. A temperature below 1,000 K, or past
#! 40,000, is taken as either.
#!
#! The color is of Krystek's fit of the Planckian locus, within a thousandth of
#! it from 1,000 K to 15,000, made sRGB of CIE 1931's XYZ, a channel past sRGB's
#! colors taken as none.
pub def color_of_temperature(kelvin: f32): vec3 {
val t := math::clamp(kelvin, 1000f, 40000f);
val tt := t * t;
# Where it lies in CIE 1960's u and v, then in 1931's x and y.
val u := (0.860117757 + 1.54118254e-4 * t + 1.28641212e-7 * tt) / (1 + 8.42420235e-4 * t + 7.08145163e-7 * tt);
val v := (0.317398726 + 4.22806245e-5 * t + 4.20481691e-8 * tt) / (1 - 2.89741816e-5 * t + 1.61456053e-7 * tt);
val d := 2 * u - 8 * v + 4;
val x := 3 * u / d;
val y := 2 * v / d;
# Of a brightness of 1.
val big_x := x / y;
val big_z := (1 - x - y) / y;
val r := math::max(3.2404542 * big_x - 1.5371385 - 0.4985314 * big_z, 0f);
val g := math::max(-0.9692660 * big_x + 1.8760108 + 0.0415560 * big_z, 0f);
val b := math::max(0.0556434 * big_x - 0.2040259 + 1.0572252 * big_z, 0f);
val most := math::max(r, math::max(g, b));
return vec3{ r / most, g / most, b / most };
}