Class WaterObject
Core component for mesh-based buoyancy simulation. Calculates buoyancy and hydrodynamic forces by slicing mesh triangles at the water surface. Requires a MeshFilter with a valid mesh and a Rigidbody to apply forces to. Use WaterObjectWizard for one-click setup or manually configure the simulation mesh.
Namespace: NWH.DWP2.WaterObjects
Assembly: NWH.DWP2.dll
Syntax
[RequireComponent(typeof(MeshFilter))]
[DisallowMultipleComponent]
public class WaterObject : MonoBehaviour
Fields
| Edit this page View SourceCurrentWaterDataProvider
Currently active WaterDataProvider.
Declaration
[NonSerialized]
[Tooltip("Currently active WaterDataProvider.")]
public WaterDataProvider CurrentWaterDataProvider
Field Value
| Type | Description |
|---|---|
| WaterDataProvider |
LocalVertices
Vertices used for simulation, in local space.
Declaration
[NonSerialized]
[Tooltip("Vertices used for simulation, in local space.")]
public Vector3[] LocalVertices
Field Value
| Type | Description |
|---|---|
| Vector3[] |
ResultAreas
Result triangle areas.
Declaration
[NonSerialized]
[Tooltip("Result triangle areas.")]
public float[] ResultAreas
Field Value
| Type | Description |
|---|---|
| float[] |
ResultCenters
Result triangle centers in world coordinates.
Declaration
[NonSerialized]
[Tooltip("Result triangle centers in world coordinates.")]
public Vector3[] ResultCenters
Field Value
| Type | Description |
|---|---|
| Vector3[] |
ResultDistances
Result distances to water surface.
Declaration
[NonSerialized]
[Tooltip("Result distances to water surface.")]
public float[] ResultDistances
Field Value
| Type | Description |
|---|---|
| float[] |
ResultForce
Total result force.
Declaration
[NonSerialized]
[Tooltip("Total result force.")]
public Vector3 ResultForce
Field Value
| Type | Description |
|---|---|
| Vector3 |
ResultForces
Per-triangle result forces.
Declaration
[NonSerialized]
[Tooltip("Per-triangle result forces.")]
public Vector3[] ResultForces
Field Value
| Type | Description |
|---|---|
| Vector3[] |
ResultNormals
Result triangle normals. Not equal to the mesh normals.
Declaration
[NonSerialized]
[Tooltip("Result triangle normals. Not equal to the mesh normals.")]
public Vector3[] ResultNormals
Field Value
| Type | Description |
|---|---|
| Vector3[] |
ResultP0s
Result sliced triangle vertices.
Declaration
[NonSerialized]
[Tooltip("Result sliced triangle vertices.")]
public Vector3[] ResultP0s
Field Value
| Type | Description |
|---|---|
| Vector3[] |
ResultStates
Result triangle states. 0 - Triangle is underwater 1 - Triangle is partially underwater 2 - Triangle is above water 3 - Triangle's object is disabled 4 - Triangle's object is deleted
Declaration
[NonSerialized]
[Tooltip("Result triangle states.\r\n0 - Triangle is underwater\r\n1 - Triangle is partially underwater\r\n2 - Triangle is above water\r\n3 - Triangle's object is disabled\r\n4 - Triangle's object is deleted")]
public int[] ResultStates
Field Value
| Type | Description |
|---|---|
| int[] |
ResultTorque
Result total torque acting on the Rigidbody.
Declaration
[NonSerialized]
[Tooltip("Result total torque acting on the Rigidbody.")]
public Vector3 ResultTorque
Field Value
| Type | Description |
|---|---|
| Vector3 |
ResultVelocities
Result velocities at triangle centers.
Declaration
[NonSerialized]
[Tooltip("Result velocities at triangle centers.")]
public Vector3[] ResultVelocities
Field Value
| Type | Description |
|---|---|
| Vector3[] |
RigidbodyAngVel
Angular velocity of the target Rigidbody.
Declaration
[NonSerialized]
[Tooltip("Angular velocity of the target Rigidbody.")]
public Vector3 RigidbodyAngVel
Field Value
| Type | Description |
|---|---|
| Vector3 |
RigidbodyCoM
Center-of-mass of the target Rigidbody.
Declaration
[NonSerialized]
[Tooltip("Center of mass of the target Rigidbody.")]
public Vector3 RigidbodyCoM
Field Value
| Type | Description |
|---|---|
| Vector3 |
RigidbodyLinearVel
(Linear) velocity of the target Rigidbody.
Declaration
[NonSerialized]
[Tooltip("(Linear) velocity of the target Rigidbody.")]
public Vector3 RigidbodyLinearVel
Field Value
| Type | Description |
|---|---|
| Vector3 |
TriIndices
Triangles indices of the simulation mesh.
Declaration
[NonSerialized]
[Tooltip("Triangles indices of the simulation mesh.")]
public int[] TriIndices
Field Value
| Type | Description |
|---|---|
| int[] |
WaterFlows
Water flows of the current water data provider. Default is used if no water data provider is present.
Declaration
[NonSerialized]
[Tooltip("Water flows of the current water data provider. Default is used if no water data provider is present.")]
public Vector3[] WaterFlows
Field Value
| Type | Description |
|---|---|
| Vector3[] |
WaterHeights
Water surface heights of the current water data provider. Default is used if no water data provider is present.
Declaration
[NonSerialized]
[Tooltip("Water surface heights of the current water data provider. Default is used if no water data provider is present.")]
public float[] WaterHeights
Field Value
| Type | Description |
|---|---|
| float[] |
WaterNormals
Water normals of the current water data provider. Default is used if no water data provider is present.
Declaration
[NonSerialized]
[Tooltip("Water normals of the current water data provider. Default is used if no water data provider is present.")]
public Vector3[] WaterNormals
Field Value
| Type | Description |
|---|---|
| Vector3[] |
WorldVertices
Simulation vertices converted to world coordinates.
Declaration
[NonSerialized]
[Tooltip("Simulation vertices converted to world coordinates.")]
public Vector3[] WorldVertices
Field Value
| Type | Description |
|---|---|
| Vector3[] |
buoyantForceCoefficient
Coefficient by which the buoyancy forces are multiplied.
Declaration
[Tooltip("Coefficient by which the the buoyancy forces are multiplied.")]
public float buoyantForceCoefficient
Field Value
| Type | Description |
|---|---|
| float |
calculateWaterFlows
Should the water flows be queried and calculated?
Declaration
[Tooltip("Should the water flows be queried and calculated?")]
public bool calculateWaterFlows
Field Value
| Type | Description |
|---|---|
| bool |
calculateWaterHeights
Should the water heights be queried and calculated?
Declaration
[Tooltip("Should the water heights be queried and calculated?")]
public bool calculateWaterHeights
Field Value
| Type | Description |
|---|---|
| bool |
calculateWaterNormals
Should the water normals be queried and calculated?
Declaration
[Tooltip("Should the water normals be queried and calculated?")]
public bool calculateWaterNormals
Field Value
| Type | Description |
|---|---|
| bool |
convexifyMesh
Should the simulation mesh be made convex? Can be useful for half-open hulls and otherwise partially hollow meshes.
Declaration
[Tooltip("Should the simulation mesh be made convex?\r\nCan be useful for half-open hulls and otherwise partially hollow meshes.")]
public bool convexifyMesh
Field Value
| Type | Description |
|---|---|
| bool |
defaultWaterFlow
World water flow to be used when there is no WaterDataProvider present and calculateWaterFlows is enabled.
Declaration
[Tooltip("World water flow to be used when there is no WaterDataProvider present and calculateWaterFlows is enabled.")]
public Vector3 defaultWaterFlow
Field Value
| Type | Description |
|---|---|
| Vector3 |
defaultWaterHeight
World water height to be used when there is no WaterDataProvider present.
Declaration
[Tooltip("World water height to be used when there is no WaterDataProvider present.")]
public float defaultWaterHeight
Field Value
| Type | Description |
|---|---|
| float |
defaultWaterNormal
World water normal to be used when there is no WaterDataProvider present and calculateWaterNormals is enabled.
Declaration
[Tooltip("World water normal to be used when there is no WaterDataProvider present and calculateWaterNormals is enabled.")]
public Vector3 defaultWaterNormal
Field Value
| Type | Description |
|---|---|
| Vector3 |
finalForceCoefficient
Coefficient by which the result force will get multiplied.
Declaration
[Tooltip("Coefficient by which the result force will get multiplied.")]
public float finalForceCoefficient
Field Value
| Type | Description |
|---|---|
| float |
finalTorqueCoefficient
The coefficient by which the torque force will get multiplied.
Declaration
[Tooltip("Coefficient by which the torque force will get multiplied.")]
public float finalTorqueCoefficient
Field Value
| Type | Description |
|---|---|
| float |
fluidDensity
Density of the fluid the object is in.
Declaration
[Tooltip("Density of the fluid the object is in.")]
public float fluidDensity
Field Value
| Type | Description |
|---|---|
| float |
hydrodynamicForceCoefficient
Coefficient by which the hydrodynamic forces are multiplied.
Declaration
[Tooltip("Coefficient by which the hydrodynamic forces are multiplied..")]
public float hydrodynamicForceCoefficient
Field Value
| Type | Description |
|---|---|
| float |
instanceID
Cached instance ID of the GameObject.
Declaration
[NonSerialized]
[Tooltip("Cached instance ID of the GameObject.")]
public int instanceID
Field Value
| Type | Description |
|---|---|
| int |
originalMesh
Original mesh of the object, non-simplified and non-convexified.
Declaration
[SerializeField]
[Tooltip("Original mesh of the object, non-simplified and non-convexified.")]
public Mesh originalMesh
Field Value
| Type | Description |
|---|---|
| Mesh |
serializedSimulationMesh
Serialized version of the simulation mesh, used to prevent having to store the mesh in game files.
Declaration
[SerializeField]
[Tooltip("Serialized version of the simulation mesh, used to prevent having to store the mesh in game files.")]
public SerializedMesh serializedSimulationMesh
Field Value
| Type | Description |
|---|---|
| SerializedMesh |
simplifyMesh
Should the simulation mesh be simplified / decimated?
Declaration
[Tooltip("Should the simulation mesh be simplified / decimated?")]
public bool simplifyMesh
Field Value
| Type | Description |
|---|---|
| bool |
skinDragCoefficient
The coefficient by which the skin drag force is multiplied. Skin drag is a force caused by water flowing over a surface.
Declaration
[Tooltip("Coefficient by which the skin drag force is multiplied.\r\nSkin drag is a force caused by water flowing over a surface.")]
public float skinDragCoefficient
Field Value
| Type | Description |
|---|---|
| float |
slamForceCoefficient
Coefficient applied to forces when a face of the object is entering the water.
Declaration
[Tooltip("Coefficient applied to forces when a face of the object is entering the water.")]
public float slamForceCoefficient
Field Value
| Type | Description |
|---|---|
| float |
submergedVolume
Current submerged volume in cubic meters. Updated each physics step based on how much of the mesh is underwater.
Declaration
[Tooltip("Resulting volume of buoyancy calculation.")]
public float submergedVolume
Field Value
| Type | Description |
|---|---|
| float |
suctionForceCoefficient
Coefficient applied to forces when a face of the object is leaving the water.
Declaration
[Tooltip("Coefficient applied to forces when a face of the object is leaving the water.")]
public float suctionForceCoefficient
Field Value
| Type | Description |
|---|---|
| float |
targetRigidbody
Rigidbody that the forces will be applied to.
Declaration
[Tooltip("Rigidbody that the forces will be applied to.")]
public Rigidbody targetRigidbody
Field Value
| Type | Description |
|---|---|
| Rigidbody |
targetTriangleCount
Target triangle count for the simulation mesh. The original mesh will be decimated to this number of triangles is "SimplifyMesh" is enabled.
Declaration
[FormerlySerializedAs("targetTris")]
[Range(8, 256)]
[Tooltip("Target triangle count for the simulation mesh.\r\nOriginal mesh will be decimated to this number of triangles is 'SimplifyMesh' is enabled.\r\nOtherwise does nothing.")]
public int targetTriangleCount
Field Value
| Type | Description |
|---|---|
| int |
triangleCount
Total number of triangles (not indices) on the simulation mesh.
Declaration
[Tooltip("Total number of triangles (not indices) on the simulation mesh.")]
public int triangleCount
Field Value
| Type | Description |
|---|---|
| int |
velocityDotPower
Determines to which power the dot product between velocity and triangle normal will be raised. Higher values will result in lower hydrodynamic forces for situations in which the triangle is nearly parallel to the water velocity.
Declaration
[Range(0.01, 2)]
[Tooltip("Determines to which power the dot product between velocity and triangle normal will be raised.\r\nHigher values will result in lower hydrodynamic forces for situations in which the triangle is nearly parallel to the\r\nwater velocity.")]
public float velocityDotPower
Field Value
| Type | Description |
|---|---|
| float |
vertexCount
Total number of vertices on the simulation mesh.
Declaration
[Tooltip("Total number of vertices on the simulation mesh.")]
public int vertexCount
Field Value
| Type | Description |
|---|---|
| int |
weldColocatedVertices
If true, the vertices with the same position will be welded. Improves performance and is highly recommended.
Declaration
[Tooltip(" If true vertices with same position will be welded.\r\n Improves performance and is highly recommended.")]
public bool weldColocatedVertices
Field Value
| Type | Description |
|---|---|
| bool |
Properties
| Edit this page View SourcePreviewEnabled
Returns true if the simulation mesh is currently being previewed in the scene view.
Declaration
public bool PreviewEnabled { get; }
Property Value
| Type | Description |
|---|---|
| bool |
SimulationMesh
Gets the simulation mesh, deserializing from serialized data if necessary.
Declaration
public Mesh SimulationMesh { get; }
Property Value
| Type | Description |
|---|---|
| Mesh |
Methods
| Edit this page View SourceGenerateSimMesh()
Generates or regenerates the simulation mesh based on current settings. Applies simplification, convexification, and vertex welding as configured. Must be called after changing mesh processing settings.
Declaration
public void GenerateSimMesh()
GetWaterHeightSingle(Vector3)
Returns water height at a single point using the current WaterDataProvider. Falls back to defaultWaterHeight if no provider is active.
Declaration
public float GetWaterHeightSingle(Vector3 point)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | point | Position in world coordinates. |
Returns
| Type | Description |
|---|---|
| float | Water height at the given point. |
IsTouchingWater()
Checks if any part of the object is currently in contact with water. Returns true if at least one triangle is fully or partially submerged. Cache the result if calling frequently.
Declaration
public bool IsTouchingWater()
Returns
| Type | Description |
|---|---|
| bool | True if object is touching water. |
OnEnterWaterDataProvider(WaterDataProvider)
Called when the WaterObject enters a WaterDataProvider's trigger volume. Registers the provider for water data queries.
Declaration
public void OnEnterWaterDataProvider(WaterDataProvider wdp)
Parameters
| Type | Name | Description |
|---|---|---|
| WaterDataProvider | wdp | WaterDataProvider that was entered. |
OnExitWaterDataProvider(WaterDataProvider)
Called when the WaterObject exits a WaterDataProvider's trigger volume. Unregisters the provider and resets to default water data if no providers remain.
Declaration
public void OnExitWaterDataProvider(WaterDataProvider wdp)
Parameters
| Type | Name | Description |
|---|---|---|
| WaterDataProvider | wdp | WaterDataProvider that was exited. |
StartSimMeshPreview()
Enables visualization of the simulation mesh in the scene view. Temporarily replaces the MeshFilter's mesh with the simulation mesh.
Declaration
public void StartSimMeshPreview()
StopSimMeshPreview()
Disables simulation mesh visualization in the scene view. Restores the original mesh to the MeshFilter.
Declaration
public void StopSimMeshPreview()