-
Notifications
You must be signed in to change notification settings - Fork 1
/
animation_test.js
370 lines (321 loc) · 10.4 KB
/
animation_test.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
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
370
var canvas = document.getElementById("renderCanvas");
var engine = null;
var scene = null;
var sceneToRender = null;
var createDefaultEngine = function() { return new BABYLON.Engine(canvas, true, { preserveDrawingBuffer: true, stencil: true, disableWebGL2Support: false}); };
var playerAsset = [];
const playerPath = [
"asset/playerTest2.babylon",
];
const pi = Math.PI;
const frameRate = 60;
const speed = 2.5;
function addAnimation(leg, forward=true) {
const joint1Anim = new BABYLON.Animation("joint1Anim", "rotation.y",
frameRate, BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
const joint2Anim = new BABYLON.Animation("joint2Anim", "rotation.z",
frameRate, BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
const joint3Anim = new BABYLON.Animation("joint3Anim", "rotation.z",
frameRate, BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
const keyFrames1 = [];
const keyFrames2 = [];
var joint1 = playerAsset[1];
var joint2 = playerAsset[4];
var joint3 = playerAsset[5];
var i = 1;
var j1 = 1;
var j2 = 0;
var k = 1;
var add = 0;
var f = 1;
if (!forward) f = -1;
if (leg == "fr") {
joint1 = playerAsset[6];
joint2 = playerAsset[9];
joint3 = playerAsset[12];
i = 1;
j1 = 0;
j2 = -1;
add = 10;
}
else if (leg == "rl") {
joint1 = playerAsset[7];
joint2 = playerAsset[10];
joint3 = playerAsset[13];
i = -1;
j1 = 0;
j2 = 1;
add = 10;
}
else if (leg == "rr") {
joint1 = playerAsset[8];
joint2 = playerAsset[11];
joint3 = playerAsset[14];
i = -1;
j1 = -1;
j2 = 0;
add = 0;
}
keyFrames1.push({
frame: 0,
value: 0
})
keyFrames1.push({
frame: 0.5*frameRate/speed,
value: f*i*(25-add)/180*pi
});
keyFrames1.push({
frame: 1.5*frameRate/speed,
value: f*i*(-15-add)/180*pi
});
keyFrames1.push({
frame: 2*frameRate/speed,
value: 0
});
joint1Anim.setKeys(keyFrames1);
joint3Anim.setKeys(keyFrames1);
keyFrames2.push({
frame: 0,
value: j1*(12)/180*pi
});
keyFrames2.push({
frame: 0.3*frameRate/speed,
value: j1*(12)/180*pi
});
keyFrames2.push({
frame: 0.45*frameRate/speed,
value: j2*(12)/180*pi
});
keyFrames2.push({
frame: 1.3*frameRate/speed,
value: j2*(12)/180*pi
});
keyFrames2.push({
frame: 1.45*frameRate/speed,
value: j1*(12)/180*pi
});
keyFrames2.push({
frame: 2*frameRate/speed,
value: j1*(12)/180*pi
});
joint2Anim.setKeys(keyFrames2);
joint1Anim.enableBlending = true;
joint2Anim.enableBlending = true;
joint3Anim.enableBlending = true;
joint1Anim.blendingSpeed = 0.05;
joint2Anim.blendingSpeed = 0.05;
joint3Anim.blendingSpeed = 0.05;
joint1.animations.push(joint1Anim);
joint2.animations.push(joint2Anim);
joint3.animations.push(joint3Anim);
}
function bodyAnimation() {
const bodyAnim = new BABYLON.Animation("bodyAnim", "position.y",
frameRate, BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
keyFrames = [];
keyFrames.push({
frame: 0,
value: 0
});
keyFrames.push({
frame: 0.45*frameRate/speed,
value: -0.02
});
keyFrames.push({
frame: 0.6*frameRate/speed,
value: 0
});
keyFrames.push({
frame: 1.45*frameRate/speed,
value: -0.02
});
keyFrames.push({
frame: 1.6*frameRate/speed,
value: 0
});
keyFrames.push({
frame: 2*frameRate/speed,
value: 0
});
bodyAnim.setKeys(keyFrames);
bodyAnim.enableBlending = true;
bodyAnim.blendingSpeed = 0.05;
playerAsset[2].animations.push(bodyAnim);
}
function stopAnimation() {
const bodyStopAnim = new BABYLON.Animation("bodyStopAnim", "position.y",
frameRate, BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CONSTANT);
const yRotAnim = new BABYLON.Animation("yRotAnim", "rotation.y",
frameRate, BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CONSTANT);
const zRotAnim = new BABYLON.Animation("zRotAnim", "rotation.z",
frameRate, BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CONSTANT);
const indicesJoints1 = [1, 6, 7, 8];
const indicesJoints23 = [4, 5, 9, 10, 11, 12, 13, 14];
keyFrames = [];
keyFrames.push({
frame: 0,
value: 0
});
keyFrames.push({
frame: 0.5*frameRate/speed,
value: 0
});
keyFrames.push({
frame: frameRate/speed,
value: 0
});
bodyStopAnim.setKeys(keyFrames);
yRotAnim.setKeys(keyFrames);
zRotAnim.setKeys(keyFrames);
bodyStopAnim.enableBlending = true;
yRotAnim.enableBlending = true;
zRotAnim.enableBlending = true;
bodyStopAnim.blendingSpeed = 0.05;
yRotAnim.blendingSpeed = 0.05;
zRotAnim.blendingSpeed = 0.05;
indicesJoints1.forEach(n => {
playerAsset[n].animations.push(yRotAnim);
});
indicesJoints23.forEach(n => {
playerAsset[n].animations.push(zRotAnim);
});
playerAsset[2].animations.push(bodyStopAnim);
}
function main() {
const player = playerAsset[2];
slider1 = document.getElementById("front left joint 1");
slider2 = document.getElementById("front left joint 2");
slider3 = document.getElementById("front left joint 3");
value1 = document.getElementById("slider1-value");
value2 = document.getElementById("slider2-value");
value3 = document.getElementById("slider3-value");
button = document.getElementById("anim");
buttonReverse = document.getElementById("animReverse");
buttonPause = document.getElementById("animPause");
buttonStop = document.getElementById("animStop");
slider1.oninput = function() {
playerAsset[1].rotation.y = slider1.value/180*pi;
value1.innerHTML = slider1.value;
}
slider2.oninput = function() {
playerAsset[4].rotation.z = slider2.value/180*pi;
value2.innerHTML = slider2.value;
}
slider3.oninput = function() {
playerAsset[5].rotation.z = slider3.value/180*pi;
value3.innerHTML = slider3.value;
}
player.rotation.x = pi/2;
player.rotation.z = pi/2+pi/4;
var forward = true;
var animations = [];
button.onclick = function() {
if (animations.length == 0) {
addAnimation("fl");
addAnimation("fr");
addAnimation("rl");
addAnimation("rr");
bodyAnimation();
for (var i = 0; i < playerAsset.length; i++) {
animations.push(scene.beginAnimation(playerAsset[i], 0, 2*frameRate, true));
}
}
else {
for (var i = 0; i < playerAsset.length; i++) {
animations[i].restart();
}
}
}
buttonReverse.onclick = function() {
forward = !forward;
for (var i = 0; i < playerAsset.length; i++) {
animations[i].stop();
playerAsset[i].animations.length = 0;
}
animations.length = 0;
addAnimation("fl", forward);
addAnimation("fr", forward);
addAnimation("rl", forward);
addAnimation("rr", forward);
bodyAnimation();
for (var i = 0; i < playerAsset.length; i++) {
animations.push(scene.beginAnimation(playerAsset[i], 0, 2*frameRate, true));
}
}
buttonPause.onclick = function() {
for (var i = 0; i < playerAsset.length; i++) {
animations[i].pause();
}
}
buttonStop.onclick = function() {
for (var i = 0; i < playerAsset.length; i++) {
animations[i].stop();
playerAsset[i].animations.length = 0;
}
animations.length = 0;
stopAnimation();
for (var i = 0; i < playerAsset.length; i++) {
scene.beginAnimation(playerAsset[i], 0, frameRate/speed, false);
}
}
}
const createScene = () => {
const scene = new BABYLON.Scene(engine);
const camera = new BABYLON.ArcRotateCamera("Camera", - pi / 1.5, pi / 2.5, 5, BABYLON.Vector3.Zero());
camera.attachControl(canvas, true);
camera.wheelPrecision = 50;
const light1 = new BABYLON.DirectionalLight("DirectionalLight", new BABYLON.Vector3(0, -1, 1));
const light2 = new BABYLON.HemisphericLight("HemiLight", new BABYLON.Vector3(0, 1, 0));
light1.intensity =0.75;
light2.intensity =0.5;
var assetsManager = new BABYLON.AssetsManager(scene);
playerPath.forEach(asset => {
const name='load '+asset;
const path='./';
const meshTask = assetsManager.addMeshTask(name, "", path, asset);
console.log("loading : "+ asset);
meshTask.onSuccess = (task) => {
// disable the original mesh and store it in the data structure
console.log('loaded and stored '+asset);
//for is needed if multiple meshes in same object
for(var i=0; i< task.loadedMeshes.length; i++) {
//task.loadedMeshes[i].setEnabled(false);
playerAsset.push(task.loadedMeshes[i]);
}
}
meshTask.onError = function (task, message, exception) {
console.log(message, exception);
}
});
assetsManager.onFinish = function(tasks) {
main();
};
assetsManager.load();
return scene;
};
window.initFunction = async function() {
var asyncEngineCreation = async function() {
try {
return createDefaultEngine();
}
catch(e) {
console.log("the available createEngine function failed. Creating the default engine instead");
return createDefaultEngine();
}
}
window.engine = await asyncEngineCreation();
if (!engine) throw 'engine should not be null.';
window.scene = createScene();
};
initFunction().then(() => {
sceneToRender = scene
engine.runRenderLoop(function () {
if (sceneToRender && sceneToRender.activeCamera) {
sceneToRender.render();
}
});
});
// Resize
window.addEventListener("resize", function () {
engine.resize();
});