-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.js
More file actions
171 lines (164 loc) · 7.07 KB
/
controller.js
File metadata and controls
171 lines (164 loc) · 7.07 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
162
163
164
165
166
167
168
169
170
171
import { WebGLRenderer, Shader, mat4, vec2, vec4,quat, vec3 } from './lib/threeD.js';
import {vertexShaderSrc} from './shaders/vertex.js';
import {fragmentShaderSrc} from './shaders/fragment.js';
import {fragmentPickerShaderSrc} from './shaders/fragemntPicker.js'
import {Scene} from './Game/scene.js';
export class Controller{
constructor(renderer,numPlayers=3,numSides=5){
this.scene= new Scene(renderer.domElement.width,renderer.domElement.height,numPlayers,numSides);
this.shader = new Shader(renderer.glContext(),vertexShaderSrc,fragmentShaderSrc,"normal");
this.shaderPicker= new Shader(renderer.glContext(),vertexShaderSrc,fragmentPickerShaderSrc,"picker");
this.screenWidth=renderer.domElement.width;
this.screenHeight=renderer.domElement.height;
this.shader.use();
this.move=false;
this.guiSelect=false;
this.play=false;
this.playerMove=false;
this.trackballSpehereRadiusOg=1;
this.trackballSpehereRadius=1;
this.initialMouseCoordinates=[0,0,0];
this.zoomFactor=0.1;
this.zoom=1;
this.renderer=renderer;
this.mouse=vec2.create(); //initial mouse coordinates
vec2.set(this.mouse,0,0)
this.moveMouse=vec2.create(); // final mouse coordinates
vec2.set(this.moveMouse,0,0);
}
processEvent(event){
if(event.type==='keydown'){
if((event.key==='m' || event.key==='M') && this.play===false){
this.guiSelect=!this.guiSelect;
}
else if(event.key === 'c'){
this.scene.mode= this.scene.mode===1? 2:1;
}
else if(event.key==='x' && typeof this.scene.modelSelected === 'object'&& this.scene.fraction===0){
this.scene.modelSelected.transform.rotX(this.scene.modelSelected.transform.angleX+(10*Math.PI/180));
}
else if(event.key==='y' && typeof this.scene.modelSelected === 'object' && this.scene.fraction===0){
this.scene.modelSelected.transform.rotY(this.scene.modelSelected.transform.angleY+(10*Math.PI/180));
}
else if(event.key==='z' && typeof this.scene.modelSelected === 'object' && this.scene.fraction===0){
this.scene.modelSelected.transform.rotZ(this.scene.modelSelected.transform.angleZ+(10*Math.PI/180));
}
else if(event.key==='r' && typeof this.scene.modelSelected === 'object'){
this.scene.modelSelected.reset();
}
else if(event.key==='ArrowUp' && typeof this.scene.modelSelected=== 'object' ){
this.scene.modelSelected.transform.scaleBy(0.2);
}
else if(event.key==='ArrowDown' && typeof this.scene.modelSelected=== 'object' ){
this.scene.modelSelected.transform.scaleBy(-0.2);
}
}
else if (event.type==='mousedown'){
if(this.guiSelect===false && this.scene.mode===2 && this.play===false){
this.move=true;
vec2.set(this.mouse,event.x,event.y);
vec2.set(this.moveMouse,event.x,event.y);
this.initialMouseCoordinates=this.projectToSphere(this.mouse);
}
if(this.guiSelect===false && this.play===true){
this.playerMove=true;
let normalizedMouseX=(2*event.x - this.screenWidth)/this.screenWidth;
let normalizedMouseY=(this.screenHeight- 2*event.y)/this.screenHeight;
this.playerStart=[normalizedMouseX,normalizedMouseY];
this.playerEnd=[normalizedMouseX,normalizedMouseY];
this.mouseDirection=[this.playerEnd[0]-this.playerStart[0],this.playerEnd[1]-this.playerStart[1]];
}
}
else if (event.type==='mousemove' ){
if(this.move===true && this.guiSelect===false && this.scene.mode===2 && this.play===false){
vec2.set(this.moveMouse,event.x,event.y);
this.rotateCamera3D();
}
else if(this.guiSelect===false && this.playerMove===true){
let normalizedMouseX=(2*event.x - this.screenWidth)/this.screenWidth;
let normalizedMouseY=(this.screenHeight- 2*event.y)/this.screenHeight;
this.playerEnd=[normalizedMouseX,normalizedMouseY];
this.mouseDirection=[this.playerEnd[0]-this.playerStart[0],this.playerEnd[1]-this.playerStart[1]];
let a=vec2.len(this.mouseDirection);
let theta=vec2.angle(this.mouseDirection,this.scene.moveDirection);
if((theta <Math.PI/6)||(theta> 5*(Math.PI/6))){
let projectionValue=(a*Math.cos(theta));
// *(this.scene.modelSelected.moveLength/4);
this.scene.configureFraction(projectionValue);
this.scene.resetModel();
this.scene.movePlayers();
}
}
}
else if(event.type==='mouseup'){
if(this.move===true && this.guiSelect===false && this.scene.mode===2 && this.play===false){
vec2.set(this.mouse,event.x,event.y)
vec2.set(this.moveMouse,event.x,event.y)
this.scene.setQuatIdentity();
this.move=false;
}
else if(this.guiSelect===false && this.play===true && this.playerMove===true){
this.playerMove=false;
let normalizedMouseX=(2*event.x - this.screenWidth)/this.screenWidth;
let normalizedMouseY=(this.screenHeight- 2*event.y)/this.screenHeight;
this.playerStart=[normalizedMouseX,normalizedMouseY];
this.playerEnd=[normalizedMouseX,normalizedMouseY];
}
}
else if(event.type==='wheel'){
if(this.guiSelect===false && this.scene.mode===2 ){
this.zoom+=event.deltaY>0?this.zoomFactor: -this.zoomFactor;
if(this.zoom<0) this.zoom=0;
if(this.zoom>15) this.zoomFactor=2;
else this.zoomFactor=0.1;
this.trackballSpehereRadius= this.trackballSpehereRadiusOg*this.zoom;
this.scene.scaleBy(this.zoom);
}
}
else if(event.type==='click'){
if(this.guiSelect===false){
this.shaderPicker.use();
this.renderer.clear(0.1, 0.1, 0.1, 1);
this.renderer.render(this.scene,this.shaderPicker);
this.shader.use();
let object=this.shaderPicker.readPixel(event.x,this.screenHeight-event.y-1);
if(this.scene.modelSelected.id=== undefined && object[0]!==200){
for(let model of this.scene.models){
if(model.id===object[0]){
model.select();
this.scene.configurePlayers(model);
this.play=true;
break;
}
}
}
else if(this.scene.modelSelected.id===object[0] && this.play===true){
this.scene.clearPlayerConfigurations();
this.play=false;
this.playerMove=false;
}
}
}
}
projectToSphere(mouse){
let normalizedMouseX=(2*mouse[0] - this.screenWidth)/this.screenWidth;
let normalizedMouseY=(this.screenHeight- 2*mouse[1])/this.screenHeight;
//keeping in between -1 to 1
normalizedMouseX=normalizedMouseX>1?1:normalizedMouseX;
normalizedMouseX=normalizedMouseX<-1?-1:normalizedMouseX;
normalizedMouseY=normalizedMouseY>1?1:normalizedMouseY;
normalizedMouseY=normalizedMouseY<-1?-1:normalizedMouseY;
let d=normalizedMouseX*normalizedMouseX + normalizedMouseY*normalizedMouseY;
let sphereZ=d<this.trackballSpehereRadius? Math.sqrt(this.trackballSpehereRadius-d):0.0;
let normalizeFinal= 1.0/Math.sqrt(normalizedMouseX*normalizedMouseX +normalizedMouseY*normalizedMouseY + sphereZ*sphereZ);
normalizedMouseX*=normalizeFinal, normalizedMouseY*=normalizeFinal, sphereZ*=normalizeFinal;
return [normalizedMouseX, normalizedMouseY,sphereZ]
}
rotateCamera3D(){
if(Math.abs(this.mouse[0] -this.moveMouse[0]) || Math.abs(this.mouse[1] -this.moveMouse[1])){
let finalMouseCoordinates=this.projectToSphere(this.moveMouse);
this.scene.globalTrackball(this.initialMouseCoordinates,finalMouseCoordinates);
this.initialMouseCoordinates=finalMouseCoordinates;
}
}
}