forked from byWulf/threejs-dice
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rolling.html
170 lines (139 loc) · 5.03 KB
/
rolling.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
160
161
162
163
164
165
166
167
168
169
170
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="../node_modules/three/build/three.js"></script>
<script src="../node_modules/cannon-es/build/cannon-es.js"></script>
<script src="../node_modules/three/examples/js/controls/OrbitControls.js"></script>
<script src="../node_modules/three/examples/js/libs/stats.min.js"></script>
<script src="../lib/dice.js"></script>
</head>
<body>
<div id="ThreeJS" style="position: absolute; left:0px; top:0px"></div>
<script>
// MAIN
// standard global variables
var container, scene, camera, renderer, controls, stats, world, dice = [];
init();
// FUNCTIONS
function init()
{
// SCENE
scene = new THREE.Scene();
// CAMERA
var SCREEN_WIDTH = window.innerWidth, SCREEN_HEIGHT = window.innerHeight;
var VIEW_ANGLE = 45, ASPECT = SCREEN_WIDTH / SCREEN_HEIGHT, NEAR = 0.01, FAR = 20000;
camera = new THREE.PerspectiveCamera( VIEW_ANGLE, ASPECT, NEAR, FAR);
scene.add(camera);
camera.position.set(0,30,30);
// RENDERER
renderer = new THREE.WebGLRenderer( {antialias:true} );
renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
container = document.getElementById( 'ThreeJS' );
container.appendChild( renderer.domElement );
// EVENTS
// CONTROLS
controls = new THREE.OrbitControls( camera, renderer.domElement );
// STATS
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.bottom = '0px';
stats.domElement.style.zIndex = '100';
container.appendChild( stats.domElement );
let ambient = new THREE.AmbientLight('#ffffff', 0.3);
scene.add(ambient);
let directionalLight = new THREE.DirectionalLight('#ffffff', 0.5);
directionalLight.position.x = -1000;
directionalLight.position.y = 1000;
directionalLight.position.z = 1000;
scene.add(directionalLight);
let light = new THREE.SpotLight(0xefdfd5, 1.3);
light.position.y = 100;
light.target.position.set(0, 0, 0);
light.castShadow = true;
light.shadow.camera.near = 50;
light.shadow.camera.far = 110;
light.shadow.mapSize.width = 1024;
light.shadow.mapSize.height = 1024;
scene.add(light);
// FLOOR
var floorMaterial = new THREE.MeshPhongMaterial( { color: '#00aa00', side: THREE.DoubleSide } );
var floorGeometry = new THREE.PlaneGeometry(30, 30, 10, 10);
var floor = new THREE.Mesh(floorGeometry, floorMaterial);
floor.receiveShadow = true;
floor.rotation.x = Math.PI / 2;
scene.add(floor);
// SKYBOX/FOG
var skyBoxGeometry = new THREE.BoxGeometry( 10000, 10000, 10000 );
var skyBoxMaterial = new THREE.MeshPhongMaterial( { color: 0x9999ff, side: THREE.BackSide } );
var skyBox = new THREE.Mesh( skyBoxGeometry, skyBoxMaterial );
// scene.add(skyBox);
scene.fog = new THREE.FogExp2( 0x9999ff, 0.00025 );
////////////
// CUSTOM //
////////////
world = new CANNON.World();
world.gravity.set(0, -9.82 * 20, 0);
world.broadphase = new CANNON.NaiveBroadphase();
world.solver.iterations = 16;
DiceManager.setWorld(world);
//Floor
let floorBody = new CANNON.Body({mass: 0, shape: new CANNON.Plane(), material: DiceManager.floorBodyMaterial});
floorBody.quaternion.setFromAxisAngle(new CANNON.Vec3(1, 0, 0), -Math.PI / 2);
world.add(floorBody);
//Walls
var colors = ['#ff0000', '#ffff00', '#00ff00', '#0000ff', '#ff00ff'];
for (var i = 0; i < 5; i++) {
var die = new DiceD6({size: 1.5, backColor: colors[i]});
scene.add(die.getObject());
dice.push(die);
}
function randomDiceThrow() {
var diceValues = [];
for (var i = 0; i < dice.length; i++) {
let yRand = Math.random() * 20
dice[i].resetBody(); // As the die is going to be reused between throws, it is necessary to reset the body
dice[i].getObject().position.x = -15 - (i % 3) * 1.5;
dice[i].getObject().position.y = 2 + Math.floor(i / 3) * 1.5;
dice[i].getObject().position.z = -15 + (i % 3) * 1.5;
dice[i].getObject().quaternion.x = (Math.random()*90-45) * Math.PI / 180;
dice[i].getObject().quaternion.z = (Math.random()*90-45) * Math.PI / 180;
dice[i].updateBodyFromMesh();
let rand = Math.random() * 5;
dice[i].getObject().body.velocity.set(25 + rand, 40 + yRand, 15 + rand);
dice[i].getObject().body.angularVelocity.set(20 * Math.random() -10, 20 * Math.random() -10, 20 * Math.random() -10);
diceValues.push({dice: dice[i], value: i + 1});
}
DiceManager.prepareValues(diceValues);
}
setInterval(randomDiceThrow, 3000);
randomDiceThrow();
requestAnimationFrame( animate );
}
function animate()
{
updatePhysics();
render();
update();
requestAnimationFrame( animate );
}
function updatePhysics() {
world.step(1.0 / 60.0);
for (var i in dice) {
dice[i].updateMeshFromBody();
}
}
function update()
{
controls.update();
stats.update();
}
function render()
{
renderer.render( scene, camera );
}
</script>
</body>
</html>