-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
308 lines (217 loc) · 7.72 KB
/
index.html
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<!DOCTYPE html>
<html lang="en">
<head>
<title>Thesaurus</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
color: #cccccc;
font-family:Monospace;
font-size:13px;
text-align:center;
background-color: #050505;
margin: 0px;
overflow: hidden;
}
#info {
position: absolute;
top: 0px; width: 100%;
padding: 5px;
}
a {
color: #0080ff;
}
</style>
</head>
<body>
<div id="container"></div>
<div id="info"><a href="http://threejs.org" target="_blank">three.js</a> webgl - buffergeometry - particles</div>
<script src="three.min.js"></script>
<script src="js/data.js"></script>
<script src="js/TypedArrayUtils.js"></script>
<script src="js/FirstPersonControls.js"></script>
<script type="x-shader/x-vertex" id="vertexshader">
attribute float alpha;
attribute vec3 color;
varying float vAlpha;
varying vec3 vColor;
void main() {
vAlpha = alpha;
vColor = color;
vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
gl_PointSize = 32.0 / length(mvPosition.xyz);
gl_Position = projectionMatrix * mvPosition;
}
</script>
<script type="x-shader/x-fragment" id="fragmentshader">
varying float vAlpha;
varying vec3 vColor;
void main() {
gl_FragColor = vec4( vColor, vAlpha );
}
</script>
<script>
var container, stats;
var camera, controls, scene, renderer, kdTree, geometry;
var clock = new THREE.Clock();
var mesh;
// Contruct hash table of words by position for lookup later
// necessary because kd-tree construction scrambles word order
function xyzHash(coords) {
return 400*coords[0] + 20*coords[1] + coords[2]
}
var pointsToWords = {}
var particles = window.data.length;
var positions = new Float32Array( particles * 3 );
var colors = new Float32Array(particles * 3);
var alphas = new Float32Array(particles);
var nearby_old = new Set();
init();
animate();
function init() {
container = document.getElementById( 'container' );
//
camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
// First Person Controls
controls = new THREE.FirstPersonControls( camera );
controls.movementSpeed = 5;
controls.lookSpeed = 0.1;
scene = new THREE.Scene();
scene.fog = new THREE.Fog( 0x050505, 2000, 3500 );
//
geometry = new THREE.BufferGeometry();
// var group = new THREE.Group();
// Set positions & alphas buffer
for ( var i = 0; i < particles; i ++ ) {
positions[ i*3 ] = window.data[i][0]
positions[ i*3 + 1 ] = window.data[i][1]
positions[ i*3 + 2 ] = window.data[i][2]
var posHash = xyzHash(positions.subarray(i*3, i*3+3))
pointsToWords[posHash] = window.words[i]
alphas[i] = 1
}
// Construct kd-tree
kdTree = new THREE.TypedArrayUtils.Kdtree(positions, function(a, b) {
return (a[0]-b[0])*(a[0]-b[0]) + (a[1]-b[1])*(a[1]-b[1]) + (a[2]-b[2])*(a[2]-b[2]);
}, 3);
// Set colors buffer
for (var i = 0; i < particles; i++) {
var x = positions[i*3]
var y = positions[i*3 + 1]
var z = positions[i*3 + 2]
var color = new THREE.Color()
var dist = x*x+y*y+z*z / 10000
var rot = Math.atan2(y, x)/Math.PI + 1
color.setHSL(rot, dist, 0.5)
colors[i*3] = color.r
colors[i*3+1] = color.g
colors[i*3+2] = color.b
}
// scene.add(group)
geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
geometry.addAttribute( 'color', new THREE.BufferAttribute( colors, 3 ) );
geometry.addAttribute( 'alpha', new THREE.BufferAttribute( alphas, 1 ) );
geometry.computeBoundingSphere();
//
// var material = new THREE.PointCloudMaterial( { size: 0.5, vertexColors: THREE.VertexColors } );
var shaderMaterial = new THREE.ShaderMaterial( {
attributes: {
alpha: { type: 'f', value: [] },
color: {type: 'c', value: []}
},
vertexShader: document.getElementById( 'vertexshader' ).textContent,
fragmentShader: document.getElementById( 'fragmentshader' ).textContent,
transparent: true
});
particleSystem = new THREE.PointCloud( geometry, shaderMaterial );
scene.add( particleSystem );
//
renderer = new THREE.WebGLRenderer( { antialias: false } );
renderer.setClearColor( scene.fog.color );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
//
window.addEventListener( 'resize', onWindowResize, false );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
controls.handleResize();
render();
}
//
function animate() {
requestAnimationFrame( animate );
displayNearest(camera, kdTree)
controls.update( clock.getDelta() )
render();
}
function render() {
var time = Date.now() * 0.001;
renderer.render( scene, camera );
}
function displayNearest(camera, kdTree) {
var nearby = kdTree.nearest([camera.position.x, camera.position.y, camera.position.z], 100, 10)
var _frustum = new THREE.Frustum();
var _projScreenMatrix = new THREE.Matrix4();
camera.matrixWorldInverse.getInverse( camera.matrixWorld );
_projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
_frustum.setFromMatrix( _projScreenMatrix );
// nearby_old.forEach(function(pointIdx) {
// alphas[pointIdx] = 1
// })
var newIdxs = []
nearby.forEach(function(object) {
var idx = object[0].pos
if (nearby_old.has(idx)) return
var objectPoint = new THREE.Vector3().fromArray(object[0].obj);
if (_frustum.containsPoint(objectPoint)) {
// alphas[idx] = 0;
alphas[idx] = 0
newIdxs.push(idx)
var posHash = xyzHash(object[0].obj)
var word = pointsToWords[posHash]
var wordTexture = textSprite(word, {
size: 64,
color: "#" + (new THREE.Color(colors[idx*3], colors[idx*3+1], colors[idx*3+2])).getHexString()
})
var wordMaterial = new THREE.SpriteMaterial( { map: wordTexture, color: 0xffffff, fog: true } );
var sprite = new THREE.Sprite( wordMaterial );
sprite.position.set(object[0].obj[0], object[0].obj[1], object[0].obj[2]);
scene.add(sprite);
}
geometry.attributes.alpha.needsUpdate = true
})
// nearby_old.clear();
newIdxs.forEach(function(idx){nearby_old.add(idx)})
}
function textSprite(text, params) {
if (params === undefined) params = {}
var font = params.font || "Arial",
size = params.size || 130,
color = params.color || "#676767";
padding = 10;
font = "bold " + size + "px " + font;
var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
context.font = font;
// get size data (height depends only on font size)
var metrics = context.measureText(text),
textWidth = metrics.width;
canvas.width = textWidth + 3;
canvas.height = size + 6;
context.font = font;
context.fillStyle = color;
context.fillText(text, 0, size+3);
//context.style.border="3px solid blue";
// canvas contents will be used for a texture
var texture = new THREE.Texture(canvas);
texture.needsUpdate = true;
return texture;
}
</script>
</body>
</html>