public void DrawIds(MeshMaterialLibrary meshMat, List <Decal> decals, List <PointLight> pointLights, List <DirectionalLight> dirLights, EnvironmentSample envSample, List <DebugEntity> debug, Matrix viewProjection, Matrix view, EditorLogic.EditorSendData editorData) { _graphicsDevice.SetRenderTarget(_idRenderTarget2D); _graphicsDevice.BlendState = BlendState.Opaque; _graphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise; _graphicsDevice.DepthStencilState = DepthStencilState.Default; meshMat.Draw(MeshMaterialLibrary.RenderType.IdRender, viewProjection); //Now onto the billboards DrawBillboards(decals, pointLights, dirLights, envSample, debug, viewProjection, view); //Now onto the gizmos DrawGizmos(viewProjection, editorData, _assets); Rectangle sourceRectangle = new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 1, 1); Color[] retrievedColor = new Color[1]; try { if (sourceRectangle.X >= 0 && sourceRectangle.Y >= 0 && sourceRectangle.X < _idRenderTarget2D.Width - 2 && sourceRectangle.Y < _idRenderTarget2D.Height - 2) { _idRenderTarget2D.GetData(0, sourceRectangle, retrievedColor, 0, 1); } } catch { //nothing } HoveredId = IdGenerator.GetIdFromColor(retrievedColor[0]); }
public void DrawOutlines(MeshMaterialLibrary meshMat, Matrix viewProjection, bool drawAll, int hoveredId, EditorLogic.EditorSendData editorData, bool mouseMoved) { _graphicsDevice.SetRenderTarget(_idRenderTarget2D); if (!mouseMoved) { _graphicsDevice.Clear(ClearOptions.Target, Color.Black, 0, 0); } else { _graphicsDevice.Clear(Color.Black); } _graphicsDevice.BlendState = BlendState.Opaque; _graphicsDevice.DepthStencilState = DepthStencilState.Default; _graphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise; int selectedId = editorData.SelectedObjectId; //Selected entity if (selectedId != 0) { //UPdate the size of our outlines! if (!drawAll) { meshMat.Draw(MeshMaterialLibrary.RenderType.IdOutline, viewProjection, false, false, false, selectedId); } Shaders.IdRenderEffectParameterColorId.SetValue(_selectedColor); meshMat.Draw(MeshMaterialLibrary.RenderType.IdOutline, viewProjection, false, false, outlined: true, outlineId: selectedId); } if (selectedId != hoveredId && hoveredId != 0 && mouseMoved) { if (!drawAll) { meshMat.Draw(MeshMaterialLibrary.RenderType.IdOutline, viewProjection, false, false, false, hoveredId); } Shaders.IdRenderEffectParameterColorId.SetValue(_hoveredColor); meshMat.Draw(MeshMaterialLibrary.RenderType.IdOutline, viewProjection, false, false, outlined: true, outlineId: hoveredId); } }
public RenderTarget2D Draw(GraphicsDevice graphicsDevice, RenderTarget2D input, RenderTarget2D output, MeshMaterialLibrary meshMat, Matrix viewProjection) { graphicsDevice.SetRenderTarget(output); graphicsDevice.DepthStencilState = DepthStencilState.Default; meshMat.Draw(MeshMaterialLibrary.RenderType.SubsurfaceScattering, viewProjection, renderModule: this); return(output); }
public void Draw(GraphicsDevice _graphicsDevice, RenderTargetBinding[] _renderTargetBinding, MeshMaterialLibrary meshMaterialLibrary, Matrix _viewProjection, Matrix _view) { _graphicsDevice.SetRenderTargets(_renderTargetBinding); //Clear the GBuffer if (GameSettings.g_ClearGBuffer) { _graphicsDevice.RasterizerState = RasterizerState.CullNone; _graphicsDevice.BlendState = BlendState.Opaque; _graphicsDevice.DepthStencilState = DepthStencilState.Default; _clearGBufferPass.Apply(); _fullScreenTriangle.Draw(_graphicsDevice); } //Draw the Gbuffer! meshMaterialLibrary.Draw(renderType: MeshMaterialLibrary.RenderType.Opaque, viewProjection: _viewProjection, lightViewPointChanged: true, view: _view, renderModule: this); }
/// <summary> /// Draw forward shaded, alpha blended materials. Very basic and unoptimized algorithm. Can be improved to use tiling in future. /// </summary> /// <param name="graphicsDevice"></param> /// <param name="output"></param> /// <param name="meshMat"></param> /// <param name="viewProjection"></param> /// <param name="camera"></param> /// <param name="pointLights"></param> /// <param name="frustum"></param> /// <returns></returns> public RenderTarget2D Draw(GraphicsDevice graphicsDevice, RenderTarget2D output, MeshMaterialLibrary meshMat, Matrix viewProjection, Camera camera, List <PointLight> pointLights, BoundingFrustum frustum) { graphicsDevice.DepthStencilState = DepthStencilState.Default; SetupLighting(camera, pointLights, frustum); //Draw Frustum debug test //Matrix view = Matrix.CreateLookAt(new Vector3(-88, -11f, 4), new Vector3(38, 8, 32), Vector3.UnitZ); //Matrix projection = Matrix.CreatePerspectiveFieldOfView((float)(Math.PI / 2), 1.6f, 1, 100); //BoundingFrustumEx frustum2 = new BoundingFrustumEx(view * projection); //LineHelperManager.AddFrustum(frustum2, 1, Color.Red); //Vector3[] corners = frustum.GetCorners(); //BoundingFrustumEx frustum3 = new BoundingFrustumEx(ref corners); //TiledLighting(frustum, pointLights, 20, 10); meshMat.Draw(MeshMaterialLibrary.RenderType.Forward, viewProjection, renderModule: this); return(output); }
private void DrawObjects(MeshMaterialLibrary meshMaterialLibrary) { _graphicsDevice.SetRenderTarget(null); _graphicsDevice.Clear(GameSettings.g_UpdateShading ? Color.CadetBlue : Color.DarkViolet); _graphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise; _graphicsDevice.DepthStencilState = DepthStencilState.Default; Shaders.GBufferEffectParameter_Material_Texture.SetValue(GameSettings.g_FixSeams ? _textureBufferSeamFix : _textureBuffer); meshMaterialLibrary.Draw(renderType: MeshMaterialLibrary.RenderType.FinalMesh, graphicsDevice: _graphicsDevice, viewProjection: _viewProjection, lightViewPointChanged: true, view: _view); _graphicsDevice.RasterizerState = RasterizerState.CullClockwise; _graphicsDevice.BlendState = BlendState.NonPremultiplied; //Skybox ModelMeshPart part = _assets.Isosphere.Meshes[0].MeshParts[0]; _graphicsDevice.SetVertexBuffer(part.VertexBuffer); _graphicsDevice.Indices = (part.IndexBuffer); int primitiveCount = part.PrimitiveCount; int vertexOffset = part.VertexOffset; //int vCount = meshLib.GetMesh().NumVertices; int startIndex = part.StartIndex; Matrix world = Matrix.CreateScale(100); Shaders.GBufferEffectParameter_WorldViewProj.SetValue(world * _viewProjection); Shaders.GBufferEffectParameter_WorldIT.SetValue(world); Shaders.GBufferEffectTechniques_DrawSkybox.Passes[0].Apply(); _graphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, vertexOffset, startIndex, primitiveCount); //Performance Profiler if (GameSettings.d_profiler) { long performanceCurrentTime = _performanceTimer.ElapsedTicks; GameStats.d_profileDrawGBuffer = performanceCurrentTime - _performancePreviousTime; _performancePreviousTime = performanceCurrentTime; } }
/// <summary> /// Draw all our meshes to the GBuffer - albedo, normal, depth - for further computation /// </summary> /// <param name="meshMaterialLibrary"></param> private void DrawTextureBuffer(MeshMaterialLibrary meshMaterialLibrary) { if (!Input.WasKeyPressed(Keys.C) && !GameSettings.g_UpdateShading) { return; } _graphicsDevice.SetRenderTarget(_textureBuffer); _graphicsDevice.Clear(Color.TransparentBlack); _graphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise; _graphicsDevice.DepthStencilState = DepthStencilState.None; meshMaterialLibrary.Draw(renderType: MeshMaterialLibrary.RenderType.TextureBuffer, graphicsDevice: _graphicsDevice, viewProjection: _viewProjection, lightViewPointChanged: true, view: _view); //Performance Profiler if (GameSettings.d_profiler) { long performanceCurrentTime = _performanceTimer.ElapsedTicks; GameStats.d_profileDrawGBuffer = performanceCurrentTime - _performancePreviousTime; _performancePreviousTime = performanceCurrentTime; } }
/// <summary> /// Only one shadow map needed for a directional light /// </summary> /// <param name="light"></param> /// <param name="shadowResolution"></param> /// <param name="meshMaterialLibrary"></param> /// <param name="entities"></param> private void CreateShadowMapDirectionalLight(GraphicsDevice graphicsDevice, DirectionalLight light, int shadowResolution, MeshMaterialLibrary meshMaterialLibrary, List <BasicEntity> entities) { //Create a renderTarget if we don't have one yet if (light.ShadowMap == null) { //if (lightSource.ShadowFiltering != DirectionalLightSource.ShadowFilteringTypes.VSM) //{ light.ShadowMap = new RenderTarget2D(graphicsDevice, shadowResolution, shadowResolution, false, SurfaceFormat.Single, DepthFormat.Depth24, 0, RenderTargetUsage.DiscardContents); //} //else //For a VSM shadowMap we need 2 components //{ // lightSource.ShadowMap = new RenderTarget2D(_graphicsDevice, shadowResolution, shadowResolution, false, // SurfaceFormat.Vector2, DepthFormat.Depth24, 0, RenderTargetUsage.DiscardContents); //} } if (light.HasChanged) { Matrix lightProjection = Matrix.CreateOrthographic(light.ShadowSize, light.ShadowSize, -light.ShadowDepth, light.ShadowDepth); Matrix lightView = Matrix.CreateLookAt(light.Position, light.Position + light.Direction, Vector3.Down); light.LightView = lightView; light.LightViewProjection = lightView * lightProjection; _boundingFrustumShadow = new BoundingFrustum(light.LightViewProjection); graphicsDevice.SetRenderTarget(light.ShadowMap); graphicsDevice.Clear(ClearOptions.DepthBuffer, Color.White, 1, 0); meshMaterialLibrary.FrustumCulling(entities, _boundingFrustumShadow, true, light.Position); // Rendering! _FarClip.SetValue(light.ShadowDepth); _SizeBias.SetValue(GameSettings.ShadowBias * 2048 / light.ShadowResolution); meshMaterialLibrary.Draw(MeshMaterialLibrary.RenderType.ShadowLinear, light.LightViewProjection, light.HasChanged, false, false, 0, light.LightView, renderModule: this); } else { _boundingFrustumShadow = new BoundingFrustum(light.LightViewProjection); bool hasAnyObjectMoved = meshMaterialLibrary.FrustumCulling(entities: entities, boundingFrustrum: _boundingFrustumShadow, hasCameraChanged: false, cameraPosition: light.Position); if (!hasAnyObjectMoved) { return; } meshMaterialLibrary.FrustumCulling(entities: entities, boundingFrustrum: _boundingFrustumShadow, hasCameraChanged: true, cameraPosition: light.Position); graphicsDevice.SetRenderTarget(light.ShadowMap); graphicsDevice.Clear(ClearOptions.DepthBuffer, Color.White, 1, 0); _FarClip.SetValue(light.ShadowDepth); _SizeBias.SetValue(GameSettings.ShadowBias * 2048 / light.ShadowResolution); meshMaterialLibrary.Draw(MeshMaterialLibrary.RenderType.ShadowLinear, light.LightViewProjection, false, true, false, 0, light.LightView, renderModule: this); } //Blur! //if (lightSource.ShadowFiltering == DirectionalLightSource.ShadowFilteringTypes.VSM) //{ // lightSource.ShadowMap = _gaussianBlur.DrawGaussianBlur(lightSource.ShadowMap); //} }
/// <summary> /// Create the shadow map for each cubemapside, then combine into one cubemap /// </summary> /// <param name="light"></param> /// <param name="size"></param> /// <param name="meshMaterialLibrary"></param> /// <param name="entities"></param> private void CreateShadowCubeMap(GraphicsDevice graphicsDevice, PointLight light, int size, MeshMaterialLibrary meshMaterialLibrary, List <BasicEntity> entities) { //For VSM we need 2 channels, -> Vector2 //todo: check if we need preserve contents if (light.ShadowMap == null) { light.ShadowMap = new RenderTarget2D(graphicsDevice, size, size * 6, false, SurfaceFormat.HalfSingle, DepthFormat.Depth24, 0, RenderTargetUsage.PreserveContents); } Matrix lightViewProjection = new Matrix(); CubeMapFace cubeMapFace; // = CubeMapFace.NegativeX; if (light.HasChanged) { graphicsDevice.SetRenderTarget(light.ShadowMap); Matrix lightProjection = Matrix.CreatePerspectiveFieldOfView((float)(Math.PI / 2), 1, 1, light.Radius); Matrix lightView; // = identity //Reset the blur array light.faceBlurCount = new int[6]; graphicsDevice.SetRenderTarget(light.ShadowMap); graphicsDevice.Clear(Color.Black); for (int i = 0; i < 6; i++) { // render the scene to all cubemap faces cubeMapFace = (CubeMapFace)i; switch (cubeMapFace) { case CubeMapFace.PositiveX: { lightView = Matrix.CreateLookAt(light.Position, light.Position + Vector3.UnitX, Vector3.UnitZ); lightViewProjection = lightView * lightProjection; light.LightViewProjectionPositiveX = lightViewProjection; break; } case CubeMapFace.NegativeX: { lightView = Matrix.CreateLookAt(light.Position, light.Position - Vector3.UnitX, Vector3.UnitZ); lightViewProjection = lightView * lightProjection; light.LightViewProjectionNegativeX = lightViewProjection; break; } case CubeMapFace.PositiveY: { lightView = Matrix.CreateLookAt(light.Position, light.Position + Vector3.UnitY, Vector3.UnitZ); lightViewProjection = lightView * lightProjection; light.LightViewProjectionPositiveY = lightViewProjection; break; } case CubeMapFace.NegativeY: { lightView = Matrix.CreateLookAt(light.Position, light.Position - Vector3.UnitY, Vector3.UnitZ); lightViewProjection = lightView * lightProjection; light.LightViewProjectionNegativeY = lightViewProjection; break; } case CubeMapFace.PositiveZ: { lightView = Matrix.CreateLookAt(light.Position, light.Position + Vector3.UnitZ, Vector3.UnitX); lightViewProjection = lightView * lightProjection; light.LightViewProjectionPositiveZ = lightViewProjection; break; } case CubeMapFace.NegativeZ: { lightView = Matrix.CreateLookAt(light.Position, light.Position - Vector3.UnitZ, Vector3.UnitX); lightViewProjection = lightView * lightProjection; light.LightViewProjectionNegativeZ = lightViewProjection; break; } } if (_boundingFrustumShadow != null) { _boundingFrustumShadow.Matrix = lightViewProjection; } else { _boundingFrustumShadow = new BoundingFrustum(lightViewProjection); } meshMaterialLibrary.FrustumCulling(entities, _boundingFrustumShadow, true, light.Position); // Rendering! graphicsDevice.Viewport = new Viewport(0, light.ShadowResolution * (int)cubeMapFace, light.ShadowResolution, light.ShadowResolution); //_graphicsDevice.ScissorRectangle = new Rectangle(0, light.ShadowResolution* (int) cubeMapFace, light.ShadowResolution, light.ShadowResolution); _FarClip.SetValue(light.Radius); _LightPositionWS.SetValue(light.Position); graphicsDevice.ScissorRectangle = new Rectangle(0, light.ShadowResolution * (int)cubeMapFace, light.ShadowResolution, light.ShadowResolution); meshMaterialLibrary.Draw(renderType: MeshMaterialLibrary.RenderType.ShadowOmnidirectional, viewProjection: lightViewProjection, lightViewPointChanged: true, hasAnyObjectMoved: light.HasChanged, renderModule: this); } } else { bool draw = false; for (int i = 0; i < 6; i++) { // render the scene to all cubemap faces cubeMapFace = (CubeMapFace)i; switch (cubeMapFace) { case CubeMapFace.NegativeX: lightViewProjection = light.LightViewProjectionNegativeX; break; case CubeMapFace.NegativeY: lightViewProjection = light.LightViewProjectionNegativeY; break; case CubeMapFace.NegativeZ: lightViewProjection = light.LightViewProjectionNegativeZ; break; case CubeMapFace.PositiveX: lightViewProjection = light.LightViewProjectionPositiveX; break; case CubeMapFace.PositiveY: lightViewProjection = light.LightViewProjectionPositiveY; break; case CubeMapFace.PositiveZ: lightViewProjection = light.LightViewProjectionPositiveZ; break; } if (_boundingFrustumShadow != null) { _boundingFrustumShadow.Matrix = lightViewProjection; } else { _boundingFrustumShadow = new BoundingFrustum(lightViewProjection); } bool hasAnyObjectMoved = meshMaterialLibrary.FrustumCulling(entities, _boundingFrustumShadow, false, light.Position); if (!hasAnyObjectMoved) { continue; } if (!draw) { graphicsDevice.SetRenderTarget(light.ShadowMap); draw = true; } graphicsDevice.Viewport = new Viewport(0, light.ShadowResolution * (int)cubeMapFace, light.ShadowResolution, light.ShadowResolution); //_graphicsDevice.Clear(Color.TransparentBlack); //_graphicsDevice.Clear(ClearOptions.DepthBuffer, Color.White, 0, 0); graphicsDevice.ScissorRectangle = new Rectangle(0, light.ShadowResolution * (int)cubeMapFace, light.ShadowResolution, light.ShadowResolution); meshMaterialLibrary.Draw(renderType: MeshMaterialLibrary.RenderType.ShadowOmnidirectional, viewProjection: lightViewProjection, lightViewPointChanged: light.HasChanged, hasAnyObjectMoved: true, renderModule: this); } } }