Files
grass-cutters/Assets/Shaders/Planet.hlsl
NiemandKG 57d0f4d31a Frame time on a big farm: batched grass, sleeping bundles, no light probes
Measured in a real 2560x1440 window on the player's Mommy save (RTX 4090):
the GPU took ~5 ms, the main thread the rest.

- Grass: a fully mown sector draws only its floor (one draw, not up to six);
  untouched and fully mown sectors share a mow map look, so the fur pile
  draws them all in one instanced call per pile (GC_SECTOR_BUFFER, sector
  frames from a structured buffer). Grass draws 1112 -> 337 at the base,
  876 -> 439 in the meadow.
- Bundles: loose ones sleep when they come to rest (our gravity kept ~600
  bodies awake), ones on belts skip the ground checks, and all of them are
  stepped from one CargoTick instead of two Unity messages each.
- Belts leave a jammed line alone and only look every few steps whether it
  can move; splitters test distance before transforming every bundle.
- Light probes off on everything (the planet has none) and the GPU Resident
  Drawer off: both cost CPU per moving renderer for nothing.
- -demogpu N -onscreen measures frame time with one feature off at a time.

Base view 17-19 ms -> 14-15 ms, meadow 14-16 ms -> 11.5-12 ms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 20:01:02 +06:00

114 lines
4.2 KiB
HLSL

// What every shader on Herbaria-7 shares: the ball (Planet.cs), its soil heights, a tiling 3D noise, and the sector being
// drawn with its mow map (HexGrid.cs, MowMap.cs). "up" is simply the direction from the centre.
#ifndef GC_PLANET_INCLUDED
#define GC_PLANET_INCLUDED
TEXTURE2D_ARRAY(_GcHeights); SAMPLER(sampler_GcHeights);
TEXTURE3D(_GcNoise3); SAMPLER(sampler_GcNoise3);
TEXTURE2D(_GcMaskTex); SAMPLER(sampler_GcMaskTex);
float4 _GcPlanet; // centre, radius
float4 _GcLampPos, _GcLampDir; // the headlamp: position and reach, direction and power
float _GcCloudShade; // how much of the sun a passing cloud takes away
/// How much of the player's own headlamp reaches a point.
float GcLamp(float3 p)
{
float3 to = p - _GcLampPos.xyz;
float d = max(length(to), 1e-3);
float cone = smoothstep(0.84, 0.95, dot(to / d, _GcLampDir.xyz));
float fall = saturate(1 - d / _GcLampPos.w);
return _GcLampDir.w * cone * fall * fall * 2.2;
}
float4 _GcContacts[24]; // where the machines stand (ContactShade.cs): a ground point and how far its dark reaches
float _GcContactCount;
/// The dark under the machines: 1 out in the open, less right under a tyre or a body. Measured in 3D, so a grass blade
/// standing up out of it is darker at its root than at its tip.
float GcContact(float3 p)
{
float k = 1;
int n = (int)_GcContactCount;
[loop] for (int c = 0; c < n; c++)
{
float4 q = _GcContacts[c];
float3 to = p - q.xyz;
float d2 = dot(to, to) / max(q.w * q.w, 1e-4);
float f = saturate(1 - d2);
k *= 1 - 0.5 * f * f;
}
return k;
}
float _GcHeightRes;
// per draw: the sector (MaterialPropertyBlock)
float4 _GcC, _GcE1, _GcE2;
float4 _GcSalt; // xy = seed, z = flat frame metres to mow map uv
float4 _GcBorders[6];
// a shader that draws many sectors in one go defines these first (GrassFur: GC_SECTOR_BUFFER)
#ifndef GC_SECTOR_C
#define GC_SECTOR_C _GcC
#define GC_SECTOR_E1 _GcE1
#define GC_SECTOR_E2 _GcE2
#define GC_SECTOR_SALT _GcSalt
#define GC_SECTOR_BORDER(k) _GcBorders[k]
#endif
float GcHeight(float3 d)
{
float3 a = abs(d);
float2 uv;
float f;
if (a.x >= a.y && a.x >= a.z) { uv = d.zy / a.x; f = d.x > 0 ? 0 : 1; }
else if (a.y >= a.z) { uv = d.xz / a.y; f = d.y > 0 ? 2 : 3; }
else { uv = d.xy / a.z; f = d.z > 0 ? 4 : 5; }
uv = ((uv * 0.5 + 0.5) * (_GcHeightRes - 1) + 0.5) / _GcHeightRes;
return SAMPLE_TEXTURE2D_ARRAY_LOD(_GcHeights, sampler_GcHeights, uv, f, 0).r;
}
float3 GcSoil(float3 d) { return _GcPlanet.xyz + d * (_GcPlanet.w + GcHeight(d)); }
// r, g, b, a: four unrelated value noises, one lattice cell per unit of p
float4 GcNoise(float3 p) { return SAMPLE_TEXTURE3D_LOD(_GcNoise3, sampler_GcNoise3, p / 64.0, 0); }
// a cloud's shadow going over: 1 in the sun, less under it. Big soft blobs that drift with the wind.
float GcCloudSun(float3 p, float time)
{
float n = GcNoise(p * 0.009 + float3(-0.8, 0.1, -0.45) * time * 0.02).a;
return 1 - smoothstep(0.5, 0.62, n) * _GcCloudShade;
}
// tangent axes that flow smoothly over the ball, t1 along the wind
void GcTangents(float3 up, out float3 t1, out float3 t2)
{
const float3 wind = float3(0.87, 0.11, 0.48);
t1 = normalize(wind - up * dot(wind, up) + 1e-5);
t2 = cross(up, t1);
}
// this sector's mow map at a spot of its flat frame: r = cut (0..1), g = nothing grows here
float2 GcMask(float2 plane)
{
return SAMPLE_TEXTURE2D_LOD(_GcMaskTex, sampler_GcMaskTex, plane * GC_SECTOR_SALT.z + 0.5, 0).rg;
}
// how close a cut is: the map blurred over half a metre, so the grass beside a narrow strip can stand lower than the rest
float GcMaskNear(float2 plane)
{
return SAMPLE_TEXTURE2D_LOD(_GcMaskTex, sampler_GcMaskTex, plane * GC_SECTOR_SALT.z + 0.5, 1.5).r;
}
// A spot in the flat frame of this draw's sector, on the ball. Each strand belongs to exactly one sector: the one whose
// borders all lie ahead of it. edge = metres to the nearest of them.
bool GcInSector(float2 plane, out float3 dir, out float edge)
{
dir = normalize(GC_SECTOR_C.xyz * _GcPlanet.w + GC_SECTOR_E1.xyz * plane.x + GC_SECTOR_E2.xyz * plane.y);
edge = 1e5;
for (int k = 0; k < 6; k++) edge = min(edge, -dot(dir, GC_SECTOR_BORDER(k).xyz));
return edge >= 0;
}
#endif