-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
204 lines (171 loc) · 4.59 KB
/
index.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import THREE, { OrbitControls } from "./src/js/three";
import CANNON from "cannon";
import { DiceManager, DiceD6 } from "threejs-dice/lib/dice";
import Stats from "stats.js";
import * as vars from "./src/js/variables";
import Game from "./src/js/game";
// standard global variables
let container;
let scene;
let camera;
let renderer;
let controls;
let stats;
let world;
let dice = [];
let game;
function addCamera() {
camera = new THREE.PerspectiveCamera(
vars.VIEW_ANGLE,
vars.ASPECT,
vars.NEAR,
vars.FAR
);
scene.add(camera);
camera.position.set(0, 30, 30);
}
function addRenderer() {
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(vars.SCREEN_WIDTH, vars.SCREEN_HEIGHT);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
}
function addLights() {
const ambient = new THREE.AmbientLight("#ffffff", 0.3);
scene.add(ambient);
const directionalLight = new THREE.DirectionalLight("#ffffff", 0.5);
directionalLight.position.x = -1000;
directionalLight.position.y = 1000;
directionalLight.position.z = 1000;
scene.add(directionalLight);
const light = new THREE.SpotLight(0xefdfd5, 0.5);
light.position.y = 100;
light.target.position.set(0, 0, 0);
light.castShadow = true;
light.shadow.camera.near = 70;
light.shadow.camera.far = 110;
light.shadow.mapSize.width = 1024;
light.shadow.mapSize.height = 1024;
scene.add(light);
}
function addStats() {
stats = new Stats();
stats.domElement.style.position = "absolute";
stats.domElement.style.bottom = "0px";
stats.domElement.style.zIndex = 100;
container.appendChild(stats.domElement);
}
function createFloor() {
const floorMaterial = new THREE.MeshPhongMaterial({
color: vars.floorColor,
side: THREE.DoubleSide
});
const floorGeometry = new THREE.PlaneGeometry(50, 50, 10, 10);
const floor = new THREE.Mesh(floorGeometry, floorMaterial);
floor.receiveShadow = true;
floor.rotation.x = Math.PI / 2;
scene.add(floor);
}
function createSkyBox() {
const skyBoxGeometry = new THREE.CubeGeometry(10000, 10000, 10000);
const skyBoxMaterial = new THREE.MeshPhongMaterial({
color: 0x9999ff,
side: THREE.BackSide
});
const skyBox = new THREE.Mesh(skyBoxGeometry, skyBoxMaterial);
scene.add(skyBox);
scene.fog = new THREE.FogExp2(0x9999ff, 0.00025);
}
function createCANNONWorld() {
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);
}
function createDice() {
for (let i = 0; i < 5; i++) {
const die = new DiceD6({ size: 1.5, backColor: vars.colors[i] });
scene.add(die.getObject());
dice.push(die);
}
}
export function randomDiceThrow() {
const diceValues = [];
for (let i = 0; i < dice.length; i++) {
let yRand = Math.random() * 20;
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, 10 + 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: Math.floor(Math.random() * 6) + 1
});
}
DiceManager.prepareValues(diceValues);
return diceValues;
}
function updatePhysics() {
world.step(1.0 / 60.0);
for (let i in dice) {
dice[i].updateMeshFromBody();
}
}
function update() {
controls.update();
stats.update();
}
function init() {
// Create scene
scene = new THREE.Scene();
addCamera();
addRenderer();
container = document.getElementById("ThreeJS");
container.appendChild(renderer.domElement);
controls = new OrbitControls(camera, renderer.domElement);
addStats();
addLights();
createFloor();
createSkyBox();
createCANNONWorld();
createDice();
// Call randomDiceThrow to initialize dice position (first throw)
randomDiceThrow();
requestAnimationFrame(animate);
game = new Game();
}
function animate() {
updatePhysics();
render();
update();
requestAnimationFrame(animate);
}
function render() {
renderer.render(scene, camera);
}
init();