skip to Main Content

Scripting Support C#

All the script properties are accesible through its class. To change any property using C# you need to get a reference to the LiquidVolume component of the target gameObject using:

using LiquidVolumeFX;

…

LiquidVolume liquid = <gameobject>.GetComponent<LiquidVolume>();

Then, to change the current fill level to 65%, you can do:

liquid.level = 0.65;

If detail level is set to Multiple, the individual layer levels can be changed through the liquidLayers array and then calling:

liquid.UpdateLayers(false or true); // pass “true” to force immediate update

In addition to the look & feel properties, you will find useful the following API:

 

float liquidSurfaceYPosition;

Returns the world space vertical position of the liquid surface (without turbulences).

 

public void MoveToLiquidSurface(Transform transform, BuoyancyEffect effect, Transform root = null, float dampen = 0.8f)

Moves an object to liquid surface. This method can approximate the displacement and rotation caused by turbulences. Check demo scene “Buoyancy” for an example.

  • transform: the transform of the object to be moved.
  • effect: kind of buoyancy effect, if only position is changed or position and rotation as well.
  • root: if effect includes rotation, a root or parent object to the object to be moved must be provided.
  • dampen: amount of turbulence received by the object (0-1).

 

public bool GetSpillPoint(out Vector3 spillPosition, float apertureStart = 1f)

Calculates approximate point where liquid starts pouring over the flask when it’s rotated. Returns true if liquid spills out.

  • spillPosition: the position where the liquid starts pouring.
  • apertureStart: a value that determines where the aperture of the flask starts (0-1 where 0 is the flask center and 1 is the very top of the mesh).

 

public bool GetSpillPoint(out Vector3 spillPosition, out float spillAmount, float apertureStart = 1f, LEVEL_COMPENSATION rotationCompensation = LEVEL_COMPENSATION.None)

Calculates approximate point where liquid starts pouring over the flask when it’s rotated. Returns true if liquid spills out.

  • spillPosition: the position where the liquid starts pouring.
  • spillAmount: a value that represents an amount of liquid spilt.
  • apertureStart: a value that determines where the aperture of the flask starts (0-1 where 0 is the flask center and 1 is the very top of the mesh).
  • rotationCompensation: the method to compute the amount of liquid spilt.

 

public void BakeRotation();

Applies current transform rotation and scale to the vertices and resets the transform rotation and scale to default values. This operation makes the game object transform point upright as normal game objects and is required for Liquid Volume to work on imported models that comes with a rotation

 

public void CenterPivot(Vector3 offset, bool closeMesh);

This operation computes the geometric center of all vertices and displaces them so the pivot is centered in the model:

  • offset: displacement added to the pivot.
  • closeMesh: if true, the mesh will be modified so it results in a closed shape (useful for open glasses for example).

 

public void SetLayerDensity(int layerIndex, float newDensity, bool keepVolume);

Convenient function that can change the density of a layer while also keeping the same volume. Usually increasing the density of a liquid will decrease the volume (amount must be increased to compensate; this method does that):

  • layerIndex: layer to be modified.
  • newDensity: the new density value.
  • keepVolume: the amount of the liquid in this layer will be modified so it keeps the previous volume.

 

public float GetMeshVolumeWS()

Returns the approximate volume of a mesh.

 

public float GetMeshVolumeUnderLevel(float level01, float yExtent)

Approximates the mesh volume under a fill level expressed in 0-1 range:

  • level01: fill level (0-1)
  • yExtent: y-extent of the volume (ie. renderer.bounds.extents.y).

 

public float GetMeshVolumeWSFast()

Returns the approximate volume of a mesh (fast method).

 

public float GetMeshVolumeUnderLevelWSFast(float level)

Returns the approximate volume of a mesh (fast method):

  • level: fill level (0-1)
Back To Top