// GPU meadow for Herbaria-7. Drawn by GrassField.cs with RenderMeshPrimitives: every instance derives its world cell, // jitter, size and colour from SV_InstanceID, so there is no per-instance buffer. _Plant = 1 draws hemp (alpha-cut leaf // cards, albedo from the leaf texture) instead of blade tufts. Shader "GrassCutter/Grass" { Properties { _RootColor ("Root", Color) = (0.07, 0.2, 0.05, 1) _TipColor ("Tip", Color) = (0.45, 0.78, 0.2, 1) _DryColor ("Dry tip", Color) = (0.86, 0.8, 0.36, 1) _CutColor ("Fresh cut", Color) = (0.62, 0.74, 0.25, 1) _MainTex ("Leaf (rgb albedo, a cutout)", 2D) = "white" {} _Plant ("Plant mode", Float) = 0 _PlantScale ("Plant height vs grass", Float) = 1.15 _Metres ("Plant height in metres, 0 goes by the grass", Float) = 0 _Tint ("Plant tint", Color) = (1, 1, 1, 1) _Patchy ("Grow in patches", Float) = 0 _Wind ("Wind", Float) = 1 _Translucency ("Translucency", Float) = 0.5 } SubShader { Tags { "RenderType" = "Opaque" "Queue" = "AlphaTest" "RenderPipeline" = "UniversalPipeline" "IgnoreProjector" = "True" } Pass { Name "GrassForward" 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, _CutColor, _Tint; float4 _MainTex_ST; float _Plant, _PlantScale, _Metres, _Patchy, _Wind, _Translucency; CBUFFER_END TEXTURE2D(_MainTex); SAMPLER(sampler_MainTex); // per draw (MaterialPropertyBlock) float4 _GcGrid; // first cell x, first cell z, cell, cells per row float4 _GcFade; // fade start, fade end, inner skip radius, density // globals float4 _GcCam; 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; struct Attributes { float3 positionOS : POSITION; float3 normalOS : NORMAL; float4 color : COLOR; float2 uv : TEXCOORD0; uint iid : SV_InstanceID; }; struct Varyings { float4 positionCS : SV_POSITION; float3 positionWS : TEXCOORD0; float3 normalWS : TEXCOORD1; float4 albedo : TEXCOORD2; // rgb, a = along-blade (for translucency) float2 uv : TEXCOORD3; float fog : TEXCOORD4; float sun : TEXCOORD5; // 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 + (_Plant > 0.5 ? 71.3 : 0)); if (h.w > _GcFade.w) return Collapse(); float2 plane = (cid + h.xy) * cell; float3 up; float edge; if (!GcInSector(plane, up, edge)) return Collapse(); float3 root = GcSoil(up); if (_Patchy > 0.5 && GcNoise(root * 0.035).b < 0.52) return Collapse(); float dist = length(root - _GcCam.xyz); if (dist > _GcFade.y || dist < _GcFade.z - 6) return Collapse(); float fade = 1 - smoothstep(_GcFade.x, _GcFade.y, dist); if (_GcFade.z > 0) fade *= smoothstep(_GcFade.z - 6, _GcFade.z + 5, dist); float2 mask = GcMask(plane); if (mask.g > 0.5) return Collapse(); float cut = saturate(mask.r); // big soft patches of taller / shorter, greener / drier grass float patch = GcNoise(root * 0.045).r; float H = _GcHeight * lerp(0.72, 1.28, h.z) * lerp(0.8, 1.15, patch); float stubble = lerp(1, 0.09, cut); if (_Plant > 0.5) { H = _Metres > 0 ? _Metres * lerp(0.86, 1.14, h.z) : (_GcHeight * _PlantScale + 0.35) * lerp(0.8, 1.2, h.z); stubble = lerp(1, 0.06, smoothstep(0.15, 0.6, cut)); } H *= stubble * fade; if (H < 0.02) return Collapse(); // rotate + place the clump float3 t1, t2; GcTangents(up, t1, t2); float yaw = h.z * 6.2831853 + h.x * 3.0; float s, c; sincos(yaw, s, c); float3 p = v.positionOS; float widthMul = _Plant > 0.5 ? H : lerp(0.9, 1.4, h.y) * (1 + (1 - fade) * 1.5); float2 local = float2(p.x * c - p.z * s, p.x * s + p.z * c) * widthMul; float along = _Plant > 0.5 ? saturate(p.y) : v.color.r; // wind: rolling gusts float t = _GcTime; float gust = GcNoise(root * 0.06 + float3(-0.8, 0.1, -0.45) * t * 0.9).g * 0.8 + 0.2; float sway = sin(t * 2.3 + dot(root, float3(0.7, 0.3, 0.4)) + h.y * 6.0) * 0.35; float bendK = along * along * H * _Wind * (1.0 + _GcGust) * (_Plant > 0.5 ? 0.12 : 0.34); float2 off = float2(gust * 0.9 + sway * 0.4, sway * 0.3) * bendK; // mowing spots: tips get pulled 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 r2 = dot(d, d) / max(0.2, reach * reach * 2.5); float f = _GcVortex[i].w * exp(-r2); float2 swirl = float2(-d.y, d.x); off += (-normalize(d + 1e-4) * 0.8 + normalize(swirl + 1e-4) * 0.5) * f * along * H * 0.7; lift += f * along * 0.25; off += sin(t * 31.0 + h.x * 20.0) * f * 0.05 * along; } // people and vehicles push the grass aside for (int j = 0; j < (int)_GcPusherCount; j++) { float4 ps = _GcPushers[j]; float r = ps.w; if (r <= 0) continue; float3 toP = root - ps.xyz; float2 d = float2(dot(toP, t1), dot(toP, t2)); float f = saturate(1 - length(d) / r) * step(-dot(toP, up) - 0.6, H); off += normalize(d + 1e-4) * f * along * H * 0.9; lift -= f * along * 0.35; } float offLen = length(off); float2 flat_ = local + off; float3 wpos = root + t1 * flat_.x + t2 * flat_.y + up * (p.y * H + lift * H - offLen * offLen / max(0.3, H) * 0.35 * along); Varyings o; o.positionWS = wpos; o.positionCS = TransformWorldToHClip(wpos); float3 nrm = v.normalOS; nrm = t1 * (nrm.x * c - nrm.z * s) + up * nrm.y + t2 * (nrm.x * s + nrm.z * c); o.normalWS = normalize(lerp(nrm, up, 0.35)); float3 col; if (_Plant > 0.5) { // hemp: leaf texture provides colour; a touch of per-plant hue col = lerp(float3(1, 1, 1), float3(0.92, 1.05, 0.85), h.y) * lerp(0.85, 1.1, h.z) * _Tint.rgb; // wildflowers: every patch has its own petals if (_Patchy > 0.5 && v.uv.x > 0.5 && v.uv.y > 0.5) { float pick = frac(GcNoise(root * 0.018).g * 9.0); col = pick < 0.34 ? float3(1, 1, 0.96) : pick < 0.6 ? float3(1, 0.82, 0.16) : pick < 0.82 ? float3(0.66, 0.46, 0.98) : float3(1, 0.46, 0.66); } } else { float3 tip = lerp(_TipColor.rgb, _DryColor.rgb, saturate(patch * 0.55 + h.y * 0.25 - 0.15)); tip = lerp(tip, _CutColor.rgb, cut); col = lerp(_RootColor.rgb, tip, pow(along, 0.8)) * lerp(0.82, 1.12, h.w); col *= lerp(0.55, 1, saturate(along * 1.6)); // root occlusion } o.albedo = float4(col, along); o.uv = v.uv; o.fog = ComputeFogFactor(o.positionCS.z); o.sun = GcCloudSun(root, t); return o; } half4 frag(Varyings i, bool front : SV_IsFrontFace) : SV_Target { float3 albedo = i.albedo.rgb; if (_Plant > 0.5) { half4 tex = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv); clip(tex.a - 0.5); albedo *= tex.rgb; } float3 n = normalize(i.normalWS) * (front ? 1 : -1); 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) * _Translucency * i.albedo.a; float shadow = lerp(0.35, 1, main.shadowAttenuation) * i.sun; float3 col = albedo * (main.color * (ndl + back) * shadow + SampleSH(float3(0, 1, 0)) * 0.9 + GcLamp(i.positionWS)); // a soft sheen on the tips col += main.color * pow(saturate(dot(n, normalize(main.direction + v))), 24) * 0.08 * i.albedo.a * shadow; col *= GcContact(i.positionWS); col = MixFog(col, i.fog); return half4(col, 1); } ENDHLSL } } }