-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
161 lines (131 loc) · 3.75 KB
/
Copy pathindex.js
File metadata and controls
161 lines (131 loc) · 3.75 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
// TODO : randomly drop a blop and/or create a new one
import { Bundle, Client, Server } from "node-osc";
import { createNoise2D } from "simplex-noise";
const PORT = 10000;
const FLOOR_WALL_TX_MAXIMUM = 5.863999843597412;
const FLOOR_WALL_TY_MAXIMUM = 6.25;
const FLOOR_TX_MAXIMUM = 5.863999843597412;
const FLOOR_TY_MAXIMUM = 3.125;
const WALL_TX_MAXIMUM = 5.863999843597412;
const WALL_TY_MAXIMUM = 3.125;
function* idGenerator(id) {
while (true) {
id += 1;
yield id;
}
}
const idIterator = idGenerator(Math.floor(Math.random() * 10000) + 1);
let wall = getBlobArray(10);
let floor = getBlobArray(20);
const noise2D = createNoise2D();
const NOISE_WALKING_SPEED = 0.1;
// // lOOPBACK LISTENER FOR TESTING
// let log = "";
// const oscServer = new Server(PORT, "0.0.0.0", () => {
// console.log("Server started");
// });
// oscServer.on("message", function (msg) {
// console.log(`Message: ${msg}`);
// log += "\n" + msg;
// });
// oscServer.on("bundle", function (bundle) {
// bundle.elements.forEach((element, i) => {
// if (bundle.timetag[i]) {
// console.log(`Timestamp: ${bundle.timetag[i]}`);
// }
// console.log("" + element);
// });
// });
const client = new Client("127.0.0.1", PORT);
function getBlobArray(size) {
let blobs = new Array(size);
for (let i = 0; i < size; i++) {
if (Math.random() < 0.7) {
blobs[i] = {
id: 0,
// tx: 0, // redundant, will be derived from u
// ty: 0, // redundant, will be derived from v
u: 0,
v: 0,
};
} else {
blobs[i] = {
id: idIterator.next().value,
// tx: 0, // redundant, will be derived from u
// ty: 0, // redundant, will be derived from v
u: 0,
v: 0,
};
}
}
return blobs;
}
function updateBlobArray(blobs) {
for (let i = 0; i < blobs.length; i++) {
if (blobs[i].id == 0) {
continue;
}
blobs[i].u =
noise2D(Date.now() * NOISE_WALKING_SPEED + blobs[i].id, 0) * 0.5;
blobs[i].v =
noise2D(0, Date.now() * NOISE_WALKING_SPEED + blobs[i].id) * 0.5;
}
}
setInterval(() => {
updateBlobArray(floor);
updateBlobArray(wall);
let bundleContent = [["/_samplerate", 60]];
const floor_wallPart = wall
.concat(floor)
.map((blob, i) => {
const prefix = `/SOL_MUR/blobs/blob${i}/`;
return [
[prefix + "id", blob.id],
[prefix + "tx", blob.u * FLOOR_WALL_TX_MAXIMUM],
[prefix + "ty", blob.v * FLOOR_WALL_TY_MAXIMUM],
[prefix + "u", blob.u],
[prefix + "v", blob.v],
];
})
.flat(1);
const floor_wallSpecs = [
["/SOL_MUR/specs/Scalex", FLOOR_WALL_TX_MAXIMUM],
["/SOL_MUR/specs/Scaley", FLOOR_WALL_TY_MAXIMUM],
];
const wallPart = wall.map((blob, i) => {
const prefix = `/MUR/blobs/blob${i}/`;
return [
[prefix + "id", blob.id],
[prefix + "tx", blob.u * WALL_TX_MAXIMUM],
[prefix + "ty", blob.v * WALL_TY_MAXIMUM],
[prefix + "u", blob.u],
[prefix + "v", blob.v],
];
});
const wallSpecs = [
["/MUR/specs/Scalex", WALL_TX_MAXIMUM],
["/MUR/specs/Scaley", WALL_TY_MAXIMUM],
];
const floorPart = floor.map((blob, i) => {
const prefix = `/SOL/blobs/blob${i}/`;
return [
[prefix + "id", blob.id],
[prefix + "tx", blob.u * FLOOR_TX_MAXIMUM],
[prefix + "ty", blob.v * FLOOR_TY_MAXIMUM],
[prefix + "u", blob.u],
[prefix + "v", blob.v],
];
});
const floorSpecs = [
["/SOL/specs/Scalex", FLOOR_TX_MAXIMUM],
["/SOL/specs/Scaley", FLOOR_TY_MAXIMUM],
];
const finalBundle = bundleContent
.concat(floor_wallPart)
.concat(floor_wallSpecs)
.concat(wallPart)
.concat(wallSpecs)
.concat(floorPart)
.concat(floorSpecs);
client.send(new Bundle(...finalBundle));
}, 0);