void calculateResults() { //Make sure a gameobject is selected! if (SelectedObject != null) { bool Failed = false; //First calculate the density of the object. if (SelectedObject.GetComponent <MeshFilter>() && SelectedObject.GetComponent <MeshFilter>().sharedMesh != null) { // Use meshfilter based bounds CalculatedVolume = MeshVolume.getVolume(SelectedObject as GameObject); } else if (SelectedObject.GetComponent <Collider>()) { // Use collider based bounds CalculatedVolume = MeshVolume.getVolumeByColliderBounds(SelectedObject.GetComponent <Collider>()); } else { Debug.Log("[Realistic Water Physics] Object is missing a mesh filter or collider."); Failed = true; } if (!Failed) // a small fix! { //Now the percentage thats solit and the remaining is air. float percentageSolidMass = CalculatedVolume * ((selectedMaterialValue / 100) * percentageSolid); float percentageAir = CalculatedVolume * ((RealisticWaterPhysics.currentAirDensity / 100) * (100 - percentageSolid)); //The totaal mass of the object is the amount of solit material + the remaining amount of air. CalculatedMass = percentageSolidMass + percentageAir; //the density of the intire object is its calculated mass / its volume. CalculatedDensity = CalculatedMass / CalculatedVolume; CalculatedWaterDensity = PhysicsMaterialsList.getLiquidsMaterialValue(selectedOceanLiquidsMaterial); CalculatedAirDensity = airDensity; if (CalculatedDensity > CalculatedWaterDensity) // if denser then water density it will sink. { CalculatedWillFloatInWater = false; } else { CalculatedWillFloatInWater = true; } if (CalculatedDensity > CalculatedAirDensity) // if denser then air density it will sink { CalculatedWillFloatInAir = false; } else { CalculatedWillFloatInAir = true; } DidTest = true; } } }
public void GetMesh() { Stopwatch timer = new Stopwatch(); if (meshRenderer == null || meshFilter == null) { meshRenderer = GetComponent <MeshRenderer>(); meshFilter = GetComponent <MeshFilter>(); } timer.Start(); MeshVolume volume = new MeshVolume(inputMesh, resolution); Surface surface = new Surface(volume, simplifyMesh); surface.GetMeshData(); meshFilter.sharedMesh = surface.MeshData.GetMesh(); meshFilter.sharedMesh.RecalculateNormals(); timer.Stop(); string vtxSpeed = timer.ElapsedMilliseconds > 0f ? "" + (meshFilter.sharedMesh.vertexCount / timer.ElapsedMilliseconds) : "inf", triSpeed = timer.ElapsedMilliseconds > 0f ? "" + (meshFilter.sharedMesh.triangles.Length / 3 / timer.ElapsedMilliseconds) : "inf"; Display = ( "Dimensions: (" + volume.Dimensions.x + ", " + volume.Dimensions.y + ", " + volume.Dimensions.z + ")\n" + "Scale: (" + volume.Scale.x + ", " + volume.Scale.y + ", " + volume.Scale.z + ")\n" + "Verticies: " + meshFilter.sharedMesh.vertexCount + " (" + vtxSpeed + " verts/ms)\n" + "Triangles: " + (meshFilter.sharedMesh.triangles.Length / 3) + " (" + triSpeed + " tris/ms)\n\n" + "Import Time: " + volume.LastImportTime + "ms\n" + "Voxel Time: " + surface.LastVoxelTime + "ms\n" + "Total Time: " + timer.ElapsedMilliseconds + "ms\n" ); }
// Jacob changed this from private to public public void setup() { //Eanble support #if PW findPlayWaterWater(); #endif if (RealisticWaterPhysics.currentDebugEnabled) { forces = new List <Vector3[]>(); // For drawing force gizmos } selectedMaterialValue = getSelectedMaterialValue(); _thisRigidbody = this.GetComponent <Rigidbody>(); waterDensity = RealisticWaterPhysics.currentWaterDensity; isMeshCollider = GetComponent <MeshCollider>() != null; if (this.gameObject.GetComponent <MeshFilter>() && this.gameObject.GetComponent <MeshFilter>().sharedMesh != null) { volume = MeshVolume.getVolume(this.gameObject); Bounds meshBounds = GetComponent <MeshFilter>().sharedMesh.bounds; bounds = new Bounds(); //A fix for a odd bug. bounds.center = meshBounds.center; bounds.extents = new Vector3( meshBounds.extents.x * this.transform.localScale.x, meshBounds.extents.y * this.transform.localScale.y, meshBounds.extents.z * this.transform.localScale.z); //meshBounds.extents * this.transform.localScale; if (RealisticWaterPhysics.currentDebugEnabled) { Debug.LogFormat("bounds.extents={0} |bounds.center={1} |this.transform.position={2} |_thisRigidbody.centerOfMass={3}", bounds.extents, bounds.center, this.transform.position, _thisRigidbody.centerOfMass); } //if object scale = 10,10,10 and sharedMesh.bounds; returns 0.5 then result must be 5,5,5; FIX THIS !!! secondSetup(); } else if (this.gameObject.GetComponent <Collider>()) { // Use collider based bounds if (RealisticWaterPhysics.currentDebugEnabled) { Debug.LogFormat("[Realistic Water Physics] Object: {0} is missing a mesh filter, using collider Bounds!", this.gameObject.name); } volume = MeshVolume.getVolumeByColliderBounds(this.gameObject.GetComponent <Collider>()); bounds = GetComponent <Collider>().bounds; secondSetup(); } else { Debug.LogFormat("[Realistic Water Physics] Object: {0} is missing a mesh filter and collider.", this.gameObject.name); this.gameObject.SetActive(false); } }
public Mesh UpdateMesh(List <Vector2> points) { this.points = points; this.mesh = ExtrudeShape.Generate(this.points, this.depth); this.volume = MeshVolume.Calculate(mesh); this.mass = (defaultMass + this.volume * 100.0f); return(this.mesh); }
public void Initialize() { List <Mesh> meshList = new List <Mesh>(); List <MeshRenderer> meshRendererList = new List <MeshRenderer>(); List <Transform> transformList = new List <Transform>(); if (GetComponent <Collider>() != null) { meshList.Add(GetComponent <MeshFilter>().sharedMesh); meshRendererList.Add(GetComponent <MeshRenderer>()); transformList.Add(transform); } for (int i = 0; i < transform.childCount; i++) { if (transform.GetChild(i).GetComponent <Collider>() != null) { Transform child = transform.GetChild(i); meshList.Add(child.GetComponent <MeshFilter>().sharedMesh); meshRendererList.Add(child.GetComponent <MeshRenderer>()); transformList.Add(child); } } Mesh[] meshes = meshList.ToArray(); MeshRenderer[] meshRenderers = meshRendererList.ToArray(); Transform[] transforms = transformList.ToArray(); float[] boundsVolumes = new float[meshes.Length]; for (int i = 0; i < boundsVolumes.Length; i++) { boundsVolumes[i] = meshRenderers[i].bounds.size.x * meshRenderers[i].bounds.size.y * meshRenderers[i].bounds.size.z; } float totalBoundsVolume = boundsVolumes.Sum(); float[] meshVolumes = new float[meshes.Length]; for (int i = 0; i < meshVolumes.Length; i++) { meshVolumes[i] = MeshVolume.VolumeOfMesh(meshes[i], transforms[i]); } float totalMeshVolume = meshVolumes.Sum(); Rigidbody rb = GetComponent <Rigidbody>(); rb.isKinematic = true; rb.useGravity = false; rb.mass = density * totalMeshVolume; rb.drag = 0.0f; rb.angularDrag = 0.0f; meshSampler = new MeshSampler(meshRenderers, transforms, DistributeSamples(boundsVolumes, totalBoundsVolume) /*, stratifiedDivisions*/); gravity = new Gravity(rb, meshSampler); buoyancy = new Buoyancy(rb, meshSampler, totalMeshVolume); waterDrag = new WaterDrag(rb, meshSampler, viscosity); rb.isKinematic = false; }
/// Calculates physics. /// private void CalculatePhysics() { if (_thisRigidbody.isKinematic == false) { if (HeavyRunTimeEdits || LightRunTimeEdits && CheckOlds()) { if (HeavyRunTimeEdits) { if (this.gameObject.GetComponent <MeshFilter>() && this.gameObject.GetComponent <MeshFilter>().mesh != null) { volume = MeshVolume.getVolume(this.gameObject); } else if (this.gameObject.GetComponent <Collider>()) { // Use collider based bounds volume = MeshVolume.getVolumeByColliderBounds(this.gameObject.GetComponent <Collider>()); } else { return; } } percentageSolidMass = volume * ((selectedMaterialValue / 100) * percentageSolid); percentageAir = volume * ((RealisticWaterPhysics.currentAirDensity / 100) * (100 - percentageSolid)); //The totaal mass of the object is the amount of solit material + the remaining amount of air. mass = percentageSolidMass + percentageAir; //the density of the intire object is its calculated mass / its volume. density = mass / volume; _thisRigidbody.mass = mass; getRunTimeEditsInfo(); } if (RealisticWaterPhysics.currentDebugEnabled) // Just skip this if debug info is disabled. { forces.Clear(); // For drawing force gizmos } if (BuoyancyEnabled) { if (voxels == null) { if (reSetupTimeout < 5) // its a fail safe, if indeed no voxels are found and resetup is done 5 times, it wil trip! { reSetup(); } else { this.GetComponent <RealisticBuoyancy>().enabled = false; Debug.LogErrorFormat("Gameobject {0} Disabled due to incorrect setup!", this.name); } } else { inWater = false; for (int i = 0; i < voxels.Count; i++) { var wp = transform.TransformPoint(voxels[i]); float waterLevel = GetWaterLevel(wp.x, wp.z); if (wp.y - voxelHalfHeight < waterLevel) // if underwater { inWater = true; float k = (waterLevel - wp.y) / (2 * voxelHalfHeight) + 0.5f; if (k > 1) { k = 1f; } else if (k < 0) { k = 0f; } var velocity = GetComponent <Rigidbody>().GetPointVelocity(wp); var localDampingForce = -velocity * DAMPFER * _thisRigidbody.mass; var force = localDampingForce + Mathf.Sqrt(k) * localArchimedesForce; //Again this is for MovePosition if (ignoreChanges == true && ignoreChangesDelay < ignoreChangesMaxDelay) { ignoreChangesDelay++; } else { if (ignoreChangesDelay > ignoreChangesMaxDelay) { ignoreChanges = false; } _thisRigidbody.AddForceAtPosition(force, wp); } if (RealisticWaterPhysics.currentDebugEnabled) { forces.Add(new[] { wp, force }); // For drawing force gizmos } } else //not underwater? so above water! { if (RealisticWaterPhysics.currentAirDensity > 0f) //is there air to calculate with ? { float k = ((waterLevel + RealisticWaterPhysics.currentAirLevel) - wp.y) / (2 * voxelHalfHeight) + 0.5f; if (k > 1) { k = 1f; } else if (k < 0) { k = 0f; } var velocity = GetComponent <Rigidbody>().GetPointVelocity(wp); //var localDampingForce = -velocity * DAMPFER * GetComponent<Rigidbody>().mass; var localDampingForce = -velocity * 0.01f * _thisRigidbody.mass; //remove the dempfer, but keep a tiny bit in. var force = localDampingForce + Mathf.Sqrt(k) * localArchimedesForceAir; if (ignoreChanges == true && ignoreChangesDelay < ignoreChangesMaxDelay) { ignoreChangesDelay++; } else { if (ignoreChangesDelay > ignoreChangesMaxDelay) { ignoreChanges = false; } _thisRigidbody.AddForceAtPosition(force, wp); } if (RealisticWaterPhysics.currentDebugEnabled) // Just skip this if debug info is disabled. { forces.Add(new[] { wp, force }); // For drawing force gizmos } } } } } } else { if (HeavyRunTimeEdits || LightRunTimeEdits) { #if DW if (RealisticWaterPhysics.currentUseDW == true) { if (GetComponent <BuoyancyForce>() == false || GetComponent <BuoyancyForce>().enabled == false) { //System is disabled or not found so turn the system back on! BuoyancyEnabled = true; } } #endif } } BuoyancyActive = BuoyancyEnabled; } }