Skip to content

Commit

Permalink
Merge pull request #52 from artoolkitx/phil-pattern-display
Browse files Browse the repository at this point in the history
Phil pattern display
  • Loading branch information
philip-lamb authored Apr 21, 2023
2 parents 38458a2 + fc54da0 commit 39a8b09
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 238 deletions.
133 changes: 0 additions & 133 deletions ProjectSettings/NavMeshAreas.asset

This file was deleted.

2 changes: 0 additions & 2 deletions ProjectSettings/ProjectVersion.txt

This file was deleted.

1 change: 0 additions & 1 deletion Source/Package/.gitignore

This file was deleted.

41 changes: 28 additions & 13 deletions Source/Package/Assets/artoolkitX-Unity/Scripts/ARPattern.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@

public class ARPattern
{
public Texture2D texture = null;
private Texture2D _texture = null;
private int _trackableID = ARTrackable.NO_ID;
private int _patternID = 0;

public Matrix4x4 matrix;
public float width;
public float height;
Expand All @@ -49,6 +52,9 @@ public class ARPattern

public ARPattern(int trackableID, int patternID)
{
_trackableID = trackableID;
_patternID = patternID;

float[] matrixRawArray = new float[16];
float widthRaw = 0.0f;
float heightRaw = 0.0f;
Expand All @@ -64,35 +70,44 @@ public ARPattern(int trackableID, int patternID)
}
width = widthRaw*0.001f;
height = heightRaw*0.001f;
//ARController.Log($"arwGetTrackablePatternConfig({trackableID}, { patternID}, ...) got widthRaw={widthRaw}, heightRaw={heightRaw}, imageSizeX={imageSizeX}, imageSizeY={imageSizeY}");

matrixRawArray[12] *= 0.001f; // Scale the position from artoolkitX units (mm) into Unity units (m).
matrixRawArray[13] *= 0.001f;
matrixRawArray[14] *= 0.001f;

Matrix4x4 matrixRaw = ARUtilityFunctions.MatrixFromFloatArray(matrixRawArray);
//ARController.Log("arwGetMarkerPatternConfig(" + markerID + ", " + patternID + ", ...) got matrix: [" + Environment.NewLine + matrixRaw.ToString("F3").Trim() + "]");
//ARController.Log($"arwGetTrackablePatternConfig({trackableID}, { patternID}, ...) got matrix: [" + Environment.NewLine + matrixRaw.ToString("F3").Trim() + "]");

// artoolkitX uses right-hand coordinate system where the marker lies in x-y plane with right in direction of +x,
// up in direction of +y, and forward (towards viewer) in direction of +z.
// Need to convert to Unity's left-hand coordinate system where marker lies in x-y plane with right in direction of +x,
// up in direction of +y, and forward (towards viewer) in direction of -z.
matrix = ARUtilityFunctions.LHMatrixFromRHMatrix(matrixRaw);
}

public Texture2D getTexture()
{
if (_texture) return _texture;

// Handle pattern image.
if (imageSizeX > 0 && imageSizeY > 0) {
if (imageSizeX > 0 && imageSizeY > 0)
{
// Allocate a new texture for the pattern image
texture = new Texture2D(imageSizeX, imageSizeY, TextureFormat.RGBA32, false);
texture.filterMode = FilterMode.Point;
texture.wrapMode = TextureWrapMode.Clamp;
texture.anisoLevel = 0;
_texture = new Texture2D(imageSizeX, imageSizeY, TextureFormat.ARGB32, false);
_texture.filterMode = FilterMode.Point;
_texture.wrapMode = TextureWrapMode.Clamp;
_texture.anisoLevel = 0;

// Get the pattern image data and load it into the texture
Color[] colors = new Color[imageSizeX * imageSizeY];
if (ARController.Instance.PluginFunctions.arwGetTrackablePatternImage(trackableID, patternID, colors)) {
texture.SetPixels(colors);
texture.Apply();
Color32[] colors32 = new Color32[imageSizeX * imageSizeY];
if (ARController.Instance.PluginFunctions.arwGetTrackablePatternImage(_trackableID, _patternID, colors32))
{
_texture.SetPixels32(colors32);
_texture.Apply();
}
}

}
return _texture;
}
}
43 changes: 17 additions & 26 deletions Source/Package/Assets/artoolkitX-Unity/Scripts/ARTrackable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public int UID
public float TwoDImageWidth { get; private set; } = 1.0f;
public void ConfigureAsTwoD(string imageFilePath, float imageWidth)
{
Unload();
Type = TrackableType.TwoD;
if (!string.IsNullOrEmpty(imageFilePath)) TwoDImageFile = imageFilePath;
if (imageWidth > 0.0f) TwoDImageWidth = imageWidth;
Expand All @@ -138,6 +139,7 @@ public void ConfigureAsTwoD(string imageFilePath, float imageWidth)
public float PatternWidth { get; private set; } = 0.08f;
public void ConfigureAsSquarePattern(string patternFilePath, float width)
{
Unload();
Type = TrackableType.Square;
if (!string.IsNullOrEmpty(patternFilePath))
{
Expand All @@ -150,6 +152,7 @@ public void ConfigureAsSquarePattern(string patternFilePath, float width)
}
public void ConfigureAsSquareBarcode(long barcodeID, float width)
{
Unload();
Type = TrackableType.SquareBarcode;
if (barcodeID >= 0) BarcodeID = barcodeID;
if (width > 0.0f) PatternWidth = width;
Expand All @@ -162,6 +165,7 @@ public void ConfigureAsSquareBarcode(long barcodeID, float width)
public string MultiConfigFile { get; private set; } = "";
public void ConfigureAsMultiSquare(string multiConfigFilePath)
{
Unload();
Type = TrackableType.Multimarker;
if (!string.IsNullOrEmpty(multiConfigFilePath)) MultiConfigFile = multiConfigFilePath;
loadError = false;
Expand All @@ -177,6 +181,7 @@ public void ConfigureAsMultiSquare(string multiConfigFilePath)
#endif
public void ConfigureAsNFT(string nftDataName)
{
Unload();
Type = TrackableType.NFT;
if (!string.IsNullOrEmpty(nftDataName)) NFTDataName = nftDataName;
loadError = false;
Expand All @@ -199,11 +204,6 @@ public void ConfigureAsNFT(string nftDataName)
[SerializeField]
private float currentNFTScale = 1.0f; // NFT marker only; scale factor applied to marker size.

[NonSerialized]
public float NFTWidth; //> Once marker is loaded, this holds the width of the marker in Unity units.
[NonSerialized]
public float NFTHeight; //> Once marker is loaded, this holds the height of the marker in Unity units.

// Realtime tracking information
private bool visible = false; // Trackable is visible or not
private Matrix4x4 transformationMatrix; // Full transformation matrix as a Unity matrix
Expand Down Expand Up @@ -405,34 +405,25 @@ public void Load()
// Trackable loaded. Do any additional configuration.
//ARController.Log("Added marker with cfg='" + cfg + "'");

// Any additional trackable-type-specific config not included in the trackable config string used at load time.
if (Type == TrackableType.Square || Type == TrackableType.SquareBarcode) UseContPoseEstimation = currentUseContPoseEstimation;
if (Type == TrackableType.NFT) NFTScale = currentNFTScale;

// Any additional config.
Filtered = currentFiltered;
FilterSampleRate = currentFilterSampleRate;
FilterCutoffFreq = currentFilterCutoffFreq;

// Retrieve any required information from the configured native ARTrackable.
if (Type == TrackableType.NFT || Type == TrackableType.TwoD) {
if (Type == TrackableType.NFT) NFTScale = currentNFTScale;

int imageSizeX, imageSizeY;
ARController.Instance.PluginFunctions.arwGetTrackablePatternConfig(UID, 0, null, out NFTWidth, out NFTHeight, out imageSizeX, out imageSizeY);
NFTWidth *= 0.001f;
NFTHeight *= 0.001f;
//ARController.Log("Got NFTWidth=" + NFTWidth + ", NFTHeight=" + NFTHeight + ".");

} else {

// Create array of patterns. A single marker will have array length 1.
int numPatterns = ARController.Instance.PluginFunctions.arwGetTrackablePatternCount(UID);
//ARController.Log("Trackable with UID=" + UID + " has " + numPatterns + " patterns.");
if (numPatterns > 0) {
patterns = new ARPattern[numPatterns];
for (int i = 0; i < numPatterns; i++) {
patterns[i] = new ARPattern(UID, i);
}
// Create array of patterns. A single marker will have array length 1.
int numPatterns = ARController.Instance.PluginFunctions.arwGetTrackablePatternCount(UID);
//ARController.Log("Trackable with UID=" + UID + " has " + numPatterns + " patterns.");
if (numPatterns > 0) {
patterns = new ARPattern[numPatterns];
for (int i = 0; i < numPatterns; i++) {
patterns[i] = new ARPattern(UID, i);
}

}

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public static class ARX_pinvoke
[DllImport(LIBRARY_NAME, CallingConvention=CallingConvention.Cdecl)]
#endif
[return: MarshalAsAttribute(UnmanagedType.I1)]
public static extern bool arwGetTrackablePatternImage(int trackableId, int patternID, [In, Out]Color[] colors);
public static extern bool arwGetTrackablePatternImage(int trackableId, int patternID, [In, Out]Color32[] colors32);



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ public class ARToolKitMenuEditor : MonoBehaviour {
private const string SOURCE_URL = "https://github.com/artoolkitx/artoolkitx";
private const string PLUGIN_SOURCE_URL = "https://github.com/artoolkitx/arunityx";
//private const string TOOLS_URL = "http://artoolkit.org/download-artoolkit-sdk#unity";
private const string VERSION = MENU_PATH_BASE + "/artoolkitX for Unity Version 1.1.5";
private const string VERSION = MENU_PATH_BASE + "/artoolkitX for Unity Version 1.1.6";
private const string WINDOWS_UNITY_MESSAGE = "Thank you for choosing artoolkitX for Unity! " +
"<b>artoolkitX requires the Microsoft C++ Redistributables to be installed on your system.</b>\n" +
"Please select \"{0}\" from the menu above, and install the required packages.";
private const string GET_TOOLS_MESSAGE = "artoolkitX for Unity Version 1.1.5! <b>To make your own markers, you'll need to download our tools.</b>\n" +
private const string GET_TOOLS_MESSAGE = "artoolkitX for Unity Version 1.1.6! <b>To make your own markers, you'll need to download our tools.</b>\n" +
"Please select {0} from menu above to download them.";

static ARToolKitMenuEditor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,22 @@ public override void OnInspectorGUI()
m.FilterCutoffFreq = EditorGUILayout.Slider("Cutoff freq.:", m.FilterCutoffFreq, 1.0f, 30.0f);
}

EditorGUILayout.BeginHorizontal();
//EditorGUILayout.BeginHorizontal();

// Draw all the marker images
if (m.Patterns != null) {
for (int i = 0; i < m.Patterns.Length; i++) {
GUILayout.Label(new GUIContent("Pattern " + i + ", " + m.Patterns[i].width.ToString("n3") + " m", m.Patterns[i].texture), GUILayout.ExpandWidth(false)); // n3 -> 3 decimal places.
float imageMinWidth = Math.Max(m.Patterns[i].imageSizeX, 32);
float imageMinHeight = Math.Max(m.Patterns[i].imageSizeY, 32);
GUILayout.Label(new GUIContent("Pattern " + i + ", " + m.Patterns[i].width.ToString("n3") + " m")); // n3 -> 3 decimal places.
Rect r = EditorGUILayout.GetControlRect(false, imageMinHeight + 4.0f, GUILayout.MinWidth(4.0f + imageMinWidth));
Rect r0 = new Rect(r.x + 2.0f, r.y + 2.0f, imageMinWidth, imageMinHeight);
GUI.DrawTexture(r, m.Patterns[i].getTexture(), ScaleMode.ScaleToFit, false);
//GUILayout.Label(new GUIContent("Pattern " + i + ", " + m.Patterns[i].width.ToString("n3") + " m", m.Patterns[i].getTexture()), GUILayout.ExpandWidth(false)); // n3 -> 3 decimal places.
}
}

EditorGUILayout.EndHorizontal();
//EditorGUILayout.EndHorizontal();
EditorGUILayout.EndVertical();

}
Expand Down
Loading

0 comments on commit 39a8b09

Please sign in to comment.