-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonLoader.js
More file actions
289 lines (222 loc) · 7.55 KB
/
jsonLoader.js
File metadata and controls
289 lines (222 loc) · 7.55 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#pragma strict
//import SimpleJSON;
import System.Collections.Generic;
import System.IO;
@script RequireComponent(MeshFilter);
@script RequireComponent(MeshRenderer);
static var kdtree : KDTree;
static var uniquePoints : Vector3[];
static var colorList : Color[];
static var ptColorList : Color[];
static var idList : String[];
static var positionLookup = new Hashtable();
static var colorLookup = new Hashtable();
static var neighborLookup = new Hashtable();
static var selectedIndices : List.<int>;
static var indexLookup = new Hashtable();
static var indicesMaster : int[];
static var selectedIndex:int = -1;
static var savedColor = new Color(0,0,0,0);
static var highlightColor = new Color(1,1,1,0.1);
public var finger:Transform;
public var tooltip:GameObject;
public var HIC1:HandInputController;
var highLighted = false;
var fps = 0;
var frames = 0;
static var opacity:float = 0.05f;
static var scaling = 500.0;
static var cccc = 0;
static var nodesMesh:Mesh;
static var edgesMesh:Mesh;
public var offset = -7.22;
public var sphere : GameObject;
var content = new Hashtable();
var handStatus:int;
public var url: String = "localhost:9000/api/list";
function Start() {
sphere = GameObject.Find("Sphere");
var www = new WWW(url);
yield www;
Debug.Log(www.error);
content = JSONUtils.ParseJSON(www.text);
processData(content);
UpdateMeshes();
}
function Update () {
fps += 1.0 / Time.deltaTime;
frames++;
if(frames%600 ==0){
Debug.Log("AVG FPS: " + fps / frames);
}
}
function testUpdate(){
}
function ColorMap(index){
}
public static function unHighlight(index){
var nodesChild:GameObject = GameObject.Find("Network/Nodes");
var edgesChild:GameObject = GameObject.Find("Network/Edges");
var nodesMesh:Mesh = nodesChild.GetComponent.<MeshFilter>().mesh;
var edgesMesh:Mesh = edgesChild.GetComponent.<MeshFilter>().mesh;
if( selectedIndex >= 0 ){
var nsColor = new Color(savedColor[0],savedColor[0],savedColor[0],1);
var esColor = new Color(savedColor[0],savedColor[0],savedColor[0], opacity);
nodesMesh.colors[selectedIndex] = nsColor;
edgesMesh.colors[selectedIndex] = esColor;
}
selectedIndex = -1;
}
function highlight(){
//var p : Vector3 = new Vector3(finger.position.x , finger.position.y, finger.position.z);
var p = sphere.transform.position;
offset = this.transform.position.y;
p -= this.transform.position;
p /= this.transform.localScale.x;
var index:int = kdtree.FindNearest(p);
highlightIndex(index);
}
function highlightIndex(index:int){
var edgesChild:GameObject = GameObject.Find("Edges");
var cs:Color[] = colorList;
var ptCs:Color[] = ptColorList;
if( selectedIndex >= 0 ){
var nsColor = new Color(savedColor[0],savedColor[1],savedColor[2],1);
var esColor = new Color(savedColor[0],savedColor[1],savedColor[2], opacity);
cs[selectedIndex] = esColor;
ptCs[selectedIndex] = nsColor;
}
savedColor = cs[index];
cs[index] = highlightColor;
ptCs[index] = highlightColor;
selectedIndex = index;
nodesMesh.vertices = uniquePoints;
edgesMesh.vertices = uniquePoints;
nodesMesh.colors = ptCs;
edgesMesh.colors = cs;
var ht:Hashtable = positionLookup[index];
var location:Vector3 = uniquePoints[index];
location *= this.transform.localScale.x;
location += this.transform.position;
if(tooltip.GetComponent(TextMesh).text != ht["screen_name"]){
tooltip.transform.position = location + new Vector3(0,0.03,0);
tooltip.GetComponent(TextMesh).text = ht["screen_name"];
// var www: WWW = new WWW(ht["profile_image_url"]);
// yield www;
// if(!www.error){
// Debug.Log(ht["profile_image_url"]);
// www.LoadImageIntoTexture(imagePlane.GetComponent.<Renderer>().material.mainTexture);
// }
// else{
// Debug.Log(www.error);
// }
}
tooltip.transform.LookAt(2 * tooltip.transform.position - Camera.main.transform.position);
}
function processData(content : Hashtable){
positionLookup = processNodes(content["nodes"]);
Debug.Log("done w/ nodes");
Debug.Log("node count: " + uniquePoints.length);
indicesMaster = processEdges(content["edges"], indicesMaster);
Debug.Log("done w/ edges");
Debug.Log("edge count * 2: " + indicesMaster.length);
}
function createMeshObject(mesh:Mesh, name:String, points:Vector3[], colors:Color[], indices:int[], topoType:int){
var meshObject:GameObject = new GameObject(name);
var material = new Material(Shader.Find ("Mobile/Particles/Additive"));
if( name == "Nodes" ){
material = new Material(Shader.Find ("Mobile/Particles/Alpha Blended"));
}
meshObject.transform.parent = this.transform;
meshObject.transform.localScale = new Vector3(1,1,1);
meshObject.transform.localPosition = new Vector3(0,0,0);
meshObject.AddComponent(MeshRenderer);
meshObject.GetComponent.<MeshRenderer>().material = material;
meshObject.AddComponent(MeshFilter);
meshObject.GetComponent.<MeshFilter>().mesh = mesh;
mesh.MarkDynamic();
mesh.vertices = points;
mesh.colors = colors;
mesh.SetIndices(indices, topoType, 0);
}
function UpdateMeshes() {
var identityList:int[] = new int[uniquePoints.length];
for(var i = 0; i < uniquePoints.length; i++){
identityList[i] = i;
}
// nodesMesh = new Mesh();
edgesMesh = new Mesh();
createMeshObject(edgesMesh, "Edges", uniquePoints, colorList, indicesMaster, MeshTopology.Lines);
//createMeshObject(nodesMesh, "Nodes", uniquePoints, ptColorList, identityList, MeshTopology.Points);
}
function processNodes(nodes : Array) {
var nodeLookup = new Hashtable();
var ind = 0;
// set global vars
uniquePoints = new Vector3[nodes.length];
idList = new String[nodes.length];
colorList = new Color[nodes.length];
ptColorList = new Color[nodes.length];
for(var ht:Hashtable in nodes){
var id = ht["id"].ToString();
var poslist:Array = ht["positions"];
var pos:Array = poslist[0];
var rgblist:Array = ht["colors"];
var rgb:Array = rgblist[0];
var x = parseFloat(pos[0].ToString());
var y = parseFloat(pos[1].ToString());
var z = parseFloat(pos[2].ToString());
nodeLookup[ind] = ht;
uniquePoints[ind] = new Vector3(x,y,z);
idList[ind] = id;
indexLookup[id] = ind;
var r:double = rgb[0];
var g:double = rgb[1];
var b:double = rgb[2];
var div:double = 256.00000;
var c:Color;
var cpt:Color;
c = new Color(r/div, g/div, b/div, opacity);
cpt = new Color(r/div, g/div, b/div, 1);
colorLookup[id] = c;
colorList[ind] = c;
ptColorList[ind] = cpt;
ind ++;
}
kdtree = KDTree.MakeFromPoints(uniquePoints);
return nodeLookup;
}
function addNeighbor(lookup : Hashtable, source : String, target : String, index : int){
if(lookup[source]){
var h : Hashtable = lookup[source];
var a : List.<String> = h["indices"];
var b : List.<String> = h["neighbors"];
a.Add(index.ToString());
b.Add(target);
}
else{
lookup[source] = new Hashtable();
var localHash : Hashtable = lookup[source];
var aNew : List.<String> = new List.<String>();
var bNew : List.<String> = new List.<String>();
aNew.Add(index.ToString());
bNew.Add(target);
localHash["indices"] = aNew;
localHash["neighbors"] = bNew;
}
}
function processEdges(edges : Array, indices : int[]){
indices = new int[2 * edges.length];
var i = 0;
var g = 0;
for(var edge:Hashtable in edges){
var source = edge["source"].ToString();
var target = edge["target"].ToString();
//addNeighbor(neighborLookup, source, target, i);
//addNeighbor(neighborLookup, target, source, i + 1);
indices[i] = indexLookup[source];
indices[i+1] = indexLookup[target];
i += 2;
}
return indices;
}