bool CreateRenderTarget() { DestroyRenderTarget(); if( RendererWorld.Instance == null ) return false; Vec2I textureSize = GetDemandTextureSize(); if( textureSize.X < 1 || textureSize.Y < 1 ) return false; string textureName = TextureManager.Instance.GetUniqueName( "WPFRenderTexture" ); int hardwareFSAA = 0; if( !RendererWorld.InitializationOptions.AllowSceneMRTRendering ) { if( !int.TryParse( RendererWorld.InitializationOptions.FullSceneAntialiasing, out hardwareFSAA ) ) hardwareFSAA = 0; } texture = TextureManager.Instance.Create( textureName, Texture.Type.Type2D, textureSize, 1, 0, Engine.Renderer.PixelFormat.R8G8B8, Texture.Usage.RenderTarget, false, hardwareFSAA ); if( texture == null ) return false; currentTextureSize = textureSize; renderTexture = texture.GetBuffer().GetRenderTarget(); renderTexture.AutoUpdate = false; renderTexture.AllowAdditionalMRTs = true; camera = SceneManager.Instance.CreateCamera( SceneManager.Instance.GetUniqueCameraName( "UserControl" ) ); camera.Purpose = Camera.Purposes.MainCamera; //update camera settings camera.NearClipDistance = cameraNearFarClipDistance.Minimum; camera.FarClipDistance = cameraNearFarClipDistance.Maximum; camera.AspectRatio = (float)texture.Size.X / (float)texture.Size.Y; camera.FixedUp = cameraFixedUp; camera.Position = cameraPosition; camera.Direction = cameraDirection; camera.Fov = cameraFov; camera.ProjectionType = cameraProjectionType; camera.OrthoWindowHeight = cameraOrthoWindowHeight; viewport = renderTexture.AddViewport( camera ); //Initialize HDR compositor for HDR render technique if( EngineApp.RenderTechnique == "HDR" ) { viewport.AddCompositor( "HDR", 0 ); viewport.SetCompositorEnabled( "HDR", true ); } //Initialize Fast Approximate Antialiasing (FXAA) { bool useMRT = RendererWorld.InitializationOptions.AllowSceneMRTRendering; string fsaa = RendererWorld.InitializationOptions.FullSceneAntialiasing; if( ( useMRT && ( fsaa == "" || fsaa == "RecommendedSetting" ) && IsActivateFXAAByDefault() ) || fsaa == "FXAA" ) { if( RenderSystem.Instance.HasShaderModel3() ) InitializeFXAACompositor(); } } //add listener renderTargetListener = new ViewRenderTargetListener( this ); renderTexture.AddListener( renderTargetListener ); if( guiRenderer == null ) guiRenderer = new GuiRenderer( viewport ); else guiRenderer.ChangeViewport( viewport ); if( controlManager == null ) controlManager = new ScreenControlManager( guiRenderer ); //initialize D3DImage output if( d3dImageIsSupported && allowUsingD3DImage ) { // create a D3DImage to host the scene and monitor it for changes in front buffer availability if( d3dImage == null ) { d3dImage = new D3DImage(); d3dImage.IsFrontBufferAvailableChanged += D3DImage_IsFrontBufferAvailableChanged; CompositionTarget.Rendering += D3DImage_OnRendering; } // set output to background image Background = new ImageBrush( d3dImage ); // set the back buffer using the new scene pointer HardwarePixelBuffer buffer = texture.GetBuffer( 0, 0 ); GetD3D9HardwarePixelBufferData data = new GetD3D9HardwarePixelBufferData(); data.hardwareBuffer = buffer._GetRealObject(); data.outPointer = IntPtr.Zero; unsafe { GetD3D9HardwarePixelBufferData* pData = &data; if( !RenderSystem.Instance.CallCustomMethod( "Get D3D9HardwarePixelBuffer getSurface", (IntPtr)pData ) ) Log.Fatal( "Get D3D9HardwarePixelBuffer getSurface failed." ); } d3dImage.Lock(); d3dImage.SetBackBuffer( D3DResourceType.IDirect3DSurface9, data.outPointer ); d3dImage.Unlock(); } return true; }
private void CreateViewport() { int index = instance.views.IndexOf(this); DestroyViewport(); Vec2I textureSize = GetNeededTextureSize(); string textureName = TextureManager.Instance.GetUniqueName( string.Format("MultiViewRendering{0}", index)); PixelFormat format = PixelFormat.R8G8B8; int fsaa; if (!int.TryParse(RendererWorld.InitializationOptions.FullSceneAntialiasing, out fsaa)) fsaa = 0; texture = TextureManager.Instance.Create(textureName, Texture.Type.Type2D, textureSize, 1, 0, format, Texture.Usage.RenderTarget, false, fsaa); if (texture == null) { Log.Fatal("MultiViewRenderingManager: Unable to create texture."); return; } RenderTarget renderTarget = texture.GetBuffer().GetRenderTarget(); renderTarget.AutoUpdate = true; renderTarget.AllowAdditionalMRTs = true; //create camera camera = SceneManager.Instance.CreateCamera( SceneManager.Instance.GetUniqueCameraName(string.Format("MultiViewRendering{0}", index))); camera.Purpose = Camera.Purposes.MainCamera; //add viewport viewport = renderTarget.AddViewport(camera, 0); viewport.ShadowsEnabled = true; //Create compositor for HDR render technique bool hdrCompositor = RendererWorld.Instance.DefaultViewport.GetCompositorInstance("HDR") != null; if (hdrCompositor) { viewport.AddCompositor("HDR"); viewport.SetCompositorEnabled("HDR", true); } //FXAA antialiasing post effect bool fxaaCompositor = RendererWorld.Instance.DefaultViewport.GetCompositorInstance("FXAA") != null; if (fxaaCompositor) { viewport.AddCompositor("FXAA"); viewport.SetCompositorEnabled("FXAA", true); } //add listener renderTargetListener = new ViewRenderTargetListener(this); renderTarget.AddListener(renderTargetListener); initializedTextureSize = textureSize; }
void CreateReflectionTexture() { DestroyReflectionTexture(); Vec2i textureSize = GetRequiredReflectionTextureSize(); //create render texture string textureName = TextureManager.Instance.GetUniqueName( "WaterPlaneReflection" ); bool hdr = RendererWorld.Instance.DefaultViewport.GetCompositorInstance( "HDR" ) != null; reflectionTexture = TextureManager.Instance.Create( textureName, Texture.Type.Type2D, textureSize, 1, 0, hdr ? PixelFormat.Float16RGB : PixelFormat.R8G8B8, Texture.Usage.RenderTarget ); reflectionRenderTexture = reflectionTexture.GetBuffer().GetRenderTarget(); //create camera reflectionCamera = SceneManager.Instance.CreateCamera(); reflectionCamera.AllowFrustumTestMode = true; //add viewport reflectionViewport = reflectionRenderTexture.AddViewport( reflectionCamera ); reflectionViewport.ShadowsEnabled = false; reflectionViewport.MaterialScheme = MaterialSchemes.Low.ToString(); //add listener renderTargetListener = new ReflectionRenderTargetListener( this ); reflectionRenderTexture.AddListener( renderTargetListener ); reflectionRenderTexture.AutoUpdate = Visible; }