// 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]; 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 * _GcSalt.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 * _GcSalt.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(_GcC.xyz * _GcPlanet.w + _GcE1.xyz * plane.x + _GcE2.xyz * plane.y); edge = 1e5; for (int k = 0; k < 6; k++) edge = min(edge, -dot(dir, _GcBorders[k].xyz)); return edge >= 0; } #endif