From d4234208039784a2d4d6989e2a3e432e304fd64b Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 6 Dec 2026 04:40:30 +0100 Subject: [PATCH] Cut 13d Pass 2 fixes: block small arms, canon xforms, charred cull, LOD1 shadows & pitch guard --- client/FoliageRenderer.cs | 68 +++++++++++++++---------- client/tree_impostor.gdshader | 4 +- src/RustToDust.Core/SionTreeField.cs | 18 ++++--- tests/RustToDust.Tests/SionTreeTests.cs | 39 ++++++++++++++ 4 files changed, 94 insertions(+), 35 deletions(-) diff --git a/client/FoliageRenderer.cs b/client/FoliageRenderer.cs index d3b4594..c66be47 100644 --- a/client/FoliageRenderer.cs +++ b/client/FoliageRenderer.cs @@ -14,11 +14,11 @@ namespace RustToDust.Client; /// public partial class FoliageRenderer : Node3D { - public const float Lod0MaxDistanceM = 38f; - public const float Lod1MaxDistanceM = 95f; - public const float Lod2MaxDistanceM = 260f; - public const float ImpostorMaxDistanceM = 1500f; - public const float StumpMaxDistanceM = 260f; + public const float Lod0MaxDistanceM = 28f; + public const float Lod1MaxDistanceM = 76f; + public const float Lod2MaxDistanceM = 230f; + public const float ImpostorMaxDistanceM = 1450f; + public const float StumpMaxDistanceM = 230f; private const float RebuildMoveThresholdSq = 8.0f * 8.0f; private const float RebuildTurnCosThreshold = 0.866f; // ~30 degrees private const float FrustumCullMinDistSq = 48.0f * 48.0f; @@ -60,6 +60,7 @@ public partial class FoliageRenderer : Node3D { public int ArchetypeIdx; public Transform3D WorldXform; + public Transform3D WorldXformCanon; public Vector3 Position; public Color CustomDataBase; // (0, phase, height, 1) } @@ -76,6 +77,7 @@ public partial class FoliageRenderer : Node3D private int _lastRenderVersion = -1; private Vector3 _lastCamPos = new(float.NaN, float.NaN, float.NaN); private Vector2 _lastCamFwdXz = new(0f, 1f); + private bool _lastCamConeActive; private bool _assetsLoaded; private bool _assetsLoadAttempted; @@ -105,6 +107,7 @@ public partial class FoliageRenderer : Node3D _lastRenderVersion = -1; _lastCamPos = new Vector3(float.NaN, float.NaN, float.NaN); _lastCamFwdXz = new Vector2(0f, 1f); + _lastCamConeActive = false; } public bool VerifyAllLodsHaveVertexColor(out string error) @@ -188,34 +191,39 @@ public partial class FoliageRenderer : Node3D int curVer = destruction?.RenderVersion ?? 0; Vector2 camFwdXz = new(0f, 1f); - bool hasCamDir = false; + bool useCamCone = false; var cam = GetViewport()?.GetCamera3D(); if (cam is not null) { Vector3 fwd = -cam.GlobalTransform.Basis.Z; - float lenSq = fwd.X * fwd.X + fwd.Z * fwd.Z; - if (lenSq > 1e-4f) + if (MathF.Abs(fwd.Y) <= 0.50f) { - float inv = 1f / MathF.Sqrt(lenSq); - camFwdXz = new Vector2(fwd.X * inv, fwd.Z * inv); - hasCamDir = true; + float lenSq = fwd.X * fwd.X + fwd.Z * fwd.Z; + if (lenSq > 1e-4f) + { + float inv = 1f / MathF.Sqrt(lenSq); + camFwdXz = new Vector2(fwd.X * inv, fwd.Z * inv); + useCamCone = true; + } } } bool movedFar = !_lastCamPos.IsFinite() || _lastCamPos.DistanceSquaredTo(camPos) >= RebuildMoveThresholdSq; - bool turnedFar = hasCamDir && (_lastCamFwdXz.X * camFwdXz.X + _lastCamFwdXz.Y * camFwdXz.Y) < RebuildTurnCosThreshold; - if (!movedFar && !turnedFar && curVer == _lastRenderVersion) + bool coneToggled = useCamCone != _lastCamConeActive; + bool turnedFar = useCamCone && (_lastCamFwdXz.X * camFwdXz.X + _lastCamFwdXz.Y * camFwdXz.Y) < RebuildTurnCosThreshold; + if (!movedFar && !coneToggled && !turnedFar && curVer == _lastRenderVersion) { return; } _lastCamPos = camPos; - if (hasCamDir) + _lastCamConeActive = useCamCone; + if (useCamCone) { _lastCamFwdXz = camFwdXz; } _lastRenderVersion = curVer; - RebuildBuckets(destruction, camPos, hasCamDir ? camFwdXz : null); + RebuildBuckets(destruction, camPos, useCamCone ? camFwdXz : null); } private void EnsureAssetsLoaded(string sionDir) @@ -319,11 +327,11 @@ public partial class FoliageRenderer : Node3D arch.BucketLod0 = CreateBucket($"{slug}_LOD0", arch.Lod0, castShadows: true, visibilityEndM: Lod0MaxDistanceM + 8f); if (variant == 0) { - arch.BucketLod1 = CreateBucket($"{slug}_LOD1", arch.Lod1, castShadows: false, visibilityEndM: Lod1MaxDistanceM + 10f); + arch.BucketLod1 = CreateBucket($"{slug}_LOD1", arch.Lod1, castShadows: true, visibilityEndM: Lod1MaxDistanceM + 10f); arch.BucketLod2 = CreateBucket($"{slug}_LOD2", arch.Lod2, castShadows: false, visibilityEndM: Lod2MaxDistanceM + 12f); arch.BucketImpostor = CreateBucket($"{slug}_Impostor", arch.ImpostorQuad, castShadows: false, visibilityEndM: ImpostorMaxDistanceM + 20f); arch.BucketStump = CreateBucket($"{slug}_Stump", arch.SnappedStump, castShadows: false, visibilityEndM: StumpMaxDistanceM + 10f); - arch.BucketCharred = CreateBucket($"{slug}_Charred", arch.CharredSkeleton, castShadows: true, visibilityEndM: 120f); + arch.BucketCharred = CreateBucket($"{slug}_Charred", arch.CharredSkeleton, castShadows: true, visibilityEndM: Lod2MaxDistanceM + 12f); } int idx = _archetypes.Count; @@ -394,9 +402,14 @@ public partial class FoliageRenderer : Node3D } var arch = _archetypes[archIdx]; + var canonArch = _archetypes[arch.CanonicalArchIdx]; + Basis yawBasis = Basis.FromEuler(new Vector3(0f, t.Yaw, 0f)); float scaleY = Math.Clamp(t.HeightM / arch.CanonicalHeightM, 0.35f, 2.2f); - float scaleXz = scaleY; - Basis rot = Basis.FromEuler(new Vector3(0f, t.Yaw, 0f)).Scaled(new Vector3(scaleXz, scaleY, scaleXz)); + float canonScaleY = Math.Clamp(t.HeightM / canonArch.CanonicalHeightM, 0.35f, 2.2f); + Basis rot = yawBasis.Scaled(new Vector3(scaleY, scaleY, scaleY)); + Basis canonRot = ReferenceEquals(arch, canonArch) + ? rot + : yawBasis.Scaled(new Vector3(canonScaleY, canonScaleY, canonScaleY)); Vector3 pos = new(t.X, t.Y, t.Z); float phase = ((t.TreeId * 0x9E3779B9u) & 0xFFFFu) / 65536f; @@ -404,6 +417,7 @@ public partial class FoliageRenderer : Node3D { ArchetypeIdx = archIdx, WorldXform = new Transform3D(rot, pos), + WorldXformCanon = new Transform3D(canonRot, pos), Position = pos, CustomDataBase = new Color(0f, phase, t.HeightM, 1f) }; @@ -501,12 +515,12 @@ public partial class FoliageRenderer : Node3D for (int a = 0; a < _archetypes.Count; a++) { var arch = _archetypes[a]; - c0 += CommitBucket(arch.BucketLod0, destruction, forceCharred: false); - if (arch.BucketLod1 is not null) c1 += CommitBucket(arch.BucketLod1, destruction, forceCharred: false); - if (arch.BucketLod2 is not null) c2 += CommitBucket(arch.BucketLod2, destruction, forceCharred: false); - if (arch.BucketImpostor is not null) cImp += CommitBucket(arch.BucketImpostor, destruction, forceCharred: false); - if (arch.BucketStump is not null) cStump += CommitBucket(arch.BucketStump, destruction, forceCharred: false); - if (arch.BucketCharred is not null) cChar += CommitBucket(arch.BucketCharred, destruction, forceCharred: true); + c0 += CommitBucket(arch.BucketLod0, destruction, forceCharred: false, useCanonXform: false); + if (arch.BucketLod1 is not null) c1 += CommitBucket(arch.BucketLod1, destruction, forceCharred: false, useCanonXform: true); + if (arch.BucketLod2 is not null) c2 += CommitBucket(arch.BucketLod2, destruction, forceCharred: false, useCanonXform: true); + if (arch.BucketImpostor is not null) cImp += CommitBucket(arch.BucketImpostor, destruction, forceCharred: false, useCanonXform: true); + if (arch.BucketStump is not null) cStump += CommitBucket(arch.BucketStump, destruction, forceCharred: false, useCanonXform: true); + if (arch.BucketCharred is not null) cChar += CommitBucket(arch.BucketCharred, destruction, forceCharred: true, useCanonXform: true); } VisibleLod0Count = c0; @@ -517,7 +531,7 @@ public partial class FoliageRenderer : Node3D VisibleCharredCount = cChar; } - private int CommitBucket(BucketState bucket, SionTreeDestructionState? destruction, bool forceCharred) + private int CommitBucket(BucketState bucket, SionTreeDestructionState? destruction, bool forceCharred, bool useCanonXform) { int count = bucket.Indices.Count; if (count == 0) @@ -542,7 +556,7 @@ public partial class FoliageRenderer : Node3D { int treeIdx = bucket.Indices[k]; ref readonly var pt = ref _precomputed[treeIdx]; - bucket.MultiMesh.SetInstanceTransform(k, pt.WorldXform); + bucket.MultiMesh.SetInstanceTransform(k, useCanonXform ? pt.WorldXformCanon : pt.WorldXform); Vector3 p = pt.Position; if (p.X < minX) minX = p.X; diff --git a/client/tree_impostor.gdshader b/client/tree_impostor.gdshader index 5bf00cd..59ca2d4 100644 --- a/client/tree_impostor.gdshader +++ b/client/tree_impostor.gdshader @@ -99,7 +99,9 @@ void vertex() { vec3 flow_local = (inverse(MODEL_MATRIX) * vec4(fs.rgb, 0.0)).xyz; local_flow = flow_local * ((0.04 + fs.a * 0.10) * in_flow * top_w); } - vec3 ambient_local = normalize((inverse(MODEL_MATRIX) * vec4(ambient_wind_dir, 0.0)).xyz); + vec3 amb_local_raw = (inverse(MODEL_MATRIX) * vec4(ambient_wind_dir, 0.0)).xyz; + float amb_len = length(amb_local_raw); + vec3 ambient_local = amb_len > 1e-5 ? amb_local_raw / amb_len : vec3(1.0, 0.0, 0.0); offset += ambient_local * (sway * mix(1.0, 0.5, in_flow)) + local_flow; vec3 vertex_to_camera = pivot_to_camera - offset; diff --git a/src/RustToDust.Core/SionTreeField.cs b/src/RustToDust.Core/SionTreeField.cs index 0853050..542e043 100644 --- a/src/RustToDust.Core/SionTreeField.cs +++ b/src/RustToDust.Core/SionTreeField.cs @@ -336,15 +336,22 @@ namespace RustToDust.Core continue; } + if (blastDamage < 25f || (t.TrunkRadiusM >= 0.50f && blastDamage < 45f)) + { + continue; + } + float dist = MathF.Sqrt(distSq); float shellDist = MathF.Max(0f, dist - t.TrunkRadiusM); float atten = MathF.Max(0f, 1f - shellDist / effectiveRadius); float impulse = blastDamage * atten; - if (impulse <= 0f && blastDamage < 25f) + if (impulse <= 0f) { continue; } - float snapThreshold = 35f + t.TrunkRadiusM * 180f; + float snapThreshold = t.TrunkRadiusM >= 0.50f + ? 85f + t.TrunkRadiusM * 240f + : 35f + t.TrunkRadiusM * 180f; float totalDmg = _treeDamage.TryGetValue(t.TreeId, out float prevDmg) ? prevDmg + impulse : impulse; @@ -357,11 +364,8 @@ namespace RustToDust.Core } else { - if (impulse > 0f) - { - _treeDamage[t.TreeId] = totalDmg; - } - if (cur == SionTreeState.Intact && blastDamage >= 25f) + _treeDamage[t.TreeId] = totalDmg; + if (cur == SionTreeState.Intact) { targetState = SionTreeState.Charred; } diff --git a/tests/RustToDust.Tests/SionTreeTests.cs b/tests/RustToDust.Tests/SionTreeTests.cs index 95e0dcc..0d5412b 100644 --- a/tests/RustToDust.Tests/SionTreeTests.cs +++ b/tests/RustToDust.Tests/SionTreeTests.cs @@ -339,6 +339,45 @@ namespace RustToDust.Tests Assert.Equal(SionTreeState.Charred, trees.GetState(med.TreeId)); } + [Fact] + public void SionTreeDestruction_SmallArmsCannotFellOrCharMediumOakOrMonumentalSequoia() + { + var sion = SionMapLoader.Load(); + var trees = new SionTreeDestructionState(sion.Trees); + + SionTreePlacement oak = default; + SionTreePlacement sequoia = default; + bool foundOak = false, foundSequoia = false; + foreach (var t in sion.Trees.Trees) + { + if (!foundOak && t.Species == SionTreeSpecies.QuercusPubescens && t.TrunkRadiusM >= 0.32f) + { + oak = t; + foundOak = true; + } + if (!foundSequoia && t.Species == SionTreeSpecies.Sequoiadendron) + { + sequoia = t; + foundSequoia = true; + } + if (foundOak && foundSequoia) break; + } + Assert.True(foundOak && foundSequoia); + + float mgDmg = Catalog.Mg762.WeaponDamage; // 12f (< 25f) + var oakHit = new Vector3(oak.X + oak.TrunkRadiusM, oak.Y + 1.0f, oak.Z); + var seqHit = new Vector3(sequoia.X + sequoia.TrunkRadiusM, sequoia.Y + 1.0f, sequoia.Z); + + for (int i = 0; i < 30; i++) + { + trees.ApplyBlast(oakHit, splashRadius: 2.5f, blastDamage: mgDmg); + trees.ApplyBlast(seqHit, splashRadius: 2.5f, blastDamage: mgDmg); + } + + Assert.Equal(SionTreeState.Intact, trees.GetState(oak.TreeId)); + Assert.Equal(SionTreeState.Intact, trees.GetState(sequoia.TreeId)); + } + [Fact] public void CultNet_RemoteClientSyncsTreeScarsInPersistentSnapshotBubbleAndRepair() {