public float HitDistance(Plane plane) { float d = Vector3.Dot(plane.Normal, Direction); if (Math.Abs(d) >= float.Epsilon) { float t = -(Vector3.Dot(plane.Normal, Origin) + plane.D) / d; if (t >= 0.0f) return t; else return float.PositiveInfinity; } else return float.PositiveInfinity; }
void SetupViewport() { var renderer = GetSubsystem<Renderer>(); var cache = GetSubsystem<ResourceCache>(); renderer.SetViewport(0, new Viewport(scene, CameraNode.GetComponent<Camera>())); // Create a mathematical plane to represent the water in calculations waterPlane = new Plane(waterNode.WorldRotation * new Vector3(0.0f, 1.0f, 0.0f), waterNode.WorldPosition); // Create a downward biased plane for reflection view clipping. Biasing is necessary to avoid too aggressive clipping waterClipPlane = new Plane(waterNode.WorldRotation * new Vector3(0.0f, 1.0f, 0.0f), waterNode.WorldPosition - new Vector3(0.0f, 0.1f, 0.0f)); // Create camera for water reflection // It will have the same farclip and position as the main viewport camera, but uses a reflection plane to modify // its position when rendering reflectionCameraNode = CameraNode.CreateChild(); var reflectionCamera = reflectionCameraNode.CreateComponent<Camera>(); reflectionCamera.FarClip = 750.0f; reflectionCamera.ViewMask= 0x7fffffff; // Hide objects with only bit 31 in the viewmask (the water plane) reflectionCamera.AutoAspectRatio = false; reflectionCamera.UseReflection = true; reflectionCamera.ReflectionPlane = waterPlane; reflectionCamera.UseClipping = true; // Enable clipping of geometry behind water plane reflectionCamera.ClipPlane = waterClipPlane; // The water reflection texture is rectangular. Set reflection camera aspect ratio to match reflectionCamera.AspectRatio = (float)graphics.Width / graphics.Height; // View override flags could be used to optimize reflection rendering. For example disable shadows //reflectionCamera.ViewOverrideFlags = ViewOverrideFlags.DisableShadows; // Create a texture and setup viewport for water reflection. Assign the reflection texture to the diffuse // texture unit of the water material int texSize = 1024; Texture2D renderTexture = new Texture2D(); renderTexture.SetSize(texSize, texSize, Graphics.GetRGBFormat(), TextureUsage.TEXTURE_RENDERTARGET); renderTexture.FilterMode = TextureFilterMode.FILTER_BILINEAR; RenderSurface surface = renderTexture.RenderSurface; var rttViewport = new Viewport(scene, reflectionCamera); surface.SetViewport(0, rttViewport); var waterMat = cache.Get<Material>("Materials/Water.xml"); waterMat.SetTexture(TextureUnit.TU_DIFFUSE, renderTexture); }
private static extern void csi_Atomic_Camera_SetReflectionPlane(IntPtr self, ref Plane retValue);
private static extern void csi_Atomic_Camera_GetClipPlane(IntPtr self, ref Plane retValue);
public void SetReflectionPlane(Plane plane) { csi_Atomic_Camera_SetReflectionPlane(nativeInstance, ref plane); }
public void SetClipPlane(Plane plane) { csi_Atomic_Camera_SetClipPlane(nativeInstance, ref plane); }
public Plane GetReflectionPlane() { Plane plane = new Plane(); csi_Atomic_Camera_GetReflectionPlane(nativeInstance, ref plane); return plane; }