-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateController.js
136 lines (123 loc) · 3.48 KB
/
CreateController.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
/**
* @author quinnciccoretti
* @class CreateController
* Creates things
*/
class CreateController extends BasicController {
constructor(id){
super( id, "#0066ff", "Create");
//Initialize all the geometries that will be thrown in a scene
var modelabel;
/**Table*/
var table_geometry = new THREE.BoxGeometry( 0.5, 0.8, 0.5 );
var table_material = new THREE.MeshBasicMaterial({ color: 0x888888 })
var table = new Physijs.BoxMesh( table_geometry, table_material, 0);
table.castShadow = true;
table.receiveShadow = true;
/**Sphere*/
var sphere = new Physijs.SphereMesh(
new THREE.SphereGeometry( .25, 12, 12 ),
new THREE.MeshBasicMaterial({ color: 0xff0000 }),
0 //mass
);
/**Brick*/
var brick_material = Physijs.createMaterial(
new THREE.MeshLambertMaterial({ map: loader.load( 'img/brick.jpg' ) }),
.4, // low friction
.4 // high restitution
);
brick_material.map.wrapS = THREE.RepeatWrapping;
brick_material.map.repeat.set( .25, .25 );
var brick = new Physijs.BoxMesh(
new THREE.BoxGeometry( 0.2, 0.2, 0.2 ),
brick_material,
1
);
var object;
var modelist = ["table", "sphere","scene", "brick"];
var mode = 0;
this.addEventListener( 'gripsdown', this.onGripsDown );
this.addEventListener( 'triggerdown', this.onTriggerDown );
this.addEventListener( 'triggerup', this.onTriggerUp );
}
/**
* The following three methods, on_activate, on_deactivate, and handle_update are a rather compicated way to
* remove the physics object around the controller so it does not
* collide with created objects.
*/
on_activate (){
this.userData.points = [ new THREE.Vector3(), new THREE.Vector3() ];
this.userData.matrices = [ new THREE.Matrix4(), new THREE.Matrix4() ];
user.add( this );
//add the model of the controller
this.add(basic_controller_models[id]);
this.make_nameplate();
//remove physobj
scene.remove(phys_obj_list[id]);
}
/**
* add back the physics object
*/
on_deactivate (){
//this removes the model to conserve memory
this.remove(basic_controller_models[id]);
user.remove(this);
//add back physobj
scene.add(phys_obj_list[id]);
}
// /**
// * Updates controller position data based on gamepad pose
// */
// handle_update () {
// this.update(); //refreshes controller data
// //dont update physics
// //this.update_phys_objects();
// }
update_phys_objects (){}
/**
* Creates object
*/
onTriggerDown(){
//a sphere to launch
if ( mode == 0 ) {
object = table.clone();
}
if ( mode == 1 ) {
object = sphere.clone();
}
if (mode == 2){
var my_pos = this.get_absolute_position();
create_scene_objects(my_pos.x, my_pos.y, my_pos.z);
object = brick.clone();
}
if (mode == 3){
object = brick.clone();
}
//sets it a little higher than the halo
//relative to controller
object.position.y = -0.016;
object.position.z = -0.043;
this.add(object);
}
/** Add object to scene */
onTriggerUp(){
object.matrix.premultiply( this.matrixWorld );
object.matrix.decompose( object.position, object.quaternion, object.scale );
var pos = this.get_absolute_position();
object.position.set(pos.x, pos.y, pos.z);
this.remove(object);
scene.add(object);
object.setLinearVelocity(this.get_velocity().multiplyScalar(4));
this.pulse(.5,25);
}
/** Switch modes */
onGripsDown( event ) {
this.ui.remove(modelabel);
mode +=1;
if(mode == modelist.length){
mode = 0;
}
modelabel = create_text_mesh(modelist[mode], 2, "#ff0000");
this.ui.add(modelabel);
}
}