Skip to content

Commit 164cfb1

Browse files
Add fluid support
1 parent a20ec75 commit 164cfb1

File tree

4 files changed

+25
-13
lines changed

4 files changed

+25
-13
lines changed

fluid.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ export const fluid = new FluidDynamics(canvas, {
1212
height: canvas.height,
1313
curl: 0,
1414
// See src/fluid-dynamics.js for all options.
15-
dyeDissipation: 0.75,
16-
pressureDissipation: 0.5,
15+
dyeDissipation: 0.5,
16+
velocityDissipation: 0.9,
17+
pressureDissipation: 0.9,
1718
autoUpdate: true,
1819
paused: false,
1920
});

functions.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as THREE from 'three';
2+
import * as Fluid from './fluid.js';
23
import { globalColor } from './main.js';
34

45
let WhiteKeyWidth = 0.025;
@@ -108,6 +109,16 @@ export function lightKeyOff(midiNote) {
108109
}
109110
}
110111

112+
export function spawnFluid(midi, velocity) {
113+
Fluid.addEmitter(
114+
noteTransform(midi).position.x * (Fluid.canvas.width / (WhiteKeyWidth * 52)) + (Fluid.canvas.width / 2),
115+
Fluid.canvas.height / 2,
116+
20 + (velocity / 127) * 30,
117+
(velocity / 127) * 0.5,
118+
{ x: 0, y: 500 + (velocity / 127) * 500 }
119+
);
120+
}
121+
111122
export function createNote(MIDI) {
112123
const { position, scale } = noteTransform(MIDI);
113124
const geometry = new THREE.PlaneGeometry(1, 1, 1);

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<title>MIDI Visualizer</title>
77
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
88
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
9-
<base href="/THREE-MIDI-Visualizer/">
9+
<base href="./">
1010
<script type="importmap">
1111
{
1212
"imports": {

main.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as THREE from 'three';
22
import * as Functions from './functions.js';
3-
import * as Shaders from './shaders.js';
43
import * as UI from './ui.js';
54
import * as Fluid from './fluid.js';
65
import { PostProcessing, WebGPURenderer } from 'three/webgpu';
@@ -192,13 +191,13 @@ if ( WebGL.isWebGL2Available() ) {
192191
}
193192
});
194193

195-
window.addEventListener("keydown", (e) => {
196-
if (e.key === " ") {
197-
if (UI.inProject) {
198-
Fluid.addEmitter(Fluid.canvas.width / 2, Fluid.canvas.height /2, 20, 0.2, {x: 0, y: 500});
199-
}
200-
}
201-
});
194+
// window.addEventListener("keydown", (e) => {
195+
// if (e.key === " ") {
196+
// if (UI.inProject) {
197+
// Fluid.addEmitter(Fluid.canvas.width / 2, Fluid.canvas.height /2, 20, 0.2, {x: 0, y: 500});
198+
// }
199+
// }
200+
// });
202201

203202
//* =-=-=-=-=-=| GEOMETRY SETUP |=-=-=-=-=-=
204203

@@ -215,8 +214,8 @@ if ( WebGL.isWebGL2Available() ) {
215214
scene.add(laser);
216215

217216
const fluidTexture = new THREE.CanvasTexture(document.getElementById("fluid-canvas"));
218-
const planeGeometry = new THREE.PlaneGeometry(1 * Fluid.fluid._aspectRatio, 1); // adjust size to fit keyboard
219-
const planeMaterial = new THREE.MeshBasicMaterial({ map: fluidTexture, transparent: true, side: THREE.DoubleSide, alphaMap: fluidTexture, });
217+
const planeGeometry = new THREE.PlaneGeometry(0.025 * 52, 1 / Fluid.fluid._aspectRatio, 1); // adjust size to fit keyboard
218+
const planeMaterial = new THREE.MeshBasicMaterial({ color: globalColor, map: fluidTexture, transparent: true, side: THREE.DoubleSide, alphaMap: fluidTexture, });
220219
const fluidPlane = new THREE.Mesh(planeGeometry, planeMaterial);
221220

222221
// position it slightly above the keyboard
@@ -229,6 +228,7 @@ if ( WebGL.isWebGL2Available() ) {
229228
const { note, velocity } = e.detail;
230229

231230
Functions.lightKeyOn(note);
231+
Functions.spawnFluid(note, velocity);
232232

233233
const newnote = Functions.createNote(note);
234234
activeNotes.push(newnote);

0 commit comments

Comments
 (0)