Skip to content

Commit 39a8b09

Browse files
authored
Merge pull request #52 from artoolkitx/phil-pattern-display
Phil pattern display
2 parents 38458a2 + fc54da0 commit 39a8b09

15 files changed

+132
-238
lines changed

ProjectSettings/NavMeshAreas.asset

Lines changed: 0 additions & 133 deletions
This file was deleted.

ProjectSettings/ProjectVersion.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

Source/Package/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

Source/Package/Assets/artoolkitX-Unity/Scripts/ARPattern.cs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@
4040

4141
public class ARPattern
4242
{
43-
public Texture2D texture = null;
43+
private Texture2D _texture = null;
44+
private int _trackableID = ARTrackable.NO_ID;
45+
private int _patternID = 0;
46+
4447
public Matrix4x4 matrix;
4548
public float width;
4649
public float height;
@@ -49,6 +52,9 @@ public class ARPattern
4952

5053
public ARPattern(int trackableID, int patternID)
5154
{
55+
_trackableID = trackableID;
56+
_patternID = patternID;
57+
5258
float[] matrixRawArray = new float[16];
5359
float widthRaw = 0.0f;
5460
float heightRaw = 0.0f;
@@ -64,35 +70,44 @@ public ARPattern(int trackableID, int patternID)
6470
}
6571
width = widthRaw*0.001f;
6672
height = heightRaw*0.001f;
73+
//ARController.Log($"arwGetTrackablePatternConfig({trackableID}, { patternID}, ...) got widthRaw={widthRaw}, heightRaw={heightRaw}, imageSizeX={imageSizeX}, imageSizeY={imageSizeY}");
6774

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

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

7582
// artoolkitX uses right-hand coordinate system where the marker lies in x-y plane with right in direction of +x,
7683
// up in direction of +y, and forward (towards viewer) in direction of +z.
7784
// Need to convert to Unity's left-hand coordinate system where marker lies in x-y plane with right in direction of +x,
7885
// up in direction of +y, and forward (towards viewer) in direction of -z.
7986
matrix = ARUtilityFunctions.LHMatrixFromRHMatrix(matrixRaw);
87+
}
88+
89+
public Texture2D getTexture()
90+
{
91+
if (_texture) return _texture;
8092

8193
// Handle pattern image.
82-
if (imageSizeX > 0 && imageSizeY > 0) {
94+
if (imageSizeX > 0 && imageSizeY > 0)
95+
{
8396
// Allocate a new texture for the pattern image
84-
texture = new Texture2D(imageSizeX, imageSizeY, TextureFormat.RGBA32, false);
85-
texture.filterMode = FilterMode.Point;
86-
texture.wrapMode = TextureWrapMode.Clamp;
87-
texture.anisoLevel = 0;
88-
97+
_texture = new Texture2D(imageSizeX, imageSizeY, TextureFormat.ARGB32, false);
98+
_texture.filterMode = FilterMode.Point;
99+
_texture.wrapMode = TextureWrapMode.Clamp;
100+
_texture.anisoLevel = 0;
101+
89102
// Get the pattern image data and load it into the texture
90-
Color[] colors = new Color[imageSizeX * imageSizeY];
91-
if (ARController.Instance.PluginFunctions.arwGetTrackablePatternImage(trackableID, patternID, colors)) {
92-
texture.SetPixels(colors);
93-
texture.Apply();
103+
Color32[] colors32 = new Color32[imageSizeX * imageSizeY];
104+
if (ARController.Instance.PluginFunctions.arwGetTrackablePatternImage(_trackableID, _patternID, colors32))
105+
{
106+
_texture.SetPixels32(colors32);
107+
_texture.Apply();
94108
}
95109
}
96110

97-
}
111+
return _texture;
112+
}
98113
}

Source/Package/Assets/artoolkitX-Unity/Scripts/ARTrackable.cs

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public int UID
117117
public float TwoDImageWidth { get; private set; } = 1.0f;
118118
public void ConfigureAsTwoD(string imageFilePath, float imageWidth)
119119
{
120+
Unload();
120121
Type = TrackableType.TwoD;
121122
if (!string.IsNullOrEmpty(imageFilePath)) TwoDImageFile = imageFilePath;
122123
if (imageWidth > 0.0f) TwoDImageWidth = imageWidth;
@@ -138,6 +139,7 @@ public void ConfigureAsTwoD(string imageFilePath, float imageWidth)
138139
public float PatternWidth { get; private set; } = 0.08f;
139140
public void ConfigureAsSquarePattern(string patternFilePath, float width)
140141
{
142+
Unload();
141143
Type = TrackableType.Square;
142144
if (!string.IsNullOrEmpty(patternFilePath))
143145
{
@@ -150,6 +152,7 @@ public void ConfigureAsSquarePattern(string patternFilePath, float width)
150152
}
151153
public void ConfigureAsSquareBarcode(long barcodeID, float width)
152154
{
155+
Unload();
153156
Type = TrackableType.SquareBarcode;
154157
if (barcodeID >= 0) BarcodeID = barcodeID;
155158
if (width > 0.0f) PatternWidth = width;
@@ -162,6 +165,7 @@ public void ConfigureAsSquareBarcode(long barcodeID, float width)
162165
public string MultiConfigFile { get; private set; } = "";
163166
public void ConfigureAsMultiSquare(string multiConfigFilePath)
164167
{
168+
Unload();
165169
Type = TrackableType.Multimarker;
166170
if (!string.IsNullOrEmpty(multiConfigFilePath)) MultiConfigFile = multiConfigFilePath;
167171
loadError = false;
@@ -177,6 +181,7 @@ public void ConfigureAsMultiSquare(string multiConfigFilePath)
177181
#endif
178182
public void ConfigureAsNFT(string nftDataName)
179183
{
184+
Unload();
180185
Type = TrackableType.NFT;
181186
if (!string.IsNullOrEmpty(nftDataName)) NFTDataName = nftDataName;
182187
loadError = false;
@@ -199,11 +204,6 @@ public void ConfigureAsNFT(string nftDataName)
199204
[SerializeField]
200205
private float currentNFTScale = 1.0f; // NFT marker only; scale factor applied to marker size.
201206

202-
[NonSerialized]
203-
public float NFTWidth; //> Once marker is loaded, this holds the width of the marker in Unity units.
204-
[NonSerialized]
205-
public float NFTHeight; //> Once marker is loaded, this holds the height of the marker in Unity units.
206-
207207
// Realtime tracking information
208208
private bool visible = false; // Trackable is visible or not
209209
private Matrix4x4 transformationMatrix; // Full transformation matrix as a Unity matrix
@@ -405,34 +405,25 @@ public void Load()
405405
// Trackable loaded. Do any additional configuration.
406406
//ARController.Log("Added marker with cfg='" + cfg + "'");
407407

408+
// Any additional trackable-type-specific config not included in the trackable config string used at load time.
408409
if (Type == TrackableType.Square || Type == TrackableType.SquareBarcode) UseContPoseEstimation = currentUseContPoseEstimation;
410+
if (Type == TrackableType.NFT) NFTScale = currentNFTScale;
411+
412+
// Any additional config.
409413
Filtered = currentFiltered;
410414
FilterSampleRate = currentFilterSampleRate;
411415
FilterCutoffFreq = currentFilterCutoffFreq;
412416

413-
// Retrieve any required information from the configured native ARTrackable.
414-
if (Type == TrackableType.NFT || Type == TrackableType.TwoD) {
415-
if (Type == TrackableType.NFT) NFTScale = currentNFTScale;
416-
417-
int imageSizeX, imageSizeY;
418-
ARController.Instance.PluginFunctions.arwGetTrackablePatternConfig(UID, 0, null, out NFTWidth, out NFTHeight, out imageSizeX, out imageSizeY);
419-
NFTWidth *= 0.001f;
420-
NFTHeight *= 0.001f;
421-
//ARController.Log("Got NFTWidth=" + NFTWidth + ", NFTHeight=" + NFTHeight + ".");
422-
423-
} else {
424-
425-
// Create array of patterns. A single marker will have array length 1.
426-
int numPatterns = ARController.Instance.PluginFunctions.arwGetTrackablePatternCount(UID);
427-
//ARController.Log("Trackable with UID=" + UID + " has " + numPatterns + " patterns.");
428-
if (numPatterns > 0) {
429-
patterns = new ARPattern[numPatterns];
430-
for (int i = 0; i < numPatterns; i++) {
431-
patterns[i] = new ARPattern(UID, i);
432-
}
417+
// Create array of patterns. A single marker will have array length 1.
418+
int numPatterns = ARController.Instance.PluginFunctions.arwGetTrackablePatternCount(UID);
419+
//ARController.Log("Trackable with UID=" + UID + " has " + numPatterns + " patterns.");
420+
if (numPatterns > 0) {
421+
patterns = new ARPattern[numPatterns];
422+
for (int i = 0; i < numPatterns; i++) {
423+
patterns[i] = new ARPattern(UID, i);
433424
}
434-
435425
}
426+
436427
}
437428
}
438429
}

Source/Package/Assets/artoolkitX-Unity/Scripts/ARX_pinvoke.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public static class ARX_pinvoke
215215
[DllImport(LIBRARY_NAME, CallingConvention=CallingConvention.Cdecl)]
216216
#endif
217217
[return: MarshalAsAttribute(UnmanagedType.I1)]
218-
public static extern bool arwGetTrackablePatternImage(int trackableId, int patternID, [In, Out]Color[] colors);
218+
public static extern bool arwGetTrackablePatternImage(int trackableId, int patternID, [In, Out]Color32[] colors32);
219219

220220

221221

Source/Package/Assets/artoolkitX-Unity/Scripts/Editor/ARToolKitMenuEditor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ public class ARToolKitMenuEditor : MonoBehaviour {
5353
private const string SOURCE_URL = "https://github.com/artoolkitx/artoolkitx";
5454
private const string PLUGIN_SOURCE_URL = "https://github.com/artoolkitx/arunityx";
5555
//private const string TOOLS_URL = "http://artoolkit.org/download-artoolkit-sdk#unity";
56-
private const string VERSION = MENU_PATH_BASE + "/artoolkitX for Unity Version 1.1.5";
56+
private const string VERSION = MENU_PATH_BASE + "/artoolkitX for Unity Version 1.1.6";
5757
private const string WINDOWS_UNITY_MESSAGE = "Thank you for choosing artoolkitX for Unity! " +
5858
"<b>artoolkitX requires the Microsoft C++ Redistributables to be installed on your system.</b>\n" +
5959
"Please select \"{0}\" from the menu above, and install the required packages.";
60-
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" +
60+
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" +
6161
"Please select {0} from menu above to download them.";
6262

6363
static ARToolKitMenuEditor() {

Source/Package/Assets/artoolkitX-Unity/Scripts/Editor/ARTrackableEditor.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,16 +195,22 @@ public override void OnInspectorGUI()
195195
m.FilterCutoffFreq = EditorGUILayout.Slider("Cutoff freq.:", m.FilterCutoffFreq, 1.0f, 30.0f);
196196
}
197197

198-
EditorGUILayout.BeginHorizontal();
198+
//EditorGUILayout.BeginHorizontal();
199199

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

207-
EditorGUILayout.EndHorizontal();
213+
//EditorGUILayout.EndHorizontal();
208214
EditorGUILayout.EndVertical();
209215

210216
}

0 commit comments

Comments
 (0)