Vector2 getFluidVelocity(Vector3 position) { Ray ray = Camera.main.ScreenPointToRay(position); RaycastHit hitInfo = new RaycastHit(); if (fluid.GetComponent <Collider>().Raycast(ray, out hitInfo, 10)) { Vector2 simSize = new Vector2(fluid.GetWidth(), fluid.GetHeight()); Vector2 posInSimSpace = new Vector2(hitInfo.textureCoord.x * simSize.x, hitInfo.textureCoord.y * simSize.y); Vector2 velInSimSpace = fluid.GetVelocity((int)posInSimSpace.x, (int)posInSimSpace.y) * Time.deltaTime; return(velInSimSpace); } return(Vector2.zero); }
void LateUpdate() { if (m_fluid) { Ray ray = new Ray(gameObject.transform.position, new Vector3(0, 0, 1)); RaycastHit hitInfo = new RaycastHit(); if (m_fluid.GetComponent <Collider>().Raycast(ray, out hitInfo, 10)) { Vector2 simSize = new Vector2(m_fluid.GetWidth(), m_fluid.GetHeight()); Vector2 posInSimSpace = new Vector2(hitInfo.textureCoord.x * simSize.x, hitInfo.textureCoord.y * simSize.y); Vector2 velInSimSpace = m_fluid.GetVelocity((int)posInSimSpace.x, (int)posInSimSpace.y) * Time.deltaTime; Vector2 worldSize = m_particleArea.GetRenderSize(); Vector2 velInWorldSpace = new Vector2((velInSimSpace.x * worldSize.x) / simSize.x, (velInSimSpace.y * worldSize.y) / simSize.y); gameObject.transform.position = gameObject.transform.position + new Vector3(velInWorldSpace.x, velInWorldSpace.y, 0f); } } }
private void LinkToFluidSimulation() { float fResolutionRatio = m_simulation.Resolution / (float)m_nResolution; m_simulation.SetSize((int)(m_nWidth * fResolutionRatio), (int)(m_nHeight * fResolutionRatio)); m_simulation.InitShaders(); m_advectParticles.SetInts("_VelocitySize", new int[] { m_simulation.GetWidth(), m_simulation.GetHeight() }); }