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
|
function lineCloud(graph){
let points = [];
let nodes = graph.nodes;
let links = graph.links.forEach((link) => {
let startNode = nodes.find((n) => n.id == link.sourceId);
let endNode = nodes.find((n) => n.id == link.targetId);
points.push(startNode.position);
points.push(endNode.position);
});
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const material = new THREE.LineBasicMaterial({ color: "#3498db" });
let lines = new THREE.LineSegments(geometry, material);
return lines;
}
function pointCloud(graph)
{
let pointsGeometry = new THREE.BufferGeometry();
let points = [];
graph.nodes.forEach((n) => {
let vertex = n.position;
points.push(vertex);
});
pointsGeometry.setFromPoints(points);
// let pointsMaterial = new THREE.PointsMaterial({
// size: 8,
// sizeAttenuation: false,
// color: "#92F22A",
// map: circle_sprite_aa,
// transparent: true,
// alphaTest: 0.5
// });
let pointsMaterial = new THREE.ShaderMaterial({
vertexShader: nodeVertextShader,
fragmentShader: nodeFragmentShader,
transparent: true,
alphaTest: 0.1
});
return new THREE.Points(pointsGeometry, pointsMaterial);
}
function parseGraph(data) {
var id = 0;
var nodes = [];
var links = [];
function traval(root, children) {
if (!children) {
return;
}
let node = {
id: id,
property: {
name: root.name
}
};
node.position = new THREE.Vector3(
Math.random() * 5 * 2 - 5,
Math.random() * 5 * 2 - 5,
Math.random() * 5 * 2 - 5
);
nodes.push(node);
let neibours = root.children
.map((c) => {
id++;
let n = {
id: id,
name: c.name
};
n.position = new THREE.Vector3(
Math.random() * 5 * 2 - 5,
Math.random() * 5 * 2 - 5,
Math.random() * 5 * 2 - 5
);
return {
node: n,
children: c.children
};
})
.forEach((c) => {
nodes.push(c.node);
let link = {
id: node.id + "-" + c.node.id,
sourceId: node.id,
targetId: c.node.id
};
links.push(link);
traval(c, c.children);
});
}
traval(data, data.children);
return {
nodes,
links
};
}
var url = window.parent.dataUrl
var data = fetch(url).then(d=>d.json()).then(d=>{
let graph = parseGraph(d)
nodeCloud = pointCloud(graph)
edgeCloud = lineCloud(graph)
scene.add(nodeCloud);
scene.add(edgeCloud);
})
var nodeVertextShader = `
precision highp float;
void main() {
vec3 finalPosition = position;
vec4 mvPosition = modelViewMatrix * vec4( finalPosition, 1.0 );
float vSize = 3.0 * ( 50.0 / -mvPosition.z );
gl_PointSize = vSize;
gl_Position = projectionMatrix * mvPosition;
}
`
var nodeFragmentShader = `
void antiAlia(){
// anti-aliased support
float len = length(gl_PointCoord- vec2(0.5, 0.5));
float delta = 0.0, alpha = 1.0;
delta = fwidth(len);
alpha = smoothstep(.495-delta, 0.495 + delta, len);
vec4 bColor = vec4(0.0);
gl_FragColor = mix(gl_FragColor, bColor, alpha);
gl_FragColor = gl_FragColor * (1.0 - alpha);
}
void renderCircle(vec4 color){
float r = length(gl_PointCoord- vec2(0.5, 0.5)), delta = 0.0, alpha = 1.0;
if (r > .5) {
discard;
}
gl_FragColor = color;
}
void main() {
vec4 color = vec4(231.0/255.0, 76.0/255.0, 60.0/255.0,1.0);
float len = length(gl_PointCoord- vec2(0.5, 0.5));
renderCircle(color);
antiAlia();
}
`
var scene = new THREE.Scene();
// Create a basic perspective camera
var camera = new THREE.PerspectiveCamera( 75, window.innerWidth/window.innerHeight, 0.1, 1000 );
camera.position.z = 4;
// Create a renderer with Antialiasing
var renderer = new THREE.WebGLRenderer({antialias:true});
// Configure renderer clear color
renderer.setClearColor("#000000");
const controls = new THREE.OrbitControls( camera, renderer.domElement );
// Configure renderer size
let width = d3.select("#three-canvas").node().getBoundingClientRect().width
let height = width/2;
renderer.setSize(width,height);
// Append Renderer to DOM
let canvas = document.getElementById("three-canvas")
canvas.appendChild( renderer.domElement );
// Render Loop
var render = function () {
requestAnimationFrame( render );
controls.update()
// Render the scene
renderer.render(scene, camera);
};
render();
|