Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance increase #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Runtime.InteropServices;
using UnityEngine;

Expand All @@ -11,29 +11,39 @@ class SpaceNavigatorWindows : SpaceNavigator {
private const float TransSensScale = 0.0001f, RotSensScale = 0.0008f;

// Public API
public override Vector3 GetTranslation() {
public override Vector3 GetTranslation()
{
if (SubInstance._sensor != null && Translation == null)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Match the indent of the rest of this method, especially since you're not using { } (which I would highly recommend using).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is a real-world example of why you should really use curly braces (TLDR: major security failure in Apple's SSL).
https://blog.codecentric.de/en/2014/02/curly-braces/

Translation = SubInstance._sensor.Translation;

float sensitivity = Application.isPlaying ? Settings.PlayTransSens : Settings.TransSens[Settings.CurrentGear];
return (SubInstance._sensor == null ?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing SubInstance._sensor == null twice. Can be put at the top of the method with an early return.

Vector3.zero :
new Vector3(
Settings.GetLock(DoF.Translation, Axis.X) ? 0 : (float)SubInstance._sensor.Translation.X,
Settings.GetLock(DoF.Translation, Axis.Y) ? 0 : (float)SubInstance._sensor.Translation.Y,
Settings.GetLock(DoF.Translation, Axis.Z) ? 0 : -(float)SubInstance._sensor.Translation.Z) *
Settings.GetLock(DoF.Translation, Axis.X) ? 0 : (float)Translation.X,
Settings.GetLock(DoF.Translation, Axis.Y) ? 0 : (float)Translation.Y,
Settings.GetLock(DoF.Translation, Axis.Z) ? 0 : -(float)Translation.Z) *
sensitivity * TransSensScale);
}
public override Quaternion GetRotation() {
float sensitivity = Application.isPlaying ? Settings.PlayRotSens : Settings.RotSens;
if (SubInstance._sensor != null && Rotation == null)
Rotation = SubInstance._sensor.Rotation;

float sensitivity = Application.isPlaying ? Settings.PlayRotSens : Settings.RotSens;
return (SubInstance._sensor == null ?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, testing SubInstance._sensor == null twice. Same solution.

Quaternion.identity :
Quaternion.AngleAxis(
(float)SubInstance._sensor.Rotation.Angle * sensitivity * RotSensScale,
(float)Rotation.Angle * sensitivity * RotSensScale,
new Vector3(
Settings.GetLock(DoF.Rotation, Axis.X) ? 0 : -(float)SubInstance._sensor.Rotation.X,
Settings.GetLock(DoF.Rotation, Axis.Y) ? 0 : -(float)SubInstance._sensor.Rotation.Y,
Settings.GetLock(DoF.Rotation, Axis.Z) ? 0 : (float)SubInstance._sensor.Rotation.Z)));
Settings.GetLock(DoF.Rotation, Axis.X) ? 0 : -(float)Rotation.X,
Settings.GetLock(DoF.Rotation, Axis.Y) ? 0 : -(float)Rotation.Y,
Settings.GetLock(DoF.Rotation, Axis.Z) ? 0 : (float)Rotation.Z)));
}

// Device variables
private AngleAxis Rotation;
private Vector3D Translation;

private Sensor _sensor;
private Device _device;
//private Keyboard _keyboard;
Expand All @@ -53,7 +63,7 @@ private SpaceNavigatorWindows() {
_device.Connect();
}
catch (COMException ex) {
Debug.LogError(ex.ToString());
//Debug.LogError(ex.ToString());
}
}

Expand All @@ -79,4 +89,4 @@ public override void Dispose() {
#endregion - IDisposable -
}
#endif // UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
}
}