├── .gitignore
├── README.md
├── package-lock.json
├── package.json
├── public
├── favicon.ico
├── index.html
├── logo192.png
├── logo512.png
├── manifest.json
└── robots.txt
└── src
├── App.css
├── App.js
├── components
├── FluidField.js
├── glsl
│ ├── advection.frag.js
│ ├── color.frag.js
│ ├── divergence.frag.js
│ ├── externalForce.frag.js
│ ├── face.vert.js
│ ├── line.vert.js
│ ├── mouse.vert.js
│ ├── poisson.frag.js
│ ├── pressure.frag.js
│ └── viscous.frag.js
└── modules
│ ├── Advection.js
│ ├── Divergence.js
│ ├── ExternalForce.js
│ ├── Mouse.js
│ ├── Poisson.js
│ ├── Pressure.js
│ ├── ShaderPass.js
│ ├── Simulation.js
│ └── Viscous.js
├── index.css
├── index.js
└── logo.svg
/.gitignore:
--------------------------------------------------------------------------------
1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2 |
3 | # dependencies
4 | /node_modules
5 | /.pnp
6 | .pnp.js
7 |
8 | # testing
9 | /coverage
10 |
11 | # production
12 | /build
13 |
14 | # misc
15 | .DS_Store
16 | .env.local
17 | .env.development.local
18 | .env.test.local
19 | .env.production.local
20 |
21 | npm-debug.log*
22 | yarn-debug.log*
23 | yarn-error.log*
24 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Getting Started with Create React App
2 |
3 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
4 |
5 | ## Available Scripts
6 |
7 | In the project directory, you can run:
8 |
9 | ### `npm start`
10 |
11 | Runs the app in the development mode.\
12 | Open [http://localhost:3000](http://localhost:3000) to view it in your browser.
13 |
14 | The page will reload when you make changes.\
15 | You may also see any lint errors in the console.
16 |
17 | ### `npm test`
18 |
19 | Launches the test runner in the interactive watch mode.\
20 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
21 |
22 | ### `npm run build`
23 |
24 | Builds the app for production to the `build` folder.\
25 | It correctly bundles React in production mode and optimizes the build for the best performance.
26 |
27 | The build is minified and the filenames include the hashes.\
28 | Your app is ready to be deployed!
29 |
30 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
31 |
32 | ### `npm run eject`
33 |
34 | **Note: this is a one-way operation. Once you `eject`, you can't go back!**
35 |
36 | If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
37 |
38 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own.
39 |
40 | You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it.
41 |
42 | ## Learn More
43 |
44 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
45 |
46 | To learn React, check out the [React documentation](https://reactjs.org/).
47 |
48 | ### Code Splitting
49 |
50 | This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)
51 |
52 | ### Analyzing the Bundle Size
53 |
54 | This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)
55 |
56 | ### Making a Progressive Web App
57 |
58 | This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)
59 |
60 | ### Advanced Configuration
61 |
62 | This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)
63 |
64 | ### Deployment
65 |
66 | This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)
67 |
68 | ### `npm run build` fails to minify
69 |
70 | This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
71 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "cra-threejs",
3 | "version": "0.1.0",
4 | "private": true,
5 | "dependencies": {
6 | "@react-three/fiber": "^8.16.1",
7 | "@testing-library/jest-dom": "^5.17.0",
8 | "@testing-library/react": "^13.4.0",
9 | "@testing-library/user-event": "^13.5.0",
10 | "@types/three": "^0.163.0",
11 | "react": "^18.2.0",
12 | "react-dom": "^18.2.0",
13 | "react-scripts": "5.0.1",
14 | "three": "^0.163.0",
15 | "web-vitals": "^2.1.4"
16 | },
17 | "scripts": {
18 | "start": "react-scripts start",
19 | "build": "react-scripts build",
20 | "test": "react-scripts test",
21 | "eject": "react-scripts eject"
22 | },
23 | "eslintConfig": {
24 | "extends": [
25 | "react-app",
26 | "react-app/jest"
27 | ]
28 | },
29 | "browserslist": {
30 | "production": [
31 | ">0.2%",
32 | "not dead",
33 | "not op_mini all"
34 | ],
35 | "development": [
36 | "last 1 chrome version",
37 | "last 1 firefox version",
38 | "last 1 safari version"
39 | ]
40 | },
41 | "devDependencies": {
42 | "dat.gui": "^0.7.9"
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ardeshiir/react-three-fiber-fluid-simulation/ca1b70ffca5333ee96e016892d3fd9c206b8c738/public/favicon.ico
--------------------------------------------------------------------------------
/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
12 |
13 |
17 |
18 |
27 | React App
28 |
29 |
30 | You need to enable JavaScript to run this app.
31 |
32 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/public/logo192.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ardeshiir/react-three-fiber-fluid-simulation/ca1b70ffca5333ee96e016892d3fd9c206b8c738/public/logo192.png
--------------------------------------------------------------------------------
/public/logo512.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ardeshiir/react-three-fiber-fluid-simulation/ca1b70ffca5333ee96e016892d3fd9c206b8c738/public/logo512.png
--------------------------------------------------------------------------------
/public/manifest.json:
--------------------------------------------------------------------------------
1 | {
2 | "short_name": "React App",
3 | "name": "Create React App Sample",
4 | "icons": [
5 | {
6 | "src": "favicon.ico",
7 | "sizes": "64x64 32x32 24x24 16x16",
8 | "type": "image/x-icon"
9 | },
10 | {
11 | "src": "logo192.png",
12 | "type": "image/png",
13 | "sizes": "192x192"
14 | },
15 | {
16 | "src": "logo512.png",
17 | "type": "image/png",
18 | "sizes": "512x512"
19 | }
20 | ],
21 | "start_url": ".",
22 | "display": "standalone",
23 | "theme_color": "#000000",
24 | "background_color": "#ffffff"
25 | }
26 |
--------------------------------------------------------------------------------
/public/robots.txt:
--------------------------------------------------------------------------------
1 | # https://www.robotstxt.org/robotstxt.html
2 | User-agent: *
3 | Disallow:
4 |
--------------------------------------------------------------------------------
/src/App.css:
--------------------------------------------------------------------------------
1 | .App {
2 | text-align: center;
3 | }
4 |
5 | .App-logo {
6 | height: 40vmin;
7 | pointer-events: none;
8 | }
9 |
10 | @media (prefers-reduced-motion: no-preference) {
11 | .App-logo {
12 | animation: App-logo-spin infinite 20s linear;
13 | }
14 | }
15 |
16 | .App-header {
17 | background-color: #282c34;
18 | min-height: 100vh;
19 | display: flex;
20 | flex-direction: column;
21 | align-items: center;
22 | justify-content: center;
23 | font-size: calc(10px + 2vmin);
24 | color: white;
25 | }
26 |
27 | .App-link {
28 | color: #61dafb;
29 | }
30 |
31 | @keyframes App-logo-spin {
32 | from {
33 | transform: rotate(0deg);
34 | }
35 | to {
36 | transform: rotate(360deg);
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/App.js:
--------------------------------------------------------------------------------
1 | import logo from './logo.svg';
2 | import './App.css';
3 | import {Canvas} from "@react-three/fiber";
4 | import FluidField from "./components/FluidField";
5 |
6 | function App() {
7 | return (
8 |
9 |
10 |
11 | );
12 | }
13 |
14 | export default App;
15 |
--------------------------------------------------------------------------------
/src/components/FluidField.js:
--------------------------------------------------------------------------------
1 | import React, {useEffect, useRef} from 'react';
2 | import {useFrame, useThree} from "@react-three/fiber";
3 | import face_vert from "./glsl/face.vert";
4 | import color_frag from "./glsl/color.frag";
5 |
6 | import Simulation from "./modules/Simulation";
7 | import * as THREE from "three";
8 | import Mouse from "./modules/Mouse";
9 |
10 | const FluidField = () => {
11 | const ref = useRef()
12 | const rootState = useThree()
13 | const simulationRef = useRef(new Simulation(rootState))
14 | useFrame(({ gl, scene, camera }) => {
15 | Mouse.update()
16 | simulationRef?.current?.update()
17 |
18 | gl.autoClear = false
19 | gl.setRenderTarget(null);
20 | gl.render(scene, camera);
21 |
22 | }, 1)
23 |
24 |
25 | useEffect(()=>{
26 | Mouse.init();
27 | },[])
28 |
29 | return (
30 |
33 |
34 |
41 |
42 | )
43 | };
44 |
45 | export default FluidField;
--------------------------------------------------------------------------------
/src/components/glsl/advection.frag.js:
--------------------------------------------------------------------------------
1 | export default`precision highp float;
2 | uniform sampler2D velocity;
3 | uniform float dt;
4 | uniform bool isBFECC;
5 | // uniform float uvScale;
6 | uniform vec2 fboSize;
7 | uniform vec2 px;
8 | varying vec2 uv;
9 |
10 | void main(){
11 | vec2 ratio = max(fboSize.x, fboSize.y) / fboSize;
12 |
13 | if(isBFECC == false){
14 | vec2 vel = texture2D(velocity, uv).xy;
15 | vec2 uv2 = uv - vel * dt * ratio;
16 | vec2 newVel = texture2D(velocity, uv2).xy;
17 | gl_FragColor = vec4(newVel, 0.0, 0.0);
18 | } else {
19 | vec2 spot_new = uv;
20 | vec2 vel_old = texture2D(velocity, uv).xy;
21 | // back trace
22 | vec2 spot_old = spot_new - vel_old * dt * ratio;
23 | vec2 vel_new1 = texture2D(velocity, spot_old).xy;
24 |
25 | // forward trace
26 | vec2 spot_new2 = spot_old + vel_new1 * dt * ratio;
27 |
28 | vec2 error = spot_new2 - spot_new;
29 |
30 | vec2 spot_new3 = spot_new - error / 2.0;
31 | vec2 vel_2 = texture2D(velocity, spot_new3).xy;
32 |
33 | // back trace 2
34 | vec2 spot_old2 = spot_new3 - vel_2 * dt * ratio;
35 | // gl_FragColor = vec4(spot_old2, 0.0, 0.0);
36 | vec2 newVel2 = texture2D(velocity, spot_old2).xy;
37 | gl_FragColor = vec4(newVel2, 0.0, 0.0);
38 | }
39 | }
40 | `
--------------------------------------------------------------------------------
/src/components/glsl/color.frag.js:
--------------------------------------------------------------------------------
1 | export default `precision highp float;
2 | uniform sampler2D velocity;
3 | varying vec2 uv;
4 |
5 | void main(){
6 | vec2 vel = texture2D(velocity, uv).xy;
7 | float len = length(vel);
8 | vel = vel * 0.5 + 0.5;
9 |
10 | vec3 color = vec3(vel.x+0.5, vel.y + 0.15,(vel.y*vel.x + 0.8));
11 | color = mix(vec3(0.0), color, len);
12 |
13 | gl_FragColor = vec4(color, 1.0);
14 | }
15 | `
--------------------------------------------------------------------------------
/src/components/glsl/divergence.frag.js:
--------------------------------------------------------------------------------
1 | export default`precision highp float;
2 | uniform sampler2D velocity;
3 | uniform float dt;
4 | uniform vec2 px;
5 | varying vec2 uv;
6 |
7 | void main(){
8 | float x0 = texture2D(velocity, uv-vec2(px.x, 0)).x;
9 | float x1 = texture2D(velocity, uv+vec2(px.x, 0)).x;
10 | float y0 = texture2D(velocity, uv-vec2(0, px.y)).y;
11 | float y1 = texture2D(velocity, uv+vec2(0, px.y)).y;
12 | float divergence = (x1-x0 + y1-y0) / 2.0;
13 |
14 | gl_FragColor = vec4(divergence / dt);
15 | }
16 | `
--------------------------------------------------------------------------------
/src/components/glsl/externalForce.frag.js:
--------------------------------------------------------------------------------
1 | export default`precision highp float;
2 |
3 | uniform vec2 force;
4 | uniform vec2 center;
5 | uniform vec2 scale;
6 | uniform vec2 px;
7 | varying vec2 vUv;
8 |
9 | void main(){
10 | vec2 circle = (vUv - 0.5) * 2.0;
11 | float d = 1.0-min(length(circle), 1.0);
12 | d *= d;
13 | gl_FragColor = vec4(force * d, 0, 1);
14 | }
15 | `
--------------------------------------------------------------------------------
/src/components/glsl/face.vert.js:
--------------------------------------------------------------------------------
1 | export default`attribute vec3 position;
2 | uniform vec2 px;
3 | uniform vec2 boundarySpace;
4 | varying vec2 uv;
5 |
6 | precision highp float;
7 |
8 | void main(){
9 | vec3 pos = position;
10 | vec2 scale = 1.0 - boundarySpace * 2.0;
11 | pos.xy = pos.xy * scale;
12 | uv = vec2(0.5)+(pos.xy)*0.5;
13 | gl_Position = vec4(pos, 1.0);
14 | }
15 | `
--------------------------------------------------------------------------------
/src/components/glsl/line.vert.js:
--------------------------------------------------------------------------------
1 | export default`attribute vec3 position;
2 | varying vec2 uv;
3 | uniform vec2 px;
4 |
5 |
6 | precision highp float;
7 |
8 | void main(){
9 | vec3 pos = position;
10 | uv = 0.5 + pos.xy * 0.5;
11 | vec2 n = sign(pos.xy);
12 | pos.xy = abs(pos.xy) - px * 1.0;
13 | pos.xy *= n;
14 | gl_Position = vec4(pos, 1.0);
15 | }`
--------------------------------------------------------------------------------
/src/components/glsl/mouse.vert.js:
--------------------------------------------------------------------------------
1 | export default `precision highp float;
2 |
3 | attribute vec3 position;
4 | attribute vec2 uv;
5 | uniform vec2 center;
6 | uniform vec2 scale;
7 | uniform vec2 px;
8 | varying vec2 vUv;
9 |
10 | void main(){
11 | vec2 pos = position.xy * scale * 2.0 * px + center;
12 | vUv = uv;
13 | gl_Position = vec4(pos, 0.0, 1.0);
14 | }
15 | `
--------------------------------------------------------------------------------
/src/components/glsl/poisson.frag.js:
--------------------------------------------------------------------------------
1 | export default `precision highp float;
2 | uniform sampler2D pressure;
3 | uniform sampler2D divergence;
4 | uniform vec2 px;
5 | varying vec2 uv;
6 |
7 | void main(){
8 | // poisson equation
9 | float p0 = texture2D(pressure, uv+vec2(px.x * 2.0, 0)).r;
10 | float p1 = texture2D(pressure, uv-vec2(px.x * 2.0, 0)).r;
11 | float p2 = texture2D(pressure, uv+vec2(0, px.y * 2.0 )).r;
12 | float p3 = texture2D(pressure, uv-vec2(0, px.y * 2.0 )).r;
13 | float div = texture2D(divergence, uv).r;
14 |
15 | float newP = (p0 + p1 + p2 + p3) / 4.0 - div;
16 | gl_FragColor = vec4(newP);
17 | }
18 | `
--------------------------------------------------------------------------------
/src/components/glsl/pressure.frag.js:
--------------------------------------------------------------------------------
1 | export default `precision highp float;
2 | uniform sampler2D pressure;
3 | uniform sampler2D velocity;
4 | uniform vec2 px;
5 | uniform float dt;
6 | varying vec2 uv;
7 |
8 | void main(){
9 | float step = 1.0;
10 |
11 | float p0 = texture2D(pressure, uv+vec2(px.x * step, 0)).r;
12 | float p1 = texture2D(pressure, uv-vec2(px.x * step, 0)).r;
13 | float p2 = texture2D(pressure, uv+vec2(0, px.y * step)).r;
14 | float p3 = texture2D(pressure, uv-vec2(0, px.y * step)).r;
15 |
16 | vec2 v = texture2D(velocity, uv).xy;
17 | vec2 gradP = vec2(p0 - p1, p2 - p3) * 0.5;
18 | v = (v - gradP * dt);
19 | gl_FragColor = vec4(v, 0.0, 1.0);
20 | }
21 | `
--------------------------------------------------------------------------------
/src/components/glsl/viscous.frag.js:
--------------------------------------------------------------------------------
1 | export default`precision highp float;
2 | uniform sampler2D velocity;
3 | uniform sampler2D velocity_new;
4 | uniform float v;
5 | uniform vec2 px;
6 | uniform float dt;
7 |
8 | varying vec2 uv;
9 |
10 | void main(){
11 | // poisson equation
12 | vec2 old = texture2D(velocity, uv).xy;
13 | vec2 new0 = texture2D(velocity_new, uv + vec2(px.x * 2.0, 0)).xy;
14 | vec2 new1 = texture2D(velocity_new, uv - vec2(px.x * 2.0, 0)).xy;
15 | vec2 new2 = texture2D(velocity_new, uv + vec2(0, px.y * 2.0)).xy;
16 | vec2 new3 = texture2D(velocity_new, uv - vec2(0, px.y * 2.0)).xy;
17 |
18 | vec2 new = 4.0 * old + v * dt * (new0 + new1 + new2 + new3);
19 | new /= 4.0 * (1.0 + v * dt);
20 |
21 | gl_FragColor = vec4(new, 0.0, 1.0,0.0);
22 | }
23 | `
--------------------------------------------------------------------------------
/src/components/modules/Advection.js:
--------------------------------------------------------------------------------
1 | import face_vert from "../glsl/face.vert.js";
2 | import line_vert from "../glsl/line.vert.js";
3 |
4 | import advection_frag from "../glsl/advection.frag.js";
5 | import ShaderPass from "./ShaderPass";
6 |
7 | import * as THREE from "three";
8 |
9 |
10 | export default class Advection extends ShaderPass{
11 | constructor(simProps,rootState){
12 | super({
13 | material: {
14 | vertexShader: face_vert,
15 | fragmentShader: advection_frag,
16 | uniforms: {
17 | boundarySpace: {
18 | value: simProps.cellScale
19 | },
20 | px: {
21 | value: simProps.cellScale
22 | },
23 | fboSize: {
24 | value: simProps.fboSize
25 | },
26 | velocity: {
27 | value: simProps.src.texture
28 | },
29 | dt: {
30 | value: simProps.dt
31 | },
32 | isBFECC: {
33 | value: true
34 | }
35 | },
36 | },
37 | output: simProps.dst
38 | },rootState);
39 |
40 | this.init();
41 | }
42 |
43 | init(){
44 | super.init();
45 | this.createBoundary();
46 | }
47 |
48 | createBoundary(){
49 | const boundaryG = new THREE.BufferGeometry();
50 | const vertices_boundary = new Float32Array([
51 | // left
52 | -1, -1, 0,
53 | -1, 1, 0,
54 |
55 | // top
56 | -1, 1, 0,
57 | 1, 1, 0,
58 |
59 | // right
60 | 1, 1, 0,
61 | 1, -1, 0,
62 |
63 | // bottom
64 | 1, -1, 0,
65 | -1, -1, 0
66 | ]);
67 | boundaryG.setAttribute( 'position', new THREE.BufferAttribute( vertices_boundary, 3 ) );
68 |
69 | const boundaryM = new THREE.RawShaderMaterial({
70 | vertexShader: line_vert,
71 | fragmentShader: advection_frag,
72 | uniforms: this.uniforms
73 | });
74 |
75 | this.line = new THREE.LineSegments(boundaryG, boundaryM);
76 | this.scene.add(this.line);
77 | }
78 |
79 | update({ dt, isBounce, BFECC }){
80 |
81 | this.uniforms.dt.value = dt;
82 | this.line.visible = isBounce;
83 | this.uniforms.isBFECC.value = BFECC;
84 |
85 | super.update();
86 | }
87 | }
--------------------------------------------------------------------------------
/src/components/modules/Divergence.js:
--------------------------------------------------------------------------------
1 | import face_vert from "../glsl/face.vert.js";
2 | import divergence_frag from "../glsl/divergence.frag.js";
3 |
4 | import ShaderPass from "./ShaderPass";
5 |
6 | export default class Divergence extends ShaderPass{
7 | constructor(simProps,rootState){
8 | super({
9 | material: {
10 | vertexShader: face_vert,
11 | fragmentShader: divergence_frag,
12 | uniforms: {
13 | boundarySpace: {
14 | value: simProps.boundarySpace
15 | },
16 | velocity: {
17 | value: simProps.src.texture
18 | },
19 | px: {
20 | value: simProps.cellScale
21 | },
22 | dt: {
23 | value: simProps.dt
24 | }
25 | }
26 | },
27 | output: simProps.dst
28 | },rootState)
29 |
30 | this.init();
31 | }
32 |
33 | update({ vel }){
34 | this.uniforms.velocity.value = vel.texture;
35 | super.update();
36 | }
37 | }
--------------------------------------------------------------------------------
/src/components/modules/ExternalForce.js:
--------------------------------------------------------------------------------
1 | import mouse_vert from "../glsl/mouse.vert.js";
2 | import externalForce_frag from "../glsl/externalForce.frag.js";
3 | import ShaderPass from "./ShaderPass";
4 | import Mouse from "./Mouse";
5 |
6 | import * as THREE from "three";
7 |
8 | export default class ExternalForce extends ShaderPass{
9 | constructor(simProps,rootState){
10 | super({
11 | output: simProps.dst
12 | },rootState);
13 |
14 | this.init(simProps);
15 | }
16 |
17 | init(simProps){
18 | super.init();
19 | const mouseG = new THREE.PlaneGeometry (
20 | 1, 1
21 | );
22 |
23 | const mouseM = new THREE.RawShaderMaterial({
24 | vertexShader: mouse_vert,
25 | fragmentShader: externalForce_frag,
26 | blending: THREE.AdditiveBlending,
27 | uniforms: {
28 | px: {
29 | value: simProps.cellScale
30 | },
31 | force: {
32 | value: new THREE.Vector2(0.0, 0.0)
33 | },
34 | center: {
35 | value: new THREE.Vector2(0.0, 0.0)
36 | },
37 | scale: {
38 | value: new THREE.Vector2(simProps.cursor_size, simProps.cursor_size)
39 | }
40 | },
41 | })
42 |
43 | this.mouse = new THREE.Mesh(mouseG, mouseM);
44 | this.scene.add(this.mouse);
45 | }
46 |
47 | update(props){
48 | const forceX = Mouse.diff.x / 2 * props.mouse_force;
49 | const forceY = Mouse.diff.y / 2 * props.mouse_force;
50 |
51 | const cursorSizeX = props.cursor_size * props.cellScale.x;
52 | const cursorSizeY = props.cursor_size * props.cellScale.y;
53 |
54 | const centerX = Math.min(Math.max(Mouse.coords.x, -1 + cursorSizeX + props.cellScale.x * 2), 1 - cursorSizeX - props.cellScale.x * 2);
55 | const centerY = Math.min(Math.max(Mouse.coords.y, -1 + cursorSizeY + props.cellScale.y * 2), 1 - cursorSizeY - props.cellScale.y * 2);
56 |
57 | const uniforms = this.mouse.material.uniforms;
58 | uniforms.force.value.set(forceX, forceY);
59 | uniforms.center.value.set(centerX, centerY);
60 | uniforms.scale.value.set(props.cursor_size, props.cursor_size);
61 |
62 | super.update();
63 | }
64 |
65 | }
--------------------------------------------------------------------------------
/src/components/modules/Mouse.js:
--------------------------------------------------------------------------------
1 | import * as THREE from "three";
2 |
3 | class Mouse{
4 | constructor(){
5 | this.mouseMoved = false;
6 | this.coords = new THREE.Vector2();
7 | this.coords_old = new THREE.Vector2();
8 | this.diff = new THREE.Vector2();
9 | this.timer = null;
10 | this.count = 0;
11 | }
12 |
13 | init(){
14 | document.body.addEventListener( 'mousemove', this.onDocumentMouseMove.bind(this), false );
15 | document.body.addEventListener( 'touchstart', this.onDocumentTouchStart.bind(this), false );
16 | document.body.addEventListener( 'touchmove', this.onDocumentTouchMove.bind(this), false );
17 | }
18 |
19 | setCoords( x, y ) {
20 | if(this.timer) clearTimeout(this.timer);
21 | this.coords.set( ( x / window.innerWidth ) * 2 - 1, - ( y / window.innerHeight ) * 2 + 1 );
22 | this.mouseMoved = true;
23 | this.timer = setTimeout(() => {
24 | this.mouseMoved = false;
25 | }, 100);
26 |
27 | }
28 | onDocumentMouseMove( event ) {
29 | this.setCoords( event.clientX, event.clientY );
30 | }
31 | onDocumentTouchStart( event ) {
32 | if ( event.touches.length === 1 ) {
33 | // event.preventDefault();
34 | this.setCoords( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
35 | }
36 | }
37 | onDocumentTouchMove( event ) {
38 | if ( event.touches.length === 1 ) {
39 | // event.preventDefault();
40 |
41 | this.setCoords( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
42 | }
43 | }
44 |
45 | update(){
46 | this.diff.subVectors(this.coords, this.coords_old);
47 | this.coords_old.copy(this.coords);
48 |
49 | if(this.coords_old.x === 0 && this.coords_old.y === 0) this.diff.set(0, 0);
50 | }
51 | }
52 |
53 | export default new Mouse();
54 |
--------------------------------------------------------------------------------
/src/components/modules/Poisson.js:
--------------------------------------------------------------------------------
1 | import face_vert from "../glsl/face.vert.js";
2 | import poisson_frag from "../glsl/poisson.frag.js";
3 |
4 | import ShaderPass from "./ShaderPass";
5 |
6 | export default class Divergence extends ShaderPass{
7 | constructor(simProps,rootState){
8 | super({
9 | material: {
10 | vertexShader: face_vert,
11 | fragmentShader: poisson_frag,
12 | uniforms: {
13 | boundarySpace: {
14 | value: simProps.boundarySpace
15 | },
16 | pressure: {
17 | value: simProps.dst_.texture
18 | },
19 | divergence: {
20 | value: simProps.src.texture
21 | },
22 | px: {
23 | value: simProps.cellScale
24 | }
25 | },
26 | },
27 | output: simProps.dst,
28 |
29 | output0: simProps.dst_,
30 | output1: simProps.dst
31 | },rootState);
32 |
33 | this.init();
34 | }
35 |
36 | update({iterations}){
37 | let p_in, p_out;
38 |
39 | for(var i = 0; i < iterations; i++) {
40 | if(i % 2 == 0){
41 | p_in = this.props.output0;
42 | p_out = this.props.output1;
43 | } else {
44 | p_in = this.props.output1;
45 | p_out = this.props.output0;
46 | }
47 |
48 | this.uniforms.pressure.value = p_in.texture;
49 | this.props.output = p_out;
50 | super.update();
51 | }
52 |
53 | return p_out;
54 | }
55 | }
--------------------------------------------------------------------------------
/src/components/modules/Pressure.js:
--------------------------------------------------------------------------------
1 | import face_vert from "../glsl/face.vert.js";
2 | import pressure_frag from "../glsl/pressure.frag.js";
3 | import ShaderPass from "./ShaderPass";
4 |
5 | export default class Divergence extends ShaderPass{
6 | constructor(simProps,rootState){
7 | super({
8 | material: {
9 | vertexShader: face_vert,
10 | fragmentShader: pressure_frag,
11 | uniforms: {
12 | boundarySpace: {
13 | value: simProps.boundarySpace
14 | },
15 | pressure: {
16 | value: simProps.src_p.texture
17 | },
18 | velocity: {
19 | value: simProps.src_v.texture
20 | },
21 | px: {
22 | value: simProps.cellScale
23 | },
24 | dt: {
25 | value: simProps.dt
26 | }
27 | }
28 | },
29 | output: simProps.dst
30 | },rootState);
31 |
32 | this.init();
33 | }
34 |
35 | update({vel, pressure}){
36 | this.uniforms.velocity.value = vel.texture;
37 | this.uniforms.pressure.value = pressure.texture;
38 | super.update();
39 | }
40 |
41 | }
--------------------------------------------------------------------------------
/src/components/modules/ShaderPass.js:
--------------------------------------------------------------------------------
1 | import * as THREE from "three";
2 |
3 | export default class ShaderPass{
4 |
5 | constructor(props,rootState){
6 | this.props = props;
7 | this.uniforms = this.props.material?.uniforms;
8 | this.rootState = rootState
9 | }
10 |
11 | init(){
12 | this.scene = new THREE.Scene();
13 | this.camera = new THREE.Camera();
14 |
15 | if(this.uniforms){
16 | this.material = new THREE.RawShaderMaterial(this.props.material);
17 | this.geometry = new THREE.PlaneGeometry(2.0, 2.0);
18 | this.plane = new THREE.Mesh(this.geometry, this.material);
19 | this.scene.add(this.plane);
20 | }
21 |
22 | }
23 |
24 | update(){
25 | this.rootState.gl.setRenderTarget(this.props.output);
26 | this.rootState.gl.render(this.scene, this.camera);
27 | this.rootState.gl.setRenderTarget(null);
28 | }
29 | }
--------------------------------------------------------------------------------
/src/components/modules/Simulation.js:
--------------------------------------------------------------------------------
1 | import * as THREE from "three";
2 |
3 | import Advection from "./Advection";
4 | import ExternalForce from "./ExternalForce";
5 | import Viscous from "./Viscous";
6 | import Divergence from "./Divergence";
7 | import Poisson from "./Poisson";
8 | import Pressure from "./Pressure";
9 |
10 | export default class Simulation{
11 | constructor(rootState){
12 | this.rootState = rootState;
13 |
14 | this.fbos = {
15 | vel_0: null,
16 | vel_1: null,
17 |
18 | // for calc next velocity with viscous
19 | vel_viscous0: null,
20 | vel_viscous1: null,
21 |
22 | // for calc pressure
23 | div: null,
24 |
25 | // for calc poisson equation
26 | pressure_0: null,
27 | pressure_1: null,
28 | };
29 |
30 | this.options = {
31 | iterations_poisson: 32,
32 | iterations_viscous: 32,
33 | mouse_force: 20,
34 | resolution: 0.5,
35 | cursor_size: 100,
36 | viscous: 30,
37 | isBounce: false,
38 | dt: 0.014,
39 | isViscous: false,
40 | BFECC: true
41 | };
42 |
43 |
44 | this.fboSize = new THREE.Vector2();
45 | this.cellScale = new THREE.Vector2();
46 | this.boundarySpace = new THREE.Vector2();
47 |
48 | this.init();
49 | }
50 |
51 |
52 | init(){
53 | this.calcSize();
54 | this.createAllFBO();
55 | this.createShaderPass();
56 | }
57 |
58 | createAllFBO(){
59 | const type = ( /(iPad|iPhone|iPod)/g.test( navigator.userAgent ) ) ? THREE.HalfFloatType : THREE.FloatType;
60 |
61 | for(let key in this.fbos){
62 | this.fbos[key] = new THREE.WebGLRenderTarget(
63 | this.fboSize.x,
64 | this.fboSize.y,
65 | {
66 | type: type
67 | }
68 | )
69 | }
70 | }
71 |
72 | createShaderPass(){
73 | this.advection = new Advection({
74 | cellScale: this.cellScale,
75 | fboSize: this.fboSize,
76 | dt: this.options.dt,
77 | src: this.fbos.vel_0,
78 | dst: this.fbos.vel_1
79 | },this.rootState);
80 |
81 | this.externalForce = new ExternalForce({
82 | cellScale: this.cellScale,
83 | cursor_size: this.options.cursor_size,
84 | dst: this.fbos.vel_1,
85 | },this.rootState);
86 |
87 | this.viscous = new Viscous({
88 | cellScale: this.cellScale,
89 | boundarySpace: this.boundarySpace,
90 | viscous: this.options.viscous,
91 | src: this.fbos.vel_1,
92 | dst: this.fbos.vel_viscous1,
93 | dst_: this.fbos.vel_viscous0,
94 | dt: this.options.dt,
95 | },this.rootState);
96 |
97 | this.divergence = new Divergence({
98 | cellScale: this.cellScale,
99 | boundarySpace: this.boundarySpace,
100 | src: this.fbos.vel_viscous0,
101 | dst: this.fbos.div,
102 | dt: this.options.dt,
103 | },this.rootState);
104 |
105 | this.poisson = new Poisson({
106 | cellScale: this.cellScale,
107 | boundarySpace: this.boundarySpace,
108 | src: this.fbos.div,
109 | dst: this.fbos.pressure_1,
110 | dst_: this.fbos.pressure_0,
111 | },this.rootState);
112 |
113 | this.pressure = new Pressure({
114 | cellScale: this.cellScale,
115 | boundarySpace: this.boundarySpace,
116 | src_p: this.fbos.pressure_0,
117 | src_v: this.fbos.vel_viscous0,
118 | dst: this.fbos.vel_0,
119 | dt: this.options.dt,
120 | },this.rootState);
121 | }
122 |
123 | calcSize(){
124 | const width = Math.round(this.options.resolution * window.innerWidth);
125 | const height = Math.round(this.options.resolution * window.innerHeight);
126 |
127 | const px_x = 1.0 / width;
128 | const px_y = 1.0 / height;
129 |
130 | this.cellScale.set(px_x, px_y);
131 | this.fboSize.set(width, height);
132 | }
133 |
134 | resize(){
135 | this.calcSize();
136 |
137 | for(let key in this.fbos){
138 | this.fbos[key].setSize(this.fboSize.x, this.fboSize.y);
139 | }
140 | }
141 |
142 |
143 | update(){
144 |
145 | if(this.options.isBounce){
146 | this.boundarySpace.set(0, 0);
147 | } else {
148 | this.boundarySpace.copy(this.cellScale);
149 | }
150 |
151 | this.advection.update(this.options);
152 |
153 | this.externalForce.update({
154 | cursor_size: this.options.cursor_size,
155 | mouse_force: this.options.mouse_force,
156 | cellScale: this.cellScale
157 | });
158 |
159 | let vel = this.fbos.vel_1;
160 |
161 | if(this.options.isViscous){
162 | vel = this.viscous.update({
163 | viscous: this.options.viscous,
164 | iterations: this.options.iterations_viscous,
165 | dt: this.options.dt
166 | });
167 | }
168 |
169 | this.divergence.update({vel});
170 |
171 | const pressure = this.poisson.update({
172 | iterations: this.options.iterations_poisson,
173 | });
174 |
175 | this.pressure.update({ vel , pressure});
176 | }
177 | }
--------------------------------------------------------------------------------
/src/components/modules/Viscous.js:
--------------------------------------------------------------------------------
1 | import face_vert from "../glsl/face.vert.js";
2 | import viscous_frag from "../glsl/viscous.frag.js";
3 | import ShaderPass from "./ShaderPass";
4 |
5 | export default class Viscous extends ShaderPass{
6 | constructor(simProps,rootState){
7 | super({
8 | material: {
9 | vertexShader: face_vert,
10 | fragmentShader: viscous_frag,
11 | uniforms: {
12 | boundarySpace: {
13 | value: simProps.boundarySpace
14 | },
15 | velocity: {
16 | value: simProps.src.texture
17 | },
18 | velocity_new: {
19 | value: simProps.dst_.texture
20 | },
21 | v: {
22 | value: simProps.viscous,
23 | },
24 | px: {
25 | value: simProps.cellScale
26 | },
27 | dt: {
28 | value: simProps.dt
29 | }
30 | }
31 | },
32 |
33 | output: simProps.dst,
34 |
35 | output0: simProps.dst_,
36 | output1: simProps.dst
37 | },rootState)
38 |
39 | this.init();
40 | }
41 |
42 | update({ viscous, iterations, dt }){
43 | let fbo_in, fbo_out;
44 | this.uniforms.v.value = viscous;
45 | for(var i = 0; i < iterations; i++){
46 | if(i % 2 == 0){
47 | fbo_in = this.props.output0;
48 | fbo_out = this.props.output1;
49 | } else {
50 | fbo_in = this.props.output1;
51 | fbo_out = this.props.output0;
52 | }
53 |
54 | this.uniforms.velocity_new.value = fbo_in.texture;
55 | this.props.output = fbo_out;
56 | this.uniforms.dt.value = dt;
57 |
58 | super.update();
59 | }
60 |
61 | return fbo_out;
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/src/index.css:
--------------------------------------------------------------------------------
1 | body {
2 | margin: 0;
3 | padding: 0;
4 | position: fixed;
5 | width: 100vw;
6 | height: 100vh;
7 | }
8 |
--------------------------------------------------------------------------------
/src/index.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import ReactDOM from 'react-dom/client';
3 | import './index.css';
4 | import App from './App';
5 |
6 | const root = ReactDOM.createRoot(document.getElementById('root'));
7 | root.render(
8 |
9 |
10 |
11 | );
12 |
13 |
--------------------------------------------------------------------------------
/src/logo.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------