forked from jeeliz/jeelizFaceFilter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.js
216 lines (174 loc) · 6.08 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
207
208
209
210
211
212
213
214
215
"use strict";
// some globalz:
let THREECAMERA = null;
const ARRAY_BILLS = [];
// 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');
}
}
function create_mat2d(threeTexture, isTransparent){ // MT216: we put the creation of the video material in a func because we will also use it for the frame
return new THREE.RawShaderMaterial({
depthWrite: false,
depthTest: false,
transparent: isTransparent,
vertexShader: "attribute vec2 position;\n\
varying vec2 vUV;\n\
void main(void){\n\
gl_Position = vec4(position, 0., 1.);\n\
vUV = 0.5 + 0.5*position;\n\
}",
fragmentShader: "precision lowp float;\n\
uniform sampler2D samplerVideo;\n\
varying vec2 vUV;\n\
void main(void){\n\
gl_FragColor = texture2D(samplerVideo, vUV);\n\
}",
uniforms:{
samplerVideo: { value: threeTexture }
}
});
}
// build the 3D. called once when Jeeliz Face Filter is OK
function init_threeScene(spec) {
// We use the helper
window.THREESTUFF = JeelizThreeHelper.init(spec, detect_callback);
// CREATE OUR MASK OBJECT AND ADD IT TO OUR SCENE
const casaLoader = new THREE.BufferGeometryLoader();
casaLoader.load(
'./models/casa_de_papel/casa_de_papel.json',
(maskGeometry) => {
const maskMaterial = new THREE.MeshPhongMaterial({
map: new THREE.TextureLoader().load('./models/casa_de_papel/CasaDePapel_DIFFUSE.png'),
normalMap: new THREE.TextureLoader().load('./models/casa_de_papel/CasaDePapel_NRM.png'),
reflectivity: 1,
emissiveMap: new THREE.TextureLoader().load('./models/casa_de_papel/CasaDePapel_REFLECT.png')
});
const maskMesh = new THREE.Mesh(maskGeometry, maskMaterial);
maskMesh.scale.multiplyScalar(0.06);
maskMesh.position.y = -0.8;
maskMesh.scale.x = 0.07;
addDragEventListener(maskMesh);
THREESTUFF.faceObject.add(maskMesh);
}
)
// Create the bills:
const billGeometry = new THREE.PlaneGeometry(0.4, 0.4);
const billMaterial = new THREE.MeshLambertMaterial({
map: new THREE.TextureLoader().load('./images/billet_50.png'),
side: THREE.DoubleSide,
transparent: true,
});
// Position each bill randomly + add animations:
for (let i = 0; i < 40; i++) {
const xRand = Math.random()*1 - 0.5;
const yRand = 3;
const zRand = (Math.random()*3 - 1.5) - 1.5;
const billMesh = new THREE.Mesh(billGeometry, billMaterial);
billMesh.renderOrder = 100;
billMesh.frustumCulled = false;
billMesh.visible = false;
billMesh.position.set(xRand, yRand, zRand);
billMesh.rotation.y = xRand;
billMesh.rotation.z = zRand;
billMesh.scale.multiplyScalar(0.4);
billMesh.scale.z = xRand * 10;
ARRAY_BILLS.push(billMesh);
const button = document.getElementById('buttonPlayAudio');
button.classList.remove('disabled');
}
//MT216 : create the frame. We reuse the geometry of the video
const calqueMesh = new THREE.Mesh(THREESTUFF.videoMesh.geometry, create_mat2d(new THREE.TextureLoader().load('./images/calque.png'), true))
calqueMesh.renderOrder = 999; // render last
calqueMesh.frustumCulled = false;
THREESTUFF.scene.add(calqueMesh);
// CREATE THE CAMERA
THREECAMERA = JeelizThreeHelper.create_camera();
// CREATE AN AMBIENT LIGHT
const ambientLight = new THREE.AmbientLight(0xffffff, 0.8);
THREESTUFF.scene.add(ambientLight);
// CREATE A DIRECTIONALLIGHT
const dirLight = new THREE.DirectionalLight(0xffffff, 0.5);
dirLight.position.set(100, 1000, 1000);
THREESTUFF.scene.add(dirLight);
} // end init_threeScene()
// Animate the falling bills:
function animateBill(mesh, index) {
mesh.visible = true;
let count = 0;
setInterval(() => {
if (mesh.position.y < -3) {
mesh.position.y = 3;
}
mesh.position.x = mesh.position.x + (0.005 * Math.cos(Math.PI / 40 * count));
mesh.position.y -= 0.01;
mesh.rotation.y = mesh.rotation.y + (0.005 * Math.cos(Math.PI / 40 * count));
mesh.rotation.x += 0.03;
mesh.rotation.z += 0.02;
count += 0.9;
}, 16)
}
let contextAudio = null;
// Plays the theme song and starts animation for the bills:
function playAudio() {
ARRAY_BILLS.forEach((bill, i) => {
setTimeout(() => {
animateBill(bill, i);
THREESTUFF.scene.add(bill);
}, 230*i)
})
const button = document.getElementById('buttonPlayAudio');
button.style.display = 'none';
// INIT WEB AUDIO
try {
window.AudioContext = window.AudioContext || window.webkitAudioContext;
contextAudio = new AudioContext();
} catch (e) {
console.log('Web Audio API is not supported in this browser.');
}
if (contextAudio) {
const bufferLoader = new BufferLoader(
contextAudio,
['./audio/bella_ciao.mp3'],
(bufferList) => {
const around = contextAudio.createBufferSource();
around.buffer = bufferList[0];
around.connect(contextAudio.destination);
around.loop = true;
around.start();
}
);
bufferLoader.load();
}
}
// entry point, launched by body.onload() :
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) {
JeelizThreeHelper.render(detectState, THREECAMERA);
} // end callbackTrack()
}); // end JEEFACEFILTERAPI.init call
} // end main()