A clean start for the shared repository: the Unity project (Assets, Packages, ProjectSettings), ArtSource, Docs and Tools as they stood at f3666f0 in the old local repository, with its generated prefabs and scene brought up to date. Images, audio, models and binaries go through Git LFS (.gitattributes). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
225 lines
11 KiB
GLSL
225 lines
11 KiB
GLSL
// Herbaria-7's meadow up close: every strand of the pile is its own long round "sausage". GrassField.cs draws a small
|
|
// camera-facing ribbon with RenderMeshPrimitives, one draw per sector, one instance per cell of the sector's flat frame;
|
|
// the root, height, tint and bend all come from SV_InstanceID and the cell, so there is no per-instance buffer. The ribbon is shaded as a cylinder with a rounded
|
|
// tip, and nothing is cut out per pixel (no discard), so early-z keeps the overdraw cheap. Mown cells keep nothing.
|
|
// Position stream: x = side (-1..1), y = along the strand (0..1).
|
|
Shader "GrassCutter/GrassStrand"
|
|
{
|
|
Properties
|
|
{
|
|
_RootColor ("Root", Color) = (0.05, 0.16, 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 strands", Color) = (0.2, 0.5, 0.2, 1)
|
|
_CutColor ("Fresh stub", Color) = (0.86, 0.82, 0.46, 1)
|
|
_Border ("Sector border", Color) = (0.98, 0.76, 0.26, 1)
|
|
_Wind ("Wind", Float) = 1
|
|
}
|
|
|
|
SubShader
|
|
{
|
|
Tags { "RenderType" = "Opaque" "Queue" = "Geometry+10" "RenderPipeline" = "UniversalPipeline" "IgnoreProjector" = "True" }
|
|
|
|
Pass
|
|
{
|
|
Name "StrandForward"
|
|
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
|
|
|
|
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
|
|
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
|
|
#include "Planet.hlsl"
|
|
|
|
CBUFFER_START(UnityPerMaterial)
|
|
float4 _RootColor, _TipColor, _DryColor, _HempColor, _CutColor, _Border;
|
|
float _Wind;
|
|
CBUFFER_END
|
|
|
|
// per draw (MaterialPropertyBlock)
|
|
float4 _GcGrid; // first cell x, first cell z, cell, cells per row
|
|
float4 _GcStrand; // inner radius, outer radius, seam band, strand width
|
|
// globals
|
|
float4 _GcCam, _GcCamFwd, _GcCamUp;
|
|
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, _GcFurMax;
|
|
|
|
struct Attributes
|
|
{
|
|
float3 positionOS : POSITION;
|
|
uint iid : SV_InstanceID;
|
|
};
|
|
|
|
struct Varyings
|
|
{
|
|
float4 positionCS : SV_POSITION;
|
|
float3 positionWS : TEXCOORD0;
|
|
float3 rightWS : TEXCOORD1;
|
|
float3 viewWS : TEXCOORD2;
|
|
float4 albedo : TEXCOORD3; // rgb, a = along the strand
|
|
float side : TEXCOORD4;
|
|
float fog : TEXCOORD5;
|
|
float sun : TEXCOORD6; // through the clouds
|
|
};
|
|
|
|
float4 Hash4(float2 p)
|
|
{
|
|
float4 p4 = frac(float4(p.xyxy) * float4(0.1031, 0.1030, 0.0973, 0.1099));
|
|
p4 += dot(p4, p4.wzxy + 33.33);
|
|
return frac((p4.xxyz + p4.yzzw) * p4.zywx);
|
|
}
|
|
|
|
Varyings Collapse()
|
|
{
|
|
Varyings o = (Varyings)0;
|
|
o.positionCS = float4(0, 0, -2, 1); // behind the near plane: clipped
|
|
return o;
|
|
}
|
|
|
|
Varyings vert(Attributes v)
|
|
{
|
|
uint n = (uint)_GcGrid.w;
|
|
float cell = _GcGrid.z;
|
|
uint ix = v.iid % n, iz = v.iid / n;
|
|
float2 cid = _GcGrid.xy + float2(ix, iz);
|
|
float4 h = Hash4(cid + _GcSalt.xy);
|
|
float2 plane = (cid + h.xy) * cell;
|
|
float3 up;
|
|
float edge;
|
|
if (!GcInSector(plane, up, edge)) return Collapse();
|
|
float groundH = GcHeight(up);
|
|
float3 root = _GcPlanet.xyz + up * (_GcPlanet.w + groundH);
|
|
|
|
// only this ring, handing over strand by strand; strands behind the camera are skipped right here
|
|
float3 toRoot = root - _GcCam.xyz;
|
|
float dist = length(toRoot);
|
|
float e = (h.w - 0.5) * _GcStrand.z;
|
|
if ((_GcStrand.x > 0 && dist < _GcStrand.x + e) || dist >= _GcStrand.y + e) return Collapse();
|
|
if (dist > 4 && dot(toRoot, _GcCamFwd.xyz) < -2) return Collapse();
|
|
|
|
float2 mask = GcMask(plane);
|
|
if (mask.g > 0.5 || mask.r > 0.6) return Collapse();
|
|
float cut = saturate(mask.r);
|
|
|
|
float4 big = GcNoise(root * 0.045);
|
|
float patch = big.r;
|
|
float H = min(_GcHeight * lerp(0.85, 1.15, patch), _GcFurMax);
|
|
// grass beside a cut stands lower and doesn't lean over it: even a strip as narrow as the scissors' shows
|
|
float near = saturate(GcMaskNear(plane) * 2.0);
|
|
float len = H * lerp(0.74, 1.12, h.z) * lerp(1, 0.5, near);
|
|
float full = len;
|
|
len *= 1 - cut; // mown means gone, no stubble
|
|
if (len < 0.02) return Collapse();
|
|
|
|
float t = v.positionOS.y, s = v.positionOS.x;
|
|
float3 t1, t2;
|
|
GcTangents(up, t1, t2);
|
|
|
|
// wind: rolling gusts
|
|
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)) + h.y * 6.0) * 0.3;
|
|
float bend = t * t * len * _Wind * (1.0 + _GcGust) * 0.35;
|
|
float2 off = float2(gust * 0.9 + sway * 0.4, sway * 0.3) * bend;
|
|
|
|
// combed in slow swirls, every strand with a bit of its own lean
|
|
float ca = GcNoise(root * 0.11).b * 9.0 + (h.x - 0.5) * 2.2;
|
|
off += float2(cos(ca), sin(ca)) * t * t * len * lerp(0.25, 0.5, h.y) * (1 - near * 0.7);
|
|
|
|
// mowing spots pull the tips 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 * len * 0.6;
|
|
lift += f * t * 0.2;
|
|
}
|
|
// people and vehicles part them
|
|
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, len);
|
|
off += normalize(d + 1e-4) * f * t * len * 0.9;
|
|
lift -= f * t * 0.6;
|
|
}
|
|
float offLen = length(off);
|
|
float rise = t * len * (1 + lift) - offLen * offLen / max(0.15, len) * 0.3 * t;
|
|
float3 center = root + t1 * off.x + t2 * off.y + up * rise;
|
|
|
|
// the ribbon turns to the camera around the strand's own direction, and rounds off at the tip
|
|
float3 tangent = normalize((t1 * off.x + t2 * off.y) * 2.0 / max(t, 0.1) + up * len);
|
|
float3 view = normalize(GetCameraPositionWS() - center);
|
|
float3 right = cross(tangent, view);
|
|
right /= max(1e-4, length(right));
|
|
float tipK = saturate((t - 0.86) / 0.14);
|
|
float width = _GcStrand.w * lerp(0.8, 1.2, h.w) * sqrt(saturate(1 - tipK * tipK));
|
|
float3 wpos = center + right * s * width * 0.5;
|
|
|
|
Varyings o;
|
|
o.positionWS = wpos;
|
|
o.positionCS = TransformWorldToHClip(wpos);
|
|
o.rightWS = right;
|
|
o.viewWS = cross(right, tangent);
|
|
o.side = s;
|
|
|
|
// colour: dark roots, bright tips, big dry patches, strands of hemp, pale fresh stubs, gold along the borders
|
|
float mid = GcNoise(root * 0.33).r;
|
|
float macro = big.a * 0.55 + mid * 0.45;
|
|
float3 tip = lerp(_TipColor.rgb, _DryColor.rgb, saturate(patch * 0.7 + macro * 0.3 + h.w * 0.2 - 0.42));
|
|
tip = lerp(tip, _HempColor.rgb, step(0.87, h.y) * 0.85);
|
|
tip = lerp(tip, _CutColor.rgb * lerp(1, float3(0.8, 0.73, 0.72), smoothstep(0.4, 0.6, mid)), cut);
|
|
tip = lerp(tip, _Border.rgb, 1 - smoothstep(0.1, 0.24, edge));
|
|
float k = saturate(t * len / max(0.05, full) + cut * t); // height in the pile, not along this strand
|
|
float3 base = lerp(_RootColor.rgb, _CutColor.rgb * 0.7, cut);
|
|
float3 col = lerp(base, tip, pow(k, 0.75)) * lerp(0.84, 1.12, h.z) * lerp(0.86, 1.08, macro);
|
|
col *= lerp(lerp(0.45, 0.95, cut), 1, k); // the pile shades its own roots
|
|
col *= 1 + (gust - 0.6) * 0.3 * k * (1 - cut); // a gust lays the grass over, and it flashes paler
|
|
o.albedo = float4(col, k);
|
|
o.fog = ComputeFogFactor(o.positionCS.z);
|
|
o.sun = GcCloudSun(root, tm);
|
|
return o;
|
|
}
|
|
|
|
half4 frag(Varyings i) : SV_Target
|
|
{
|
|
// a flat ribbon lit like a round tube
|
|
float s = i.side;
|
|
float3 n = normalize(i.rightWS * s + i.viewWS * sqrt(saturate(1 - s * s)) + _GcCamUp.xyz * 0.35);
|
|
float k = i.albedo.a;
|
|
Light main = GetMainLight(TransformWorldToShadowCoord(i.positionWS));
|
|
float3 v = normalize(GetCameraPositionWS() - i.positionWS);
|
|
float ndl = saturate(dot(n, main.direction) * 0.6 + 0.4);
|
|
float back = pow(saturate(dot(v, -main.direction)), 3) * 0.45 * k;
|
|
float shadow = lerp(0.35, 1, main.shadowAttenuation) * i.sun;
|
|
float3 col = i.albedo.rgb * (main.color * (ndl + back) * shadow + SampleSH(float3(0, 1, 0)) * 0.9 + GcLamp(i.positionWS));
|
|
// the soft shine down the middle of a sausage
|
|
col += main.color * pow(saturate(dot(n, normalize(main.direction + v))), 18) * 0.14 * k * shadow;
|
|
col *= GcContact(i.positionWS);
|
|
col = MixFog(col, i.fog);
|
|
return half4(col, 1);
|
|
}
|
|
ENDHLSL
|
|
}
|
|
}
|
|
}
|