forked from jeeliz/jeelizFaceFilter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.js
207 lines (160 loc) · 5.48 KB
/
demo.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
"use strict";
// some globalz :
let THREECAMERA = null;
let BUTTERFLYOBJ3D = null;
const NUMBERBUTTERFLIES = 10;
const MIXERS = [];
const ACTIONS = [];
let ISANIMATED = false;
// callback : launched if a face is detected or lost
function detect_callback(isDetected) {
if (isDetected) {
console.log('INFO in detect_callback(): DETECTED');
} else {
console.log('INFO in detect_callback(): LOST');
}
}
// build the 3D. called once when Jeeliz Face Filter is OK:
function init_threeScene(spec) {
const threeStuffs = JeelizThreeHelper.init(spec, detect_callback);
// ADD OUR BUTTERFLY:
const butterflyLoader = new THREE.JSONLoader();
butterflyLoader.load(
'./models/butterfly/butterfly.json',
(geometry) => {
const materialBody = new THREE.MeshBasicMaterial({
color: 0x000000,
depthWrite: false,
opacity: 0
});
// let butterFlyInstance
// let action;
let clips = null;
let clip = null;
let xRand = null;
let yRand = null;
let zRand = null;
let sign = null;
BUTTERFLYOBJ3D = new THREE.Object3D();
for (let i = 2; i <= NUMBERBUTTERFLIES; i++) {
const indexTexture = i % 6 === 0 ? 1 : i % 6;
const materialWings = new THREE.MeshLambertMaterial({
map: new THREE.TextureLoader().load(`./models/butterfly/Wing_Diffuse_${indexTexture}.jpg`),
alphaMap: new THREE.TextureLoader().load('./models/butterfly/Wing_Alpha.jpg'),
transparent: true,
morphTargets: true,
opacity: 0
});
const butterFlyInstance = new THREE.Mesh(geometry, [materialWings, materialBody]);
xRand = Math.random() * 2 - 1;
yRand = Math.random() * 1 + 0.1;
zRand = Math.random() * 1 + 0.5;
sign = i % 2 === 0 ? -1 : 1;
butterFlyInstance.position.set(xRand, yRand, zRand);
butterFlyInstance.scale.multiplyScalar(0.55);
butterFlyInstance.visible = false;
let BUTTERFLYINSTANCEOBJ3D = new THREE.Object3D();
setTimeout(() => {
animateFly(butterFlyInstance, 0.01*(i + 3)*0.1 + 0.002, i)
butterFlyInstance.material[0].opacity = 1;
butterFlyInstance.material[1].opacity = 1;
butterFlyInstance.visible = true
BUTTERFLYINSTANCEOBJ3D.add(butterFlyInstance)
}, 600*i);
// CREATE WING FLAP ANIMATION
if (!ISANIMATED) {
// This is where adding our animation begins
const mixer = new THREE.AnimationMixer(butterFlyInstance);
clips = butterFlyInstance.geometry.animations;
clip = clips[0];
const action = mixer.clipAction(clip);
ACTIONS.push(action);
MIXERS.push(mixer);
}
// ADD OUR LIGHTS INSIDE THE BUTTERFLY TO CREATE A GLOWING EFFECT
let pointLight = new THREE.PointLight(0x77ffff, 1, 1, 0.1);
pointLight.position.set(xRand, yRand, zRand);
setTimeout(() => {
animatePointLightButterfly(pointLight);
animateFly(pointLight, 0.01*(i + 3)*0.1 + 0.002, i);
}, 600*i);
BUTTERFLYINSTANCEOBJ3D.add(pointLight);
BUTTERFLYOBJ3D.add(BUTTERFLYINSTANCEOBJ3D);
}
// We play the animation for each butterfly and shift their cycles
// by adding a small timeout
ACTIONS.forEach((a, index) => {
setTimeout(() => {
a.play();
}, index*33)
})
ISANIMATED = true;
threeStuffs.faceObject.add(BUTTERFLYOBJ3D);
}
);
// CREATE THE CAMERA
THREECAMERA = JeelizThreeHelper.create_camera();
} // end init_threeScene()
// Create the animation for the wings
function animateFly(mesh, theta, index) {
let count = 0
const x = mesh.position.x;
const y = mesh.position.y;
const z = mesh.position.z;
setInterval(() => {
count += 0.01
mesh.position.set(
(x + (index*0.01)) * Math.cos(count),
(y*0.5 + (index*0.01)) * Math.sin(count*0.2*index) + 1,
(z + (index*0.01)) * Math.sin(count)
)
mesh.rotation.y = (1.5 * Math.cos(count+0.05)) + 0.3;
mesh.rotation.z = 0.2 * Math.sin(count);
}, 16)
}
// Animates the soptlight for each butterfly
function animatePointLightButterfly (light) {
const opacityUp = new TWEEN.Tween(light)
.to({ intensity: 0.6 }, 2000);
const opacityDown = new TWEEN.Tween(light)
.to({ intensity: 0 }, 2000);
opacityUp.chain(opacityDown);
opacityDown.onComplete(() => {
opacityUp.start()
});
opacityUp.start();
}
// entry point:
function main(){
JeelizResizer.size_canvas({
canvasId: 'jeeFaceFilterCanvas',
callback: function(isError, bestVideoSettings){
init_faceFilter(bestVideoSettings);
}
})
}
function init_faceFilter(videoSettings){
JEEFACEFILTERAPI.init({
canvasId: 'jeeFaceFilterCanvas',
NNCpath: '../../../dist/', // path of NNC.json file
videoSettings: videoSettings,
callbackReady: function (errCode, spec) {
if (errCode) {
console.log('AN ERROR HAPPENS. SORRY BRO :( . ERR =', errCode);
return;
}
console.log('INFO : JEEFACEFILTERAPI IS READY');
init_threeScene(spec);
}, // end callbackReady()
// called at each render iteration (drawing loop):
callbackTrack: function (detectState) {
TWEEN.update();
if (MIXERS.length > 1) {
MIXERS.forEach((m) => {
m.update(0.13);
});
}
JeelizThreeHelper.render(detectState, THREECAMERA);
} // end callbackTrack()
}); // end JEEFACEFILTERAPI.init call
} // end main()