-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdice_roller.html
369 lines (301 loc) · 13.1 KB
/
dice_roller.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>
Dice Roller
</title>
<script src="js/ammo.js" type="text/javascript">
</script>
<script src="js/CubicVR.js" type="text/javascript">
</script>
<script type='text/javascript'>
addedObjects = []
mainScene = null;
mainPhysics = null;
mainObjects = null;
function resetDice()
{
if(mainScene == null)
return;
for (var i = 0; i < addedObjects.length; i++) {
var src = addedObjects[i];
mainScene.removeSceneObject(src.scene);
mainPhysics.remove(src.physics);
}
addedObjects = []
spawnObjects(mainScene, mainPhysics, mainObjects)
}
function generateObjects() {
var result = [];
var colladaScene = CubicVR.loadCollada("images/die_4.dae","images/");
var die4mesh = colladaScene.getSceneObject("Die4Side").obj;
var die4Collision = new CubicVR.CollisionMap({
type: "mesh",
mesh: die4mesh
});
var colladaScene = CubicVR.loadCollada("images/die_6.dae","images/");
var die6mesh = colladaScene.getSceneObject("Die6n").obj;
var die6Collision = new CubicVR.CollisionMap({
type: "mesh",
mesh: die6mesh
});
var colladaScene = CubicVR.loadCollada("images/die_8.dae","images/");
var die8mesh = colladaScene.getSceneObject("Die").obj;
var die8Collision = new CubicVR.CollisionMap({
type: "mesh",
mesh: die8mesh
});
var colladaScene = CubicVR.loadCollada("images/die_10.dae","images/");
var die10mesh = colladaScene.getSceneObject("Die10").obj;
var die10Collision = new CubicVR.CollisionMap({
type: "mesh",
mesh: die10mesh
});
var colladaScene = CubicVR.loadCollada("images/die_12.dae","images/");
var die12mesh = colladaScene.getSceneObject("Die12").obj;
var die12Collision = new CubicVR.CollisionMap({
type: "mesh",
mesh: die12mesh
});
var colladaScene = CubicVR.loadCollada("images/die_20.dae","images/");
var die20mesh = colladaScene.getSceneObject("Die20").obj;
var die20Collision = new CubicVR.CollisionMap({
type: "mesh",
mesh: die20mesh
});
result.push({mesh:die4mesh,collision:die4Collision});
result.push({mesh:die6mesh,collision:die6Collision});
result.push({mesh:die8mesh,collision:die8Collision});
result.push({mesh:die10mesh,collision:die10Collision});
result.push({mesh:die12mesh,collision:die12Collision});
result.push({mesh:die20mesh,collision:die20Collision});
return result;
}
function spawnObjects(scene,physics,objlist) {
var nobjs = objlist.length;
for (var i = 0; i < nobjs; i++) {
var src = objlist[i];
var sceneObj = new CubicVR.SceneObject({
mesh:src.mesh,
position:[(i -2) * 2, -3, 0],
rotation:[Math.random()*360,Math.random()*360,Math.random()*360]
});
var rigidObj = new CubicVR.RigidBody(sceneObj, {
type: "dynamic",
mass: 15,
collision: src.collision
});
scene.bind(sceneObj);
physics.bind(rigidObj);
addedObjects.push({scene: sceneObj, physics: rigidObj});
}
}
function webGLStart() {
// by default generate a full screen canvas with automatic resize
var gl = CubicVR.init();
var canvas = CubicVR.getCanvas();
if (!gl) {
alert("Sorry, no WebGL support.");
return;
};
// New scene with our canvas dimensions and default camera with FOV 80
var scene = new CubicVR.Scene({
camera: {
width: canvas.width,
height: canvas.height,
fov: 80,
position: [5, 5, -5],
target: [0, -3, 0]
},
light: {
type: "area",
intensity: 0.9,
mapRes: 2048,
areaCeiling: 40,
areaFloor: -40,
areaAxis: [-2,-2], // specified in degrees east/west north/south
distance: 60
}
});
CubicVR.setSoftShadows(true);
var floorMaterial = new CubicVR.Material();
var floorMesh = new CubicVR.Mesh({
primitive: {
type: "box",
size: 1.0,
material: {
specular:[0,0,0],
shininess: 0.9,
env_amount: 1.0,
textures: {
color: "images/wood.jpg"
}
},
uv: {
projectionMode: "cubic",
scale: [0.05, 0.05, 0.05]
}
},
compile:true
});
var floorObject = new CubicVR.SceneObject({
mesh: floorMesh,
scale: [25, 1, 25],
position: [0, -5, 0],
});
var wallMesh = new CubicVR.Mesh({
primitive: {
type: "box",
size: 1.0,
material: {
specular:[0,0,0],
shininess: 0.9,
env_amount: 1.0,
opacity: 0,
textures: {
color: "images/wood.jpg"
}
},
uv: {
projectionMode: "cubic",
scale: [0.05, 0.05, 0.05]
}
},
compile:true
});
var wallObject = new CubicVR.SceneObject({
mesh: wallMesh,
scale: [1, 100, 25],
position: [13, 45, 0],
});
var wallObject2 = new CubicVR.SceneObject({
mesh: wallMesh,
scale: [1, 100, 25],
position: [-13, 45, 0],
});
var wallObject3 = new CubicVR.SceneObject({
mesh: wallMesh,
scale: [25, 100, 1],
position: [0, 45, 13],
});
var wallObject4 = new CubicVR.SceneObject({
mesh: wallMesh,
scale: [25, 100, 1],
position: [0, 45, -13],
});
floorObject.shadowCast = false;
wallObject.shadowCast = false;
wallObject2.shadowCast = false;
wallObject3.shadowCast = false;
wallObject4.shadowCast = false;
// init physics manager
var physics = new CubicVR.ScenePhysics();
// create floor rigid body
var rigidFloor = new CubicVR.RigidBody(floorObject, {
type: "static",
collision: {
type: "box",
size: floorObject.scale
}
});
// create floor rigid body
var rigidWall = new CubicVR.RigidBody(wallObject, {
type: "static",
collision: {
type: "box",
size: wallObject.scale
}
});
var rigidWall2 = new CubicVR.RigidBody(wallObject2, {
type: "static",
collision: {
type: "box",
size: wallObject2.scale
}
});
var rigidWall3 = new CubicVR.RigidBody(wallObject3, {
type: "static",
collision: {
type: "box",
size: wallObject3.scale
}
});
var rigidWall4 = new CubicVR.RigidBody(wallObject4, {
type: "static",
collision: {
type: "box",
size: wallObject4.scale
}
});
// bind floor to physics
physics.bind(rigidFloor);
physics.bind(rigidWall);
physics.bind(rigidWall2);
physics.bind(rigidWall3);
physics.bind(rigidWall4);
// Add SceneObject containing the mesh to the scene
scene.bind(floorObject);
scene.bind(wallObject);
scene.bind(wallObject2);
scene.bind(wallObject3);
scene.bind(wallObject4);
mainPhysics = physics;
mainScene = scene;
// initialize a mouse view controller
mvc = new CubicVR.MouseViewController(canvas, scene.camera);
// Add our scene to the window resize list
CubicVR.addResizeable(scene);
var objlist = generateObjects();
spawnObjects(scene,physics,objlist);
mainObjects = objlist;
var pickConstraint = null;
var pickDist = 0;
mvc.setEvents({
mouseMove: function (ctx, mpos, mdelta, keyState) {
if (!ctx.mdown) return;
if (pickConstraint) {
pickConstraint.setPosition(scene.camera.unProject(mpos[0],mpos[1],pickDist));
} else {
ctx.orbitView(mdelta);
}
// ctx.panView(mdelta);
},
mouseWheel: function (ctx, mpos, wdelta, keyState) {
ctx.zoomView(wdelta);
},
mouseDown: function (ctx, mpos, keyState) {
var rayTo = scene.camera.unProject(mpos[0],mpos[1]);
var result = physics.getRayHit(scene.camera.position,rayTo);
if (result && !pickConstraint) {
pickConstraint = new CubicVR.Constraint({
type: CubicVR.enums.physics.constraint.P2P,
rigidBody: result.rigidBody,
positionA: result.localPosition
});
physics.addConstraint(pickConstraint);
pickDist = CubicVR.vec3.length(CubicVR.vec3.subtract(scene.camera.position,result.position));
pickConstraint.setPosition(scene.camera.unProject(mpos[0],mpos[1],pickDist));
}
},
mouseUp: function(ctx, mpos, keyState) {
if (pickConstraint) {
physics.removeConstraint(pickConstraint);
pickConstraint = null;
}
},
keyDown: null,
keyUp: null
});
window.addEventListener("keypress",function(evt) { physics.reset(); },false);
// Start our main drawing loop, it provides a timer and the gl context as parameters
CubicVR.MainLoop(function(timer, gl) {
physics.stepSimulation(timer.getLastUpdateSeconds());
scene.render();
});
}
</script>
</head>
<body onLoad="webGLStart();">
<button type="button" id="reset-button" style="z-index: 1; position: absolute; right: 15px; bottom: 15px;" onclick="resetDice()">Reset Dice</button>
</body>
</html>