Some animation adjustments, WIP UDP server

rob
Jurij Podgoršek 2022-03-04 16:07:22 +01:00
parent e0db3d674b
commit 2ea68dcc8d
3 changed files with 257 additions and 23 deletions

142
anim.js
View File

@ -17,7 +17,7 @@ var FOV = 90;
var width = 6; var width = 6;
// Limit stevila objektov // Limit stevila objektov
var obj_limit = 1000; var obj_limit = 10;
// Prvotno prazno polje objektov. Lahko bi kak buffer to bil pozneje // Prvotno prazno polje objektov. Lahko bi kak buffer to bil pozneje
var objekti = []; var objekti = [];
@ -57,10 +57,14 @@ var qWWo = new THREE.Quaternion(-1, 0, 0, 0);
var qWWd = new THREE.Quaternion(-1, 0, 0, 0); var qWWd = new THREE.Quaternion(-1, 0, 0, 0);
var qObj = new THREE.Quaternion(-1, 0, 0, 0); var qObj = new THREE.Quaternion(-1, 0, 0, 0);
var qStart = new THREE.Quaternion(-1, 0, 0, 0); var qStart = new THREE.Quaternion(-1, 0, 0, 0);
var qCalibrate = new THREE.Quaternion();
var calibrate = true; var calibrate = true;
var reset = false; var reset = false;
// Razlika v eulerjih
var dqX = 0;
var dqY = 0;
var dqZ = 0;
// Gumbi in pa pospesek // Gumbi in pa pospesek
var keysPressed = [0, 0, 0, 0]; var keysPressed = [0, 0, 0, 0];
var accel = [0, 0, 0]; var accel = [0, 0, 0];
@ -128,8 +132,9 @@ function modulirajParametre() {
drotacijaZ += qWWd.z / 10; drotacijaZ += qWWd.z / 10;
} }
if (keysPressed[0]) { if (keysPressed[0]) {
width += qWWd.x * 10; width *= 1 + (dqY / 3);
barva_mod += qWW.y / 10000; barva_mod += (dqX / 30);
obj_limit *= 1 + (dqZ / 3);
} }
} }
@ -226,14 +231,134 @@ function onWindowResize() {
window.addEventListener('resize', onWindowResize, false); window.addEventListener('resize', onWindowResize, false);
const getVal = function (msg) { function getVal(msg) {
return msg.value; return msg.value;
} }
oscCallbacks = { kbdPressed = {
a: false,
s: false,
d: false,
f: false
};
window.addEventListener('keydown', (e) => {
kbdPressed[e.key] = true;
})
window.addEventListener('keyup', (e) => {
if (e.key in kbdPressed) {
kbdPressed[e.key] = false;
}
})
function eulerFromQuaternion( quaternion, order ) {
// Quaternion to matrix.
const w = quaternion[0], x = quaternion[1], y = quaternion[2], z = quaternion[3];
const x2 = x + x, y2 = y + y, z2 = z + z;
const xx = x * x2, xy = x * y2, xz = x * z2;
const yy = y * y2, yz = y * z2, zz = z * z2;
const wx = w * x2, wy = w * y2, wz = w * z2;
const matrix = [
1 - ( yy + zz ),
xy + wz,
xz - wy,
0,
xy - wz,
1 - ( xx + zz ),
yz + wx,
0,
xz + wy,
yz - wx,
1 - ( xx + yy ),
0,
0,
0,
0,
1
];
// Matrix to euler
function clamp( value, min, max ) {
return Math.max( min, Math.min( max, value ) );
}
const m11 = matrix[ 0 ], m12 = matrix[ 4 ], m13 = matrix[ 8 ];
const m21 = matrix[ 1 ], m22 = matrix[ 5 ], m23 = matrix[ 9 ];
const m31 = matrix[ 2 ], m32 = matrix[ 6 ], m33 = matrix[ 10 ];
var euler = [ 0, 0, 0 ];
switch ( order ) {
case "XYZ":
euler[1] = Math.asin( clamp( m13, - 1, 1 ) );
if ( Math.abs( m13 ) < 0.9999999 ) {
euler[0] = Math.atan2( - m23, m33 );
euler[2] = Math.atan2( - m12, m11 );
} else {
euler[0] = Math.atan2( m32, m22 );
euler[2] = 0;
}
break;
case "YXZ":
euler[0] = Math.asin( - clamp( m23, - 1, 1 ) );
if ( Math.abs( m23 ) < 0.9999999 ) {
euler[1] = Math.atan2( m13, m33 );
euler[2] = Math.atan2( m21, m22 );
} else {
euler[1] = Math.atan2( - m31, m11 );
euler[2] = 0;
}
break;
case "ZXY":
euler[0] = Math.asin( clamp( m32, - 1, 1 ) );
if ( Math.abs( m32 ) < 0.9999999 ) {
euler[1] = Math.atan2( - m31, m33 );
euler[2] = Math.atan2( - m12, m22 );
} else {
euler[1] = 0;
euler[2] = Math.atan2( m21, m11 );
}
break;
case "ZYX":
euler[1] = Math.asin( - clamp( m31, - 1, 1 ) );
if ( Math.abs( m31 ) < 0.9999999 ) {
euler[0] = Math.atan2( m32, m33 );
euler[2] = Math.atan2( m21, m11 );
} else {
euler[0] = 0;
euler[2] = Math.atan2( - m12, m22 );
}
break;
case "YZX":
euler[2] = Math.asin( clamp( m21, - 1, 1 ) );
if ( Math.abs( m21 ) < 0.9999999 ) {
euler[0] = Math.atan2( - m23, m22 );
euler[1] = Math.atan2( - m31, m11 );
} else {
euler[0] = 0;
euler[1] = Math.atan2( m13, m33 );
}
break;
case "XZY":
euler[2] = Math.asin( - clamp( m12, - 1, 1 ) );
if ( Math.abs( m12 ) < 0.9999999 ) {
euler[0] = Math.atan2( m32, m22 );
euler[1] = Math.atan2( m13, m11 );
} else {
euler[0] = Math.atan2( - m23, m33 );
euler[1] = 0;
}
break;
}
return euler;
}
var oscCallbacks = {
'/keys/': [ '/keys/': [
function(args) { function(args) {
keysPressed = args.map(getVal); keysPressed = args.map(getVal);
keysPressed[0] |= kbdPressed['a'];
keysPressed[1] |= kbdPressed['s'];
keysPressed[2] |= kbdPressed['d'];
keysPressed[3] |= kbdPressed['f'];
//console.log(keysPressed, kbdPressed);
} }
], ],
'/quaternion/': [ '/quaternion/': [
@ -247,6 +372,11 @@ oscCallbacks = {
qWWd = qWW.clone(); qWWd = qWW.clone();
qWWd.conjugate(); qWWd.conjugate();
qWWd.multiply(qWWo); qWWd.multiply(qWWo);
var euler = eulerFromQuaternion([qWWd.w, qWWd.z, qWWd.x, qWWd.y], 'XYZ');
dqX = euler[0];
dqY = euler[1];
dqZ = euler[2];
} }
], ],
/* /*

View File

@ -1,15 +1,39 @@
s.boot(); s.boot();
~gW = 0; (
~gW = -1;
~gX = 0; ~gX = 0;
~gY = 0; ~gY = 0;
~gZ = 0; ~gZ = 0;
~dgW = -1;
~dgX = 0;
~dgY = 0;
~dgZ = 0;
~oW = -1;
~oX = 0;
~oY = 0;
~oZ = 0;
~euler;
~calibrate = true;
~quaternionToEuler = { |q|
var euler = Array.newClear(3);
euler[0] = atan2( (2 * q[1] * q[2]) - (2 * q[0] * q[3]), (2 * q[0]*q[0]) + (2 * q[1] * q[1]) - 1); // psi
euler[1] = asin( (2 * q[1] * q[3]) + (2 * q[0] * q[2])).neg; // theta
euler[2] = atan2( (2 * q[2] * q[3]) - (2 * q[0] * q[1]), (2 * q[0] * q[0]) + (2 * q[3] * q[3]) - 1); // phi
euler.raddeg
}
)
( (
z = { z = {
arg f=440, amp=1, phase=0; arg f=440, amp=1, phase=0;
var so; var so;
so = SinOsc.ar(f, phase) * amp; so = VarSaw.ar(f, phase) * amp;
}.play; }.play;
) )
@ -17,23 +41,90 @@ z = {
y = { y = {
arg f=440, amp=1, phase=0; arg f=440, amp=1, phase=0;
var so; var so;
so = SinOsc.ar(f, phase) * amp; so = VarSaw.ar(f, phase) * amp;
}.play; }.play;
) )
(
w = {
arg fa=1, fb=1, fc=1;
var so;
so = VarSaw.ar(LFPulse.kr(fa, 0, 0.3, 200 * fb, 200 * fc), 0, 0.2, 0.1)
}.play;
);
// WW // WW
// Receiver function // Receiver function
( (
o = OSCFunc({ o = OSCFunc({
arg msg, time, addr, recvPort; arg msg, time, addr, recvPort;
~oW = ~gW;
~oX = ~gX;
~oY = ~gY;
~oZ = ~gZ;
//[msg, time, addr, recvPort].postln; //[msg, time, addr, recvPort].postln;
~gX = msg[1]; ~gW = msg[1];
~gY = msg[2]; ~gZ = msg[2];
~gZ = msg[3]; ~gX = msg[3];
~gW = msg[0]; ~gY = msg[0];
z.set(\f, ~gX * 800);
y.set(\f, ~gY * 800); ~gX = ~gX * -1;
y.set(\phase, ~gZ); ~gZ = ~gZ * -1;
//z.set(\f, ~gX * 800);
//y.set(\f, ~gY * 800);
//y.set(\phase, ~gZ);
//w.set(\f1, ~gX);
//w.set(\f2, ~gY);
//w.set(\f3, ~gZ);
~dgW = ~gW;
~dgX = -1 * ~gX;
~dgY = -1 * ~gY;
~dgZ = -1 * ~gZ;
~dgW = (~dgW * ~oW) - (~dgX * ~oX) - (~oY * ~oY) - (~dgZ * ~oZ);
~dgX = (~dgW * ~oX) - (~dgX * ~oW) - (~dgY * ~oZ) - (~dgZ * ~oY);
~dgY = (~dgW * ~oY) - (~dgX * ~oZ) - (~dgY * ~oW) - (~dgZ * ~oX);
~dgZ = (~dgW * ~oZ) - (~dgX * ~oY) - (~dgY * ~oX) - (~dgZ * ~oW);
~euler = ~quaternionToEuler.([~dgW, ~dgX, ~dgY, ~dgZ]);
~euler.postln;
}, '/quaternion/', n); }, '/quaternion/', n);
) )
// ENDWW // ENDWW
MIDIClient.init;
MIDIFunc.trace(false);
(
MIDIdef(\tMidi, {
arg msg;
msg.postln;
z.set(\f, msg * 100);
}, 10, 0, \control)
)
(
MIDIdef(\tMidi, {
arg msg;
msg.postln;
z.set(\fb, msg);
}, 11, 0, \control)
)
(
MIDIdef(\tMidi, {
arg msg;
msg.postln;
z.set(\fc, msg);
}, 12, 0, \control)
)

View File

@ -114,11 +114,13 @@ function openBT() {
}) })
} }
if (tty) {
if (baudrate) { if (baudrate) {
openSerial(baudrate) openSerial(baudrate)
} else { } else {
openBT() openBT()
} }
}
app.get('/', (req, res) => { app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html'); res.sendFile(__dirname + '/index.html');
@ -149,12 +151,23 @@ server.listen(port, () => console.log('listening on *:' + port))
const wss = new WebSocket.Server({ server }) const wss = new WebSocket.Server({ server })
const scudp = new osc.UDPPort({ const scudp = new osc.UDPPort({
remoteAddr: '127.0.0.1', multicastMembership: ['224.0.1.9'],
remotePort: 57120 remotePort: 6696,
multicastTTL: 10,
metadata: true
}) })
scudp.on('open', () => { scudp.on('open', () => {
console.log("UDP to OSC open") console.log("UDP to OSC open")
}) })
scudp.on('message', (msg) => {
console.log('got UDP msg', msg);
osclients.forEach( client => {
if (client) {
//console.log("sending", msg, info)
client.send(msg)
}
})
})
scudp.open() scudp.open()
const sendAll = (msg, info, oscWS, osclients) => { const sendAll = (msg, info, oscWS, osclients) => {