void UpdateReflectionTexture( ReflectionTextureItem item ) { //changing during update bool saveDrawDecorativeObjects = false; Dictionary<HeightmapTerrain, bool> saveHeightmapTerrainsSimpleRenderingState = new Dictionary<HeightmapTerrain, bool>(); //configure camera and entities { Camera camera = item.reflectionCamera; Camera mainCamera = item.mainCamera; camera.NearClipDistance = mainCamera.NearClipDistance; camera.FarClipDistance = mainCamera.FarClipDistance; camera.AspectRatio = mainCamera.AspectRatio; camera.Fov = mainCamera.Fov; camera.Position = mainCamera.Position; camera.FixedUp = mainCamera.FixedUp; camera.Direction = mainCamera.Direction; Plane reflectionPlane = new Plane( Vec3.ZAxis, Position.Z ); camera.DisableReflection(); camera.EnableReflection( reflectionPlane ); //set clip planes { Plane clipPlane = new Plane( Vec3.ZAxis, Position.Z ); Plane[] clipPlanes = new Plane[ 5 ]; clipPlanes[ 0 ] = clipPlane; Vec3 reflectedCameraPosition = camera.GetReflectionMatrix() * camera.Position; Bounds bounds = new Bounds( Position ); bounds.Expand( new Vec3( Size.X, Size.Y, 0 ) * .5f ); Vec3 p0 = new Vec3( bounds.Minimum.X, bounds.Minimum.Y, Position.Z ); Vec3 p1 = new Vec3( bounds.Maximum.X, bounds.Minimum.Y, Position.Z ); Vec3 p2 = new Vec3( bounds.Maximum.X, bounds.Maximum.Y, Position.Z ); Vec3 p3 = new Vec3( bounds.Minimum.X, bounds.Maximum.Y, Position.Z ); clipPlanes[ 1 ] = Plane.FromPoints( reflectedCameraPosition, p0, p1 ); clipPlanes[ 2 ] = Plane.FromPoints( reflectedCameraPosition, p1, p2 ); clipPlanes[ 3 ] = Plane.FromPoints( reflectedCameraPosition, p2, p3 ); clipPlanes[ 4 ] = Plane.FromPoints( reflectedCameraPosition, p3, p0 ); camera.SetClipPlanesForAllGeometry( clipPlanes ); } //set reflection level settings if( DecorativeObjectManager.Instance != null ) saveDrawDecorativeObjects = DecorativeObjectManager.Instance.Visible; camera.DrawStaticGeometry = true; camera.DrawModels = true; camera.DrawEffects = true; if( (int)ReflectionLevel < (int)ReflectionLevels.OnlyStaticGeometry ) camera.DrawStaticGeometry = false; if( (int)ReflectionLevel < (int)ReflectionLevels.OnlyModels ) camera.DrawModels = false; if( (int)ReflectionLevel < (int)ReflectionLevels.ReflectAll ) { camera.DrawEffects = false; if( DecorativeObjectManager.Instance != null ) DecorativeObjectManager.Instance.Visible = false; } //activate simple rendering mode for terrains foreach( HeightmapTerrain terrain in HeightmapTerrain.Instances ) { saveHeightmapTerrainsSimpleRenderingState.Add( terrain, terrain.SimpleRendering ); terrain.SimpleRendering = true; } } //get clip volumes List<Box> clipVolumes = new List<Box>(); foreach( WaterPlaneClipVolume volume in WaterPlaneClipVolume.Instances ) { if( volume.Editor_IsExcludedFromWorld() ) continue; clipVolumes.Add( volume.GetBox() ); } //bind clip volumes if( clipVolumes.Count != 0 ) RenderingLowLevelMethodsImpl.PushClipVolumes( clipVolumes.ToArray() ); //render item.reflectionRenderTexture.Update( false ); //unbind clip volumes if( clipVolumes.Count != 0 ) RenderingLowLevelMethodsImpl.PopClipVolumes(); //restore entity settings { //RenderSystem.Instance.ResetScissorTest(); //restore simple rendering mode state for terrains foreach( HeightmapTerrain terrain in HeightmapTerrain.Instances ) { bool simpleRendering; if( saveHeightmapTerrainsSimpleRenderingState.TryGetValue( terrain, out simpleRendering ) ) terrain.SimpleRendering = simpleRendering; } saveHeightmapTerrainsSimpleRenderingState.Clear(); //restore draw settings if( DecorativeObjectManager.Instance != null ) DecorativeObjectManager.Instance.Visible = saveDrawDecorativeObjects; } }
ReflectionTextureItem CreateReflectionTextureItem( Viewport viewport ) { ReflectionTextureItem item = new ReflectionTextureItem(); item.viewport = viewport; item.mainCamera = viewport.ViewportCamera; Vec2I textureSize = GetRequiredReflectionTextureSize( viewport ); //Log.Info( "CreateReflectionTextureItem: " + textureSize.ToString() ); //create render texture string textureName = TextureManager.Instance.GetUniqueName( "WaterPlaneReflection" ); PixelFormat format = UseHDRTexture ? PixelFormat.Float16RGB : PixelFormat.R8G8B8; item.reflectionTexture = TextureManager.Instance.Create( textureName, Texture.Type.Type2D, textureSize, 1, 0, format, Texture.Usage.RenderTarget ); if( item.reflectionTexture == null ) { Log.Warning( "WaterPlane: CreateReflectionTextureItem: Unable to create texture." ); return null; } item.hdrTexture = UseHDRTexture; item.reflectionRenderTexture = item.reflectionTexture.GetBuffer().GetRenderTarget(); item.reflectionRenderTexture.AutoUpdate = false; //uncomment it when need Soft-Particles for reflections //item.reflectionRenderTexture.AllowAdditionalMRTs = true; //create camera item.reflectionCamera = SceneManager.Instance.CreateCamera( SceneManager.Instance.GetUniqueCameraName( "WaterPlane" ) ); item.reflectionCamera.Purpose = Camera.Purposes.Special; item.reflectionCamera.AllowMapCompositorManager = false; item.reflectionCamera.AllowFrustumTestMode = true; //add viewport item.reflectionViewport = item.reflectionRenderTexture.AddViewport( item.reflectionCamera ); item.reflectionViewport.ShadowsEnabled = false; item.reflectionViewport.MaterialScheme = MaterialSchemes.Low.ToString(); //add to list reflectionTextureItems.Add( item ); return item; }
void DestroyReflectionTextureItem( ReflectionTextureItem item ) { //Log.Info( "DestroyReflectionTextureItem: " + item.reflectionTexture.Size.ToString() ); //remove reflection texture from material if( material != null ) material.RemoveReflectionMapFromMaterial(); item.reflectionViewport.Dispose(); item.reflectionCamera.Dispose(); item.reflectionTexture.Dispose(); reflectionTextureItems.Remove( item ); }