Using effects within a SpriteBatch or as a post render effect

Slides:



Advertisements
Similar presentations
2000 Prentice Hall, Inc. All rights reserved. 1 Outline 3.1Introduction 3.2Game Loop Components 3.3How to Implement in C# 3.4Adding Image to XNA Project.
Advertisements

Exploration of advanced lighting and shading techniques
POST-PROCESSING SET09115 Intro Graphics Programming.
Lecture 8 Transparency, Mirroring
Filtering Approaches for Real-Time Anti-Aliasing
Image Data Representations and Standards
Grey Level Enhancement Contrast stretching Linear mapping Non-linear mapping Efficient implementation of mapping algorithms Design of classes to support.
Spatial Filtering (Chapter 3)
Texture Mapping. Texturing  process that modifies the appearance of each point on a surface using an image or function  any aspect of appearance can.
Real-Time Rendering TEXTURING Lecture 02 Marina Gavrilova.
CHAPTER 10 Alpha Blending and Fog © 2008 Cengage Learning EMEA.
3.1. G RAPHICS I The use of images within games. Reflections and advice on the games proposed in the Week 2 Hand-in.
Real-Time Rendering SPEACIAL EFFECTS Lecture 03 Marina Gavrilova.
Video Object Tracking and Replacement for Post TV Production LYU0303 Final Year Project Spring 2004.
Introduction to Shader Programming
Processing Digital Images. Filtering Analysis –Recognition Transmission.
Advanced lighting and rendering Multipass rendering.
Lighting & Shading.
Computer Science – Game DesignUC Santa Cruz Adapted from Jim Whitehead’s slides Shaders Feb 18, 2011 Creative Commons Attribution 3.0 (Except copyrighted.
Queensland University of Technology CRICOS No J INB382/INN382 Real-Time Rendering Techniques Lecture 13: Revision Ross Brown.
Basic Graphics Concepts Day One CSCI 440. Terminology object - the thing being modeled image - view of object(s) on the screen frame buffer - memory that.
Introduction to Nikola Mihaylov, Sep 1, 2008.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
CHAPTER 4 Images XNA Game Studio 4.0. Objectives Find out how the Content Manager lets you add pictures to Microsoft XNA games. Discover how pictures.
GPU Programming Robert Hero Quick Overview (The Old Way) Graphics cards process Triangles Graphics cards process Triangles Quads.
Tools for Raster Displays CVGLab Goals of the Chapter To describe pixmaps and useful operations on them. To develop tools for copying, scaling, and rotating.
Computer Graphics Texture Mapping
Getting Started. XNA Game Studio 4.0 To download XNA Game Studio 4.0 itself, go to XNA Game.
Using Namepsaces  This section lists the namespaces that the application will be using frequently. Saves the programmer from specifying a fully qualified.
Developing the Game User Interface (UI) Lesson 5.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 5 Working with Images Starting Out with Games & Graphics in.
9.3. P ARTICLE S YSTEMS Development of a particle system.
Computer Graphics Bitmaps & Sprites CO2409 Computer Graphics Week 3.
3.2. G RAPHICS I Alpha blending within games. An exploration of the use of alpha blending within games.
Computer Graphics The Rendering Pipeline - Review CO2409 Computer Graphics Week 15.
AdeptSight Image Processing Tools Lee Haney January 21, 2010.
Advanced Computer Graphics Advanced Shaders CO2409 Computer Graphics Week 16.
XNA Game Studio 4.0 Keyboard and Mouse Controls + more on Animated Sprites.
11 Making a Sprite Session 4.2. Session Overview  Describe the principle of a game sprite, and see how to create a sprite in an XNA game  Learn more.
Introduction to Image processing using processing.
Sprites, User Input, and Collision COSC 315 Fall 2014 Bridget M. Blodgett.
09/16/03CS679 - Fall Copyright Univ. of Wisconsin Last Time Environment mapping Light mapping Project Goals for Stage 1.
2.3. A RCHITECTURAL D ESIGN I Example approach for game screen management.
Emerging Technologies for Games Deferred Rendering CO3303 Week 22.
Computer Graphics Blending CO2409 Computer Graphics Week 14.
1 Graphics CSCI 343, Fall 2015 Lecture 6 Viewing, Animation, User Interface.
1 Graphics CSCI 343, Fall 2015 Lecture 5 Color in WebGL.
2009 GRAPHICS : PROJECT 1 BASED ON DX9 BASICS. Documented by Dongjoon Kim SNU CS Ph.D Course Student Contact : NOTE.
Maths & Technologies for Games Advanced Graphics: Scene Post-Processing CO3303 Week
Graphics Graphics Korea University 1 Jang Sumi Texture Mapping Environment Mapping Stencil Buffer.
1 Graphics CSCI 343, Fall 2015 Lecture 25 Texture Mapping.
Maths & Technologies for Games Graphics Optimisation - Batching CO3303 Week 5.
What are shaders? In the field of computer graphics, a shader is a computer program that runs on the graphics processing unit(GPU) and is used to do shading.
11 Writing Text Session 5.1. Session Overview  Show how fonts are managed in computers  Discover the difference between bitmap fonts and vector fonts.
Playing with Sprites. XNA Game Lifecycle In the faceBall demo program we bounced a smiley face around the graphical display against a background image.
XNA Tutorial 1 For CS134 Lecture. Overview Some of the hard work has already been done for you. If you build and run your game now, the GraphicsDeviceManager.
An Introduction to the Cg Shading Language Marco Leon Brandeis University Computer Science Department.
Rob Miles. Creating a Broken MoodLight An XNA game contains game data which is used by the Draw and Update methods – Update updates the game data – Draw.
Computer Science – Game DesignUC Santa Cruz Tile Engine.
Processing Images and Video for An Impressionist Effect Automatic production of “painterly” animations from video clips. Extending existing algorithms.
Computer Graphics CC416 Lecture 04: Bresenham Line Algorithm & Mid-point circle algorithm Dr. Manal Helal – Fall 2014.
2D Graphics CMT3311. This covers... How to make a transparent sprite How to add a sprite to your project and draw it Properties of sprites and how to.
How to write a Pixel Shader CMT3311. Aim The aim of these slides is to introduce you to enough HLSL that you get a general understanding of pixel shaders.
CHAPTER 5 Text XNA Game Studio 4.0. Objectives Discover how text is drawn using Microsoft XNA. Add some font resources to your XNA program. Draw some.
How to use a Pixel Shader CMT3317. Pixel shaders There is NO requirement to use a pixel shader for the coursework though you can if you want to You should.
DIGITAL SIGNAL PROCESSING
Chapters VIII Image Texturing
Alpha blending within games
Computer Graphics Introduction to Shaders
Development of a particle system
Presentation transcript:

Using effects within a SpriteBatch or as a post render effect 10.1. HLSL Effects I Using effects within a SpriteBatch or as a post render effect

Question Clinic: FAQ In lecture exploration of answers to frequently asked student questions

HLSL Effects Introduction to the use of HLSL effects within a SpriteBatch or to provide a post render effect

HLSL Effects Shaders can be used to provide an interesting range of effects of use within 2D games, including: Special effects (e.g. image warping, shockwaves, etc.). Image enhancement (e.g. edge sharpening, glow, lighting effects, blur effects, etc.)

HLSL Effects (using a SpriteBatch) A SpriteBatch (itself a shader) can be used to support user-defined effects SpriteBatch Effect Begin End Begin End

HLSL Effects (using a SpriteBatch) An effect can be included (with some caveats) within a SpriteBatch, as shown: Multiple effects can be chained within the sprite batch, likewise multiple images can be drawn by each effect. protected override void Draw(GameTime gameTime) { spriteBatch.Begin( SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.None); effect.Begin(); effect.CurrentTechnique.Passes[0].Begin(); spriteBatch.Draw(....); effect.CurrentTechnique.Passes[0].End(); effect.End(); spriteBatch.End(); } The SpriteSortMode must be set to immediate The first technique pass is assumed (a foreach could be used for multiple passes Aside: The SpriteSortMode must be set to Immediate

HLSL Effects (using a SpriteBatch) If using an effect within a SpriteBatch it is only necessary to define a pixel shader as the SpriteBatch will automatically pass in a full-screen quad containing the vertices to be drawn. sampler2D textureSampler : register(s0); float4 PixelShader( float2 texCoord : TEXCOORD0) : COLOR0 { float4 colour = tex2D( textureSampler, texCoord); // Modify pixel colour as desired return colour; } technique effect pass p0 { PixelShader = compile ps_2_0 PixelShader(); A texture sampler is needed to obtain access to the texture drawn within the SpriteBatch. A pixel shader must only be defined, accepting a texture coordinate as input. The technique pass need only define the pixel shader to be used.

HLSL Effects (post-render effects) A post render effect kicks off once the entire screen has been drawn and provides some form of overall effect. This process involves creating a new render target into which the scene is drawn as normal. This render target is then modified by the post render effect and displayed in the back buffer. Terminology: Render target: A buffer where the GPU draws pixels as output from some effect. Back buffer: The default render target, containing the part of video memory that will next be displayed on the screen.

HLSL Effects (post render effects) Declare a suitable render target In each draw Tell GPU to draw to new render target Draw scene as normal Reset render target to back buffer Start sprite batch in immediate mode and start effect and pass. Obtain rendered texture Draw texture (within effect pass) End pass, effect and sprite batch Render Target Back Buffer 1 5 3 2 Sprite Batch 4 Sprite Batch 6 Effect 7

HLSL Effects (post render effects) protected override void Draw( GameTime gameTime) { graphics.GraphicsDevice.SetRenderTarget( 0, renderTarget); sprite.Begin(); // Draw scene as normal sprite.End(); 0, null); renderedTexture = renderTarget.GetTexture(); effect.Being(); // Draw rendered image using effect effect.End(); GraphicsDeviceManager graphics; SpriteBatch spriteBatch; RenderTarget2D renderTarget; Texture2D renderedTexture; renderTarget = new RenderTarget2D( graphics.GraphicsDevice, screenWidth, screenHeight, 0, SurfaceFormat.Color); Tell the GPU to draw to the new render target Define reference to the graphics device manager and a sprite batch for drawing images Draw the scene as normal (drawn to the specified render target) Define a reference to the new render target and a reference to a texture to extract the contents of the render target Tell the GPU to draw to the normal back buffer Extracted the contents of the render target Draw the rendered texture to the screen using the desired effect Create a new render target, matched to the screen size (some effects might use a larger/small size). The surface format is also matched to that of the screen.

Example HLSL Effects Selection of simple HLSL effects

HLSL Effects (colour changes) Different forms of colour enhancement, e.g. strengthening or weakening the red-green-blue channels, can be applied to provide different effects, such as: producing a ‘night-time’ colour scheme, scene transitions, etc. coloured flashes for events, e.g. red flash or red tint for low health, ‘power’ effects, etc. Aside: SpriteBatch.Draw() colour parameter can be used for most basic colour change effects.

Aside: It is assumed that strength is in the range 0-1 HLSL Effects (colour changes) float4 colourReduction(float2 texCoord : TEXCOORD0) : color0 { float4 colour; colour = tex2D(texCoordSampler, texCoord); colour.r *= strength; return colour; } Aside: It is assumed that strength is in the range 0-1 float4 colourSaturation(float2 texCoord : TEXCOORD0) : color0 { float4 colour; colour = tex2D(texCoordSampler, texCoord); colour.g /= strength; return colour; } Colours will be automatically clamped to a maximum/minimum value of 1 and 0 float4 multiColourEffect(float2 texCoord : TEXCOORD0) : color0 { float4 colour; colour = tex2D(texCoordSampler, texCoord); colour.rg /= strength; return colour; } Any combination of the colour.rgba channels can be modified.

HLSL Effects (colour changes) Other possibly interesting colour effects include: Producing a grey-scale image (potentially combined with additional ‘noisy’ effects to provide an old film visual effect). Inverting the colours in the source image.

HLSL Effects (colour changes) float4 greyscale(float2 texCoord : TEXCOORD0) : color0 { float4 colour = tex2D( texCoordSampler, texCoord); colour.rgb = dot(colour.rgb, float3(0.3, 0.59, 0.11)); return( colour ); } In order to produce a grey-scale the RGB components are not averaged as the eye is not uniformly sensitive to each colour (instead green is weighted strongest, followed by red and finally blue). float4 negative(float2 texCoord : TEXCOORD0) : color0 { float4 colour = tex2D(texCoordtSampler, texCoord.xy); colour.rgb = 1 - colour.rgb; return colour; } The inverse is simply found by subtracting the RGB from 1, with the alpha channel remaining unchanged.

HLSL Effects (scale) A shader can also be used to zoom-in/out on a particular region of a defined texture. float4 scale(float2 texCoord : TEXCOORD0) : color0 { texCoord *= strength; float4 colour = tex2D(texCoordSampler, texCoord); return colour; } As strength ranges from 0 to 1, the texture lookup coordinate is reduced, effectively sampling from a smaller region of the source texture.

HLSL Effects (waves) By using a sinusoidal function it becomes possible to introduce a wave-like pattern of sampling, providing: ‘Heat-haze’ from hot objects. A water effect (either applied to certain ‘water’ textures or the whole scene.

HLSL Effects (waves) float4 water(float2 texCoord : TEXCOORD0) : color0 { float scale = 5.0; float magnitude = 0.01; texCoord += sin( scale * (texCoord + offset) ) * magnitude; float4 colour = tex2D(texCoordSampler, texCoord); return colour; } A new texture lookup coordinate is derived based on the sine of the original offset combined with the varying offset. The scale and magnitude settings control the overall ‘strength’ of the effect. float4 wavy(float2 texCoord : TEXCOORD0) : color0 { float scale = 20.0; float magnitude = 0.01; texCoord += sin( scale * (texCoord.x + offset) ) * magnitude; float4 colour = tex2D(texCoordSampler, texCoord); return( colour ); } This effect is nearly the same as the last one, except only .x component of texCoord is used

HLSL Effects (blur) A blur effect can be added by averaging each pixel’s colour using the colours of the surrounding pixels, e.g. for explosion effects, etc. float4 blur(float2 texCoord : TEXCOORD0) : color0 { float4 colour = tex2D( texCoordSampler, float2(texCoord.x+offset, texCoord.y+offset)); colour += tex2D( texCoordSampler, float2(texCoord.x-offset, texCoord.y-offset)); colour += tex2D( texCoordSampler, float2(texCoord.x+offset, texCoord.y-offset)); colour += tex2D( texCoordSampler, float2(texCoord.x-offset, texCoord.y+offset)); colour = colour / 4; return colour; }

HLSL Effects (sharpen) A simple sharpen effect can be obtained by ‘highlighting’ changes in pixel colour. float4 sharpen(float2 texCoord : TEXCOORD0) : color0 { float sharpenAmount = 25.0; float4 colour = tex2D( texCoordSampler, texCoord); colour += tex2D( texCoordSampler, texCoord - 0.0001) * sharpenAmount; colour -= tex2D( texCoordSampler, texCoord + 0.0001) * sharpenAmount; return( colour ); }

HLSL Effects (emboss/chalk) The emboss technique applies a colour tint based on the rate of colour change in the original image (similar to sharpen). A chalk-like effect can also be obtained by using large ‘sharpen’ values.

HLSL Effects (emboss/chalk) float4 emboss(float2 texCoord : TEXCOORD0) : color0 { float sharpAmount = 50.0f; float4 colour = float4( 0.5, 0.5, 0.5, 1.0); colour -= tex2D( texCoordSampler, texCoord - 0.0001) * sharpAmount; colour += tex2D( texCoordSampler, texCoord + 0.0001) * sharpAmount; colour = (colour.r+colour.g+colour.b) / 3.0f; return( colour ); } Specify the base colour for the effect float4 chalk(float2 texCoord : TEXCOORD0) : color0 { float sharpenAmount = 50.0f; float4 colour = tex2D( texCoordSampler, texCoord); colour += tex2D( texCoordSampler, texCoord - 0.001) * sharpenAmount; colour -= tex2D( texCoordSampler, texCoord + 0.001) * sharpenAmount; return( colour ); } This is the same code as for the sharpen effect, but with larger values

To do: Summary Complete Question Clinic Today we explored: How HLSL effects can be incorporated into XNA Introductory forms of pixel shader effects To do: Complete Question Clinic If using XNA, decide if you want to introduce custom effects into your game (more effects will be explored tomorrow)