Petri 的个人资料Dark Codex Studios照片日志列表更多 ![]() | 帮助 |
Dark Codex StudiosShaders, XNA, DirectX, OpenGL, HLSL, GLSL, Cg, game programming, silverlight, demoscene, math and graphics. |
||||||||||||||||||
Takk for besøket! i added you to my bloglist.you have an very good blog with many interest themes ...
9 月 29 日
olav发表:
just added your blog to the blog list at www.spillutvikler.org
keep upp the good work
6 月 15 日
Steffen发表:
Nice to see that you've ported GrillSim to XNA 3! I'm not happy with all the gfx I made, so I'll try to find time to make something new :)
And I didn't understand anything of the DX11 post :p
1 月 19 日
|
11月19日 Getting stable againFinally, my time is starting to get stable. I'm used to traveling every week now, and both the projects I'm on is beginning to get over the "startup period". This means I will get a bit more time and be able to create some tutorials and make some updates to this blog again.
I also have used a lot of time writing a long article for a magazine about XNA Shader Programming. More info about this will be written later, but the article is finished and is in the publishing process. Quite cool! :)
Anyway, the book "3D Game Development with Microsoft Silverlight 3: Beginner's Guide"( using Balder ;) ), by Gastón C. Hillar, is published and can be found here. The book contains a lot of samples, witch can be downloaded from here.
I got a copy of the book myself and have read a lot of it and it's really great. A good read, clear, good examples and very easy to follow. If you are in to Silverlight and want to look at the Silverlight world from a 3D perspective, I would recommend this book. 11月18日 Dark Codex received 100.000 NOK for the development of myBeastAs some of you know, Dark Codex Studio and Tumbleweed is creating the game myBeast for the popular iPhone platform. After a long process, Dark Codex Studio was one of the selected companies to get support from Norsk Filmfond, for the myBeast project. With a sum of 100.000 NOK, the development of myBeast will be a lot easier as Dark Codex is a new DA company.
More information( Norwegian ):
11月6日 Dragon AgeI got my copy of Dragon Age Origins this other day, but are still waiting for the release on PS3..
So far, the game is really great. Back in the earlier days I played a lot of Baldur's Gate and Neverwinter nights and I'm sure this game will be just as great. At least I'm hooked so far.. The graphics is awesome!!!
I'm playing as a Human Rouge, going for archery for a start, but guess I will try all the start stories to see what's the most fun and then move from there.
Anyway, I spent a few days in Finland earlier this week as I'm on a project for Nokia, and will be going back there tomorrow so it won't be much time for gaming.. Guess Dragon Age got to wait a few more days..
10月31日 Happy Halloween10月21日 Lack of timeSo, it's been a while since last time I was blogging. This is because I have been VERY busy with work as I'm rolling of the Telenor Way of Work project. From next week I will work more with UI/UX both in Norway and Finland. Looking forward to this :) 10月4日 XNA Shader Programming - Tutorial 24, BloomXNA Shader Programming
Tutorial 24 - Bloom post process shader
Welcome to the XNA Shader Programming Tutorial 24. Today, we are going to implement a bloom effect. The shader will be a post process shader, and will bloom any given texture. I learned blooming from http://xna.creators.com, so this implementation is based on this example.
What is Blooming?
Well, blooming occurs when colors flows over to surrounding pixels, brightening or overexposing them in the process. The dark pixels on the edges of bright pixels will get affected by the bright pixels, making the dark pixels lose some details. Fig 24.2 shows the difference between a scene with bloom (left) and one without bloom (right).
Fig 24.2 - Difference between bloom an non-bloom scene
We will need a few render targets to archive the bloom effect. First is the original scene, then we will one texture that will contain the blooming and two textures that will contain the blurred version of the bloom scene.
The 1st blur texture will blur the bloom texture once, and the 2nd blur texture will blur the 1st blur texture, creating a better looking blur. Our blur shader is not the best way of blurring but its good enough. If you want to create a more advances blur effect, look up “Gaussian blur”.
The first thing we will do is to render the normal scene to a texture. Next, we need to extract the bright areas in the original scene and store it in another texture named BloomTexture. We will use a shader to extract the bright colors. What this shader will do is to get a pixel from the original scene, and based on a threshold variable, calculate if this color is bright enough for the blooming effect.
Threshold will contain a value between 0.0 and 1.0, and is used to remove colorinformation from Color that is below Threshold.
Fig 24.3 contains our bloom texture, based on the original scene with a Threshold of 0,3f. As you can see, the scene removes everything dark( based on Threshold ) and keeps the rest. sampler TextureSampler : register(s0);
// Get the threshold of what brightness level we want to glow float Threshold = 0.3;
float4 PixelShader(float2 texCoord : TEXCOORD0) : COLOR0 { float4 Color = tex2D(TextureSampler, texCoord);
// Get the bright areas that is brighter than Threshold and return it. return saturate((Color - Threshold) / (1 - Threshold)); }
technique Bloom { pass P0 { // A post process shader only needs a pixel shader. PixelShader = compile ps_2_0 PixelShader(); } }
Next, we will need to blur this scene. We will use the same blur effect as we used in tutorial 23 (blur), but this time we will use it two times to blur the scene a bit better. Fig 24.4 shows the 2x blurred version of our blooming texture. Fig 24.4 – Blurred version of our bloom texture
The last thing we will do is to combine the original scene with the blurred bloom texture. We could just have rendered the original scene, and blended the bloom texture over it to combine the original scene with the bloom texture, but this would not look that good and we would have non flexibility in the combination process. So, what does this mean? You guessed right, we will make a new shader that will combine the two textures!
This shader will have four parameters that you can use to control the combination process. These are the intensity of our bloom texture and the original scene texture, and the saturation of the original scene texture and the bloom textures. float BloomIntensity = 1.3;
// Controls the Intensity of the original scene texture float OriginalIntensity = 1.0;
// Saturation amount on bloom float BloomSaturation = 1.0;
// Saturation amount on original scene float OriginalSaturation = 1.0;
We give them a default value, but you can control this from the application that will use this shader.
Next, we create a function that will help us with color saturation (the difference of a color against gray, or its own brightness). This function will saturate a given input color, based on a saturation value and the gray color we used to make a grayscale scene earlier in this article. { // We define gray as the same color we used in the grayscale shader float grey = dot(color, float3(0.3, 0.59, 0.11));
return lerp(grey, color, saturation); }
Let’s move on, and start on the PixelShader function. We start by getting the color from our blurred bloom texture, and the original scene. float4 bloomColor = tex2D(BloomSampler, texCoord);
// Get our original pixel from ColorMap float4 originalColor = tex2D(ColorMapSampler, texCoord);
Next, we use the AdjustSaturation function to adjust the bloomColor and originalColor based on the parameters we added to the shader. bloomColor = AdjustSaturation(bloomColor, BloomSaturation) * BloomIntensity; originalColor = AdjustSaturation(originalColor, OriginalSaturation) * OriginalIntensity;
Using these parameters, you can make the original scene very dark, and only bloomed areas bright and vice versa. You should play around with these values to see how this works!
We continue the pixel shader by darkening the original scene where the bloomColor is bright, so we avoid making these areas burned-out. originalColor *= (1 - saturate(bloomColor));
Now, the final step is to combine the textures. return originalColor + bloomColor;
No magic behind this, just return the two colors added together!
And boom you got bloom! Pretty simple, right? All we do is really just combining a few simple shaders to create an advances effect.
Listing 11 – CombineBloom.fx // BloomIntensity, OriginalIntensity, BloomSaturation and OriginalSaturation is used // to control the blooming effect. // This shader is based on the example in creators.xna.com, where I learned this technique.
// Our bloom texture sampler BloomSampler : register(s0);
// Our original SceneTexture texture ColorMap;
// Create a sampler for the ColorMap texture using lianear filtering and clamping sampler ColorMapSampler = sampler_state { Texture = <ColorMap>; MinFilter = Linear; MagFilter = Linear; MipFilter = Linear; AddressU = Clamp; AddressV = Clamp; };
// Controls the Intensity of the bloom texture float BloomIntensity = 1.3;
// Controls the Intensity of the original scene texture float OriginalIntensity = 1.0;
// Saturation amount on bloom float BloomSaturation = 1.0;
// Saturation amount on original scene float OriginalSaturation = 1.0;
float4 AdjustSaturation(float4 color, float saturation) { // We define gray as the same color we used in the grayscale shader float grey = dot(color, float3(0.3, 0.59, 0.11));
return lerp(grey, color, saturation); }
float4 PixelShader(float2 texCoord : TEXCOORD0) : COLOR0 { // Get our bloom pixel from bloom texture float4 bloomColor = tex2D(BloomSampler, texCoord);
// Get our original pixel from ColorMap float4 originalColor = tex2D(ColorMapSampler, texCoord);
// Adjust color saturation and intensity based on the input variables to the shader bloomColor = AdjustSaturation(bloomColor, BloomSaturation) * BloomIntensity; originalColor = AdjustSaturation(originalColor, OriginalSaturation) * OriginalIntensity;
// make the originalColor darker in very bright areas, avoiding these areas look burned-out originalColor *= (1 - saturate(bloomColor));
// Combine the two images. return originalColor + bloomColor; }
technique BloomCombine { pass P0 { // A post process shader only needs a pixel shader. PixelShader = compile ps_2_0 PixelShader(); } }
YouTube - XNA Shader programming, Tutorial 24 - Bloom post process Source and Executable: Tutorial 24 - Bloom XNA Shader Programming - Tutorial 23, Blur post processXNA Shader Programming
Tutorial 23 - Blur post process shader
Welcome to the XNA Shader Programming Tutorial 23. Today, we are going to implement a simple version of blur. The shader will be a post process shader, an will blur any given texture.
Implementing the Shader
To blur a scene, we will need to implement a new post process shader that will take the average of a number of the pixels neighbor pixels, and return that color as the color we want the player to see. The blur shader will have a distance variable that will be used to modify the texture coordinate we will use as a lookup texture containing our normal scene, so it takes the top-left, top-right, bottom-left and bottom-right pixel, adds them together and divides it by 4 so we get the average.
On the image, you can see the blur shader in effect. The shader takes the average of the surrounding pixels with a given distance (in this case 0.002) set in the shader or in the application.
Fig 23.2 - This shows how the blur shader works( From tutorial 19 )
The following piece of code looks up the pixels surrounding the current pixel that is being processed.
Color = tex2D( ColorMapSampler, float2(Tex.x+BlurDistance, Tex.y+BlurDistance)); Color += tex2D( ColorMapSampler, float2(Tex.x-BlurDistance, Tex.y-BlurDistance));
Color += tex2D( ColorMapSampler, float2(Tex.x+BlurDistance, Tex.y-BlurDistance));
Color += tex2D( ColorMapSampler, float2(Tex.x-BlurDistance, Tex.y+BlurDistance));
We simply just modify the pixel to grab the surrounding pixels and add it to our color. Next, we need to divide color by 4 so we get the average color.
Color = Color / 4; Listing 23.1 – Blur post process shader
// The blur amount( how far away from our texel will we look up neighbour texels? ) float BlurDistance = 0.002f;
// This will use the texture bound to the object( like from the sprite batch ).
sampler ColorMapSampler : register(s0);
float4 PixelShader(float2 Tex: TEXCOORD0) : COLOR
{
float4 Color;
// Get the texel from ColorMapSampler using a modified texture coordinate. This
// gets the texels at the neighbour texels and adds it to Color.
Color = tex2D( ColorMapSampler, float2(Tex.x+BlurDistance, Tex.y+BlurDistance));
Color += tex2D( ColorMapSampler, float2(Tex.x-BlurDistance, Tex.y-BlurDistance));
Color += tex2D( ColorMapSampler, float2(Tex.x+BlurDistance, Tex.y-BlurDistance));
Color += tex2D( ColorMapSampler, float2(Tex.x-BlurDistance, Tex.y+BlurDistance));
// We need to devide the color with the amount of times we added
// a color to it, in this case 4, to get the avg. color
Color = Color / 4;
// returned the blurred color
return Color;
}
technique PostProcess
{
pass P0
{
// A post process shader only needs a pixel shader.
PixelShader = compile ps_2_0 PixelShader();
}
}
This was a pretty simple blur effect. You can also create blur effects by rendering to a downscaled texture. You can downscale multiple times to create a nice blur. You could also use this shader and blur a texture multiple times to create a better looking blur.
I have added a line crossing the scene, where the left side of the line is the blurred scene, and the right side of the screen is the original scene.
I will wite a tutorial on Gaussian Blur later, as this blur effect is not very nice. But to get better results with this shader, you can blur a scene multiple times using this shader.
Source and Executable: Tutorial 23 - Blur |
|
||||||||||||||||
|
|