-
Notifications
You must be signed in to change notification settings - Fork 0
/
three.html
159 lines (111 loc) · 4.25 KB
/
three.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
<!DOCTYPE html>
<html>
<head>
<!-- //importing tweenjs and cannon (we might not actually need tweenjs now as cannon is handling physics and therefore movement) -->
<script src="https://code.createjs.com/1.0.0/tweenjs.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/cannon.js/0.6.2/cannon.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://unpkg.com/three@0.85.0/examples/js/controls/OrbitControls.js"></script>
<meta charset="utf-8">
<title>My first three.js app</title>
<style>
body {
margin: 0;
}
</style>
</head>
<body>
<script>
//creating our global variables
var world, mass, body, shape, timeStep=1/60, groundBody, groundShape,
camera, scene, renderer, geometry, material, mesh;
//call our initiatilisers and animate function
//Calling our inititaliser to set up three.js
initThree();
// Initialising Cannon
initCannon();
//literally just begins the animation
animate();
//initialising Cannon (Our physics engine)
function initCannon() {
world = new CANNON.World();
//here we are setting gravity to the world
world.gravity.set(0,0,-10);
world.broadphase = new CANNON.NaiveBroadphase();
world.solver.interations = 10;
const groundMaterial = new CANNON.Material("groundMaterial");
const boxMaterial = new CANNON.Material("boxMaterial");
const boxGroundContactMaterial = new CANNON.ContactMaterial(boxMaterial, groundMaterial, {
friction: 0.3,
restitution: -1,
contactEquationStiffness:100
});
world.addContactMaterial(boxGroundContactMaterial);
shape = new CANNON.Box(new CANNON.Vec3(1,1,1));
mass = 1;
body = new CANNON.Body({
mass: 1,
material: boxMaterial
});
body.addShape(shape);
groundShape = new CANNON.Plane( new CANNON.Vec3(400,200,1))
groundBody = new CANNON.Body({ mass: 0, material:groundMaterial })
groundBody.addShape(groundShape);
groundBody.position.set(2, 40, -15);
//Adding velocity here, we can make it spin in any direction we want
body.angularVelocity.set(0,0,0);
body.angularDamping = 0.5;
world.addBody(groundBody)
world.addBody(body);
}
document.body.onkeyup = function(e){
if(e.keyCode == 32){
body.position.set(0,0,0);
}
}
//initialising three js
function initThree() {
//creating THRE
scene = new THREE.Scene();
//setting camera perspective
camera = new THREE.PerspectiveCamera (75, window.innerWidth / window.innerHeight, 1, 100);
// setting camera position
camera.position.z = 5;
//Adding camera to the scene
scene.add( camera );
geometry = new THREE.BoxGeometry(1, 1, 1);
geometry1 = new THREE.PlaneGeometry( 400, 200, 1 );
material = new THREE.MeshBasicMaterial( {color: 0x00FF00});
material2 = new THREE.MeshBasicMaterial({color: 0x0fFFF00})
floor = new THREE.Mesh(geometry1, material2);
mesh = new THREE.Mesh( geometry, material );
scene.add ( mesh );
scene.add (floor);
renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight);
document.body.appendChild( renderer.domElement );
//giving us control of the camera
const controls = new THREE.OrbitControls( camera, renderer.domElement );
}
//animation function (Basically handles runnning the animation of our scene)
function animate() {
requestAnimationFrame( animate );
updatePhysics();
render();
}
//this is the physics updater for Cannon
function updatePhysics() {
//step the physics world
world.step(timeStep);
//Copy Coordinates from Cannon.js to Three.js
floor.position.copy(groundBody.position);
mesh.position.copy(body.position);
// mesh.quaternion.copy(body.quaternion);
// floor.quaternion.copy(groundBody.quaternion);
}
function render() {
renderer.render ( scene, camera );
}
</script>
</body>
</html>