public CubemapSceneRenderer(ISceneRendererContext context, int textureSize) { this.context = context; Camera = new CameraComponent { UseCustomProjectionMatrix = true, UseCustomViewMatrix = true, }; // Replace graphics compositor (don't want post fx, etc...) gameCompositor = context.SceneSystem.GraphicsCompositor.Game; context.SceneSystem.GraphicsCompositor.Game = new SceneExternalCameraRenderer { Child = context.SceneSystem.GraphicsCompositor.SingleView, ExternalCamera = Camera }; // Setup projection matrix Camera.ProjectionMatrix = Matrix.PerspectiveFovRH(MathUtil.DegreesToRadians(90.0f), 1.0f, Camera.NearClipPlane, Camera.FarClipPlane); var renderContext = RenderContext.GetShared(context.Services); DrawContext = new RenderDrawContext(context.Services, renderContext, context.GraphicsContext); // We can't render directly to the texture cube before feature level 10.1, so let's render to a standard render target and copy instead renderTarget = Texture.New2D(context.GraphicsDevice, textureSize, textureSize, PixelFormat.R16G16B16A16_Float, TextureFlags.RenderTarget); depthStencil = Texture.New2D(context.GraphicsDevice, textureSize, textureSize, PixelFormat.D24_UNorm_S8_UInt, TextureFlags.DepthStencil); }
public CubemapSceneRenderer(ISceneRendererContext context, int textureSize) : base(context.GraphicsDevice, textureSize, PixelFormat.R16G16B16A16_Float, true) { this.context = context; var renderContext = RenderContext.GetShared(context.Services); DrawContext = new RenderDrawContext(context.Services, renderContext, context.GraphicsContext); // Replace graphics compositor (don't want post fx, etc...) gameCompositor = context.SceneSystem.GraphicsCompositor.Game; context.SceneSystem.GraphicsCompositor.Game = new SceneExternalCameraRenderer { Child = context.SceneSystem.GraphicsCompositor.SingleView, ExternalCamera = Camera }; }
public static Texture GenerateCubemap(ISceneRendererContext context, Vector3 position, int textureSize) { using (var cubemapRenderer = new CubemapSceneRenderer(context, textureSize)) { // Create target cube texture var cubeTexture = Texture.NewCube(context.GraphicsDevice, textureSize, PixelFormat.R16G16B16A16_Float); using (cubemapRenderer.DrawContext.PushRenderTargetsAndRestore()) { // Render specular probe context.GraphicsContext.CommandList.BeginProfile(Color.Red, "SpecularProbe"); cubemapRenderer.Draw(position, cubeTexture); context.GraphicsContext.CommandList.EndProfile(); } return(cubeTexture); } }
public static Dictionary <LightProbeComponent, FastList <Color3> > GenerateCoefficients(ISceneRendererContext context) { using (var cubemapRenderer = new CubemapSceneRenderer(context, 256)) { // Create target cube texture var cubeTexture = Texture.NewCube(context.GraphicsDevice, 256, PixelFormat.R16G16B16A16_Float); // Prepare shader for SH prefiltering var lambertFiltering = new LambertianPrefilteringSHNoCompute(cubemapRenderer.DrawContext.RenderContext) { HarmonicOrder = LambertHamonicOrder, RadianceMap = cubeTexture, }; var lightProbesCoefficients = new Dictionary <LightProbeComponent, FastList <Color3> >(); using (cubemapRenderer.DrawContext.PushRenderTargetsAndRestore()) { // Render light probe context.GraphicsContext.CommandList.BeginProfile(Color.Red, "LightProbes"); int lightProbeIndex = 0; foreach (var entity in context.SceneSystem.SceneInstance) { var lightProbe = entity.Get <LightProbeComponent>(); if (lightProbe == null) { continue; } var lightProbePosition = lightProbe.Entity.Transform.WorldMatrix.TranslationVector; context.GraphicsContext.ResourceGroupAllocator.Reset(context.GraphicsContext.CommandList); context.GraphicsContext.CommandList.BeginProfile(Color.Red, $"LightProbes {lightProbeIndex}"); lightProbeIndex++; cubemapRenderer.Draw(lightProbePosition, cubeTexture); context.GraphicsContext.CommandList.BeginProfile(Color.Red, "Prefilter SphericalHarmonics"); // Compute SH coefficients lambertFiltering.Draw(cubemapRenderer.DrawContext); var coefficients = lambertFiltering.PrefilteredLambertianSH.Coefficients; var lightProbeCoefficients = new FastList <Color3>(); for (int i = 0; i < coefficients.Length; i++) { lightProbeCoefficients.Add(coefficients[i] * SphericalHarmonics.BaseCoefficients[i]); } lightProbesCoefficients.Add(lightProbe, lightProbeCoefficients); context.GraphicsContext.CommandList.EndProfile(); // Prefilter SphericalHarmonics context.GraphicsContext.CommandList.EndProfile(); // Face XXX // Debug render } context.GraphicsContext.CommandList.EndProfile(); // LightProbes } cubeTexture.Dispose(); return(lightProbesCoefficients); } }
public static Texture GenerateCubemap(ISceneRendererContext context, Vector3 position, int textureSize) { return(GenerateCubemap(new CubemapSceneRenderer(context, textureSize), position)); }