Skip to content

Commit ab70ecc

Browse files
authored
FIX: Fixed incorrect default state for axes on some controllers. (#870)
1 parent 23963cc commit ab70ecc

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

Packages/com.unity.inputsystem/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ however, it has to be formatted properly to pass verification tests.
1212
### Fixed
1313

1414
-Will now close Input Action Asset Editor windows from previous sessions when the corresponding action was deleted.
15+
- Fixed incorrect default state for axes on some controllers.
1516

1617
#### Actions
1718

Packages/com.unity.inputsystem/InputSystem/Controls/AxisControl.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,16 @@ public AxisControl()
209209
m_StateBlock.format = InputStateBlock.FormatFloat;
210210
}
211211

212+
protected override void FinishSetup()
213+
{
214+
base.FinishSetup();
215+
216+
// if we don't have any default state, and we are using normalizeZero, then the default value
217+
// should not be zero. Generate it from normalizeZero.
218+
if (!hasDefaultState && normalize && Mathf.Abs(normalizeZero) > Mathf.Epsilon)
219+
m_DefaultState = stateBlock.FloatToPrimitiveValue(normalizeZero);
220+
}
221+
212222
/// <inheritdoc />
213223
public override unsafe float ReadUnprocessedValueFromState(void* statePtr)
214224
{

Packages/com.unity.inputsystem/InputSystem/State/InputStateBlock.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,74 @@ public void WriteFloat(void* statePtr, float value)
502502
}
503503
}
504504

505+
internal PrimitiveValue FloatToPrimitiveValue(float value)
506+
{
507+
if (format == FormatFloat)
508+
{
509+
Debug.Assert(sizeInBits == 32, "FLT state must have sizeInBits=32");
510+
Debug.Assert(bitOffset == 0, "FLT state must be byte-aligned");
511+
return value;
512+
}
513+
else if (format == FormatBit)
514+
{
515+
if (sizeInBits == 1)
516+
{
517+
return value >= 0.5f;
518+
}
519+
else
520+
{
521+
var maxValue = (1 << (int)sizeInBits) - 1;
522+
return (int)(value * maxValue);
523+
}
524+
}
525+
else if (format == FormatInt)
526+
{
527+
Debug.Assert(sizeInBits == 32, "INT state must have sizeInBits=32");
528+
Debug.Assert(bitOffset == 0, "INT state must be byte-aligned");
529+
return (int)(value * 2147483647.0f);
530+
}
531+
else if (format == FormatUInt)
532+
{
533+
Debug.Assert(sizeInBits == 32, "UINT state must have sizeInBits=32");
534+
Debug.Assert(bitOffset == 0, "UINT state must be byte-aligned");
535+
return (uint)(value * 4294967295.0f);
536+
}
537+
else if (format == FormatShort)
538+
{
539+
Debug.Assert(sizeInBits == 16, "SHRT state must have sizeInBits=16");
540+
Debug.Assert(bitOffset == 0, "SHRT state must be byte-aligned");
541+
return (short)(value * 32768.0f);
542+
}
543+
else if (format == FormatUShort)
544+
{
545+
Debug.Assert(sizeInBits == 16, "USHT state must have sizeInBits=16");
546+
Debug.Assert(bitOffset == 0, "USHT state must be byte-aligned");
547+
return (ushort)(value * 65535.0f);
548+
}
549+
else if (format == FormatByte)
550+
{
551+
Debug.Assert(sizeInBits == 8, "BYTE state must have sizeInBits=8");
552+
Debug.Assert(bitOffset == 0, "BYTE state must be byte-aligned");
553+
return (byte)(value * 255.0f);
554+
}
555+
else if (format == FormatSByte)
556+
{
557+
Debug.Assert(sizeInBits == 8, "SBYT state must have sizeInBits=8");
558+
Debug.Assert(bitOffset == 0, "SBYT state must be byte-aligned");
559+
return (sbyte)(value * 128.0f);
560+
}
561+
else if (format == FormatDouble)
562+
{
563+
Debug.Assert(sizeInBits == 64, "DBL state must have sizeInBits=64");
564+
Debug.Assert(bitOffset == 0, "DBL state must be byte-aligned");
565+
return value;
566+
}
567+
else
568+
{
569+
throw new Exception($"State format '{format}' is not supported as floating-point format");
570+
}
571+
}
572+
505573
////REVIEW: This is some bad code duplication here between Read/WriteFloat&Double but given that there's no
506574
//// way to use a type argument here, not sure how to get rid of it.
507575

0 commit comments

Comments
 (0)