Repository navigation
Networktransform position scale - #1104
Open
jimdroberts wants to merge 2 commits into
Open
jimdroberts wants to merge 2 commits into
jimdroberts wants to merge 2 commits into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
NetworkTransformpacks each position axis as an Int16 ofvalue * 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.cson main:float multiplier = 100f;and L1219float maxValue = short.MaxValue - 1;are locals inSerializeChanged. The position writes (L1238, L1259, L1280) arewriter.WriteInt16((short)compressed), and anything withMath.Abs(compressed) > maxValueis written as a float.DeserializePacketreads with the same constant:reader.ReadInt16() / 100f(L1429, L1436, L1443).Fix
Two commits.
With
FISHNET_NETWORKTRANSFORM_POSITION_PACKINGdefined,NetworkTransformgets two serialized fields, shown under Misc after Packing:Sixteen(default, the stock 2 bytes) orTwentyFour(3 bytes).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 andAutoPackType.Unpackedstill send floats. TheX2/Y2/Z2flags still mean "packed" andX4/Y4/Z4"float", so no flag bits are added.TwentyFourwrites aushortand abyteand 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 asshort.MaxValue - 1.Sixteenkeeps 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.
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 usesDeveloperMenu.RemoveOrAddDefineForAllBuildTargets, copied unchanged from Feat/delta prediction beta #1095 (Delta Prediction beta). After this commit,ConfigurationEditor.csis 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-treemerges 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).NetworkTransformcannot be constructed outside Unity, so the harness mirrors the range check and the two helpers line for line, running them through FishNet's realWriter/Reader. 200,000 uniform positions per row: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:
0.0, 10.0 cmat 16-bit @10 and5.0 cmat 24-bit @100.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:
TwentyFourcosts 1 byte more per axis inside the 16-bit range and saves 1 byte per axis beyond it.