-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
212 lines (182 loc) · 4.3 KB
/
sketch.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
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
let theShader;
let speed=0;
let heading=0;
let velhorx;
let velhory;
let neutralize;
let yawangle;
let pitchangle;
let rollangle;
let yaw;
let pitch;
let roll;
let initialNeutralize = null;
let velcanx=0;
let velcany=0;
let velcanz=0;
let speedtest=1.5;
let headingtest=50;
let PHI1=0;
let THETA1=180;
let cam;
let VelC;
let phi=0;
let theta=0;
let radius=200;
let beta;
let gamma;
let textureImg;
let motion = false;
let ff;
function preload() {
theShader = loadShader('vert.vert', 'frag.frag');
let constraints = {
video: {
facingMode: {
exact: "environment"
},
width: { exact: 1280 },
height: { exact: 720 }
}
};
cam = createCapture(constraints, (stream) => {
});
cam.hide();
}
function setup() {
createCanvas(windowWidth, windowHeight, WEBGL);
VelCsli=createSlider(1,30,3,1);
VelCsli.position(10,50);
ff = createP();
ff.position(10,10);
camera(0, 0, 0, 0, 0, -1, 0, 1, 0);
noStroke();
//geolocation
if ("geolocation" in navigator) {
navigator.geolocation.watchPosition(updatePosition, positionError, {
enableHighAccuracy: true
});
} else {
alert("Geolocation is not supported by your browser");
}
perspective(40*PI/180, width/height, 0.001,500);
//device orientation
if (typeof DeviceOrientationEvent.requestPermission === 'function') {
document.body.addEventListener('click', function() {
DeviceOrientationEvent.requestPermission()
.then(function() {
console.log('DeviceOrientationEvent enabled');
window.addEventListener('deviceorientation', handleOrientation);
motion = true;
})
.catch(function(error) {
console.warn('DeviceOrientationEvent not enabled', error);
})
})
} else {
motion = true;
}
}
function draw() {
background(200);
angleMode(DEGREES);
VelC=VelCsli.value();
velhorx=speed*sin(heading);//velocity coordinates in horizon frame
velhory=speed*cos(heading);
let horvelmatrix=math.matrix([
[velhorx],
[velhory],
[0]
]);
//define the Yaw Pitch Roll matrix and calculate
let YAW=math.matrix([
[cos(-yaw),-sin(-yaw),0],
[sin(-yaw),cos(-yaw),0],
[0,0,1],
]);
//landscape mode
let YAWR=math.matrix([
[cos(90),-sin(90),0],
[sin(90),cos(90),0],
[0,0,1],
]);
let ROLL=math.matrix([
[cos(-roll),0,sin(-roll)],
[0,1,0],
[-sin(-roll),0,cos(-roll)],
]);
let PITCH=math.matrix([
[1,0,0],
[0,cos(-pitch),-sin(-pitch)],
[0,sin(-pitch),cos(-pitch)],
]);
let result3=math.multiply(YAWR,math.multiply(ROLL,math.multiply(PITCH,math.multiply(YAW,horvelmatrix))));
// avoid the NaN output
if (isNaN(result3.get([0,0])) || isNaN(result3.get([1,0])) || isNaN(result3.get([2,0]))) {
velcanx=0;
velcany=0;
velcanz=0;
} else {
velcanx = Number(result3.get([0,0]));
velcany = -Number(result3.get([1,0]));
velcanz = Number(result3.get([2,0]));
}
angleMode(RADIANS);
//transform to theta and phi
let r=sqrt(velcanx*velcanx+velcany*velcany+velcanz*velcanz);
if (r===0){
THETA1=0
PH1=0;
}else{
THETA1=asin(-velcany/r);
PHI1=PI+atan2(-velcanx,-velcanz);
}
theta=THETA1;
phi=PHI1;
beta=speed/VelC;
gamma=1/Math.sqrt(1-beta*beta);
// distorted lookalike sphere
rotateY(phi);
rotateX(theta);
scale(1/gamma, 1/gamma, 1);
translate(0,0,radius*beta);
rotateX(-theta);
rotateY(-phi);
theShader.setUniform('uTex', cam);
resetShader();
shader(theShader);
sphere(200,100);
console.log(speed,heading);
ff.html(`c:${VelC}m/s`);
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}
function updatePosition(position) {
speed = position.coords.speed !== null ? position.coords.speed : 0;
heading = position.coords.heading !== null ? position.coords.heading : 0;
}
function positionError(error) {
console.error("Error getting geolocation:", error);
}
//set the horizon frame
function handleOrientation(event) {
angleMode(DEGREES);
let neutralize;
if (initialNeutralize === null) {
initialNeutralize = event.webkitCompassHeading;
}
neutralize = initialNeutralize;
let yawangle=event.alpha;
let pitchangle=event.beta;
let rollangle=event.gamma;
yaw1=yawangle;
if (yaw1>=neutralize){
yaw=yaw1-neutralize;
}
else{
yaw=360-(neutralize-yaw1);
}
pitch=pitchangle;
roll=rollangle;
}