using UnityEngine; namespace NWH.DWP2.WaterObjects { /// /// Script to calculate mass from mesh volume and material density. /// Removes need for guessing mass and ensures that all objects of same density are submerged equally. /// [RequireComponent(typeof(WaterObject))] public class MassFromVolume : MonoBehaviour { public WaterObjectMaterial material; public float density; public float mass; public float volume; private WaterObject _waterObject; private void Awake() { _waterObject = GetComponent(); if (_waterObject == null) { Debug.LogError("MassFromVolume requires WaterObject."); } if (material == null) { SetDefaultAsMaterial(); } } public void SetDefaultAsMaterial() { material = Resources.Load("Dynamic Water Physics 2/DefaultWaterObjectMaterial"); } private void Reset() { SetDefaultAsMaterial(); } /// /// Gets volume of the simulation mesh. Scale-sensitive. /// public void CalculateSimulationMeshVolume() { if (_waterObject == null) { _waterObject = GetComponent(); } if (_waterObject.SimulationMesh == null) { Debug.LogWarning( "No simulation mesh assigned/generated. Make sure that simulation mesh of WaterObject is not empty - " + "if this is the first time setup try clicking 'Update Simulation Mesh' on WaterObject."); } volume = _waterObject.SimulationMesh == null ? 0.00000001f : Mathf.Clamp(MeshUtility.VolumeOfMesh(_waterObject.SimulationMesh, _waterObject.transform), 0f, Mathf.Infinity); } /// /// Sets density of the material and adjusts mass to be correct for the volume of the mesh. /// public float CalculateAndApplyFromMaterial() { return CalculateAndApplyFromDensity(material.density); } public float CalculateAndApplyFromDensity(float density) { mass = -1; if (material != null) { if (_waterObject == null) { _waterObject = GetComponent(); } CalculateSimulationMeshVolume(); mass = density * volume; if (_waterObject.targetRigidbody != null && mass > 0) { _waterObject.targetRigidbody.mass = mass; } } return mass; } } }