Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  Generating and using distance field at runtime
#1
Hi there,

I'm using Obi Fluid in my game and so far it works very well! However, there is something very specific to my use case that I'm trying to accomplish without success so far.

I'm generating a mesh at runtime through a script. I've been able to make fluid collisions work with it by adding the ObiCollider to my mesh-containing GameObject at runtime, after the mesh is updated, like so:

Code:
void RecreateObiCollider()
   {
       Destroy(_plane.GetComponent<ObiCollider>());

       _plane.AddComponent<ObiCollider>();
       var collider = _plane.GetComponent<ObiCollider>();
       collider.CollisionMaterial = _collisionMaterial;
   }

However as my plane has many vertices, you can guess that performance is far from excellent with lots of particles.

I've investigated using a distance field to gain performance and I was able to make collisions work with a static mesh of the same plane and performance did increase substantially.

The issue I have is that I need to use a runtime generated mesh for collision detection; there is no way for me to know what the mesh will look like in advance to generate a distance field for it in the editor. Thus, I'm trying to generate and assign the distance field at runtime after generating my mesh. I haven't been able to get this working.

Here is the code I have so far. When that code runs, collisions work just fine until the distanceField.Generate() coroutine ends, after which the distance field is assigned. Then, collisions stop working completely and particles go right through my mesh. As far as the setup is concerned, it looks fine in the editor in play mode; the distance field is assigned and enabled, etc.

Code:
IEnumerator RecreateObiCollider()
   {
       Destroy(_plane.GetComponent<ObiCollider>());

       var distanceField = (ObiDistanceField)ScriptableObject.CreateInstance("ObiDistanceField");
       distanceField.InputMesh = _mesh;
       yield return StartCoroutine(distanceField.Generate());

       var collider = _plane.AddComponent<ObiCollider>();
       collider.CollisionMaterial = _collisionMaterial;
       collider.distanceField = distanceField;
       collider.UseDistanceFields = true;
   }

Any help would be appreciated!
Reply