-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
337 lines (282 loc) · 9.69 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
import * as THREE from "three";
// Data and visualization
import { CompositionShader } from "./shaders/CompositionShader.js";
import {
BASE_LAYER,
BLOOM_LAYER,
BLOOM_PARAMS,
OVERLAY_LAYER,
} from "./config/renderConfig.js";
// Rendering
// import { MapControls } from 'three/addons/controls/OrbitControls.js'
import { EffectComposer } from "three/addons/postprocessing/EffectComposer.js";
import { RenderPass } from "three/addons/postprocessing/RenderPass.js";
import { UnrealBloomPass } from "three/addons/postprocessing/UnrealBloomPass.js";
import { ShaderPass } from "three/addons/postprocessing/ShaderPass.js";
import { Galaxy } from "./objects/galaxy.js";
import { OrbitControls } from "three/addons/controls/OrbitControls.js";
// import { happyValue } from './face.js';
let canvas,
renderer,
camera,
scene,
orbit,
baseComposer,
bloomComposer,
overlayComposer;
let frameCount = 0;
const detectionInterval = 5; // Run face detection every 5 frames
let happyValue, neutralValue, sadValue;
let faceapi;
let detections = [];
let processed_happyValue, processed_neutralValue, processed_sadValue;
// let happyValue;
///////////////////////////////////////- face.js
let myp5;
function setup() {
let sketch = function (p) {
p.setup = function () {
let canvas2 = p.createCanvas(480, 360);
canvas2.id("canvas2");
let video = p.createCapture(p.VIDEO);
video.id("video");
video.size(p.width, p.height);
video.hide();
const faceOptions = {
withLandmarks: true,
withExpressions: true,
withDescriptors: true,
minConfidence: 0.5,
};
faceapi = ml5.faceApi(video, faceOptions, faceReady);
};
function faceReady() {
faceapi.detect(gotFaces);
}
//draw faces
function gotFaces(error, result) {
if (error) {
console.log(error);
return;
}
// // Call faceapi.detect again if needed
// faceapi.detect(gotFaces);
// Only process results at the specified interval
if (frameCount % detectionInterval === 0) {
if (result && result.length > 0 && result[0].expressions) {
detections = result;
happyValue = detections[0].expressions.happy;
sadValue = detections[0].expressions.sad;
neutralValue = detections[0].expressions.neutral;
console.log(
"Happy Value:",
happyValue,
"Neutral Value:",
neutralValue,
"Sad Value:",
sadValue
); // Log happyValue at intervals
// Example usage
processed_happyValue = p.nf(happyValue * 100, 2, 2);
processed_neutralValue = p.nf(neutralValue * 100, 2, 2);
processed_sadValue = p.nf(sadValue * 100, 2, 2);
updateEmotionValues(
processed_happyValue,
processed_neutralValue,
processed_sadValue
); // Update this with real values
////////////////////////////////// - galaxychanging
galaxy.updateStarVisibility(happyValue, sadValue, neutralValue);
galaxy.updateStarColor(happyValue, sadValue, neutralValue, 2000);
} else {
console.log("No valid detections found.");
}
}
// Increment the frame counter
frameCount++;
// Always call faceapi.detect again for continuous detection
faceapi.detect(gotFaces);
}
};
myp5 = new p5(sketch);
}
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
function updateEmotionValues(happy, neutral, sad) {
document.getElementById("loading").style.display = "none";
// Update the values
document.getElementById("happy-value").textContent = happy;
document.getElementById("neutral-value").textContent = neutral;
document.getElementById("sad-value").textContent = sad;
// Determine the highest value
const maxValue = Math.max(happy, neutral, sad);
// Highlight the highest value
highlightMax("happy", happy, maxValue);
highlightMax("neutral", neutral, maxValue);
highlightMax("sad", sad, maxValue);
}
function highlightMax(emotion, value, maxValue) {
const label = document.getElementById(`${emotion}-label`);
const valueDiv = document.getElementById(`${emotion}-value`);
console.log(
`Highlighting: ${emotion}, Value: ${value}, MaxValue: ${maxValue}`
);
console.log(`Elements:`, label, valueDiv); // Check if elements are correctly selected
console.log(maxValue, value);
value = Number(value);
if (value === maxValue) {
console.log("hiiiiiiiii");
label.classList.add("highlight");
valueDiv.classList.add("highlight");
} else {
label.classList.remove("highlight");
valueDiv.classList.remove("highlight");
}
}
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
function initThree() {
// grab canvas
canvas = document.querySelector("#canvas");
// scene
scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0xebe2db, 0.00003);
// camera
camera = new THREE.PerspectiveCamera(
60,
window.innerWidth / window.innerHeight,
0.1,
5000000
);
camera.position.set(0, 500, 500);
camera.up.set(0, 0, 1);
camera.lookAt(0, 0, 0);
// map orbit
orbit = new OrbitControls(camera, canvas);
orbit.enableDamping = true; // an animation loop is required when either damping or auto-rotation are enabled
orbit.dampingFactor = 0.05;
orbit.screenSpacePanning = true;
orbit.minDistance = 1;
orbit.maxDistance = 1000;
orbit.maxPolarAngle = Math.PI / 2 - Math.PI / 360;
orbit.autoRotate = true;
orbit.autoRotateSpeed = 0.5;
setup();
initRenderPipeline();
}
function initRenderPipeline() {
// Assign Renderer
renderer = new THREE.WebGLRenderer({
antialias: true,
canvas,
logarithmicDepthBuffer: true,
});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputEncoding = THREE.sRGBEncoding;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 0.5;
// General-use rendering pass for chaining
const renderScene = new RenderPass(scene, camera);
// Rendering pass for bloom
const bloomPass = new UnrealBloomPass(
new THREE.Vector2(window.innerWidth, window.innerHeight),
1.5,
0.4,
0.85
);
bloomPass.threshold = BLOOM_PARAMS.bloomThreshold;
bloomPass.strength = BLOOM_PARAMS.bloomStrength;
bloomPass.radius = BLOOM_PARAMS.bloomRadius;
// bloom composer
bloomComposer = new EffectComposer(renderer);
bloomComposer.renderToScreen = false;
bloomComposer.addPass(renderScene);
bloomComposer.addPass(bloomPass);
// overlay composer
overlayComposer = new EffectComposer(renderer);
overlayComposer.renderToScreen = false;
overlayComposer.addPass(renderScene);
// Shader pass to combine base layer, bloom, and overlay layers
const finalPass = new ShaderPass(
new THREE.ShaderMaterial({
uniforms: {
baseTexture: { value: null },
bloomTexture: { value: bloomComposer.renderTarget2.texture },
overlayTexture: { value: overlayComposer.renderTarget2.texture },
},
vertexShader: CompositionShader.vertex,
fragmentShader: CompositionShader.fragment,
defines: {},
}),
"baseTexture"
);
finalPass.needsSwap = true;
// base layer composer
baseComposer = new EffectComposer(renderer);
baseComposer.addPass(renderScene);
baseComposer.addPass(finalPass);
}
function resizeRendererToDisplaySize(renderer) {
const canvas = renderer.domElement;
const width = canvas.clientWidth;
const height = canvas.clientHeight;
const needResize = canvas.width !== width || canvas.height !== height;
if (needResize) {
renderer.setSize(width, height, false);
}
return needResize;
}
///text function
// function drawText(text, x, y, ctx) {
// ctx.font = '20px Arial';
// ctx.fillStyle = 'white';
// ctx.textAlign = 'center';
// ctx.fillText(text, x, y);
// }
async function render() {
orbit.update();
// console.log(happyValue)
// fix buffer size
if (resizeRendererToDisplaySize(renderer)) {
const canvas = renderer.domElement;
camera.aspect = canvas.clientWidth / canvas.clientHeight;
camera.updateProjectionMatrix();
}
// fix aspect ratio
const canvas = renderer.domElement;
camera.aspect = canvas.clientWidth / canvas.clientHeight;
camera.updateProjectionMatrix();
//could be modified!!!
galaxy.updateScale(camera);
// galaxy.increaseStarsOverTime(2000,10000)
// Run each pass of the render pipeline
renderPipeline();
requestAnimationFrame(render);
TWEEN.update();
// const ctx = renderer.getContext();
// Draw text
// drawText('Use your mouse to navigate', window.innerWidth / 2, 30, ctx);
}
function renderPipeline() {
// Render bloom
camera.layers.set(BLOOM_LAYER);
bloomComposer.render();
// Render overlays
camera.layers.set(OVERLAY_LAYER);
overlayComposer.render();
// Render normal
camera.layers.set(BASE_LAYER);
baseComposer.render();
}
initThree();
// let axes = new THREE.AxesHelper(5.0)
// scene.add(axes)
let galaxy = new Galaxy(scene);
requestAnimationFrame(render);