-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRGBDViewer.cs
175 lines (143 loc) · 6.2 KB
/
RGBDViewer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
This file is part of the OpenIMPRESS project.
OpenIMPRESS is free software: you can redistribute it and/or modify
it under the terms of the Lesser GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenIMPRESS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with OpenIMPRESS. If not, see <https://www.gnu.org/licenses/>.
*/
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
namespace oi.plugin.rgbd {
public class RGBDViewer : MonoBehaviour {
public Material m_material;
public FrameSource frameSource;
public bool fadeColorIfInactive = true;
private Vector2 resolution = new Vector2();
private List<GameObject> meshes = new List<GameObject>();
private int m_maxPointsInInstance = 60000;
private int m_maxPoints = 0;
public float FPS = 0.0f;
private float fpsSampleInterval = 2.0f;
private float lastSample = 0.0f;
private int fpsCounter = 0;
private float colorFade;
private float lastFrame;
private float fadeDuration = 0.8f; // 800ms fade
private float fadeAfter = 0.2f;
// Use this for initialization
void Start() {
colorFade = 0.0f;
lastFrame = float.MinValue;
/*
if (transform.parent == null) {
transform.position = Vector3.zero;
transform.rotation = Quaternion.identity;
} else {
transform.localPosition = Vector3.zero;
transform.localRotation = Quaternion.identity;
}*/
}
// Update is called once per frame
void Update() {
FrameObj frame = frameSource.GetNewFrame();
float now = Time.time;
if (lastSample + fpsSampleInterval < now) {
FPS = fpsCounter / fpsSampleInterval;
fpsCounter = 0;
lastSample = now;
}
if (frame != null) {
fpsCounter++;
lastFrame = now;
Vector2 _resolution = new Vector2(frame.posTex.width, frame.posTex.height);
if (!resolution.Equals(_resolution)) {
resolution = _resolution;
m_maxPoints = (int) (_resolution.x * _resolution.y);
CreateMesh();
}
transform.localPosition = frame.cameraPos;
transform.localRotation = frame.cameraRot;
//transform.position = frame.cameraPos;
//transform.rotation = frame.cameraRot;
Texture2D newColTex = frame.colTex;
if (newColTex != null) {
Destroy(m_material.GetTexture("_ColorTex"));
m_material.SetTexture("_ColorTex", newColTex);
}
Texture2D newPosTex = frame.posTex;
if (newPosTex != null) {
Destroy(m_material.GetTexture("_PositionTex"));
m_material.SetTexture("_PositionTex", newPosTex);
}
Texture2D newBidxTex = frame.bidxTex;
if (newBidxTex != null) {
Destroy(m_material.GetTexture("_BidxTex"));
m_material.SetTexture("_BidxTex", newBidxTex);
}
}
if (fadeColorIfInactive) {
if ((now - lastFrame) >= fadeAfter) {
float deltaFade = (now - lastFrame) - fadeAfter;
colorFade = Mathf.Max(0.0f, 1.0f - (deltaFade / fadeDuration));
} else if (colorFade < 1.0f) {
colorFade += Time.deltaTime / fadeDuration;
}
m_material.SetFloat("_Fade", colorFade);
} else {
m_material.SetFloat("_Fade", 1.0f);
}
}
void CreateMesh() {
foreach (GameObject mesh in meshes) {
Destroy(mesh);
}
int totIndex = 0;
int meshId = 0;
while (totIndex < m_maxPoints) {
GameObject go = new GameObject();
meshes.Add(go);
go.name = "Mesh" + meshId++;
MeshFilter mf = go.AddComponent<MeshFilter>();
Mesh msh = new Mesh();
mf.sharedMesh = msh;
MeshRenderer mr = go.AddComponent<MeshRenderer>();
mr.material = m_material;
go.transform.parent = transform;
int pointsInMesh = Math.Min(m_maxPointsInInstance, m_maxPoints - totIndex);
int[] indices = new int[pointsInMesh];
Vector3[] verts = new Vector3[pointsInMesh];
int[] faces = new int[pointsInMesh * 3];
Vector2[] uvs0 = new Vector2[pointsInMesh]; // pos
Vector2[] uvs1 = new Vector2[pointsInMesh]; // col
for (int i = 0; i < pointsInMesh; ++i) {
indices[i] = i;
verts[i] = new Vector3(0f, 0f, 0f);
faces[i * 3] =
i; // make sure every vertex is at least once in the faces/triangle array, or it won't get rendered
float xx = totIndex % resolution.x;
float yy = totIndex / resolution.x;
xx /= resolution.x;
yy /= resolution.y;
uvs0[i] = new Vector2(xx, yy);
uvs1[i] = new Vector2(xx, 1-yy);
totIndex++;
}
//Debug.Log(msh.GetTopology (0));
msh.vertices = verts;
msh.triangles = faces;
msh.uv = uvs0;
msh.uv2 = uvs1;
msh.SetIndices(indices, MeshTopology.Points, 0);
msh.bounds = new Bounds(Vector3.zero, new Vector3(100f, 100f, 100f));
}
}
}
}