MaxstARSDK  3.5.0
AbstractFeaturePointBehaviour.cs
1 /*==============================================================================
2 Copyright 2017 Maxst, Inc. All Rights Reserved.
3 ==============================================================================*/
4 
5 using UnityEngine;
6 using UnityEngine.Rendering;
7 using System.Collections;
8 using System.Linq;
9 using System;
10 using System.Runtime.InteropServices;
11 
12 namespace maxstAR
13 {
14  [RequireComponent(typeof(MeshRenderer))]
15  public class AbstractFeaturePointBehaviour : MonoBehaviour
16  {
17  public float FeatureSize = 1.0f;
18 
19  private Renderer meshRenderer;
20 
21  public Camera arCamera;
22  private MeshFilter meshFilter = null;
23 
24  void OnApplicationPause(bool pause)
25  {
26  if (pause)
27  {
28  Destroy(this.meshFilter);
29  }
30  }
31 
32  private void Generate(Vector3[] vertex)
33  {
34 
35  if (this.meshFilter == null)
36  {
37  this.meshFilter = gameObject.AddComponent<MeshFilter>();
38  meshFilter.mesh = new Mesh();
39  meshFilter.mesh.name = "Procedural Grid";
40  meshRenderer = GetComponent<Renderer>();
41  meshRenderer.material.SetFloat("_Type", 1.0f);
42  meshRenderer.material.SetFloat("_FeatureSize", FeatureSize);
43  }
44 
45  Matrix4x4 projectionMatrix = arCamera.projectionMatrix;
46 
47  if (Application.platform == RuntimePlatform.OSXEditor || Application.platform == RuntimePlatform.OSXPlayer)
48  {
49  Matrix4x4 orientationMatrix = Matrix4x4.Scale(new Vector3(1.0f, 1.0f, 1.0f));
50  projectionMatrix = projectionMatrix * orientationMatrix;
51  }
52  else
53  {
54  Matrix4x4 orientationMatrix = Matrix4x4.Scale(new Vector3(1.0f, -1.0f, 1.0f));
55  projectionMatrix = projectionMatrix * orientationMatrix;
56  }
57 
58  meshRenderer.material.SetMatrix("projectionMatrix", projectionMatrix);
59 
60  Vector3[] vertices = new Vector3[vertex.Length << 2];
61 
62  for (int i = 0; i < vertex.Length; i++)
63  {
64  vertices[0 + (i << 2)] = vertex[i];
65  vertices[1 + (i << 2)] = vertex[i];
66  vertices[2 + (i << 2)] = vertex[i];
67  vertices[3 + (i << 2)] = vertex[i];
68  }
69 
70  meshFilter.mesh.vertices = vertices;
71 
72  int[] triangles = new int[vertex.Length * 6];
73 
74  for (int i = 0; i < vertex.Length; i++)
75  {
76  triangles[0 + (i * 6)] = 0 + (i << 2);
77  triangles[1 + (i * 6)] = 1 + (i << 2);
78  triangles[2 + (i * 6)] = 2 + (i << 2);
79  triangles[3 + (i * 6)] = 3 + (i << 2);
80  triangles[4 + (i * 6)] = 2 + (i << 2);
81  triangles[5 + (i * 6)] = 1 + (i << 2);
82  }
83 
84  meshFilter.mesh.triangles = triangles;
85 
86  Vector2[] uv = new Vector2[vertex.Length << 2];
87 
88  for (int i = 0; i < vertex.Length; i++)
89  {
90  uv[0 + (i << 2)] = new Vector2(0, 0);
91  uv[1 + (i << 2)] = new Vector2(1, 0);
92  uv[2 + (i << 2)] = new Vector2(0, 1);
93  uv[3 + (i << 2)] = new Vector2(1, 1);
94  }
95 
96  meshFilter.mesh.uv = uv;
97  }
98 
99  private Vector3[] convertFloatToVertex3(float[] vertex, int count)
100  {
101  if (count == 0)
102  {
103  return null;
104  }
105 
106  Vector3[] tempVertex = new Vector3[count];
107 
108  for (int i = 0; i < count; i++)
109  {
110  tempVertex[i] = new Vector3(vertex[0 + (i * 3)], vertex[1 + (i * 3)], vertex[2 + (i * 3)]);
111  }
112 
113  return tempVertex;
114  }
115 
116  void Start()
117  {
118  GetComponent<Renderer>().material.renderQueue = 1600;
119  transform.localScale = arCamera.transform.localScale;
120  transform.localRotation = arCamera.transform.localRotation;
121  transform.localPosition = arCamera.transform.localPosition;
122 
123  if (SystemInfo.graphicsDeviceType == GraphicsDeviceType.Direct3D9 ||
124  SystemInfo.graphicsDeviceType == GraphicsDeviceType.Direct3D11 ||
125  SystemInfo.graphicsDeviceType == GraphicsDeviceType.Direct3D12 ||
126  SystemInfo.graphicsDeviceType == GraphicsDeviceType.OpenGLCore)
127  {
128  transform.localScale = new Vector3(1.0f, -1.0f, 1.0f);
129  }
130  }
131 
132  private void Update()
133  {
134  if (this.meshFilter != null)
135  {
136  meshFilter.mesh.Clear();
137  }
138 
140 
141  int featureCount = guideInfo.GetFeatureCount();
142  if (featureCount == 0)
143  {
144  return;
145  }
146 
147  if (featureCount > 0)
148  {
149  float[] featureBuffer = guideInfo.GetFeatureBuffer();
150  Vector3[] vertexVector3Array = convertFloatToVertex3(featureBuffer, featureCount);
151 
152  Generate(vertexVector3Array);
153  }
154  }
155 
156  }
157 }
float [] GetFeatureBuffer()
Get projected feature buffer for SLAM (Always returns same address so vertex count must be considered...
Definition: GuideInfo.cs:100
int GetFeatureCount()
Get projected feature count in SLAM (float * 3 = 1 feature)
Definition: GuideInfo.cs:82
Contains surface&#39;s data generated from slam tracking
Definition: GuideInfo.cs:17
GuideInfo GetGuideInfo()
Get guide information of the found surface on SLAM after the FindSurface method has been called ...
static TrackerManager GetInstance()
Get TrackerManager instance
Control AR Engine (Singletone)