-
Notifications
You must be signed in to change notification settings - Fork 0
/
FotN.js
188 lines (151 loc) · 4.53 KB
/
FotN.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
//leap and three.js must be included before this
// shim layer with setTimeout fallback via Paul Irish
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.msRequestAnimationFrame ||
window.oRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 1000 / 60);
};
})();
var view, camera, renderer, player, scene, playerVelocity = new THREE.Vector3(0,0,0);
var speed = 100;
window.onload = function(){
console.log("onload");
//three init
WebGLSetup(function(){
initScene(function(){
gameloop();
});
});
};
//input loop
Leap.loop({},function(frame){
/* console.log(frame.hands); */
if(frame.hands.length == 1){
var handPosition = frame.hands[0].palmPosition;
console.log("handPos:"+JSON.stringify(handPosition)+" set x:"+handPosition[0]/256);
playerVelocity.x = (handPosition[0]/256.0) * speed;
playerVelocity.y = (handPosition[2]/-256.0) * speed;
}else if(frame.hands.length >1){
var lHand, rHand;
if(frame.hands[0].x >frame.hands[1].x){
rHand = frame.hands[0].palmPosition;
lHand = frame.hands[1].palmPosition;
}else{
rHand = frame.hands[1].palmPosition;
lHand = frame.hands[0].palmPosition;
}
console.log("left hand pos:"+JSON.stringify(lHand)+" right hand pos:"+JSON.stringify(rHand));
var l = (lHand[2]/-256.0);
var r = (rHand[2]/256.0);
}else{
playerVelocity.x = 0;
playerVelocity.y = 0;
playerVelocity.z = 0;
}
});
var WebGLSetup = function(callback){
console.log("webgl setup");
view = document.getElementById('gameView');
view.height = window.innerHeight;
view.width = window.innerWidth;
var WIDTH = view.width
,HEIGHT = view.height;
console.log(WIDTH+"/"+HEIGHT);
var ASPECT = WIDTH/HEIGHT
,VIEW_ANGLE = 60
,NEAR = 0.1
,FAR = 10000;
renderer = new THREE.WebGLRenderer();
camera = new THREE.PerspectiveCamera(
VIEW_ANGLE
,ASPECT
,NEAR
,FAR);
//setup resize event
window.onresize = function(){
view.width = window.innerWidth;
view.height = window.innerHeight;
camera.aspect = view.width/view.height;
renderer.setSize(view.width,view.height);
camera.updateProjectionMatrix();
};
scene = new THREE.Scene();
scene.add(camera);
camera.position.z = 300;
camera.position.y = 40;
camera.rotation.y = 270;
renderer.setSize(WIDTH, HEIGHT);
view.appendChild(renderer.domElement);
callback();
};
var initScene = function(callback){
console.log("init scene");
var radius = 25
,segs = 16
,rings = 16;
var pointLight = new THREE.PointLight(0xFFFFFF);
pointLight.position.x = 10;
pointLight.position.y = 50;
pointLight.position.z = 130;
scene.add(pointLight);
var sphereMaterial = new THREE.MeshPhongMaterial({
color: 0xeeeeee
,shininess:100
,metal: true
});
player = new THREE.Mesh(
new THREE.SphereGeometry(
radius
,segs
,rings
)
,sphereMaterial
);
player.scale.y = 1.5;
scene.add(player);
//terrain
/*
//trying to use TerrainGeometry
var image = new Image();
image.src = "https://raw.github.com/BonsaiDen/TerrainGeometry/gh-pages/images/height.png";
var geo = new TerrainGeometry(300,300, image, 1.0)
,mat = new THREE.MeshPhongMaterial({color:0x00dd33})
,mesh = new THREE.Mesh(geo, mat);
geo.scale = mesh.scale;
geo.position = mesh.position;
scene.add(mesh);
*/
//backup flat world
var geo = new THREE.PlaneGeometry(10000,10000,5,5)
,mat = new THREE.MeshPhongMaterial({color:0x00dd33})
,mesh = new THREE.Mesh(geo,mat);
scene.add(mesh);
callback();
};
var lastTime = Date.now();
var update = function(){
/* console.log("update"); */
var currentTime = Date.now();
var dt = currentTime - lastTime
if(playerVelocity.x != 0 || playerVelocity.z != 0){
console.log("dt:"+dt+" playerVelocity:"+JSON.stringify(playerVelocity)+" playerPos:"+JSON.stringify(player.position)+"cameraPos:"+JSON.stringify(camera.position));
}
var tmpVel = playerVelocity;
player.position.add(tmpVel.multiplyScalar(dt/1000));
var tmpZ =player.position.z;
camera.position.copy(player.position);
camera.position.y = camera.position.y+40;//-(playerVelocity.y*10);
/* camera.position.x = camera.position.x-(playerVelocity.x*10); */
camera.position.z = tmpZ+300;
lastTime = currentTime;
};
var gameloop = function(callback){
/* console.log("gameloop"); */
update();
renderer.render(scene,camera);
requestAnimFrame(gameloop);
};