Grass Cutters Together: the project as of moon-sweepers f3666f0
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>
This commit is contained in:
63
Assets/Scripts/World/ContactShade.cs
Normal file
63
Assets/Scripts/World/ContactShade.cs
Normal file
@ -0,0 +1,63 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace GrassCutter
|
||||
{
|
||||
/// <summary>
|
||||
/// The dark where a machine stands: a soft patch under each wheel and a wider one under the body, on the soil and the
|
||||
/// grass (Planet.hlsl, GcContact). The sun's own shadow falls wherever the sun puts it, often well off to one side, and
|
||||
/// a machine with lit ground right under its tyres reads as floating, most of all from a low camera.
|
||||
/// </summary>
|
||||
public static class ContactShade
|
||||
{
|
||||
public const int Max = 24; // matches _GcContacts in Planet.hlsl
|
||||
const float Near = 90f; // machines further off than this get none
|
||||
const int Machines = 5;
|
||||
|
||||
static readonly Vector4[] s_pts = new Vector4[Max];
|
||||
static readonly List<Vehicle> s_near = new List<Vehicle>();
|
||||
static readonly int PtsId = Shader.PropertyToID("_GcContacts"), CountId = Shader.PropertyToID("_GcContactCount");
|
||||
|
||||
/// <summary>Once a frame, after the camera has moved.</summary>
|
||||
public static void Publish(Vector3 cam)
|
||||
{
|
||||
s_near.Clear();
|
||||
foreach (var v in Vehicle.All)
|
||||
if (v && v.isActiveAndEnabled && v.wheels != null && v.wheels.Length > 0 && (v.transform.position - cam).sqrMagnitude < Near * Near)
|
||||
s_near.Add(v);
|
||||
s_near.Sort((a, b) => (a.transform.position - cam).sqrMagnitude.CompareTo((b.transform.position - cam).sqrMagnitude));
|
||||
|
||||
int n = 0;
|
||||
for (int m = 0; m < s_near.Count && m < Machines; m++)
|
||||
{
|
||||
var v = s_near[m];
|
||||
Vector3 sum = Vector3.zero;
|
||||
int on = 0, start = n;
|
||||
for (int k = 0; k < v.wheels.Length; k++)
|
||||
{
|
||||
var w = v.wheels[k];
|
||||
if (!w || n >= Max - 1) continue;
|
||||
float radius = v.WheelRadius != null && k < v.WheelRadius.Length && v.WheelRadius[k] > 0f ? v.WheelRadius[k] : Vehicle.TyreRadius(w);
|
||||
var up = Planet.Up(w.position);
|
||||
var ground = Planet.OnSurface(up);
|
||||
// lifts off the ground (a jump, a tip): the patch thins out and is gone a metre up
|
||||
float lift = Planet.Altitude(w.position) - radius;
|
||||
float fade = 1f - Mathf.Clamp01((lift - 0.15f) / 0.9f);
|
||||
if (fade <= 0f) continue;
|
||||
s_pts[n++] = new Vector4(ground.x, ground.y, ground.z, radius * 1.3f * fade);
|
||||
sum += ground;
|
||||
on++;
|
||||
}
|
||||
if (on < 2 || n >= Max) continue;
|
||||
// the body: one wide, soft patch over the wheelbase
|
||||
var mid = sum / on;
|
||||
float reach = 0f;
|
||||
for (int i = start; i < n; i++) reach = Mathf.Max(reach, Vector3.Distance(mid, s_pts[i]));
|
||||
s_pts[n++] = new Vector4(mid.x, mid.y, mid.z, reach * 1.15f);
|
||||
}
|
||||
for (int i = n; i < Max; i++) s_pts[i] = Vector4.zero;
|
||||
Shader.SetGlobalVectorArray(PtsId, s_pts);
|
||||
Shader.SetGlobalFloat(CountId, n);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user