Position of the flow.object3D in a moving along scene

I need help to log the position of the flow.object3D on the run.

PS: I have this …

g[1]=new THREE.CylinderGeometry(0.05,0.05,0.3);
m[1]=new THREE.MeshStandardMaterial({color:'#0ff'});
s[1]=new THREE.Mesh(g[1],m[1]);

objectToCurve=s[1];
flow=new Flow(objectToCurve);
flow.updateCurve(0,curve);
flow.moveAlongCurve(500);
console.log(flow.object3D.position.x+" "+flow.object3D.position.y+" "+flow.object3D.position.z);
scene.add(flow.object3D);

and this in the animate()…

if(flow){flow.moveAlongCurve(-0.0005);}

What’s “Flow” ?

It is possible via…

import { Flow } from ‘three/addons/modifiers/CurveModifier.js’;

1 Like

is your console.log not working inside animate() ?

To monitor flow.object3D.position continuously, you need to read it in your render/animation loop. For example:

function animate() {
  requestAnimationFrame(animate);
  if (flow) {
    flow.moveAlongCurve(-0.0005);
    console.log(
      'Flow position:',
      flow.object3D.position.x,
      flow.object3D.position.y,
      flow.object3D.position.z
    );
  }
  renderer.render(scene, camera);
}
animate();

This way, you’ll see the object’s position update each frame. If printing every frame affects performance or clutters the console, you can log less frequently (e.g., every few frames) or store the data for debugging later. Also, ensure flow.object3D is indeed the object that moves along the curve, as some libraries manage internal transforms differently, requiring you to check flow.object3D.children[0] or similar if the position doesn’t change as expected.

Hi @manthrax and @HEOJUNFO

I have
console.log(flow.object3D.position.x+" “+flow.object3D.position.y+” "+flow.object3D.position.z);
inside animate() … but the output is always 0 0 0.

The link is:

https://jrlazz.eu5.org/anim/aalong.html

does this show different values:

let e = flow.object3D.matrix.elements;
console.log(e[12]+","+e[13]+","+e[14]);

Hi @manthrax

Same 0,0,0 …the link was modified with 2 console.log for You to see…

PS: GPT did not help me to solve it ( at the first try )!
PS2: No solution with 45 minutes with GPT (second try)!!

I am not sure if this 3D object is actually moving at all.

It looks like the flow shader is just painting material in a new position.

Maybe check the code of the CurveModifier.js.

2 Likes

Yes, it’s moving the vertices directly in the shader. You can see it updating the instance matrices with InstancedFlow, so the parent object position stays the same. Ref. CurveModifier.js

Since the curve object is defined, you can get the current virtual position using one of the Curve’s methods, like curve.getPoint(t).

2 Likes

Well, if the code in this standalone HTML is correct then it might help with locating position (z always seems to be zero).

Just click the blue square to start / stop logging - maybe comment out the fast moving code since it is distracting.

AALong.zip (1.7 KB)

2 Likes

Hi @Fennec and @GitHubDragonFly

I created a page based in AALong (from @GitHubDragonFly )… …

The link is:

https://jrlazz.eu5.org/anim/aalong_2.html

As we can see, the movement of the box, based on curve.getPoint(t), do not follow the flow movement.
The velocity of the box is not the same of the cylinder.
GPT, for two times carried me to this approach.
I suppose that the solution only will be possible when we find what changes inside the shader and log this in the animate function.

You might need to reconsider how you use these curves and the modifier.

Here is slightly changed example.

AALong.zip (1.9 KB)

1 Like

Hi @GitHubDragonFly
Yo have done s[1].add(s[2]); … then threre will only be a flow… no more s[2]!
My need is to log the position of flow, to use this position to make other things.
For example synchronization of other meshes, appearence of tags, touching the flow with another mesh… etc.
The post was started to call for help for correct the movement of the chain with respect to the gears here…

Anyway, Thank You very much for Your attention. :wink:

It seems like there is an offset of 0.25 between how the curve is interpolated inside the Shader and with the Curve class, I had to initiate the t used by the curve class with an offset of 0.25.

let currentT = 0.25; // 0.25 Offset
const positionAtT = new THREE.Vector3();

//...

// Inside the animation loop
if (flow) {
    flow.moveAlongCurve(0.001);

    currentT += 0.001; // Incrementing the current t
    curve.getPoint(currentT, positionAtT);
    console.log(positionAtT);
}

And here is an example updated from the official demo, with a position tracker:

image

1 Like

Hi @Fennec

Wau!!! I’ll study your post, try to log the trackerLabel position…
Thank you very much for your effort in solving the issue!!!

1 Like

My idea was to create the whole model as a single mesh and add it to the flow, this way everything should keep in sync.

But if @Fennec solution works then even better.

2 Likes

Hi @Fennec

The link:

https://jrlazz.eu5.org/anim/Fennec4.html

Fennec4.zip (1.9 KB)

I made a page to observe your proposal…
Notice that the cylinder oscillates between the letters t and r.
I believe it is a difference between shader times and the browser timer.
Please see if it is possible to correct it.
However, I think your proposal is great.

Thank you very much for your attention! :wink:

PS: Sorry for the delay. I didn’t have an Internet connection due to heavy rains here in São Paulo.

1 Like

Thanks for the kind words and feedback! :blush:

I looked into the issue and here’s what I found:

The oscillation occurs because the shader’s stores pre-computed positions in a DataTexture (resolution TEXTURE_WIDTH = 1024), and this result in a uniform interpolation, while curve.getPoint(t) produces non-uniform points (slower on curves, more points, faster on straight segments, less points), causing the mismatch.

The Solution

Instead of using curve.getPoint(t), I tried to mimic the shader, so I’ve pre-computed the curve points at the same resolution as the shader (TEXTURE_WIDTH = 1024) and interpolated through these points. And it worked :relieved:! I got a consistent behavior between the curve and the shader.

You can see it at work with the updated example.

Updated Code:

  1. updateCurvePoints: Updates the curvePoints array whenever the curve changes:

    // line 255
    function updateCurvePoints() {
      const numberOfPoints = TEXTURE_WIDTH;
      curvePoints.length = 0;
      curvePoints.push(...curve.getSpacedPoints(numberOfPoints));
    }
    
    
    • Called at line 134 (initialization) and line 204 (inside the dragging-changed event listener).
  2. interpolateCurvePoints: Interpolates through the curvePoints array for a given t:

    // line 262
    const _vec3 = new THREE.Vector3();
    function interpolateCurvePoints(t, curvePoints) {
      t = t % 1;
      const index = Math.floor(t * (curvePoints.length - 1));
      const pointsT = t * (curvePoints.length - 1) - index;
      const pointA = curvePoints[index];
      const pointB = curvePoints[index + 1];
      return _vec3.lerpVectors(pointA, pointB, pointsT);
    }
    
    • Called at line 241, inside the animation loop.
  3. Animation Loop Update:

    // line 239
    if (flow) {
      flow.moveAlongCurve(0.001);
      const positionAtT = interpolateCurvePoints((currentT += 0.001), curvePoints);
      trackerLabel.setPosition(positionAtT);
    }
    

Offset issue

The initial offset issue still exists, and I couldn’t solve it. However, it seems stable as long as the curve isn’t updated. A potential solution could be to store the offset with each predefined curve update.

1 Like

Hi @Fennec

Pure beauty… Very grateful… it’s the solution!!! :grinning:

1 Like