-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyThree.js
156 lines (126 loc) · 5.01 KB
/
myThree.js
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
function initScene() {
var WIDTH = window.innerWidth,
HEIGHT = window.innerHeight;
camera = new THREE.PerspectiveCamera(.7, WIDTH / HEIGHT, 1, 500);
camera.position.z = 150;
camera.lookAt(0, 0, 0);
scene = new THREE.Scene();
lut = new THREE.Lut('rainbow', 1024);
lut.setMax( 1000 );
//add loactor
var geometryCube = loc();
geometryCube.computeLineDistances();
var object = new THREE.LineSegments(geometryCube, new THREE.LineDashedMaterial({
color: 0xffffff,
dashSize: .005,
gapSize: .005,
linewidth: 5
}));
scene.add(object);
var geometry = new THREE.SphereGeometry( 0.01, 32, 32 );
var material = new THREE.MeshBasicMaterial( {color: 0xffff00} );
var sphere = new THREE.Mesh( geometry, material );
var pos = -1 * size / 2;
sphere.position.set(pos, pos, pos);
console.log(sphere);
scene.add(sphere);
/*var legend = lut.setLegendOn({'layout': 'vertical', position': { 'x': 1, 'y': 0, 'z': 0 }, 'dimensions': {'width': .1, 'height': .5}} );
scene.add (legend);
var labels = lut.setLegendLabels( { 'title': 'Frequency', 'um': 'Hz', 'ticks': 3, 'fontsize': 10} );
scene.add (labels['title']);
for ( var i = 0; i < Object.keys( labels[ 'ticks' ] ).length; i++ ) {
scene.add ( labels[ 'ticks' ][ i ] );
scene.add ( labels[ 'lines' ][ i ] );
}*/
renderer = new THREE.WebGLRenderer({
antialias: true
});
renderer.setClearColor(0x111111);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(WIDTH, HEIGHT);
var container = document.getElementById('container');
container.appendChild(renderer.domElement);
window.addEventListener('resize', onWindowResize, false);
controls = new THREE.OrbitControls(camera, renderer.domElement);
/*
//spawning Balls; doesent work properly
var counter = 0;
setInterval(function () {
var position = {x: counter%50 -25, y: Math.round(counter%(50*50)/50) * 5-25, z: -25};
spawnBall(counter%50/50, position.y / 10, position);
counter += 5;
}, 10);
*/
setInterval(crawlMap, 100);
}
function spawnBall(color, size, position) {
var geometry = new THREE.SphereGeometry(size, 32, 32);
var spawntime = new Date().getTime();
var rgb = hsvToRgb(color * 235, 75, 75); //the color multiplyer of 235 gives us a more intuitive looking golor spectrum
// create custom material from the shader code above
// that is within specially labeled script tags
var customMaterial = new THREE.ShaderMaterial({
uniforms: {
"c": {
type: "f",
value: 0.0
},
"p": {
type: "f",
value: 6.0
},
glowColor: {
type: "c",
value: new THREE.Color(rgb.r, rgb.g, rgb.b)
},
viewVector: {
type: "v3",
value: camera.position
}
},
vertexShader: document.getElementById('vertexShader').textContent,
fragmentShader: document.getElementById('fragmentShader').textContent,
side: THREE.FrontSide,
blending: THREE.AdditiveBlending,
transparent: true,
});
var object = new THREE.Mesh(geometry, customMaterial);
object.position.set(position.x, position.y, position.z);
scene.add(object);
var spawnTime = new Date().getTime();
var timer = setInterval(function () {
object.material.uniforms["c"].value = (new Date().getTime() - spawntime)*.3 / 5000.0; //lifetime in ms
// object.position.x += 1;
if (object.material.uniforms["c"].value > .3) {
scene.remove(object);
object = null;
clearInterval(timer);
timer = null;
}
}, 10); //max update frequency in ms
}
var arrowMap = new Map(); //the map with the arrows inside
function spawnArrow(freq, x, y, z, amplitude) {
var sourcePos = new THREE.Vector3(0, 0, 0);
//var targetPos = new THREE.Vector3(y - size / 2, z - size / 2, x - size / 2);
var targetPos = new THREE.Vector3(y,z,x);
var direction = new THREE.Vector3().subVectors(targetPos, sourcePos);
var color = lut.getColor(freq);
color.r *= amplitude * 20;
color.g *= amplitude * 20;
color.b *= amplitude * 20;
var arrow = new THREE.ArrowHelper(direction.clone().normalize(), sourcePos, .3, color);
scene.add(arrow);
if(arrowMap.get(freq) != undefined) {
scene.remove(arrowMap.get(freq).handle);
}
arrowMap.set(freq, {freq: freq, time: new Date().getTime(), handle: arrow, pos: {x: x, y: y, z: z}});
}
function crawlMap() {
for (var [key, value] of arrowMap) {
if(value.time + 300 <= new Date().getTime()) {
scene.remove(value.handle);
arrowMap.delete(key);
}
}
}