-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.html
292 lines (252 loc) · 7.98 KB
/
config.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
<!DOCTYPE html>
<html>
<head>
<title>Config Joy</title>
<style>
body {
background: #444;
color:#AAA;
overflow:hidden;
}
* { margin:0px;}
div {font-size: 10px;}
</style>
</head>
<body onunload="ws.close();">
<center><h2>Joystick Tester</h2></center>
<hr>
<div id='cv' style="width:100%; height:700px; background: #EEE;">
</div>
<hr>
<div id='typ'>--</div>
<div id='pos'>--</div>
<div id='mes'>--</div>
</body>
<!-- ======================== Evenements Joystick ====================== -->
<script src="js/Detector.js"></script>
<script src="js/three.min.js"></script>
<script src="js/optimer_bold.typeface.js"></script>
<script src="js/optimer_regular.typeface.js"></script>
<script>
function message(id) {
var node=document.getElementById(id)
var str="";
for (var i=1;i<arguments.length;i++) str+=arguments[i]+' ';
if (node) node.innerHTML=str;
}
var isGamepad=false;
var boutons=[0,0,0,0,0,0,0,0,0,0,0,0];
message('typ',"Joystick mono-axe");
function doEvent(data) {
if (isGamepad==false && data[0][5]!=100) {
isGamepad=true;
message('typ',"GamePad 4 axes");
}
if (isGamepad) {
var z=data[0][1]-100,
r=data[0][0]-100,
y=data[0][2]-100,
x=data[0][5]-100;
} else {
var x=data[0][0]-100,
y=data[0][1]-100,
z=data[0][2]-100,
r=0;
}
boutons=data[1];
mouseX = x;
mouseY = y;
mouseZ = z;
mouseR = r;
if ( boutons[1]==1) {
posx=0; posy=100; posz=0;
}
message("pos=",x,'/',y,'/',z,'R',r);
}
<!-- ======================== Utilitées ====================== -->
var camera, scene;
var webglRenderer;
function r(a) { return Math.round(a*10)/10.0 }
function hypot(a,b,c) { return Math.sqrt(a*a+b*b+c*c) }
function addMesh( geometry, scale, x, y, z, rx, ry, rz, material ) {
mesh = new THREE.Mesh( geometry, material );
mesh.position.set( 0, 0, 0 );
mesh.scale.set( scale, scale, scale );
mesh.overdraw = true;
scene.add( mesh );
mesh.position.set( x, y, z );
mesh.rotation.set( rx, ry, rz );
return(mesh);
}
function addbat(x,z, w,l,h,color,opacity) {
var mat=new THREE.MeshPhongMaterial( {
ambient: 0x303030, color: color, specular: 0x555555,
shininess: 30 , opacity: opacity
});
mat.transparent= (opacity<1.0) ? true : false ;
var b=addMesh(new THREE.CubeGeometry( w, h, l),
1,
x+w/2, h/2,z+h/2,
0 ,ar(-1.1,1.1),0,
mat);
return(b);
}
function makeText(x0,y0,z0,r,text,color,opacity,s,h) {
var textGeo = new THREE.TextGeometry( text, {
size: s,
height: h,
font: "optimer",
style: "normal",
bevelThickness: 10,
bevelSize: 0.5,
bevelEnabled: true,
bend: false,
material: 0,
extrudeMaterial: 1
});
var textMesh1 = new THREE.Mesh( textGeo , new THREE.MeshBasicMaterial( {
color: color, opacity: opacity,
transparent: ((opacity==1.0) ? false : true)
}));
textMesh1.position.x = x0;
textMesh1.position.y = z0;
textMesh1.position.z = y0;
textMesh1.rotation.x = 0;
textMesh1.rotation.y = (Math.PI*r)/180.0;
scene.add( textMesh1 );
}
var PI=3.14159;
function ar(min,max) {return(1.0+min+Math.random()*(max-min)) ;}
</script>
<!-- ======================== 3D ====================== -->
<script>
try { var ipc = require('ipc'); } catch(e) {}
var ok=false;
var ws;
function createClient(port) {
if (ok) return;
ws = new WebSocket("ws://localhost:"+port);
ws.onopen = function() { message("mes","connected"); ok=true; };
ws.onclose = function() { message("mes","Disconnected");ok=false; };
ws.onmessage = function(event) { doEvent(JSON.parse(event.data)); };
}
var mouseX = 0, mouseY = 0, mouseZ = 0, mouseR=0 ;
var posx=0,posy=200,posz=0, vx=0,vy=0, vz=0;
document.addEventListener('DOMContentLoaded', function() {
webgl=this;
setTimeout(function() {createClient(1901);} , 100);
var cv=document.getElementById('cv')
var SCREEN_WIDTH = cv.offsetWidth;
var SCREEN_HEIGHT = cv.offsetHeight;
var FLOOR = 0;
var container=cv;
var player, geometry;
var windowHalfX = SCREEN_WIDTH / 2;
var windowHalfY = SCREEN_HEIGHT / 2;
var K_LEFT=37;
var K_RIGHT=39;
var K_UP=38;
var K_DOWN=40;
var K_UP=38;
var K_DOWN=40;
var currentlyPressedKeys={};
var adir=0,vdir=0,dir=0,rad_dir=0;
init();
animate();
function init() {
// camera
camera = new THREE.PerspectiveCamera( 75, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 100000 );
camera.position.z = 75;
//scene
scene = new THREE.Scene();
// lights
var ambient = new THREE.AmbientLight( 0xA0A0A0 );
scene.add( ambient );
// more lights
var directionalLight = new THREE.DirectionalLight( 0xffeedd );
directionalLight.position.set( -800, 700, -800 ).normalize();
scene.add( directionalLight );
// renderer
webglRenderer = new THREE.WebGLRenderer();
webglRenderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
webglRenderer.domElement.style.position = "relative";
container.appendChild( webglRenderer.domElement );
createScene( ) ;
document.onkeydown = function(event) {currentlyPressedKeys[event.keyCode] = true;};
document.onkeyup = function(event) {currentlyPressedKeys[event.keyCode] = false;};
}
function createScene( ) {
//========= Floor
addMesh(new THREE.CubeGeometry( 1000, 1, 1000),
1,
0, 0,-20,
0 ,0,0,
new THREE.MeshPhongMaterial( { ambient: 0xA0A0A0, color: 0x2020FF, specular: 0x555555, shininess: 30 , opacity: 0.3} ));
// ======================== buildings, en vracs
for (var i=0;i<70;i++)
addbat(ar(-400,400),ar(-400,+400), 30,30,ar(30,80), ar(-32000,32000),0.9);
addbat(0,0, 1,1,4, 0xFFAAAA,1.0);
//for (var i=0;i<20;i++)
// addbrige(ar(-400,400),ar(-400,+400), 30,30,ar(30,80), ar(-32000,32000),0.9);
makeText(-200,470,0,5,"0 88XX 8 8 B D WM",0xA0A0A0,0.9,70,40) ;
// ================= Joueur , model en fichier OU OVNI...
if (true) {
player = addbat(0,0, 6,4,1, 0xBB0000,1);
} else {
player = addMesh(new THREE.CylinderGeometry( 5, 1 ,20 ,30),
1.4,
-400,30,-400,
Math.PI/2.0,0,0,
new THREE.MeshPhongMaterial( {
ambient: 0x000000, color: 0xFFAA00, specular: 0x101010,
shininess: 30 , opacity: 1.0
})
);
}
}
function animate() {
requestAnimationFrame( animate );
render();
}
function render() {
var ax=(mouseX*Math.cos(rad_dir))+(-mouseY*Math.cos(rad_dir+Math.PI/2.0));
var ay=(mouseX*Math.sin(rad_dir))+(-mouseY*Math.sin(rad_dir+Math.PI/2.0));
vx=(vx+ax/4000.0)*0.995;
vy=(vy+(-mouseZ-40)/6000.0)*0.95;
vz=(vz+ay/4000.0)*0.995;
if (isGamepad) {
vdir=(vdir+(-mouseR)/1200.0)*0.95;
} else {
if (currentlyPressedKeys[K_LEFT]) adir+=1.5;
else if (currentlyPressedKeys[K_RIGHT]) adir-=1.5;
else adir=0;
vdir=(vdir+adir)*0.99;
}
if (boutons[2]==1) {
vx=vy=vz=0;
}
posx+=vx;
posy=max(1,posy+vy);
posz+=vz;
dir+=vdir;
dir=(dir+360.0)%360;
rad_dir=-(Math.PI*dir)/180.0;
player.position.set(posx,posy,posz);
player.rotation.y=-rad_dir;
// ================ Inclinaison selon accelerations
player.rotation.z=-mouseX/400;
player.rotation.x=-mouseY/400;
// ================ Camera : subjestive
var camx= posx + (20*Math.cos(-rad_dir));
var camy= posy + 3 + 2*vy;
var camz= posz + (20*Math.sin(rad_dir));
camera.position.set(camx,camy,camz);
camera.lookAt(player.position);
webglRenderer.render( scene, camera );
}
});
function min(a,b) { return a<=b ? a : b ;}
function max(a,b) { return a>=b ? a : b ;}
function schdink(v,a,b) { return min(max(v,a),b) ;}
</script>
</html>