forked from afpaiva/magnetismo-CEPOC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
210 lines (184 loc) · 6.27 KB
/
app.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
import { GLTFLoader } from './tools/GLTFLoader.js';
import { OrbitControls } from './tools/OrbitControls.js';
// carrega o renderizador
const renderer = new THREE.WebGLRenderer({
antialias:true,
alpha: true
});
renderer.setSize(window.innerWidth, window.innerHeight);
document.querySelector("#viewport").appendChild(renderer.domElement);
// *****
// cria uma instância para cena
const scene = new THREE.Scene();
// insere luz na cena
const ambientLight = new THREE.AmbientLight (0x404040,5);
scene.add(ambientLight);
const directLight = new THREE.DirectionalLight( 0xffffff, 0.5 );
directLight.position.set(.2,.1,0);
scene.add(directLight);
// *****
// cria uma instância para câmera
const camera = new THREE.PerspectiveCamera(75,
window.innerWidth/window.innerHeight,
0.1,
1000);
// *****
// cria uma instância para controle de órbita
const controls = new OrbitControls (camera, renderer.domElement);
camera.position.set (0,0,8);
// *****
// mixer = instância de animação
// magnetField = objeto animado -> campo magnético
// magnets = objeto estático -> imãs
var group_hand;
var play;
var mixer_field
var mixer_tetha_dif
var mixer_tetha_90
var mixer_tetha_0_180
var action_field
var action_tetha_dif
var action_tetha_90
var action_tetha_0_180
var tetha_dif;
var tetha_90;
var tetha_0_180;
var arrows;
var field;
var hand;
var magnet;
const loader = new GLTFLoader();
loader.load('./assets/modelo.glb',
function (gltf){
//console.log(gltf.scene);
tetha_dif = gltf.scene.children[0];
tetha_90 = gltf.scene.children[1];
tetha_0_180 = gltf.scene.children[2];
arrows = gltf.scene.children[3];
field = gltf.scene.children[4];
hand = gltf.scene.children[5];
magnet = gltf.scene.children[6];
group_hand = new THREE.Group();
group_hand.add(hand);
group_hand.add(arrows);
group_hand.add(tetha_dif);
group_hand.add(tetha_90);
group_hand.add(tetha_0_180);
scene.add(field);
scene.add(magnet);
scene.add(group_hand);
group_hand.children[0].visible = true;//hand
group_hand.children[1].visible = false;//arrows
group_hand.children[2].visible = false;//tetha_dif
group_hand.children[3].visible = false;//tetha_90
group_hand.children[4].visible = false;//tetha_0_180
play = true;
mixer_field = new THREE.AnimationMixer(field);
mixer_tetha_dif = new THREE.AnimationMixer(tetha_dif);
mixer_tetha_90 = new THREE.AnimationMixer(tetha_90);
mixer_tetha_0_180 = new THREE.AnimationMixer(tetha_0_180);
console.log(gltf.animations);
action_field = mixer_field.clipAction(gltf.animations[3]);
action_tetha_dif = mixer_tetha_dif.clipAction(gltf.animations[0]);
action_tetha_90 = mixer_tetha_90.clipAction(gltf.animations[1]);
action_tetha_0_180 = mixer_tetha_0_180.clipAction(gltf.animations[2]);
action_field.play();
}),
undefined,
function(error){
console.error(error);
}
// define animação em loop
var clock1 = new THREE.Clock();
var clock2 = new THREE.Clock();
var clock3 = new THREE.Clock();
var clock4 = new THREE.Clock();
renderer.setAnimationLoop(render);
requestAnimationFrame(render);
// *****
function render() {
renderer.render(scene, camera);
if (play) {
mixer_field.update(clock1.getDelta());
mixer_tetha_dif.update(clock2.getDelta());
mixer_tetha_90.update(clock3.getDelta());
mixer_tetha_0_180.update(clock4.getDelta());
}
}
// listeners: ajuste conforme tamanho da janela do navegador
window.addEventListener('resize', onResize);
function onResize() {
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
}
// *****
// listeners: cliques e alterações de inputs
document.querySelector(".botao1").addEventListener('click', onClickPauseAnim);
function onClickPauseAnim(){
if (play) play = false;
else if (!play) play = true;
}
document.querySelector(".botao2").addEventListener('click', onClickRotate);
var a = -1.5708; // rotaciona em 90graus - valor em radiano
function onClickRotate(){
console.log (group_hand.rotation.x);
TweenLite.to(group_hand.rotation, .5 , { x: a });
a===-1.5708 ? a = 0 : a = -1.5708;
}
var b = 0;
document.querySelector(".botao3").addEventListener('click', onClickOpacity);
function onClickOpacity(){
switch (b){
case 0:
group_hand.children[0].visible = true;//hand
group_hand.children[1].visible = true;//arrows
document.querySelector(".botao3").innerHTML = "<i class=\"far fa-hand-point-left\"></i> vetor + mão";
break;
case 1:
group_hand.children[0].visible = false;//hand
group_hand.children[1].visible = true;//arrows
document.querySelector(".botao3").innerHTML = "<i class=\"far fa-hand-point-left\"></i> vetor";
break;
case 2:
group_hand.children[0].visible = true;//hand
group_hand.children[1].visible = false;//arrows
document.querySelector(".botao3").innerHTML = "<i class=\"far fa-hand-point-left\"></i> mão esquerda";
break;
}
console.log(b);
b == 2 ? b = 0 : b++;
}
//slider
var sliderTetha = document.getElementById("sld_tetha");
sliderTetha.value = 0;
sliderTetha.oninput = ()=>{
if (sliderTetha.value == 1){
document.getElementById("tetha_label").src="assets/tetha_0_180.png";
group_hand.children[2].visible = false;//tetha_dif
group_hand.children[3].visible = false;//tetha_90
group_hand.children[4].visible = true;//tetha_0_180
action_tetha_0_180.play();
}
else if (sliderTetha.value == 2){
document.getElementById("tetha_label").src="assets/tetha_dif.png";
group_hand.children[2].visible = true;//tetha_dif
group_hand.children[3].visible = false;//tetha_90
group_hand.children[4].visible = false;//tetha_0_180
action_tetha_dif.play();
}
else if (sliderTetha.value == 3){
document.getElementById("tetha_label").src="assets/tetha_90.png";
group_hand.children[2].visible = false;//tetha_dif
group_hand.children[3].visible = true;//tetha_90
group_hand.children[4].visible = false;//tetha_0_180
action_tetha_90.play();
}
else{
document.getElementById("tetha_label").src="assets/tetha_off.png";
group_hand.children[2].visible = false;//tetha_dif
group_hand.children[3].visible = false;//tetha_90
group_hand.children[4].visible = false;//tetha_0_180
}
}
// *****