-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
5225 lines (4746 loc) · 215 KB
/
main.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// THREE.js
import * as THREE from './three/three.module.min.js';
import Stats from './three/stats.module.js';
import { TextGeometry } from './three/TextGeometry.js';
import { FontLoader } from './three/FontLoader.js';
import { GLTFLoader } from './three/GLTFLoader.js';
import { TransformControls } from './three/TransformControls.js';
import { CCDIKSolver } from './three/CCDIKSolver.js';
import { OrbitControls } from './three/OrbitControls.js';
// TWEEN.js
import TWEEN from './tween/tween.esm.js';
// Set page scroll to top
document.body.scrollTop = 0; // For Safari
document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
// Get the path of the root, the hostname and the absolute path of the root
const root_folder = window.location.href.substring(0, window.location.href.lastIndexOf("/")) + "/";
const hostname = window.location.hostname;
const root_absolute_path = window.location.pathname.substring(0, window.location.pathname.lastIndexOf("/")) + "/";
// console.log("Version 1.5.0");
function get_url(resource, debug_flag = false) {
let to_ret = "";
if (resource.startsWith("/")) to_ret = resource.substring(1);
if (resource.startsWith("./")) to_ret = resource.substring(2);
to_ret = root_absolute_path + to_ret;
// console.log("Trying to load '" + resource + "' from: " + to_ret);
return to_ret;
}
// Make it so that, on any error, a <div> is added to the page with the error message, in the top-middle of the page
function on_error(error) {
let error_div = document.createElement("div");
error_div.style.position = "absolute";
error_div.style.top = "0";
error_div.style.left = "0";
error_div.style.right = "0";
error_div.style.backgroundColor = " #ff0000dd";
error_div.style.color = "white";
error_div.style.textAlign = "center";
error_div.style.padding = "10px";
error_div.style.fontSize = "20px";
error_div.style.lineHeight = "1.5";
error_div.innerHTML = "<b>An error has occurred</b><br/>" + error;
error_div.style.zIndex = "1000";
document.body.appendChild(error_div);
}
// Add the error function to the window object
window.onerror = on_error;
// Controls whether to show the overlay stats and debug information or not
let OVERLAY_STATS = true, DEBUG = true;
// Comment / uncomment the following line to enable / disable the overlay stats and the debug mode
OVERLAY_STATS = DEBUG = false;
let SHOW_CRAB_SKELETON_HELPER = false;
let SHOW_GRID_HELPER = false;
const FPS = 60;
let keyboard_versions = {
pc_keyboard: 0,
laptop_keyboard: 1,
typewriter_keyboard: 2
};
let selected_keyboard_version = keyboard_versions.pc_keyboard;
// selected_keyboard_version = keyboard_versions.typewriter_keyboard;
let scene, renderer, camera;
let controls, clock;
let stats;
let light, ambientLight, hemisphereLight;
const audioLoader = new THREE.AudioLoader();
const listener = new THREE.AudioListener();
let sounds = [/*
{
name: '...',
offset_beat_steps: 0,
play_each_beat_step: 128,
audio: audio,
play: false,
loop: true
} */
];
let scene_is_ready = false;
let orghographic_camera_size = 70;
let normal_controls_target_position = new THREE.Vector3(0, -0.5, 0);
let camera_is_focused_on_crab = false;
let function_keys = Array.from('1234567890qwertyuiopasdfghjklzxcvbnm');
let actual_function_keys = Array.from('qwertyuiopasdfghjklzxcvbnm');
// copy the above array
let all_keys = function_keys.slice();
all_keys.push(' ');
all_keys.push('?');
all_keys.push('Alt'); // This is the "Crab" icon, corresponding to character ~
all_keys.push(','); // This is the "Music" icon, corresponding to character ^
let effect_canvas, effect_canvas_context;
let ground_plane, ground_plane_for_shadows;
let effect_canvas_rectangle_size, effect_canvas_square_opacity;
let effect_canvas_square_color = '#FFFFFF';
let effect_canvas_play_each_beat, effect_canvas_square_animation_time, effect_canvas_additional_size, effect_canvas_rectangle_start_size, effect_canvas_rectangle_end_size, effect_canvas_rectangle_start_opacity;
let effect_canvas_animation_is_playing = false;
let camera_start_direction = new THREE.Vector3(-0.358, 0.5065, 0.7845);
let camera_start_zoom = 47.5;
let camera_start_position = camera_start_direction.clone().multiplyScalar(camera_start_zoom);
let background_hue_offset = 98;
let crab_idle_tween_up_left, crab_idle_tween_down_center, crab_idle_tween_up_right, crab_idle_tween_down_center_2;
let bps = 170;
let beat_duration = 120 / bps;
let base_color = [
"#7398E7", // PC keyboard
"#f0f0f0", // Laptop keyboard
"#252525" // Typewriter keyboard
]
let buttons_color = [
"#B2C6F2", // PC keyboard
"#131313", // Laptop keyboard
"#AF8515" // Typewriter keyboard
]
let key_names_color = [
"#303030", // PC keyboard
"#c1cbe0", // Laptop keyboard
"#151515" // Typewriter keyboard
]
class KeyboardButton {
constructor(object, key) {
this.object = object;
this.key = key;
this.state = 0;
// Tweens
this.active_tweens = [];
}
}
let keyboard_base_height, keyboard_base_width, keyboard_base_length,
sub_base_height, sub_base_thickness, crab_bases_height,
additional_crab_base_height, additional_crab_base_stroke_height;
let whole_keyboard_container = new THREE.Object3D();
whole_keyboard_container.name = 'whole_keyboard_container';
let num_of_crab_bases = 5;
let keyboard_crab_bases = [];
let keyboard_buttons = [];
let premade_loops = {
"Q": {
"A": ["Z", "X", "C", "B", "N", "M"],
"F": ["Z", "X", "C", "B", "N", "M"],
"H": ["Z", "X", "C", "B", "N", "M"],
"J": ["Z", "X", "C", "B", "N", "M"],
"L": ["Z", "X", "C", "B", "N", "M"]
},
"W": {
"D": ["C", "N"],
"F": ["C", "N"],
"J": ["C", "N"],
"K": ["C", "N"],
"L": ["C", "N"]
},
"E": {
"A": ["B", "N", "M"],
"S": ["B", "N", "M"],
"D": ["B", "N", "M"],
"F": ["B", "N", "M"],
"G": ["B", "N", "M"],
"J": ["B", "N", "M"],
"L": ["B", "N", "M"]
},
"R": {
"A": ["X", "C", "B", "N"],
"D": ["X", "C", "B", "N"],
"F": ["X", "C", "B", "N"],
"G": ["X", "C", "B", "N"],
"H": ["X", "C", "B", "N"],
"J": ["X", "C", "B", "N"],
"K": ["X", "C", "B", "N"],
"L": ["X", "C", "B", "N"]
},
"T": {
"A": ["X", "C", "V", "B", "N", "M"],
"S": ["X", "C", "V", "B", "N", "M"],
"D": ["X", "C", "V", "B", "N", "M"],
"F": ["X", "C", "V", "B", "N", "M"],
"G": ["X", "C", "V", "B", "N", "M"],
"H": ["X", "C", "V", "B", "N", "M"],
"J": ["X", "C", "V", "B", "N", "M"],
"K": ["X", "C", "V", "B", "N", "M"],
"L": ["X", "C", "V", "B", "N", "M"]
},
"Y": {
"A": ["Z", "C", "V", "M"],
"S": ["Z", "C", "V", "M"],
"D": ["Z", "C", "V", "M"],
"G": ["Z", "C", "V", "M"],
"H": ["Z", "C", "V", "M"],
"J": ["Z", "C", "V", "M"],
"K": ["Z", "C", "V", "M"]
},
"U": {
"D": ["C", "V", "M"],
"F": ["C", "V", "M"],
"G": ["C", "V", "M"],
"H": ["C", "V", "M"],
"J": ["C", "V", "M"],
"K": ["C", "V", "M"]
},
"I": {
"A": ["Z", "X", "B"],
"S": ["Z", "X", "B"],
"D": ["Z", "X", "B"],
"F": ["Z", "X", "B"],
"G": ["Z", "X", "B"],
"J": ["Z", "X", "B"],
"K": ["Z", "X", "B"]
},
"O": {
"A": ["X", "C", "B", "N", "M"],
"S": ["X", "C", "B", "N", "M"],
"D": ["X", "C", "B", "N", "M"],
"F": ["X", "C", "B", "N", "M"],
"G": ["X", "C", "B", "N", "M"],
"J": ["X", "C", "B", "N", "M"],
"K": ["X", "C", "B", "N", "M"],
"L": ["X", "C", "B", "N", "M"]
},
"P": {
"A": ["C", "V", "B", "N", "M"],
"S": ["C", "V", "B", "N", "M"],
"D": ["C", "V", "B", "N", "M"],
"F": ["C", "V", "B", "N", "M"],
"G": ["C", "V", "B", "N", "M"],
"J": ["C", "V", "B", "N", "M"],
"K": ["C", "V", "B", "N", "M"],
"L": ["C", "V", "B", "N", "M"]
}
}
let sub_base_pulse_color = [
"#B2C6F2", // PC keyboard (same as buttons colors)
"#ffffff", // Laptop keyboard
"#303030" // Typewriter keyboard
]
let ikSolver;
let crab_legs_targets_original_positions = {};
let original_legs_offset_from_body_back_bone = {};
let crab_arms_targets_original_positions = {};
let crab_original_main_bone_height;
let crab_original_main_bone_rotation;
let selected_crab_base_index;
let previously_selected_crab_base_index = -1;
let crab_spawn_position;
let arm_bones_original_angles_and_positions = {};
// let crab_scale = 0.00425;
let crab_scale = 0.425;
let crab_model = undefined;
let crab_bones = {}
let crab_texture_colors = [
"orange", // PC keyboard version
"gray", // Laptop keyboard version
"yellow" // Typewriter keyboard version
];
const crab_poses = {
transitioning: -1,
neutral: 0,
bass: 1,
dj_station: 2,
guitar: 3,
synth: 4,
piano: 5
};
let current_crab_pose = crab_poses.neutral;
// Contains, for each pose, the final target positions of the crab's arms
let crab_arms_targets_poses = {}
let pose_transition_animation_duration = 1000 * beat_duration / 2;
let instruments_are_on_screen = {
bass: false,
dj_station: false,
guitar: false,
synth: false,
piano: false
}
let moving_crab = false;
let crab_movement_speed = 12.5; // Units / seconds
let playing_idle = false;
let idle_flag = false; // Used to start idle animation from left and right movements alternatively
// for each leg, true if leg is grounded, false otherwise
let crab_legs_state = {
"leg_front_targetL": {
grounded: true,
moved_times: 0
},
"leg_front_targetR": {
grounded: true,
moved_times: 0
},
"leg_mid_front_targetR": {
grounded: true,
moved_times: 0
},
"leg_mid_front_targetL": {
grounded: true,
moved_times: 0
},
"leg_mid_back_targetL": {
grounded: true,
moved_times: 0
},
"leg_mid_back_targetR": {
grounded: true,
moved_times: 0
},
"leg_back_targetR": {
grounded: true,
moved_times: 0
},
"leg_back_targetL": {
grounded: true,
moved_times: 0
}
};
const instruments = {
bass: 1,
dj_station: 2,
guitar: 3,
synth: 4,
piano: 5
};
let instruments_start_scale = {
bass: new THREE.Vector3(0, 0, 0),
dj_station: new THREE.Vector3(0, 0, 0),
guitar: new THREE.Vector3(0.25, 0, 0),
synth: new THREE.Vector3(0, 0, 0),
piano: new THREE.Vector3(0, 0, 0)
};
let clicked_screen_pos = new THREE.Vector2();
let master_volume_gain = -0.15;
let pressed_beats_key_once = false;
// Keeps track of which key is being pressed and when it was last pressed
let pressing = {};
let deltaTime = 0;
let beat_step = -1;
let holding_crab_key = false;
let sounds_are_playing = false;
let melody_sound_is_playing = false;
let melody_part_to_play = 1;
// Retrieve crab bones
function get_crab_bone(bone_name) {
return crab_model.getObjectByName("crab_parts").skeleton.bones.find(bone => bone.name === bone_name);
}
function get_crab_main_bone() {
let crab_main_bone = get_crab_bone("body_back");
return crab_main_bone;
}
function refresh_crab_idle() {
// Move crab's main bone up towards the left, than down at center, then up towards the right, than back at down center (move the bdy_back bone up and down)
let body_back = get_crab_main_bone();
let vertical_delta = 0.5;
let horizontal_delta = 0.35;
let central_position = body_back.position.clone();
central_position.y = crab_original_main_bone_height;
let time = beat_duration * 1000 / 4;
// Override each tween
crab_idle_tween_up_left = new TWEEN.Tween(body_back.position)
.to({
x: central_position.x - horizontal_delta,
y: central_position.y + vertical_delta,
// z: central_position.z
}, time)
.easing(TWEEN.Easing.Sinusoidal.InOut);
crab_idle_tween_down_center = new TWEEN.Tween(body_back.position)
.to({
x: central_position.x,
y: central_position.y,
// z: central_position.z
}, time)
.easing(TWEEN.Easing.Sinusoidal.InOut);
crab_idle_tween_up_right = new TWEEN.Tween(body_back.position)
.to({
x: central_position.x + horizontal_delta,
y: central_position.y + vertical_delta,
// z: central_position.z
}, time)
.easing(TWEEN.Easing.Sinusoidal.InOut);
crab_idle_tween_down_center_2 = new TWEEN.Tween(body_back.position)
.to({
x: central_position.x,
y: central_position.y,
// z: central_position.z
}, time)
.easing(TWEEN.Easing.Sinusoidal.InOut);
crab_idle_tween_up_left.chain(crab_idle_tween_down_center);
crab_idle_tween_down_center.chain(crab_idle_tween_up_right);
crab_idle_tween_up_right.chain(crab_idle_tween_down_center_2);
crab_idle_tween_down_center_2.chain(crab_idle_tween_up_left);
}
let crab_base_texture_loader = new THREE.TextureLoader();
// let crab_textures = [];
// for (let i = 0; i < crab_texture_colors.length; i++) {
// let crab_texture = await crab_base_texture_loader.loadAsync('/3d/crab/Crab_Base_color_' + crab_texture_colors[i] + '.png')
// crab_textures.push(crab_texture);
// }
// Load the crab GLTF model
function load_crab_model(crab_texture_image = null) {
let loader = new GLTFLoader();
loader.load(get_url('/3d/crab/crab.gltf', true), function (object) {
crab_model = object.scene;
crab_model.name = 'crab_crab_model';
crab_model.position.set(crab_spawn_position.x, crab_spawn_position.y + keyboard_base_height + crab_bases_height + sub_base_height + additional_crab_base_height, crab_spawn_position.z);
crab_model.scale.set(crab_scale, crab_scale, crab_scale);
crab_model.traverse(function (child) {
if (child.isMesh) {
// Set child's geometry shadows
child.castShadow = true;
child.receiveShadow = true;
// Prevents object from being culled when outside scene camera frustum (avoids problems with movement animation)
child.frustumCulled = false;
// Set child's material
let material = new THREE.MeshPhysicalMaterial(child.material);
// Set child's texture
material.side = THREE.FrontSide;
if (child.name == "crab_parts") {
if (crab_texture_image != null && material.map != null && material.map != undefined) {
let texture = material.map.clone();
texture.image = crab_texture_image.image;
material.map = texture;
}
material.metalness = 0.0;
material.roughness = 0.65;
material.reflectivity = 0.35;
if (selected_keyboard_version == keyboard_versions.pc_keyboard) {
material.emissive = new THREE.Color(0x161616);
} else if (selected_keyboard_version == keyboard_versions.laptop_keyboard) {
material.emissive = new THREE.Color(0x252525);
material.roughness = 0.7;
material.reflectivity = 0.15;
material.metalness = 0.1;
} else if (selected_keyboard_version == keyboard_versions.typewriter_keyboard) {
material.emissive = new THREE.Color(0x101010);
material.metalnessMap = null;
material.roughnessMap = null;
material.roughness = 0.45;
material.metalness = 0.175;
material.reflectivity = 0.0;
}
} else if (child.name == "crab_eyes") {
material.map = null;
material.color = new THREE.Color(0x202020);
material.roughness = 0.3;
material.metalness = 0.5;
material.reflectivity = 0.2;
}
material.flatShading = false;
child.material = material;
} else if (child.isBone) {
crab_bones[child.name] = {
bone: child,
paprent: child.parent
}
}
});
// Add an helper for the crab's skeleton (i.e. bones)
if (SHOW_CRAB_SKELETON_HELPER) {
const helper = new THREE.SkeletonHelper(crab_model);
helper.material.linewidth = 3;
scene.add(helper);
}
// Add the IK to crab legs
let skinnedMesh = crab_model.getObjectByName('crab_parts');
/*
Bones structures:
0: root (parent: Armature)
1: body_back (parent: root)
2: leg_front_1L (parent: body_back)
3: leg_front_2L (parent: leg_front_1L)
4: leg_front_3L (parent: leg_front_2L)
5: leg_front_effectorL (parent: leg_front_3L)
6: leg_mid_front_1L (parent: body_back)
7: leg_mid_front_2L (parent: leg_mid_front_1L)
8: leg_mid_front_3L (parent: leg_mid_front_2L)
9: leg_mid_front_effectorL (parent: leg_mid_front_3L)
10: leg_mid_back_1L (parent: body_back)
11: leg_mid_back_2L (parent: leg_mid_back_1L)
12: leg_mid_back_3L (parent: leg_mid_back_2L)
13: leg_mid_back_effectorL (parent: leg_mid_back_3L)
14: leg_back_1L (parent: body_back)
15: leg_back_2L (parent: leg_back_1L)
16: leg_back_3L (parent: leg_back_2L)
17: leg_back_effectorL (parent: leg_back_3L)
18: arm_1L (parent: body_back)
19: arm_2L (parent: arm_1L)
20: arm_3L (parent: arm_2L)
21: arm_4L (parent: arm_3L)
22: arm_claw_topL (parent: arm_4L)
23: arm_claw_bottomL (parent: arm_4L)
24: arm_effectorL (parent: arm_4L)
25: arm_targetL (parent: body_back)
26: body_front (parent: body_back)
27: leg_front_1R (parent: body_back)
28: leg_front_2R (parent: leg_front_1R)
29: leg_front_3R (parent: leg_front_2R)
30: leg_front_effectorR (parent: leg_front_3R)
31: leg_mid_front_1R (parent: body_back)
32: leg_mid_front_2R (parent: leg_mid_front_1R)
33: leg_mid_front_3R (parent: leg_mid_front_2R)
34: leg_mid_front_effectorR (parent: leg_mid_front_3R)
35: leg_mid_back_1R (parent: body_back)
36: leg_mid_back_2R (parent: leg_mid_back_1R)
37: leg_mid_back_3R (parent: leg_mid_back_2R)
38: leg_mid_back_effectorR (parent: leg_mid_back_3R)
39: leg_back_1R (parent: body_back)
40: leg_back_2R (parent: leg_back_1R)
41: leg_back_3R (parent: leg_back_2R)
42: leg_back_effectorR (parent: leg_back_3R)
43: arm_1R (parent: body_back)
44: arm_2R (parent: arm_1R)
45: arm_3R (parent: arm_2R)
46: arm_4R (parent: arm_3R)
47: arm_claw_topR (parent: arm_4R)
48: arm_claw_bottomR (parent: arm_4R)
49: arm_effectorR (parent: arm_4R)
50: arm_targetR (parent: body_back)
51: leg_front_targetL (parent: root)
52: leg_mid_front_targetL (parent: root)
53: leg_mid_back_targetL (parent: root)
54: leg_back_targetL (parent: root)
55: leg_front_targetR (parent: root)
56: leg_mid_front_targetR (parent: root)
57: leg_mid_back_targetR (parent: root)
58: leg_back_targetR (parent: root)
*/
// IKs for all the crab legs
let legs_targets_start_index = 50;
let ik_iterations = 3;
let iks = [
// ----------- LEGS (8 legs) --------------------------------------------
{
target: legs_targets_start_index + 1, // "leg_front_targetL"
effector: 5,
links: [
{
index: 4
},
{
index: 3
},
{
index: 2
}
],
iteration: ik_iterations
},
{
target: legs_targets_start_index + 2, // "leg_mid_front_targetL"
effector: 9,
links: [
{
index: 8
},
{
index: 7
},
{
index: 6
}
],
iteration: ik_iterations
},
{
target: legs_targets_start_index + 3, // "leg_mid_back_targetL"
effector: 13,
links: [
{
index: 12
},
{
index: 11
},
{
index: 10
}
],
iteration: ik_iterations
},
{
target: legs_targets_start_index + 4, // "leg_back_targetL"
effector: 17,
links: [
{
index: 16
},
{
index: 15
},
{
index: 14
}
],
iteration: ik_iterations
},
{
target: legs_targets_start_index + 5, // "leg_front_targetR"
effector: 30,
links: [
{
index: 29
},
{
index: 28
},
{
index: 27
}
],
iteration: ik_iterations
},
{
target: legs_targets_start_index + 6, // "leg_mid_front_targetR"
effector: 34,
links: [
{
index: 33
},
{
index: 32
},
{
index: 31
}
],
iteration: ik_iterations
},
{
target: legs_targets_start_index + 7, // "leg_mid_back_targetR"
effector: 38,
links: [
{
index: 37
},
{
index: 36
},
{
index: 35
}
],
iteration: ik_iterations
},
{
target: legs_targets_start_index + 8, // "leg_back_targetR"
effector: 42,
links: [
{
index: 41
},
{
index: 40
},
{
index: 39
}
],
iteration: ik_iterations
},
// ----------- ARMS (2 arms) --------------------------------------------
{
target: 25, // "arm_targetL"
effector: 24,
links: [
{
index: 21
},
{
index: 20
},
{
index: 19
},
{
index: 18
}
],
iteration: ik_iterations
},
{
target: 50, // "arm_targetR"
effector: 49,
links: [
{
index: 46
},
{
index: 45
},
{
index: 44
},
{
index: 43
}
],
iteration: ik_iterations
}
];
// Compute each iks link max and min rotation (for legs)
let legsMaxAngleDelta = new THREE.Vector3(
1 * Math.PI / 24,
1 * Math.PI / 24,
1 * Math.PI / 24
);
let legsMinAngleDelta = new THREE.Vector3(
1 * Math.PI / 24,
1 * Math.PI / 24,
1 * Math.PI / 24
);
// Compute each iks link max and min rotation (for arms)
let armsMaxAngleDelta = new THREE.Vector3(
1 * Math.PI / 36,
1 * Math.PI / 36,
1 * Math.PI / 36
);
let armsMinAngleDelta = new THREE.Vector3(
1 * Math.PI / 36,
1 * Math.PI / 36,
1 * Math.PI / 36
);
let crab_parts = crab_model.getObjectByName('crab_parts');
iks.forEach(ik => {
let is_arm = crab_model.getObjectByName('crab_parts').skeleton.bones[ik.target].name.startsWith('arm');
if (is_arm) {
// Setup min/max angles of arms' IKs
let isLeftArm = ik.target.toString().endsWith('L');
ik.links.forEach((link, ind) => {
let multiplier = 1 + ind * 0;
if (ind == 0) multiplier = 7; // arm_4 (i.e. the "hand", the top & bottom claws parent)
if (ind == 1) multiplier = 4.5; // arm_3 (i.e. the "elbow")
let diagonal_multiplier = 0.75 + ind * 0;
if (ind == 0) diagonal_multiplier = 3.5; // arm_4 (i.e. the "hand", the top & bottom claws parent)
// if (ind == 1) diagonal_multiplier = 2;
let bone = crab_parts.skeleton.bones[link.index];
link.rotationMin = new THREE.Vector3(
bone.rotation.x - (isLeftArm == true ? armsMinAngleDelta.x : armsMaxAngleDelta.x) * diagonal_multiplier,
bone.rotation.y - (isLeftArm == true ? armsMinAngleDelta.y : armsMaxAngleDelta.y) * diagonal_multiplier,
bone.rotation.z - (isLeftArm == true ? armsMinAngleDelta.z : armsMaxAngleDelta.z) * multiplier
);
link.rotationMax = new THREE.Vector3(
bone.rotation.x + (isLeftArm == false ? armsMinAngleDelta.x : armsMaxAngleDelta.x) * diagonal_multiplier,
bone.rotation.y + (isLeftArm == false ? armsMinAngleDelta.y : armsMaxAngleDelta.y) * diagonal_multiplier,
bone.rotation.z + (isLeftArm == false ? armsMinAngleDelta.z : armsMaxAngleDelta.z) * multiplier
);
if (false && ind == 0) {
// Arm 4, allow major rotation around y axis
// let arm_4_max_degree_angle_delta = 45 * Math.PI / 180;
// let arm_4_min_degree_angle_delta = 15 * Math.PI / 180;
// link.rotationMin.y = bone.rotation.y - (isLeftArm == true ? arm_4_min_degree_angle_delta : arm_4_max_degree_angle_delta);
// link.rotationMax.y = bone.rotation.y + (isLeftArm == false ? arm_4_min_degree_angle_delta : arm_4_max_degree_angle_delta);
let arm_4_max_degree_angle_delta = 15 * Math.PI / 180;
let arm_4_min_degree_angle_delta = 15 * Math.PI / 180;
link.rotationMin.x = bone.rotation.x - (isLeftArm == true ? arm_4_min_degree_angle_delta : arm_4_max_degree_angle_delta);
link.rotationMax.x = bone.rotation.x + (isLeftArm == false ? arm_4_min_degree_angle_delta : arm_4_max_degree_angle_delta);
}
});
} else {
// Set up min/max angles of legs' IKs
let isLeftLeg = crab_model.getObjectByName('crab_parts').skeleton.bones[ik.target].name.endsWith('L');
ik.links.forEach((link, ind) => {
let multiplier = 2.75 + ind * 5.5;
let diagonal_multiplier = 1.65 + ind * 0.125;
let bone = crab_parts.skeleton.bones[link.index];
link.rotationMin = new THREE.Vector3(
bone.rotation.x - (isLeftLeg == true ? legsMinAngleDelta.x : legsMaxAngleDelta.x) * diagonal_multiplier,
bone.rotation.y - (isLeftLeg == true ? legsMinAngleDelta.y : legsMaxAngleDelta.y) * multiplier,
bone.rotation.z - (isLeftLeg == true ? legsMinAngleDelta.z : legsMaxAngleDelta.z) * diagonal_multiplier
);
link.rotationMax = new THREE.Vector3(
bone.rotation.x + (isLeftLeg == false ? legsMinAngleDelta.x : legsMaxAngleDelta.x) * diagonal_multiplier,
bone.rotation.y + (isLeftLeg == false ? legsMinAngleDelta.y : legsMaxAngleDelta.y) * multiplier,
bone.rotation.z + (isLeftLeg == false ? legsMinAngleDelta.z : legsMaxAngleDelta.z) * diagonal_multiplier
);
});
}
});
// console.log(iks);
ikSolver = new CCDIKSolver(skinnedMesh, iks);
// Move crab down from starting pose
let initial_crab_main_bone_delta = -0.5;
// let initial_crab_main_bone_delta = 0;
let crab_main_bone = get_crab_main_bone();
let pos = crab_main_bone.position.clone();
crab_main_bone.position.set(pos.x, pos.y + initial_crab_main_bone_delta, pos.z);
// Move crab legs towards center from starting pose (i.e. move crab legs targets towards the crab's body_back bone)
let targets = [
"leg_front_targetL",
"leg_front_targetR",
"leg_mid_front_targetL",
"leg_mid_front_targetR",
"leg_mid_back_targetL",
"leg_mid_back_targetR",
"leg_back_targetL",
"leg_back_targetR"
]
targets.forEach(target => {
let crab_leg_target = skinnedMesh.skeleton.bones.find(bone => bone.name === target);
let crab_leg_target_pos = crab_leg_target.position.clone();
let crab_body_back_bone_pos = crab_main_bone.position.clone();
let direction = crab_body_back_bone_pos.clone().sub(crab_leg_target_pos).normalize();
let delta = 0.75;
crab_leg_target.position.set(
crab_leg_target_pos.x + direction.x * delta,
crab_leg_target_pos.y,
crab_leg_target_pos.z + direction.z * delta
);
});
let updates = 100;
for (let i = 0; i < updates; i++) {
ikSolver.update();
}
/*
51: leg_front_targetL (parent: root)
52: leg_mid_front_targetL (parent: root)
53: leg_mid_back_targetL (parent: root)
54: leg_back_targetL (parent: root)
55: leg_front_targetR (parent: root)
56: leg_mid_front_targetR (parent: root)
57: leg_mid_back_targetR (parent: root)
58: leg_back_targetR (parent: root)
*/
crab_legs_targets_original_positions = {
leg_front_targetL: skinnedMesh.skeleton.bones[51].position.clone(),
leg_front_targetR: skinnedMesh.skeleton.bones[55].position.clone(),
leg_mid_front_targetR: skinnedMesh.skeleton.bones[56].position.clone(),
leg_mid_front_targetL: skinnedMesh.skeleton.bones[52].position.clone(),
leg_mid_back_targetL: skinnedMesh.skeleton.bones[53].position.clone(),
leg_mid_back_targetR: skinnedMesh.skeleton.bones[57].position.clone(),
leg_back_targetR: skinnedMesh.skeleton.bones[58].position.clone(),
leg_back_targetL: skinnedMesh.skeleton.bones[54].position.clone()
};
original_legs_offset_from_body_back_bone = {
leg_front_targetL: skinnedMesh.skeleton.bones[51].position.clone().sub(skinnedMesh.skeleton.bones[1].position),
leg_front_targetR: skinnedMesh.skeleton.bones[55].position.clone().sub(skinnedMesh.skeleton.bones[1].position),
leg_mid_front_targetR: skinnedMesh.skeleton.bones[56].position.clone().sub(skinnedMesh.skeleton.bones[1].position),
leg_mid_front_targetL: skinnedMesh.skeleton.bones[52].position.clone().sub(skinnedMesh.skeleton.bones[1].position),
leg_mid_back_targetL: skinnedMesh.skeleton.bones[53].position.clone().sub(skinnedMesh.skeleton.bones[1].position),
leg_mid_back_targetR: skinnedMesh.skeleton.bones[57].position.clone().sub(skinnedMesh.skeleton.bones[1].position),
leg_back_targetR: skinnedMesh.skeleton.bones[58].position.clone().sub(skinnedMesh.skeleton.bones[1].position),
leg_back_targetL: skinnedMesh.skeleton.bones[54].position.clone().sub(skinnedMesh.skeleton.bones[1].position)
};
// console.log(crab_legs_targets);
// console.log(original_legs_offset_from_body_back_bone);
crab_arms_targets_original_positions = {
arm_targetL: skinnedMesh.skeleton.bones[25].position.clone(),
arm_targetR: skinnedMesh.skeleton.bones[50].position.clone()
};
crab_arms_targets_poses = {
transitioning: {
arm_targetL: crab_arms_targets_original_positions.arm_targetL.clone(),
arm_targetR: crab_arms_targets_original_positions.arm_targetR.clone()
},
neutral: {
arm_targetL: crab_arms_targets_original_positions.arm_targetL.clone(),
arm_targetR: crab_arms_targets_original_positions.arm_targetR.clone()
},
bass: {
arm_targetL: new THREE.Vector3(2.92, 5.043, 0.584),
arm_targetR: new THREE.Vector3(-0.942, 5.596 + 0.1, -0.912 - 0.2)
},
dj_station: {
arm_targetL: new THREE.Vector3(2.37 + 0.5, 5.17, -1.246 - 0.375),
arm_targetR: new THREE.Vector3(-2.37 - 0.5, 5.17, -1.246 - 0.375)
},
guitar: {
arm_targetL: new THREE.Vector3(3.45, 4.67, -1.80),
arm_targetR: new THREE.Vector3(-0.551, 5.065, -1.548)
},
synth: {
arm_targetL: new THREE.Vector3(2.37 + 0.3, 5.17 - 0.5, -1.246 - 0.05),
arm_targetR: new THREE.Vector3(-2.37 - 0.3, 5.17 - 0.5, -1.246 - 0.05)
},
piano: {
arm_targetL: new THREE.Vector3(2.37, 5.17 - 0.5, -1.246 - 0.115),
arm_targetR: new THREE.Vector3(-2.37, 5.17 - 0.5, -1.246 - 0.115)
}
};
// Cycle through all the bones of the crab's skeleton and store each arm bone's original rotation and position into the "arm_bones_original_angles_and_positions" object
arm_bones_original_angles_and_positions = {};
let crab_parts_bones = crab_model.getObjectByName('crab_parts').skeleton.bones;
crab_parts_bones.forEach(bone => {
if (bone.name.startsWith('arm')) {
arm_bones_original_angles_and_positions[bone.name] = {
rotation: bone.rotation.clone(),
position: bone.position.clone()
};
}
});
// Height of the crab's body_back bone
crab_original_main_bone_height = pos.y + initial_crab_main_bone_delta;
crab_original_main_bone_rotation = crab_main_bone.rotation.clone();
refresh_crab_idle();
scene.add(crab_model);
});
}
async function set_crab_color(crab_color_string) {
if (crab_model == null || crab_model == undefined) return;
let texture_index = crab_texture_colors.indexOf(crab_color_string);
let crab_texture_image = crab_textures[texture_index];
crab_model.traverse(function (child) {
if (child.isMesh) {
// Set child's geometry shadows
child.castShadow = true;
child.receiveShadow = true;
// Prevents object from being culled when outside scene camera frustum (avoids problems with movement animation)
child.frustumCulled = false;
// Set child's material
let material = new THREE.MeshPhysicalMaterial(child.material);
// Set child's texture
material.side = THREE.FrontSide;
if (child.name == "crab_parts") {
if (crab_texture_image != null && material.map != null && material.map != undefined) {
let texture = material.map.clone();
texture.image = crab_texture_image.image;
material.map = texture;
}
material.metalness = 0.0;
material.roughness = 0.65;
material.reflectivity = 0.35;
if (selected_keyboard_version == keyboard_versions.pc_keyboard) {
material.emissive = new THREE.Color(0x161616);
} else if (selected_keyboard_version == keyboard_versions.typewriter_keyboard) {
material.metalnessMap = null;
material.roughnessMap = null;
material.roughness = 0.3;