-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain - Copy.html
261 lines (207 loc) · 10.3 KB
/
main - Copy.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
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Babylon Template</title>
<style>
html,
body {
overflow: hidden;
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
#renderCanvas {
width: 100%;
height: 100%;
touch-action: none;
}
</style>
<script src="https://preview.babylonjs.com/babylon.js"></script>
<script src="https://preview.babylonjs.com/loaders/babylonjs.loaders.min.js"></script>
<script src="https://code.jquery.com/pep/0.4.3/pep.js"></script>
<script src="https://preview.babylonjs.com/gui/babylon.gui.js"></script>
</head>
<body>
<canvas id="renderCanvas" touch-action="none"></canvas> <!-- touch-action="none" for best results from PEP -->
<script>
var canvas = document.getElementById("renderCanvas"); // Get the canvas element
var engine = new BABYLON.Engine(canvas, true); // Generate the BABYLON 3D engine
const addPointerInteraction = (scene) => {
scene.onPointerObservable.add((pointerInfo) => {
switch (pointerInfo.type) {
case BABYLON.PointerEventTypes.POINTERDOWN:
console.log("POINTER DOWN");
break;
case BABYLON.PointerEventTypes.POINTERUP:
console.log("POINTER UP");
break;
case BABYLON.PointerEventTypes.POINTERMOVE:
console.log("POINTER MOVE");
break;
case BABYLON.PointerEventTypes.POINTERWHEEL:
console.log("POINTER WHEEL");
break;
case BABYLON.PointerEventTypes.POINTERPICK:
console.log("POINTER PICK");
break;
case BABYLON.PointerEventTypes.POINTERTAP:
console.log("POINTER TAP");
break;
case BABYLON.PointerEventTypes.POINTERDOUBLETAP:
console.log("POINTER DOUBLE-TAP");
break;
}
});
}
/******* Add the create scene function ******/
var createScene = function () {
// Create the scene space
var scene = new BABYLON.Scene(engine);
addPointerInteraction(scene);
// Add a camera to the scene and attach it to the canvas
var camera = new BABYLON.ArcRotateCamera("Camera", Math.PI / 2, Math.PI / 2, 2, new BABYLON.Vector3(0, 0, 0), scene);
camera.attachControl(canvas, true);
// Add lights to the scene
var light1 = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(1, 1, 0), scene);
var light2 = new BABYLON.PointLight("light2", new BABYLON.Vector3(0, 1, -1), scene);
/****************************Key Control Multiple Keys************************************************/
var map = {}; //object for multiple key presses
scene.actionManager = new BABYLON.ActionManager(scene);
scene.actionManager.registerAction(new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnKeyDownTrigger, function (evt) {
map[evt.sourceEvent.key] = evt.sourceEvent.type == "keydown";
}));
scene.actionManager.registerAction(new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnKeyUpTrigger, function (evt) {
map[evt.sourceEvent.key] = evt.sourceEvent.type == "keydown";
}));
scene.registerAfterRender(function () {
//------------ translate
if ((map["w"] || map["W"])) {
translateCube(0, 0, 0.1)
};
if ((map["s"] || map["S"])) {
translateCube(0, 0, -0.1)
};
if ((map["d"] || map["D"])) {
translateCube(-0.1, 0, 0)
};
if ((map["a"] || map["A"])) {
translateCube(0.1, 0, 0)
};
//------------ rotate
if ((map["i"] || map["I"])) {
rotateCube(0, 0, 1)
};
if ((map["k"] || map["K"])) {
rotateCube(0, 0, -1)
};
if ((map["j"] || map["J"])) {
rotateFront(1)
};
if ((map["l"] || map["L"])) {
rotateFront(-1)
};
});
let rubiks = [];
let flatRubiks = [];
let unit = 1.1;
var faceColors = new Array(6);
faceColors[0] = new BABYLON.Color4(1, 0, 0, 1);
faceColors[1] = new BABYLON.Color4(0, 1, 0, 1);
faceColors[2] = new BABYLON.Color4(0, 0, 1, 1);
faceColors[3] = new BABYLON.Color4(1, 1, 0, 1);
faceColors[4] = new BABYLON.Color4(0, 1, 1, 1);
faceColors[5] = new BABYLON.Color4(1, 1, 1, 1);
for (let x = 1; x <= 1; x++) {
rubiks.push([]);
for (let y = 1; y <= 3; y++) {
rubiks[x - 1].push([]);
for (let z = 1; z <= 3; z++) {
let tempBox = BABYLON.MeshBuilder.CreateBox(`box${x}${y}${z}`, { faceColors }, scene);
tempBox.position.x = x * unit;
tempBox.position.y = y * unit;
tempBox.position.z = z * unit;
flatRubiks.push(tempBox);
rubiks[x - 1][y - 1].push(tempBox)
}
}
}
function degrees_to_radians(degrees) {
var pi = Math.PI;
return degrees * (pi / 180);
}
const translateCube = (x, y, z) => {
flatRubiks.forEach(cube => {
cube.position.x += x;
cube.position.y += y;
cube.position.z += z;
});
}
const rotateCube = (x, y, z) => {
flatRubiks.forEach(cube => {
cube.rotation.x += x;
cube.rotation.y += y;
cube.rotation.z += z;
});
}
const rotateFront = (angle) => {
let box1 = rubiks[0][1][1];
box1.rotation.x -= degrees_to_radians(angle);
for (let i = 0; i <= 0; i++) {
for (let j = 0; j <= 2; j++) {
for (let k = 0; k <= 2; k++) {
let tmp = rubiks[i][j][k];
let index = i + j + k;
tmp.rotation.x = box1.rotation.x;
}
}
}
rubiks[0][1][2].rotation.x = box1.rotation.x;
rubiks[0][1][2].position.y = Math.cos(box1.rotation.x) * unit + box1.position.y;
rubiks[0][1][2].position.z = Math.sin(box1.rotation.x) * unit + box1.position.z;
rubiks[0][2][2].rotation.x = box1.rotation.x;
rubiks[0][2][2].position.y = Math.cos(box1.rotation.x + degrees_to_radians(45)) * unit + box1.position.y;
rubiks[0][2][2].position.z = Math.sin(box1.rotation.x + degrees_to_radians(45)) * unit + box1.position.z;
rubiks[0][2][1].rotation.x = box1.rotation.x;
rubiks[0][2][1].position.y = Math.cos(box1.rotation.x + degrees_to_radians(90)) * unit + box1.position.y;
rubiks[0][2][1].position.z = Math.sin(box1.rotation.x + degrees_to_radians(90)) * unit + box1.position.z;
rubiks[0][2][0].rotation.x = box1.rotation.x;
rubiks[0][2][0].position.y = Math.cos(box1.rotation.x + degrees_to_radians(125)) * unit + box1.position.y;
rubiks[0][2][0].position.z = Math.sin(box1.rotation.x + degrees_to_radians(125)) * unit + box1.position.z;
rubiks[0][1][0].rotation.x = box1.rotation.x;
rubiks[0][1][0].position.y = Math.cos(box1.rotation.x + degrees_to_radians(180)) * unit + box1.position.y;
rubiks[0][1][0].position.z = Math.sin(box1.rotation.x + degrees_to_radians(180)) * unit + box1.position.z;
rubiks[0][2][0].rotation.x = box1.rotation.x;
rubiks[0][2][0].position.y = Math.cos(box1.rotation.x + degrees_to_radians(225)) * unit + box1.position.y;
rubiks[0][2][0].position.z = Math.sin(box1.rotation.x + degrees_to_radians(225)) * unit + box1.position.z;
rubiks[0][0][1].rotation.x = box1.rotation.x;
rubiks[0][0][1].position.y = Math.cos(box1.rotation.x + degrees_to_radians(270)) * unit + box1.position.y;
rubiks[0][0][1].position.z = Math.sin(box1.rotation.x + degrees_to_radians(270)) * unit + box1.position.z;
// rubiks[0][0][2].rotation.x = box1.rotation.x;
// rubiks[0][0][2].position.y = Math.cos(box1.rotation.x + degrees_to_radians(45)) * 1.4 * unit + box1.position.y;
// rubiks[0][0][2].position.z = Math.sin(box1.rotation.x + degrees_to_radians(45)) * 1.4 * unit + box1.position.z;
}
window.rotateFront = rotateFront;
window.rubiks = rubiks;
window.translateCube = translateCube;
return scene;
};
/******* End of the create scene function ******/
var scene = createScene(); //Call the createScene function
// Register a render loop to repeatedly render the scene
engine.runRenderLoop(function () {
scene.render();
});
// Watch for browser/canvas resize events
window.addEventListener("resize", function () {
engine.resize();
});
canvas.addEventListener("onClick", () => {
alert('kaki');
myPoints1.push(new Vector3(Math.random() * 10 - 5), new Vector3(Math.random() * 10 - 5), new Vector3(Math.random() * 10 - 5));
line2 = BABYLON.MeshBuilder.CreateLines("lines", { points: myPoints1, instance: line2 }, scene);
})
</script>
</body>
</html>