Skip to main content

LiquidSurfaceVisual

An abstract class in Root.Township, Alta.Liquid.

Base for the liquid surface inside a container. It tracks how far the container is tipped (through LiquidGravity) and how full it is, and on clients draws the surface. It also stays active on the server, where the tip state is still updated.

Constructors​

MemberWhat it is
protected LiquidSurfaceVisual()Unity makes components itself; use AddComponent or spawn a prefab instead of new.

Fields​

20 private and internal fields
MemberWhat it is
private VisibilityDetector androidContainerLod0Visibility detector kept from an older field name; not used in code in this build.
private static readonly int ColorIdShader ID of _Color.
private VisibilityDetector containerLod0Visibility detector on the container's main LOD; the surface only updates while it is visible (always on the server).
private Renderer dynamicRendererThe renderer of the moving liquid surface. Backing field for DynamicRenderer.
private MaterialPropertyBlock dynamicRendererPropertyBlockProperty block set on the surface renderer from the current liquid. Backing field for DynamicRendererPropertyBlock.
private Material dynamicRendererTemplateMaterialThe surface renderer's original material, used as the template for each liquid's material.
private FloatEvent fullnessChangedEvent raised with the new fullness value; exposed as FullnessChanged.
private float fullyPouringLimitTip value above which the container counts as fully pouring.
private LiquidGravity liquidGravitySimulates the liquid sloshing and tipping as the container moves. Backing field for LiquidGravity.
private MaterialPropertyBlock lodPropertyBlockProperty block set on the lower LOD renderers from the current liquid.
private Renderer[] otherLodsRenderers for the lower LODs of the liquid; shown only while there is liquid.
private float tipRead-only inspector value showing the current tip.
private float tippingThresholdTip value at which the liquid starts to pour. Backing field for TippingThreshold.
private float tippingValueRead-only inspector value for tipping; not written in this build.
private static UpdateModule updateModuleLate update module that runs UpdateVisual while the liquid is moving.
private IUnityUpdateMethod visualLoopHandle of the registered UpdateVisual loop.
private float visualTipLimitLimit on how far the surface visually tips. Backing field for VisualTipLimit.
private float volumeClampFullness is multiplied by this before the volume curve, so the surface never looks fuller than this.
private AnimationCurve volumeCurveMaps the container's fill amount to the visual fullness.
private bool wasMovingMoving state from the last fixed update, used to start and stop the visual loop.

Properties​

MemberWhat it is
Renderer DynamicRenderer { get; }The renderer that draws the moving liquid surface.
Material DynamicRendererMaterial { get; internal set; }Instance material of the dynamic renderer, made in SetLiquid.
MaterialPropertyBlock DynamicRendererPropertyBlock { get; }Property block used on the dynamic renderer.
float Fullness { get; private set; }How full it looks, 0 to 1, after the volume curve.
bool IsContainerVisible { get; private set; }True while the container's LOD 0 is visible.
bool IsContainingLiquid { get; private set; }True above 1% fullness.
bool IsFullyPouring { get; }True when tipped past the fully pouring limit.
bool IsMoving { get; private set; }True while the liquid surface is still moving.
virtual bool IsUsingUpdateLoop { get; }Whether UpdateVisual runs while moving. True by default.
LiquidData Liquid { get; private set; }The liquid shown.
LiquidGravity LiquidGravity { get; }Tracks the tip and movement of the liquid.
List<Renderer> OtherRendererLODs { get; }Lower LOD renderers for the liquid, shown only when there is liquid.
float TippingThreshold { get; set; }Tip value at which the container starts to pour.
float VisualTipLimit { get; set; }Tip limit used for the surface visuals.

Events​

MemberWhat it is
event Action DynamicRendererMaterialUpdatedRaised after the materials change for a new liquid.
event Action<float> FullnessChangedRaised with the new fullness.
event LiquidDataEventHandler LiquidChangedRaised when SetLiquid sets a new liquid.

Methods​

MemberWhat it is
virtual void SetFullness(float fullness)Sets how full it is (through the volume curve) and shows or hides the liquid.
virtual void SetLiquid(LiquidData liquid)Shows a new liquid: sets thickness and materials and raises LiquidChanged.
protected virtual void Awake()Sets up property blocks and the tip tracker, and listens for the container becoming visible.
protected virtual void FixedUpdate()Updates the tip tracker, and on clients starts or stops the visual update when movement starts or stops.
protected virtual void HandleInvisible()Called when the container goes out of view. Stays enabled on the server.
protected virtual void HandleVisible()Called when the container comes into view; enables the visual if there is liquid.
protected virtual void OnDisable()Hides the surface and stops the visual update.
protected virtual void OnEnable()Resets the tip tracker, shows the surface and starts the visual update on clients.
protected virtual void OnValidate()Pushes inspector changes to the tip tracker while playing.
protected abstract void SetLODRendererPropertyBlockFromLiquid(MaterialPropertyBlock propertyBlock, LiquidData liquid)Fills a property block for the LOD renderers from a liquid. Subclasses implement it.
protected void SetMaterialsAcordingToLiquid(LiquidData liquid)Applies the liquid's material and property blocks to the dynamic and LOD renderers.
protected abstract void SetPropertyBlockFromLiquid(MaterialPropertyBlock propertyBlock, LiquidData liquid)Fills a property block from a liquid. Subclasses implement it.
protected abstract Material SetupMaterialFromLiquidVisualSettings(LiquidVisualData visualSettings, Material materialTemplate)Builds the shared material for a liquid visual from the template. Subclasses implement it.
protected abstract void UpdateVisual()Redraws the surface while it moves. Subclasses implement it.
2 private and internal methods
MemberWhat it is
private void OnDestroy()Unhooks the container visibility events.
private void IBuildRendererPostBake.PostBake()Build step: drops missing renderers from the other LODs list.

Used in​

Members of other types that take, return or hold a LiquidSurfaceVisual: