// Herbaria-7's meadow toward the horizon, where single strands would be under a pixel (shell texturing), and the solid // floor under the near strands (one shell). GrassField.cs lays a grid over the flat frame of each sector in view, // once per shell with RenderMeshPrimitives (SV_InstanceID = shell, 0 = the top one so the upper layers hit the depth // buffer first). Every shell is lifted to its share of the pile height and bent by wind, mowers and players in the vertex // stage; the fragment stage cuts the strands out of a tiling strand texture (r = strand height, tapered to a point; g = // per-strand tint) and the mow mask, so mown trails are crisp and cost nothing extra. No blades, no per-instance data. Shader "GrassCutter/GrassFur" { Properties { _RootColor ("Root", Color) = (0.06, 0.17, 0.04, 1) _TipColor ("Tip", Color) = (0.38, 0.68, 0.17, 1) _DryColor ("Dry tip", Color) = (0.82, 0.78, 0.33, 1) _HempColor ("Hemp streaks", Color) = (0.25, 0.55, 0.22, 1) _CutColor ("Fresh stubble", Color) = (0.8, 0.74, 0.4, 1) _SoilColor ("Soil under stubble", Color) = (0.52, 0.42, 0.25, 1) _Dirt ("Dirt", Color) = (0.42, 0.33, 0.22, 1) _Gravel ("Gravel", Color) = (0.58, 0.55, 0.5, 1) _Wind ("Wind", Float) = 1 } SubShader { Tags { "RenderType" = "Opaque" "Queue" = "AlphaTest" "RenderPipeline" = "UniversalPipeline" "IgnoreProjector" = "True" } Pass { Name "FurForward" Tags { "LightMode" = "UniversalForward" } Cull Off ZWrite On HLSLPROGRAM #pragma target 4.5 #pragma vertex vert #pragma fragment frag #pragma multi_compile _ _MAIN_LIGHT_SHADOWS _MAIN_LIGHT_SHADOWS_CASCADE _MAIN_LIGHT_SHADOWS_SCREEN #pragma multi_compile_fragment _ _SHADOWS_SOFT _SHADOWS_SOFT_LOW _SHADOWS_SOFT_MEDIUM _SHADOWS_SOFT_HIGH #pragma multi_compile_fog // many sectors in one draw: instance = sector * shells + shell, the sector's frame from a buffer. Only for // sectors that share a mow map look (untouched, or mown to the last blade): on a small planet there are // hundreds of them in view, and one draw each was most of a frame's CPU. #pragma multi_compile_local _ GC_SECTOR_BUFFER #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl" #if defined(GC_SECTOR_BUFFER) struct GcSectorData { float4 c, e1, e2, salt, grid; float4 b[6]; }; StructuredBuffer _GcSectorBuf; static GcSectorData gcS; #define GC_SECTOR_C gcS.c #define GC_SECTOR_E1 gcS.e1 #define GC_SECTOR_E2 gcS.e2 #define GC_SECTOR_SALT gcS.salt #define GC_SECTOR_BORDER(k) gcS.b[k] #define GC_GRID gcS.grid #else #define GC_GRID _GcGrid #endif #include "Planet.hlsl" CBUFFER_START(UnityPerMaterial) float4 _RootColor, _TipColor, _DryColor, _HempColor, _CutColor, _SoilColor, _Dirt, _Gravel; float _Wind; CBUFFER_END TEXTURE2D(_GcFurTex); SAMPLER(sampler_GcFurTex); // per draw (MaterialPropertyBlock) float4 _GcGrid; // half a side of the sector's square, cell float4 _GcShells; // shell count, inner radius, outer radius (only this ring is drawn), dithered seam width float4 _GcFade; // the pile sinks into the ground between these distances (the horizon); z = 1: only what was cut // globals float4 _GcCam, _GcCamUp; float4 _GcFurN, _GcFurU, _GcFurV; // the cube face the strand texture lies on float4 _GcVortex[12]; // where, strength float _GcVortexReach[12]; float _GcVortexCount; float _GcGust; // the weather's wind on top of the breeze: 0 calm float4 _GcPushers[8]; // x, y, z, radius float _GcPusherCount; float _GcTime, _GcHeight, _GcFurScale, _GcFurMax; struct Attributes { float3 positionOS : POSITION; uint iid : SV_InstanceID; }; struct Varyings { float4 positionCS : SV_POSITION; float3 positionWS : TEXCOORD0; float4 data : TEXCOORD1; // x = shell 0..1, y = pile height here, z = big patch noise, w = edge fade float3 root : TEXCOORD2; // undisplaced world position (strand lookup) float fog : TEXCOORD3; nointerpolation uint sid : TEXCOORD6; // which sector of the draw float2 plane : TEXCOORD4; // the sector's flat frame (mow map) float2 air : TEXCOORD5; // x = sun through the clouds, y = gust passing }; Varyings vert(Attributes v) { float shells = _GcShells.x; uint sid = v.iid / (uint)shells; uint shell = v.iid - sid * (uint)shells; #if defined(GC_SECTOR_BUFFER) gcS = _GcSectorBuf[sid]; #endif // instance 0 is the top shell; a single shell is the solid floor under the strands float t = shells > 1 ? 1 - (float)shell / (shells - 1) : 0; float2 plane = v.positionOS.xz * GC_GRID.x; float3 up = normalize(GC_SECTOR_C.xyz * _GcPlanet.w + GC_SECTOR_E1.xyz * plane.x + GC_SECTOR_E2.xyz * plane.y); float3 t1, t2; GcTangents(up, t1, t2); // a grid cuts corners over bumps: each corner stands on the highest ground within half a cell of it, and a // little over, so no bump between corners comes up through the floor float hs = GC_GRID.y * 0.5 / _GcPlanet.w; float ground = max(GcHeight(up), max(max(GcHeight(normalize(up + t1 * hs)), GcHeight(normalize(up - t1 * hs))), max(GcHeight(normalize(up + t2 * hs)), GcHeight(normalize(up - t2 * hs))))); float3 root = _GcPlanet.xyz + up * (_GcPlanet.w + ground + 0.01 + GC_GRID.y * 0.012); float dist = length(root - _GcCam.xyz); float fade = 1 - smoothstep(_GcFade.x, _GcFade.y, dist); float patch = GcNoise(root * 0.045).r; // never taller than the crew's eyes: from inside, the shells would be seen edge-on float H = min(_GcHeight * lerp(0.85, 1.15, patch), _GcFurMax) * fade; // wind: rolling gusts that comb the whole pile float tm = _GcTime; float gust = GcNoise(root * 0.06 + float3(-0.8, 0.1, -0.45) * tm * 0.9).g * 0.8 + 0.2; float sway = sin(tm * 2.3 + dot(root, float3(0.7, 0.3, 0.4))) * 0.3; float bend = t * t * H * _Wind * (1.0 + _GcGust) * 0.35; float2 off = float2(gust * 0.9 + sway * 0.4, sway * 0.3) * bend; // the pile is combed in slowly turning swirls, so from above the strands read as brushed fur, not dots float ca = GcNoise(root * 0.11).b * 9.0 + GcNoise(root * 0.5).a * 1.5; off += float2(cos(ca), sin(ca)) * t * t * H * 0.32; // mowing spots pull the pile toward the cutter and up (sucked in) float lift = 0; for (int i = 0; i < (int)_GcVortexCount; i++) { float3 toV = root - _GcVortex[i].xyz; float2 d = float2(dot(toV, t1), dot(toV, t2)); float reach = _GcVortexReach[i]; float f = _GcVortex[i].w * exp(-dot(d, d) / max(0.2, reach * reach * 2.5)); float2 swirl = float2(-d.y, d.x); off += (-normalize(d + 1e-4) * 0.8 + normalize(swirl + 1e-4) * 0.5) * f * t * H * 0.6; lift += f * t * 0.2; } // people and vehicles part it for (int j = 0; j < (int)_GcPusherCount; j++) { float4 ps = _GcPushers[j]; if (ps.w <= 0) continue; float3 toP = root - ps.xyz; float2 d = float2(dot(toP, t1), dot(toP, t2)); float f = saturate(1 - length(d) / ps.w) * step(-dot(toP, up) - 0.6, H); off += normalize(d + 1e-4) * f * t * H * 0.9; lift -= f * t * 0.6; } float offLen = length(off); float rise = t * H * (1 + lift) - offLen * offLen / max(0.15, H) * 0.3 * t; float3 wpos = root + t1 * off.x + t2 * off.y + up * rise; Varyings o; o.positionWS = wpos; o.positionCS = TransformWorldToHClip(wpos); o.data = float4(t, H, patch, fade); o.root = root; o.plane = plane; o.fog = ComputeFogFactor(o.positionCS.z); o.air = float2(GcCloudSun(root, tm), gust); o.sid = sid; return o; } half4 frag(Varyings i, bool front : SV_IsFrontFace) : SV_Target { float t = i.data.x, H = i.data.y; float dist = length(i.root - _GcCam.xyz); // rings hand over through a dithered band (same noise on both sides, so every pixel picks exactly one) float ign = frac(52.9829189 * frac(dot(i.positionCS.xy, float2(0.06711056, 0.00583715)))); float e = (ign - 0.5) * _GcShells.w; if ((_GcShells.y > 0 && dist < _GcShells.y + e) || dist >= _GcShells.z + e) discard; // a sector's square reaches past its borders: each pixel belongs to one sector #if defined(GC_SECTOR_BUFFER) gcS = _GcSectorBuf[i.sid]; #endif float3 d = normalize(i.root - _GcPlanet.xyz); for (int b = 0; b < 6; b++) if (dot(d, GC_SECTOR_BORDER(b).xyz) > 0) discard; float2 mask = GcMask(i.plane); bool solid = t < 0.001; // the bottom of the pile: the floor everything stands on if (!solid && mask.g > 0.5) discard; if (_GcFade.z > 0.5 && mask.r < 0.5 && mask.g < 0.5) discard; // under pieces reads exactly like mown ground: no dirt patches float cut = saturate(max(mask.r, mask.g)); float top = 1 - cut; // mown means mown: nothing is left standing // the strands: tapered cones from the strand texture; far away the mips average them out, so the cut // threshold drifts down with the mip level to keep the pile its full height float2 fuv = float2(dot(d, _GcFurU.xyz), dot(d, _GcFurV.xyz)) / dot(d, _GcFurN.xyz) * _GcPlanet.w * _GcFurScale; float4 s = SAMPLE_TEXTURE2D(_GcFurTex, sampler_GcFurTex, fuv); float lod = saturate(CALCULATE_TEXTURE2D_LOD(_GcFurTex, sampler_GcFurTex, fuv) / 4.0); // dither the layer threshold by up to one shell step: equal steps otherwise show up as rings float need = (t + (ign - 0.5) / max(1, _GcShells.x - 1)) / max(0.02, top); if (t > top + 0.001) discard; if (t > 0.02 && s.r < need * lerp(1, 0.42, lod)) discard; // colour: dark roots, bright tips, big dry patches, streaks of hemp, pale fresh stubble // (two octaves of world noise keep the pile from reading as one flat colour once the mips average it) float mid = GcNoise(i.root * 0.33).r, fine = GcNoise(i.root * 1.9).g; float macro = GcNoise(i.root * 0.012).a * 0.45 + mid * 0.35 + fine * 0.2; float3 tip = lerp(_TipColor.rgb, _DryColor.rgb, saturate(i.data.z * 0.7 + macro * 0.3 + s.g * 0.2 - 0.42)); tip = lerp(tip, _HempColor.rgb, step(0.86, s.g) * 0.8); // straw stubble thinning out over the earth in loose patches float bare = smoothstep(0.4, 0.6, mid * 0.75 + fine * 0.25); tip = lerp(tip, lerp(_CutColor.rgb, _SoilColor.rgb, bare * 0.7) * lerp(0.86, 1.1, macro), cut); // far shells stand in for the tips of the strands, so they take the same bright colour float k = lerp(saturate(t / max(0.05, top)), 1, lod * step(1.5, _GcShells.x)); // the floor shows between the strands from up high: it's the shade under grass, not black if (solid) k = max(k, lerp(0.5, 0.42, cut)); // mown stubble: short straw tips over pale ground, so every trail reads clearly float3 root = lerp(_RootColor.rgb, _SoilColor.rgb, cut); float3 albedo = lerp(root, tip, pow(k, 0.7)) * lerp(0.85, 1.12, s.g) * lerp(0.84, 1.1, macro); albedo *= 1 + (i.air.y - 0.6) * 0.3 * k * (1 - cut); // a gust lays the grass over, and it flashes paler albedo *= lerp(lerp(0.38, 1, cut), 1, k); // the pile shades its own roots // a solid shell reads more saturated than lit strands with gaps: meet them at the ring seam albedo = lerp(albedo, dot(albedo, float3(0.3, 0.59, 0.11)).xxx, 0.16 * lod * step(1.5, _GcShells.x)); Light main = GetMainLight(TransformWorldToShadowCoord(i.positionWS)); float3 n = _GcCamUp.xyz; float3 v = normalize(GetCameraPositionWS() - i.positionWS); float ndl = saturate(dot(n, main.direction) * 0.55 + 0.45); float back = pow(saturate(dot(v, -main.direction)), 3) * 0.45 * k; float shadow = lerp(0.35, 1, main.shadowAttenuation) * i.air.x; float3 col = albedo * (main.color * (ndl + back) * shadow + SampleSH(float3(0, 1, 0)) * 0.9 + GcLamp(i.positionWS)); // velvet sheen at grazing angles (what makes a pile read as fur) float rim = pow(1 - saturate(dot(v, n)), 4); col += lerp(_TipColor.rgb, 1, 0.4) * rim * 0.22 * k * shadow; col *= GcContact(i.positionWS); col = MixFog(col, i.fog); return half4(col, 1); } ENDHLSL } } }