This quick start guide will explain how to set up a primitive sphere to interact with water.
An empty scene will be used for the example.
Project Settings > Player > Api Combatibility Level needs to be set to .NET 4.x.
Since all the NWH assets have been updated to use assembly definitions here is a disclaimer to avoid confusion when updating:
This asset uses Assembly Definition (.asmdef) files. There are many benefits to assembly definitions but a downside is that the whole project needs to use them or they should not be used at all.
Using, for example, Lux Water (which does not fature assembly definitions) will therefore require an addition of .asmdef file inside the Lux Water directory and a reference inside NWH.DWP2.asmdef or removal of all .asmdef files from the asset if you do not wish to use assembly definitions. Some assets such as Crest already feature .asmdefs and adding Crest as a reference to NWH.DWP2 is the only step needed.
WaterObjectManager is the main script of Dynamic Water Physics 2 and has to be present in the scene for water/object interaction to work. It does not matter to which object it is attached, it just needs to be present.
WaterObjectManager fetches the data from all the
WaterObjects in the scene, processes it and sends it to a job which then does all the physics calculations and makes use of multiple CPU cores.
WaterObjectManagerto the scene. No need to change any settings. Example:
WaterObject is a script that tells
WaterObjectManager which objects should interact with water. Any object that is active and has this script will interact with water. There are two requirements for
WaterObject to work: a
Rigidbody and a
MeshFilter from which the script will fetch the mesh (DWP2 uses mesh data to simulate object/water interaction).
Rigidbody does not have to be attached to the same object as
WaterObject, but it must be present in one of its parents. This allows for composite objects; one Rigidbody with multiple hulls - such as a trimaran.
Sphere Colliderto the Sphere if is not automatically added.
Rigidbodyto the Sphere and set its mass to 300. There is also a script called
MassFromMaterialwhich can calculate and set the
Rigidbodymass based on material density and mesh volume, but it is a helper script and not required.
WaterObjectto the Sphere. Since the sphere by default has 768 triangles
Simplify Meshoption should be used. This option automatically decimates the mesh to a
Target Triangle Count. A good triangle count is 30 or less for simple objects and around 60 for ship hulls. Using higher triangle count will only have a minor influence on simulation quality but will have a linear performance penalty (doubling the triangle count will about halve the performance). Therefore, adjusting the triangle count until the object starts to lose its shape is recommended. In the case of the example sphere 36 will be enough:
WaterDataProvider is a script that tells
WaterObjectManager where the water is. It is an interface between water systems/assets and Dynamic Water Physics and allows the two to communicate. All flat water assets/shaders use the same
FlatWaterDataProvider while for wavy assets such as Crest an asset-specific
WaterDataProvider has to be used, e.g.
As an example a flat water plane will be used:
FlatWaterDataProviderto it and press Play. The object will now float but it will appear as if it floating in empty space (which it is).
FlatWaterDataProviderassumes that water is at
transform.position.yof the object it is attached to and does not care about any shaders, colliders, etc. Changing the Y value of Transform's position will make the Sphere float at different heights.
FlatWaterDataProviderand make sure to remove
MeshColliderfrom it or the sphere will just sit on the plane through physics collision.
Default-Materialof the plane can be replaced with a material that uses some kind of flat water shader - e.g. the included
WaterProDaytimematerial which is just a standard Unity water.
WaterParticleSystem can be used to generate foam. It works with any flat water.
ParticleSystemvalues can be tweaked to suit the needs of the project.
CenterOfMass is a simple script that offsets the center of mass of a Rigidbody.
Unity calculates center of mass of the object from all of its colliders, as if the object were homogenous. In many cases this is not correct - a ship has ballast, a crate could have some load in it, a barrel could have oil, etc.
To adjust the center of mass of an object simply attach
CenterOfMass script to the same object that contains the
Rigidbody and adjust the
Center Of Mass Offset - a value in local coordinates which tells how much to offset center of mass from the Unity-calculated point. Want a ship to be less prone to capsizing? Lower the Y component of COM.
WaterObjectWizard is a helper script that sets up a
WaterObject automatically. It is still recommended to have knowledge of manual setup and how things work, but this script can automate and speed up the setup process.
A primitive Sphere will be used, same as in the rest of the guide above.
WaterObjectWizardto the newly create Sphere.
Auto-Setupand press Play after the setup is done. The Sphere now floats and generates foam. Next step would be to manually check and tweak the default values, such as
Target Triangle Count, center of mass, etc.
Burst is a compiler, it translates from IL/.NET bytecode to highly optimized native code using LLVM. This improves job performance.
It can be installed from Unity PackageManager.
By default Burst compilation is disabled. This is due to the fact that cross compilation is not supported in some cases. E.g. building for Mac OS on Windows will fail if Burst is enabled.
To enable Burst compilation:
DWP_BURSTunder Project Settings ⇒ Player ⇒ Scripting Define Symbols. Make sure that the Burst package installed beforehand.
Unity.Burstassembly reference to Assets > NWH > Dynamics Water Physics 2 > Scripts > WaterObject > NWH.DWP2.WaterObject.asmdef.
WaterObject is a script that tells
WaterObjectManager that the object should interact with water.
It also handles Simulation Mesh generation. Simulation Mesh is the mesh that will be used for simulating water/object interaction and can either be the original mesh or a simplified version of it.
WaterObject provides basic tools for mesh triangle decimation.
Simplify Mesh- Should the simulation mesh be simplified? If the mesh has more than 30 triangles it is usually a good idea to enable this option.
Target Triangle Count- How many triangles the simplified simulation mesh should have?
Convexify Mesh- Should the simulation mesh be made convex? While DWP supports both convex and concave shapes, enabling this option is recommended when there are holes in the mesh (e.g. a crate without the top or a ship hull without deck). In some cases it can also be used to lower the triangle count additionally.
Update Simulation Mesh- Generates the simulation mesh from the given options. If Simplify and Convexify are left unticked the original mesh will be used.
Toggle In-Scene Preview- Besides using the preview icons inside the inspector it is also possible to preview the simulation mesh inside the scene. When clicked this button will toggle between original and simulation mesh on the current object.
Check WaterObjectManager page for more info about instantiating
WaterObjects at run-time.
WaterObjectManager is the main script of Dynamic Water Physics 2 and has to be present in the scene for water/object interaction to work. It does not matter to which object it is attached, it just needs to be present. WaterObjectManager fetches the data from all the WaterObjects in the scene, processes it and sends it to a job which then does all the physics calculations and makes use of multiple CPU cores.
Finish Jobs In Single Frame- if set to true jobs will be finished on the same frame they started. Disabling it will improve performance, but will also add a one-frame delay to water/object interaction. This is not significant in most cases. Default is off.
Fluid Density- density of the simulated fluid in kg/m3. Default is 1030 (salt water).
Velocity Dot Power- when set to one fluid will act as a Newtonian fluid (relationship between velocity and impact force is proportional). Values below 1 will result in pseudo-plastic liquid (relationship is logaritmic) and values above 1 will result in dilatant liquid (relationship is exponential). This is a simplified explanation. Value of 1 should be used for normal water, but slightly higher value can also be used.
Simulate Water Normals- if true water normals will be used when calculating forces. Has no effect if the water system does not use normals.
Simulate Water Flow- if true water flow will be used when calculating forces. Has no effect if the water system used does not support flow.
Calculate Buoyancy Forces- buoyancy will be calculated when this field is true.
Calculate Dynamic Forces- hydrodynamics will be approximated when this field is true. Disable to use only buoyancy.
Dynamic Force Coefficient- a coefficient by which the resultant force will be multiplied.
Dynamic Force Power- an exponent to the power of which the resultant force will raised.
Calculate Skin Drag- when true a drag will be calculated as a result of liquid passing over a surface.
Skin Friction Drag- can be set to 0 or near 0 for water while goo-like fluids will have a larger value.
Generate Gizmos- gizmos will be generated from simulation data when true.
When Water Object is added to the scene during play mode it will not be automatically
registered because this requires re-allocating memory for the jobs which is quite expensive and should ideally be done during the loading screens.
There are two ways to add new objects to the scene during runtime:
WaterObjectManager.Instance.Synchronize()is the best solution.
WaterObjectManagerwill allocate memory for inactive objects too, but will not simulate them. When the object is needed it can simply be activated, without calling
Water Particle System is a component that generates particles based on simulation data from Water Object Manager. It can be added to any Water Object.
Water Particle System has been rewritten from the ground up for DWP2 and now it has virtually no performance or memory overhead except for the cost of the Unity’s Particle System it uses to render particles.
Water Particle Systems emits only along X-Z axis and does not work with wavy water assets. For that asset-specific foam has to be used (if available).
Emit- Particles will only be generated when this field is ticked.
Render Queue- Render queue of the particle material. If particles are rendered behind the water increase the value to be just above the value of the water’s render queue.
Surface Elevation- Height above water surface at which the particles will be emitted.
Start Size- Starting diameter of the particle.
Sleep Threshold Velocity- If rigidbody’s velocity is below this value particles will not be emitted. Do not set to 0 as that will result in (invisible) particles constantly being generated, even when object is still.
Initial Velocity Modifier- Velocity at the point of contact with water is multiplied by this value to get the initial particle velocity. If set too high it will seem as if the particles are flying away from the object.
Max Initial Alpha- Maximum initial alpha (transparency) of the foam. If set to 1 foam will be opaque, 0 and it will be invisible.
Initial Alpha Modifier- Higher contact force with water will result in higher initial alpha (up to Max Initial Alpha). This field sets the sensitivity of alpha related to the force.
Emit Per Cycle- How many particles should be emitted in each cycle? If there are not enough contact points with water less particles may be emitted.
Emit Time Interval- Interval between emission cycles in seconds.
Position Extrapolation Frames- To counteract the initial fade-in and apparent lag of the particles, the emission position is predicted a number of frames in advance. If this number is set too high particles will appear as if emitting in front of the object.
Due to inclusion of assembly definitions into this asset with v2.4 an additional step is required when setting up 3rd party water assets. The asset either needs to be referenced inside
NWH.DWP2.asmdef or all the .asmdef (assembly definition) files need to be removed from DWP2. If the 3rd party asset does not include an assembly definition file one should be added manually to the root of that asset, or the .asmdef files need to be removed from DWP2.
FlatWaterDataProvider is used for all flat water systems.
It can even be used with wavy water systems if the waves have 0 amplitude to improve performance (sometimes drastically as the water heights are always queried with wavy water system, even if there are no waves).
FlatWaterDataProviderto the GameObject representing the water.
CetoWaterDataProviderto the object containing
Crest supports water heights, water normals and flow. It is one of the best wavy water systems to use with DWP2 and free to boot.
Please always use the latest Crest commit with DWP2 as the Crest API has changed a few times in the past. Crest releases on GitHub are usually outdated so do not use these.
CrestWaterDataProviderto the object containing
Crestto NWH.DWP2.asmdef (located in NWH\Dynamic Water Physics 2\Scripts\WaterDataProvider)
Max query count (4096) exceeded, …create a new AnimatedWaveSettings file, set the
Max Query Countto a larger value than default and assign it to
LuxWaterDataProviderscript to the object containing
Ocean Next Gen is supported but has not been updated regularly for over two years. Crest and Ceto will be better options.
OceanNextGenWaterDataProviderto the object containing
RAMWaterDataProvider supports water heights, normals and flow and it inherits from
RaycastWaterDataProvider, meaning that the setup steps are the same.
RAMWaterDataProviderto the scene. It does not have to be attached to any specific object.
Water Layerto the River Auto Material water. Assign
Object Layerto all the
WaterObjects in the scene. This is an important step as the script will disable physical collisions between the two layers to prevent the
WaterObjects from sitting on top of the mesh collider that R.A.M. uses instead of interacting with water.
SuimonoWaterDataProviderto the object containing
WaterDataProviders, the one for Stylized Water 2 is included with the Stylized Water 2 asset instead of DWP2.
StylizedWaterDataProviderto the object containing OceanRenderer script.
KWS support is under development.
A helper script for setting center of mass of a
Unity calculates center of mass as a center of volume of all the
Colliders. This can and will result in unrealistic center of mass for objects that do not have uniform density, i.e. center of mass will not have to be adjusted for a wooden log but a ship has a ballast and is noticeably heavier around the keel and therefore the center of mass is low which in turn prevents the ship from capsizing.
Center Of Mass Offset- offset of the center of mass from the Unity calculated one in local coordinates.
Show COM- when true a green gizmo sphere will be drawn at the current center of mass.
MassFromVolume is a helper script that calculates object's mass from volume of the mesh and the density.
Massfield can also be set manually.
MassFromChildrento calculate mass of complex objects (objects having more than one child
A helper script for determining mass of a Rigidbody from the children. It sums the masses of all the children that have
WaterObjectMassHelper scripts attached. This eliminates the need for guessing the mass of the object.
WaterObjectMassHelperattached or the result will be 0 and ignored.