Skip to content

Networktransform position scale - #1104

Open
jimdroberts wants to merge 2 commits into
FirstGearGames:mainfrom
jimdroberts:feat/networktransform-position-scale
Open

jimdroberts wants to merge 2 commits into
FirstGearGames:mainfrom
jimdroberts:feat/networktransform-position-scale

Conversation

@jimdroberts

Copy link
Copy Markdown
Contributor

Problem

NetworkTransform packs each position axis as an Int16 of value * 100. Range and resolution are the same number: an axis costs 2 bytes at 0.01 resolution within ±327.66 units, and a 4-byte float anywhere beyond that. On a map larger than a 655-unit box, most moving objects are outside the packed range most of the time, so position compression rarely applies.

The only knob is the hardcoded multiplier, and lowering it trades resolution for range. A production MMO project tried 10 (a 10 cm grid, ±3,276 m). A walking NPC moves 5 cm per tick (1.5 m/s at 30 Hz), so the receiver got alternating 0 cm and 10 cm steps, which rendered as stalls and double-speed hops. Only more bits give both range and resolution.

Cause

Runtime/Generated/Component/NetworkTransform/NetworkTransform.cs on main:

  • L1216 float multiplier = 100f; and L1219 float maxValue = short.MaxValue - 1; are locals in SerializeChanged. The position writes (L1238, L1259, L1280) are writer.WriteInt16((short)compressed), and anything with Math.Abs(compressed) > maxValue is written as a float.
  • DeserializePacket reads with the same constant: reader.ReadInt16() / 100f (L1429, L1436, L1443).
  • Scale compression uses the same two locals (L1343-1385, L1472-1486), so position cannot be changed without also changing scale.

Fix

Two commits.

  1. With FISHNET_NETWORKTRANSFORM_POSITION_PACKING defined, NetworkTransform gets two serialized fields, shown under Misc after Packing:

    • Position Packing Bits: Sixteen (default, the stock 2 bytes) or TwentyFour (3 bytes).
    • Position Compression Scale: default 100. A value that is not positive falls back to 100, so a bad asset value cannot divide by zero on read.

    Only the three position write sites and three read sites change. They go through WritePackedPosition / ReadPackedPosition. The fallback is main's: Math.Abs(compressed) <= positionMaxValue, otherwise a float. NaN and infinity still fall back, and nested transforms and AutoPackType.Unpacked still send floats. The X2/Y2/Z2 flags still mean "packed" and X4/Y4/Z4 "float", so no flag bits are added. TwentyFour writes a ushort and a byte and sign-extends on read. It rounds instead of truncating, so the error is at most half a step with no bias toward the origin. Its maximum is 2^23 - 2, keeping the same one-below-max margin as short.MaxValue - 1. Sixteen keeps the stock truncation, so the default values write the stock bytes even with the define on. Scale compression keeps its own Int16 at 100.

    Without the define there are no new fields and the inspector is unchanged. The helpers hold main's exact expressions (writer.WriteInt16((short)compressed), reader.ReadInt16() / 100f), and the position multiplier and maximum are main's locals, so the bytes are identical to main.

    The receiver decodes with its own copy of the component's fields, and nothing describing them is added to the packet. Server and clients must have the same values, which they do when both use the same prefab or scene object.

  2. Adds Tools/Fish-Networking/Beta/Enable|Disable NetworkTransform Position Packing. The define changes the wire format, so it is set for every build target group and for the dedicated server target. This uses DeveloperMenu.RemoveOrAddDefineForAllBuildTargets, copied unchanged from Feat/delta prediction beta #1095 (Delta Prediction beta). After this commit, ConfigurationEditor.cs is byte-identical to that branch, so the two PRs merge cleanly in either order. If Feat/delta prediction beta #1095 merges first, this hunk becomes empty.

This PR is independent of feat/networktransform-skip-owner-discarded, which also edits NetworkTransform.cs (different lines). git merge-tree merges the two cleanly with each other and with every open PR branch of mine, including #1095 and feat/observer-send-filter.

Evidence

Compile. FishNet.Runtime, GameKit.Dependencies, FishNet.Codegen.Cecil and Unity.FishNet.Codegen compile with Unity 6000.4's Roslyn with the define off and on, with 0 errors and the same warnings as unmodified main. Also with no new diagnostics: this branch merged with feat/networktransform-skip-owner-discarded (define on), and merged with #1095 (both defines on). With the define on, the compiled runtime contains PositionPackingBits; with it off, it does not.

Harness outside Unity (evidence/networktransform-position-scale, run.sh). NetworkTransform cannot be constructed outside Unity, so the harness mirrors the range check and the two helpers line for line, running them through FishNet's real Writer/Reader. 200,000 uniform positions per row:

config               range (±)    step     B/axis  max |error|   mean error (negative = toward origin)
16-bit @100 (main)      327.66    1 cm     2       1.000 cm      -0.499 cm   (truncation)
16-bit @10             3276.60   10 cm     2       9.999 cm      -4.996 cm
24-bit @100           83886.06    1 cm     3       0.507 cm within ±1 km, 0.537 cm within ±10 km
24-bit @1000           8388.61    1 mm     3       0.098 cm       0.017 cm
float                                      4

Beyond ±40 km, the 24-bit @100 maximum error rises to 0.78 cm. At that distance the float position itself has a 3.9 to 7.8 mm step, and the product and quotient are float operations on both peers.

Position bytes per update for x and z (y unchanged), with positions uniform over a square map:

map         16-bit @100 (main)   16-bit @10   24-bit @100
±250 m      4.00 B               4.00 B       6.00 B
±1000 m     6.70 B               4.00 B       6.00 B
±2500 m     7.47 B               4.00 B       6.00 B
±5000 m     7.74 B               5.38 B       6.00 B
  • Walking at 5 cm per tick from x = 1000 m, the decoded per-tick steps are 0.0, 10.0 cm at 16-bit @10 and 5.0 cm at 24-bit @100.
  • Boundaries: a value exactly at the range edge packs. One step beyond it, the value falls back to a 4-byte float and decodes exactly. NaN and infinity fall back as on main. Negative values round-trip through sign extension down to -83,886.06.

The production project has run 24-bit packing at scale 100 on a FishNet 4.6.12 fork since early September. There has been no play-mode run on this branch.

Behavior change

None without the define; the wire format and the inspector are unchanged.

With the define on:

  • Default field values still write the stock bytes.
  • TwentyFour costs 1 byte more per axis inside the 16-bit range and saves 1 byte per axis beyond it.
  • Server and clients must be built with the same define and use the same field values. A mismatch misreads positions. There is no runtime check, as with the other wire-format betas.

NetworkTransform packs each position axis as an Int16 of value * 100, so an
axis is 2 bytes at 1 cm only within +/-327.66 units and a 4-byte float beyond.
With FISHNET_NETWORKTRANSFORM_POSITION_PACKING defined, a NetworkTransform
gets Position Packing Bits (Sixteen or TwentyFour) and Position Compression
Scale (default 100). TwentyFour writes 3 bytes per axis, rounded, giving 1 cm
within +/-83,886 units at the default scale. Out-of-range values still fall
back to floats. Scale compression keeps its own Int16 at 100. The defaults
write the stock bytes, and without the define nothing changes.
…d target

Tools/Fish-Networking/Beta/Enable|Disable NetworkTransform Position Packing
toggles FISHNET_NETWORKTRANSFORM_POSITION_PACKING. The define changes what
NetworkTransform writes, so it is set for every build target group and the
dedicated server target through DeveloperMenu.RemoveOrAddDefineForAllBuildTargets,
copied unchanged from the Delta Prediction beta so the two merge cleanly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant