Skip to content

Commit 28aa109

Browse files
committed
update sim to dynamic time-step
Signed-off-by: Guilherme Avila <[email protected]>
1 parent 4d25be1 commit 28aa109

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/glsl/integrate.frag.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ precision highp float;
33
precision highp sampler2D;
44
55
uniform vec2 tSize;
6+
uniform float dt;
67
uniform float order;
78
uniform sampler2D tOriginal;
89
uniform sampler2D tPrevious0;
910
uniform sampler2D tPrevious1;
1011
uniform sampler2D tPosition0;
1112
uniform sampler2D tPosition1;
1213
13-
#define dt2 0.000256
14-
1514
vec3 unpackPosition( vec3 pos ) {
1615
1716
pos *= 1024.0;
@@ -22,13 +21,15 @@ vec3 unpackPosition( vec3 pos ) {
2221
2322
void main() {
2423
24+
float dt2 = dt*dt;
25+
2526
vec2 uv = gl_FragCoord.xy / tSize.xy;
2627
2728
vec3 org = texture2D( tOriginal, uv ).xyz;
2829
vec3 prv = ( texture2D( tPrevious0, uv ).xyz + texture2D( tPrevious1, uv ).xyz ) / 1024.0;
2930
vec3 pos = ( texture2D( tPosition0, uv ).xyz + texture2D( tPosition1, uv ).xyz ) / 1024.0;
3031
31-
vec3 offset = ( org - pos ) * 18.5 * dt2 * 8.33333;
32+
vec3 offset = ( org - pos ) * 260.5 * dt2;
3233
vec3 disp = ( pos - prv ) * 0.94 + pos;
3334
3435
gl_FragColor = vec4( unpackPosition( disp + offset ), 1.0 );

src/modules/fbo.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ RESOLUTION,
1414
renderer, mesh, targetRT, normalsRT,
1515
originalRT, previousRT, positionRT,
1616
adjacentsRT, distancesRT,
17-
steps = 40;
17+
steps = 40, prevTime, dt;
1818

1919

2020
const
@@ -73,6 +73,8 @@ function init( WebGLRenderer ) {
7373

7474
}
7575

76+
prevTime = performance.now();
77+
7678
}
7779

7880
function copyTexture( input, output, order ) {
@@ -183,12 +185,15 @@ function integrate() {
183185

184186
mesh.material = integrateShader;
185187
integrateShader.uniforms.tSize.value = tSize;
188+
integrateShader.uniforms.dt.value = dt;
186189
integrateShader.uniforms.tOriginal.value = originalRT.texture;
187190
integrateShader.uniforms.tPrevious0.value = previousRT[ 0 ].texture;
188191
integrateShader.uniforms.tPrevious1.value = previousRT[ 1 ].texture;
189192
integrateShader.uniforms.tPosition0.value = positionRT[ 0 ].texture;
190193
integrateShader.uniforms.tPosition1.value = positionRT[ 1 ].texture;
191194

195+
// console.log(dt);
196+
192197
// integer-part
193198
integrateShader.uniforms.order.value = 1;
194199
renderer.setRenderTarget( targetRT[ 0 ] );
@@ -294,6 +299,10 @@ function computeVertexNormals( ) {
294299

295300
function update() {
296301

302+
const now = performance.now();
303+
dt = (now - prevTime)/1000;
304+
prevTime = now;
305+
297306
integrate();
298307

299308
let mouseUpdating = MOUSE.updating();
@@ -310,4 +319,8 @@ function update() {
310319

311320
}
312321

322+
window.onfocus = function() {
323+
prevTime = performance.now();
324+
}
325+
313326
export { init, update, positionRT, normalsRT };

0 commit comments

Comments
 (0)