BrunetonsOcean.Projection.SegmentPlaneIntersection(UnityEngine.Vector3, UnityEngine.Vector3, UnityEngine.Vector3, float, ref UnityEngine.Vector3)

Here are the examples of the csharp api BrunetonsOcean.Projection.SegmentPlaneIntersection(UnityEngine.Vector3, UnityEngine.Vector3, UnityEngine.Vector3, float, ref UnityEngine.Vector3) taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

1 Examples 7

19 View Source File : Projection.cs
License : MIT License
Project Creator : Scrawk

void CreateRangeMatrix(Camera cam, Matrix4x4 projectorVP)
		{
			
			m_pointList.Clear();
			
			Matrix4x4 V = cam.worldToCameraMatrix;
			
			//The inverse view projection matrix will transform
			//screen space verts to world space.
			Matrix4x4 IVP = (m_projectorP * V).inverse;
			
			Vector3 UP = Vector3.up;
			Vector4 p = Vector4.zero;
			
			float range = Mathf.Max(1.0f, MaxHeight);
			
			//Convert each screen vert to world space.
			for (int i = 0; i < 8; i++)
			{
				p = IVP * m_corners[i];
				//p /= p.w;
				p.x /= p.w;
				p.y /= p.w;
				p.z /= p.w;
				
				m_frustumCorners[i] = p;
			}
			
			//For each corner if its world space position is  
			//between the wave range then add it to the list.
			for (int i = 0; i < 8; i++)
			{
				if (m_frustumCorners[i].y <= OceanLevel + range && m_frustumCorners[i].y >= OceanLevel - range)
				{
					m_pointList.Add(m_frustumCorners[i]);
				}
			}
			
			//Now take each segment in the frustum box and check
			//to see if it intersects the ocean plane on both the
			//upper and lower ranges.
			for (int i = 0; i < 12; i++)
			{
				Vector3 p0 = m_frustumCorners[m_indices[i, 0]];
				Vector3 p1 = m_frustumCorners[m_indices[i, 1]];
				
				Vector3 max = new Vector3();
				Vector3 min = new Vector3();
				
				if (SegmentPlaneIntersection(p0, p1, UP, OceanLevel + range, ref max))
				{
					m_pointList.Add(max);
				}
				
				if (SegmentPlaneIntersection(p0, p1, UP, OceanLevel - range, ref min))
				{
					m_pointList.Add(min);
				}
				
			}

            int count = m_pointList.Count;
			
			//If list is empty the ocean can not be seen.
			if(count == 0)
			{
				m_projectorR[0, 0] = 1; m_projectorR[0, 3] = 0;
				m_projectorR[1, 1] = 1; m_projectorR[1, 3] = 0;
				return;
			}
			
			float xmin = float.PositiveInfinity;
			float ymin = float.PositiveInfinity;
			float xmax = float.NegativeInfinity;
			float ymax = float.NegativeInfinity;
			Vector4 q = Vector4.zero;
			
			//Now convert each world space position into
			//projector screen space. The min/max x/y values
			//are then used for the range conversion matrix.
			for(int i = 0; i < count; i++)
			{
				q.x = m_pointList[i].x;
				q.y = OceanLevel;
				q.z = m_pointList[i].z;
				q.w = 1.0f;
				
				p = projectorVP * q;
				//p /= p.w;
				p.x /= p.w;
				p.y /= p.w;
				
				if (p.x < xmin) xmin = p.x;
				if (p.y < ymin) ymin = p.y;
				if (p.x > xmax) xmax = p.x;
				if (p.y > ymax) ymax = p.y;
				
			}
			
			//Create the range conversion matrix and return it.
			
			m_projectorR[0, 0] = xmax - xmin; m_projectorR[0, 3] = xmin;
			m_projectorR[1, 1] = ymax - ymin; m_projectorR[1, 3] = ymin;
			
		}