Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Placing objects on volume
Hi !
I wrote this code to place objects on my spline volume :
CurvySpline spline = GetComponentInParent<CurvySpline>();
CurvyGenerator cg = transform.parent.GetComponentInChildren<CurvyGenerator>();
CGDataReference m_Volume = new CGDataReference(cg.GetComponentInChildren<BuildShapeExtrusion>(), "Volume");
CGVolume m_VolumeData = m_Volume.GetData<CGVolume>();

Vector3 p = spline.Interpolate(tfPosition);
Vector3 up = spline.GetRotatedUp(tfPosition, 0);
Vector3 fwd = spline.GetTangent(tfPosition);

if(crossTfPosition != 0 && m_VolumeData != null)
     m_VolumeData.InterpolateVolume(tfPosition, crossTfPosition, out p, out fwd, out up);
transform.localPosition = p;
transform.localRotation = Quaternion.LookRotation(fwd, up);

So basically when crossTfPosition > 0, I'm searching a position on the volume, else I'm just taking the position on the spline (as crossTF 0 is on my spline).
When tfPosition is 0 or 1, I get the same position distance on spline using spline.Interpolate() and volume.InterpolateVolume(). But for any values in between, the position distance is totally different. It's like the tfPosition is stretched or not normalized in one of that functions.

Can you tell me if my code is correct, if there is a better way to do it and what could be the issue with the TF calculation ?

Thanks for support !

The difference in the position returned by Interpolate and InterpolateVolume does not seem to be caused by a mistake in your code, but is probably an issue in Curvy itself. I will take a deeper look at this soon.

Meanwhile, some remarks:
- you can optimize your code by computing p, up, and fwd only in the else section of your if. In your current code, when crossTfPosition != 0 you end up computing these vectors twice, once through the spline and then through the volume
- was there any reason that made you code your own object placing code instead of using a Volume Spots CG module?

Have a nice day and happy new year
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
After a deeper look, here is my answer:
The spline.Interpolate method takes as an input a TF. A TF is not equally distributed on a spline. Please read this to learn more about it.
On the other hand, m_VolumeData.InterpolateVolume takes a value between 0 and 1 that is proportional to the length.

This difference beetween the two is already handled by the Volume Spots CG module, so using it will solve your issue. But if you have any reason to not use the Volume Spots module, simply replace the first parameter of your InterpolateVolume call by this:
spline.TFToDistance(tfPosition) / spline.Length

Also, on a different subject, if you are using Curvy 5.0.0, you will notice a compiler warning telling you that GetRotatedUp is obsolete and will be removed in a future update. Just inline this method in your own script to solve this. By inlining it, you will also benefit from the side effect which is that you can avoid computing the tangent twice.
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Hi Aka,
Thanks for the explaination and solution. I thought TF was equally distributed in any case!

Regarding the script, I needed a more custom object placement system with custom grid and pattern options.

Thanks again for you very fast reply and happy new year Wink

Possibly Related Threads...
Thread Author Replies Views Last Post
  Freeing objects from a spline Renald 1 440 06-20-2018, 11:10 AM
Last Post: _Aka_
  Spawn Objects of differing lengths uniformly along a Spline taylor 1 471 06-19-2018, 05:31 PM
Last Post: _Aka_
  Placing objects equally apart - Volume Spot kypopete 4 1,776 02-07-2017, 12:06 PM
Last Post: Gazoid
  "Keep Aspect" in Volume Mesh CG module does nothing freqout 5 2,666 05-27-2016, 06:00 AM
Last Post: Jake

Forum Jump: