NVIDIAGameWorks / FleX

Other
646 stars 99 forks source link

Unity FleX Get/SetSprings functions #107

Open kv-ep opened 4 years ago

kv-ep commented 4 years ago

I am trying to use the Get and SetSprings functionality exposed in the Unity plugin, and I am running into some odd behavior where the editor will periodically crash. Also, the values obtained are not as expected.

Attach this script to a FlexClothActor and it should print out a spring stiffness value and then set all the springs to the values it just got (should have no affect). Instead it prints 0 every iteration and the cloth behaves as if it has no tension and/or blows up if it touches anything. Also, every other time the editor is run, it will crash. These functions are not used throughout the rest of the plugin, so I am somewhat guessing as to how they should work. Is this the proper way to access/modify spring values?

using NVIDIA.Flex;
using UnityEngine;

[RequireComponent(typeof(FlexActor))]
public class ClothTest : MonoBehaviour
{
    private FlexActor _actor = null;
    private int _springCount = 0;
    private FlexBuffer _indicesBuffer = null;
    private FlexBuffer _restLengthBuffer = null;
    private FlexBuffer _stiffnessBuffer = null;

    private void Start()
    {
        _actor = GetComponent<FlexActor>();
        _actor.onFlexUpdate += FlexUpdate;
        _springCount = _actor.asset.handle.asset.numSprings;
        _indicesBuffer = new FlexBuffer(FlexContainer.library, _springCount * 2, sizeof(int));
        _restLengthBuffer = new FlexBuffer(FlexContainer.library, _springCount, sizeof(float));
        _stiffnessBuffer = new FlexBuffer(FlexContainer.library, _springCount, sizeof(float));
    }

    private void FlexUpdate(FlexContainer.ParticleData _particleData)
    {
        Flex.GetSprings(_actor.container.solver, _indicesBuffer.handle, _restLengthBuffer.handle, _stiffnessBuffer.handle, _springCount);
        // Read a value from the array (could modify springs here)
        float[] stiffnessValues = new float[_springCount];
        _stiffnessBuffer.Get(0, _springCount, stiffnessValues);
        Debug.Log(stiffnessValues[_springCount / 2]);
        // Set the new spring values
        Flex.SetSprings(_actor.container.solver, _indicesBuffer.handle, _restLengthBuffer.handle, _stiffnessBuffer.handle, _springCount);
    }
}
DouglasNordwall commented 9 months ago

Did you manage to solve this? Or acquire the springs in some other way?

kv-ep commented 9 months ago

I don't think I ever got it working. Pretty sure it's a bug and I doubt there will be any updates to FleX at this point.

You can get access to the same technology if you use PhysX 5 though (no existing Unity integration for it so you would have to do that yourself).