-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
158 lines (131 loc) · 3.38 KB
/
main.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
var scene, camera, renderer, world, mass, body, shape, timeStep=1/60, geometry, material, mesh;;
var camerax = 0;
var cameray = 3.5;
var cameraz = 5;
// Create a plane
var groundBody = new CANNON.Body({
mass: 0 // mass == 0 makes the body static
});
var groundShape = new CANNON.Plane();
groundBody.addShape(groundShape);
var WIDTH = window.innerWidth;
var HEIGHT = window.innerHeight;
var SPEED = 0.01;
init();
initCannon();
render();
function init()
{
scene = new THREE.Scene();
initCube();
initCamera();
initRenderer();
generateLevel();
setInterval(function()
{
if (keyPressed["W"])
{
cameraz -= 0.1;
}
if (keyPressed["A"])
{
camerax -= 0.1;
}
if (keyPressed["D"])
{
camerax += 0.1;
}
if (keyPressed["S"])
{
cameraz += 0.1;
}
if (deltax > 0)
{
camera.rotation.y -= deltax/1000;
}
else if (deltax < 0)
{
camera.rotation.y += -deltax/1000;
}
camera.position.set(camerax, cameray, cameraz);
}, 0);
document.body.appendChild(renderer.domElement);
};
function initCamera()
{
camera = new THREE.PerspectiveCamera(70, WIDTH / HEIGHT, 1, 1000);
camera.position.set(0, 3.5, 5);
camera.rotation.set(0,0,0);
scene.add(camera);
};
function initRenderer()
{
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(WIDTH, HEIGHT);
};
function initCube()
{
cube = new THREE.Mesh(new THREE.BoxGeometry(2, 2, 2), new THREE.MeshNormalMaterial());
cube.position.y = 30;
scene.add(cube);
};
function rotateCube()
{
cube.rotation.x += SPEED * 2;
cube.rotation.y += SPEED;
cube.rotation.z += SPEED * 3;
};
function render()
{
requestAnimationFrame(render);
updatePhysics();
rotateCube();
renderer.render(scene, camera);
};
function initCannon() {
world = new CANNON.World();
world.gravity.set(0,0,-9.82);
world.broadphase = new CANNON.NaiveBroadphase();
world.solver.iterations = 10;
shape = new CANNON.Box(new CANNON.Vec3(1,1,1));
mass = 1;
body = new CANNON.Body({
mass: 1
});
body.addShape(shape);
body.angularVelocity.set(0,10,0);
body.angularDamping = 0.5;
world.add(body);
world.add(groundBody);
}
function updatePhysics() {
// Step the physics world
world.step(timeStep);
// Copy coordinates from Cannon.js to Three.js
cube.position.copy(body.position);
cube.quaternion.copy(body.quaternion);
}
function generateLevel()
{
// load a texture, set wrap mode to repeat
var texture = THREE.ImageUtils.loadTexture( "./textures/moriarty.jpg" );
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set( 1.4, 1.4 );
floor = new THREE.Mesh(new THREE.CubeGeometry(30,1,1000), new THREE.MeshBasicMaterial({color: 'blue'}));
floor.position.set(0,-2,-480);
scene.add(floor);
leftWall = new THREE.Mesh(new THREE.CubeGeometry(1,10,1000), new THREE.MeshBasicMaterial({map : texture}));
leftWall.position.set(-15,-2,-480);
//leftWall.material.map = getImage('textures/moriarty.jpg');
scene.add(leftWall);
rightWall = new THREE.Mesh(new THREE.CubeGeometry(1,10,1000), new THREE.MeshBasicMaterial({color: 'red'}));
rightWall.position.set(15,-2,-480);
scene.add(rightWall);
}
function getImage(path)
{
var image = new Image();
image.src = path;
return image;
}