Back to Drift City
SOURCE / PINNED RELEASE

Made of little things.

Drift City

Release
098731aa0f0f…
Author-recorded commit
2822ea0a4f5b…
License
LICENSE
Author’s source reference
nostr://npub12lm3rxsva8kfempyjkxupt09e02n3fezurkyadyqfejdc5e0r2kscqty6r/wss%3A%2F%2Fgit.napplet.soy%2F/n-a4c0039c855

Archive hash verified: b3323e408235502b…. The source-to-build association is the author’s claim; it has not been independently rebuilt.

src/game/shadows.ts
import * as THREE from 'three';

/**
 * Soft sun shadows (percentage-closer soft shadows), in place of three's fixed-width
 * PCF soft filter.
 *
 * Real sunlight comes from a disc, not a point, so a shadow is crisp where the thing
 * casting it meets the ground and blurs the further away the caster is: a car's shadow
 * is sharp at the tyres, a tower's soft at its far end. For each point, a first pass
 * finds the average depth of whatever is between it and the sun nearby; the gap between
 * that and the point sets how wide the second pass filters. Both passes sample a spiral
 * turned a little pixel by pixel, so 26 samples look smooth. At the edge of the shadow
 * map's coverage the shadows fade out rather than stopping at a line.
 *
 * The light's shadow.radius carries the softness: shadow-map UV of penumbra per unit of
 * shadow depth (see Game.setZoom).
 */
const PCSS = /* glsl */ `
#define SUN_BLOCKER_SAMPLES 10
#define SUN_FILTER_SAMPLES 16

vec2 sunSpiral( int i, int n, float turn ) {
	float r = sqrt( ( float( i ) + 0.5 ) / float( n ) );
	float a = float( i ) * 2.39996323 + turn;
	return vec2( cos( a ), sin( a ) ) * r;
}

float sunShadow( sampler2D shadowMap, vec2 shadowMapSize, float softness, vec4 coord ) {
	vec2 texel = vec2( 1.0 ) / shadowMapSize;
	float turn = 6.2831853 * fract( 52.9829189 * fract( dot( gl_FragCoord.xy, vec2( 0.06711056, 0.00583715 ) ) ) );
	// Whatever stands between this point and the sun, nearby: how far above it is it?
	float search = max( softness * 0.1, 4.0 * texel.x );
	float sum = 0.0;
	float found = 0.0;
	for ( int i = 0; i < SUN_BLOCKER_SAMPLES; i ++ ) {
		float d = unpackRGBAToDepth( texture2D( shadowMap, coord.xy + sunSpiral( i, SUN_BLOCKER_SAMPLES, turn ) * search ) );
		if ( d < coord.z ) { sum += d; found += 1.0; }
	}
	float lit = 1.0;
	if ( found > 0.5 ) {
		float penumbra = ( coord.z - sum / found ) * softness;
		float radius = clamp( penumbra, 1.5 * texel.x, search );
		lit = 0.0;
		for ( int i = 0; i < SUN_FILTER_SAMPLES; i ++ )
			lit += texture2DCompare( shadowMap, coord.xy + sunSpiral( i, SUN_FILTER_SAMPLES, turn ) * radius, coord.z );
		lit /= float( SUN_FILTER_SAMPLES );
	}
	// Fade out towards the edge of the map's coverage.
	vec2 edge = min( coord.xy, 1.0 - coord.xy );
	return mix( 1.0, lit, smoothstep( 0.0, 0.08, min( edge.x, edge.y ) ) );
}
`;

const chunk = THREE.ShaderChunk.shadowmap_pars_fragment;
const softBranch = '#elif defined( SHADOWMAP_TYPE_PCF_SOFT )';
if (!chunk.includes(softBranch) || !chunk.includes('float getShadow('))
  console.warn('Soft shadows: three.js shadow chunk not as expected; keeping its own filter.');
else
  THREE.ShaderChunk.shadowmap_pars_fragment = chunk
    .replace('float getShadow(', `${PCSS}\nfloat getShadow(`)
    .replace(softBranch, `${softBranch}\n\t\t\tshadow = sunShadow( shadowMap, shadowMapSize, shadowRadius, shadowCoord );\n\t\t#elif 0`);

/** How soft the sun's shadows are: the tangent of the angle the light comes in over (the sun's disc and the sky's glow). */
export const SUN_SOFTNESS = 0.02;