I'm facing difficulty grasping the logic I am working on with Three.js and the GPUComputationRenderer developed by yomboprime.
(https://github.com/yomboprime/GPGPU-threejs-demos/blob/gh-pages/js/GPUComputationRenderer.js)
My goal is to create a simple Verlet Cloth Simulation. So far, here is the logic that I have managed to implement (summarized):
1) Position-Fragment-Shader: This shader utilizes the old and current position texture to calculate the new position as shown below:
vec3 position = texture2D( texturePosition, uv ).xyz;
vec3 oldPosition = texture2D( textureOldPosition, uv ).xyz;
position = (position * 2.0 - oldPosition + acceleration * delta *delta )
t = checkConstraints(position);
position += t;
gl_FragColor = vec4(position,1);
2) Old-Position-Shader: This shader simply stores the current position for the next step.
vec3 position = texture2D( texturePosition, uv ).xyz;
gl_FragColor = vec4(position,1);
While this setup works well, it restricts the ability to compute constraints more than once in a single step since each vertex is processed independently without considering the positional changes of other vertices in the first iteration.
What I aim to achieve is separating the constraint calculations from the verlet computation. Currently, the approach looks something like this:
1) Position-Shader (texturePosition)
vec3 position = texture2D( textureConstraints, uv ).xyz;
vec3 oldPosition = texture2D( textureOldPosition, uv ).xyz;
position = (position * 2.0 - oldPosition + acceleration * delta *delta );
gl_FragColor = vec4(position, 1 );
2) Constraint-Shader (textureConstraints)
vec3 position = texture2D( texturePosition, uv ).xyz;
t = checkConstraints(position);
position += t;
gl_FragColor = vec4(position,1);
3) Old-Position-Shader (textureOldPosition)
vec3 position = texture2D( textureConstraints, uv ).xyz;
gl_FragColor = vec4(position,1);
Unfortunately, this logic is not functioning properly. Even when excluding constraint calculations and passing values unchanged, the addition of any acceleration in the Position Shader leads to position values spiraling out of control.
Where could I be making an error?