Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved How to create CatMull-Rom by vfxGraph

Discussion in 'Visual Effect Graph' started by minte131, May 3, 2024.

  1. minte131

    minte131

    Joined:
    May 11, 2019
    Posts:
    7
    Hello guys,
    I tried to create a Catmull-Rom Spline using VFX Graph but it doesn't work as I thought. My method is:
    1. Finidng t = ParticleID/TotalParticle.
    2. Apply this function: p(t)=0.5×((2×p1)+(−p0+p2)×t+(2×p0−5×p1+4×p2−p3)×t2+(−p0+3×p1−3×p2+p3)×t3) (p0,p1,p2,p3 are the position which I wanna the line move over)
    3. the starting position at p1 instead of p0

    Screenshot_13.jpg

    I tried to test my setup by Sample Bezier node and it work fine Screenshot_11.jpg
    I tried to create a script to input values into the VFX Graph's exposed component, but we can't get the ParticleID. Where did I miss the point that caused this issue?

    btw why shadergraph has customfunction but vfxgraph?
     
    Last edited: May 3, 2024
  2. OrsonFavrel

    OrsonFavrel

    Unity Technologies

    Joined:
    Jul 25, 2022
    Posts:
    216
    You could use your function inside a Custom HLSL Block or Operator or directly by using regular Nodes/operators..

    But VFXGraph also comes with a SampleBezier Operator. This should allow you to get a nice Bezier interpolation thanks to input Positions.
    For this, you just need to give the input Positions and also a 0-1 value which is as you said:
    Code (CSharp):
    1.  ParticleID / (TotalParticle-1)
    upload_2024-5-14_17-2-36.png

    From here, you can go a little further and get dynamic Input Positions for your Bezier Sampling.
    For this, the easiest road would be to use the provided Multiple Position Binder Script. This allows you to reference GameObjects, and the script gets their position and stores them in a small 2dTexture.

    1. In VFXGraph create an exposed Texture2D property.
    2. name it as you like.
    3. Create an exposed Integer property.
    4. name it as you like.
    5. Select your VFX instance in the scene.
    6. in the Inspector, Add a component
    7. search for the Multiple Position binder script.
    8. Click the Small V icon on the far right of each Property and bind it to your named property.
      upload_2024-5-14_17-11-43.png
    9. If the binding is correct, you should a a green square.
    10. Check Every Frame.
    11. Set your Target GameObjects that will be used as Target Position.
      upload_2024-5-14_17-13-51.png
    12. Back in VFXGraph, create a Sample AttributeMap Operator.
    13. Use your Texture2D property for the Map and the Int Property for the Point Count.
    14. Make sure that the output of the operator is Vector3. If not change it to Vector3.
    15. Once wired, Right-click on the operator and Select duplicate with Edges three times.
    16. Change the index of the copy so that you have for node with index 0,1,2,3.
    17. Use the Vector3 Output of each node in the Sample Bezier operator.
    18. Hurrayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
      upload_2024-5-14_17-18-18.png

      Unity_VfYSYKmwxv.gif
     
    Julien-A likes this.
  3. OrsonFavrel

    OrsonFavrel

    Unity Technologies

    Joined:
    Jul 25, 2022
    Posts:
    216
    Now, to finish, you could also use a Subgraph that comes with the Additional sample that can be found in the package manager. Once installed, you'll be able to find a new Subgraph operator named Sample Multiple Position Map.
    This Subgraph has been made to work with the Multiple Position Binder Script and offers some bezier smoothing controls.

    upload_2024-5-14_17-28-13.png

    All the previous steps details in the post above apply here too.

    Unity_rzFzPFoLTV.gif

    I hope that this will help you with what you're trying to achieve.
    Have a wonderful day and don't hesitate to share your results :)
     
    Julien-A likes this.
  4. gabriel-delacruz

    gabriel-delacruz

    Unity Technologies

    Joined:
    May 19, 2021
    Posts:
    41
    Just a quick note, not sure if I'm missing something, but I think the original results were correct for a Catmull-Rom spline.
    They are defined by 4 points, same as cubic bezier, but in this case the segment is defined between p1 and p2.
    Just different type of splines