Skip to main content
Tweeted twitter.com/#!/StackGameDev/status/369656666364862464
added 2284 characters in body
Source Link
Hasse Iona
  • 157
  • 2
  • 9

Edit: Thanks to a lot of help in the chat from LuisW I now have a working greyscale shader, I didnt have to change anything about my GreyscalePixelShader but I did have to remove the second pass of the simple techique and put it into a seperate technique, as such:

technique Greyscale
{
    pass Pass0
    {
        PixelShader = compile ps_2_0 GrayscalePixelShader();
    }
}

I also had to update my XNA/C# Draw method to create a fullscreen quad that I rendered my scene with the first technique to so I could use my greyscale technique on that quad as an input. Mind you, the method Im using, with spritebatch, is known to be slow (for me that didnt matter, but if you need fast, then drawing the quad directly is supposably faster) My updated draw method:

protected override void Draw(GameTime gameTime)
{
    this.device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.DeepSkyBlue, 1.0f, 0);

    PresentationParameters pp = this.device.PresentationParameters;
    RenderTarget2D renderTarget;
    renderTarget = new RenderTarget2D(this.device, pp.BackBufferWidth, pp.BackBufferHeight, true, pp.BackBufferFormat, pp.DepthStencilFormat);
    this.device.SetRenderTarget(renderTarget);
    this.device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.DeepSkyBlue, 1.0f, 0);
    Rectangle screenRectangle = new Rectangle(0, 0, screenWidth, screenHeight);

    // Get the model's only mesh
    ModelMesh mesh = this.model.Meshes[0];
    Effect effect = mesh.Effects[0];

    effect.CurrentTechnique = effect.Techniques["Simple"];

    //<left out some noninteresting worldmatrix calculations here not relevent for this question/answer> 

    device.DepthStencilState = DepthStencilState.Default;
    device.BlendState = BlendState.Opaque;

    mesh.Draw();

    this.device.SetRenderTarget(null);

    effect.CurrentTechnique = effect.Techniques["Greyscale"];

    spriteBatch.Begin(0, BlendState.Opaque, null, null, null, effect);
    spriteBatch.Draw((Texture2D)renderTarget, screenRectangle, Color.White);
    spriteBatch.End();

    base.Draw(gameTime);
}

Edit: Thanks to a lot of help in the chat from LuisW I now have a working greyscale shader, I didnt have to change anything about my GreyscalePixelShader but I did have to remove the second pass of the simple techique and put it into a seperate technique, as such:

technique Greyscale
{
    pass Pass0
    {
        PixelShader = compile ps_2_0 GrayscalePixelShader();
    }
}

I also had to update my XNA/C# Draw method to create a fullscreen quad that I rendered my scene with the first technique to so I could use my greyscale technique on that quad as an input. Mind you, the method Im using, with spritebatch, is known to be slow (for me that didnt matter, but if you need fast, then drawing the quad directly is supposably faster) My updated draw method:

protected override void Draw(GameTime gameTime)
{
    this.device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.DeepSkyBlue, 1.0f, 0);

    PresentationParameters pp = this.device.PresentationParameters;
    RenderTarget2D renderTarget;
    renderTarget = new RenderTarget2D(this.device, pp.BackBufferWidth, pp.BackBufferHeight, true, pp.BackBufferFormat, pp.DepthStencilFormat);
    this.device.SetRenderTarget(renderTarget);
    this.device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.DeepSkyBlue, 1.0f, 0);
    Rectangle screenRectangle = new Rectangle(0, 0, screenWidth, screenHeight);

    // Get the model's only mesh
    ModelMesh mesh = this.model.Meshes[0];
    Effect effect = mesh.Effects[0];

    effect.CurrentTechnique = effect.Techniques["Simple"];

    //<left out some noninteresting worldmatrix calculations here not relevent for this question/answer> 

    device.DepthStencilState = DepthStencilState.Default;
    device.BlendState = BlendState.Opaque;

    mesh.Draw();

    this.device.SetRenderTarget(null);

    effect.CurrentTechnique = effect.Techniques["Greyscale"];

    spriteBatch.Begin(0, BlendState.Opaque, null, null, null, effect);
    spriteBatch.Draw((Texture2D)renderTarget, screenRectangle, Color.White);
    spriteBatch.End();

    base.Draw(gameTime);
}
added 171 characters in body; edited title
Source Link
House
  • 73.5k
  • 17
  • 188
  • 276

Postprocessing: HLSL grayscale pixelshaderpixel shader: gettingHow to get the original colors of the model doesnt work?

The following code gives me a completely black colored model, while it should be a model shaded in greyscale:

float4 SimplePixelShader(VertexShaderOutput input) : COLOR0 { //some lambertian shading done in the code I left out here

float4 SimplePixelShader(VertexShaderOutput input) : COLOR0
{
    //some lambertian shading done in the code I left out here
    
    return color;

}

sampler TextureSampler = sampler_state { Texture = ; };

float4 GrayscalePixelShader(float2 TextureCoordinate : TEXCOORD0) : COLOR0 { // Get the color. float4 color = tex2D(TextureSampler, TextureCoordinate);

}

sampler TextureSampler = sampler_state
{
    Texture = <ScreenTexture>;
};

float4 GrayscalePixelShader(float2 TextureCoordinate : TEXCOORD0) : COLOR0
{
    // Get the color.
    float4 color = tex2D(TextureSampler, TextureCoordinate); 
    
    // Turn pixel to grayscale.
    float grayscale = dot(color.rgb, float3(0.3, 0.59, 0.11));
    color.r = grayscale;
    color.g = grayscale;
    color.b = grayscale;
    color.a = 1.0f;
    
    // Return the result.
    return color;

}

technique Simple {

}

technique Simple
{

    pass Pass0
    {
        VertexShader = compile vs_2_0 SimpleVertexShader();
        PixelShader  = compile ps_2_0 SimplePixelShader();
    }
    
    pass Pass1
    {
        PixelShader  = compile ps_2_0 GrayscalePixelShader();
    }
}

}

As far as I could find out the problem lies here: // Get the color. float4 color = tex2D(TextureSampler, TextureCoordinate);

// Get the color.
float4 color = tex2D(TextureSampler, TextureCoordinate);

theThe whole lambertian coloring works, the grayscale seems to work properly by itself after some testing, but what doesnt seem to work is getting the red lambertian shaded model colors. Anybody any clue what ImI'm doing wrong?

Postprocessing: HLSL grayscale pixelshader: getting original colors of the model doesnt work

The following code gives me a completely black colored model, while it should be a model shaded in greyscale:

float4 SimplePixelShader(VertexShaderOutput input) : COLOR0 { //some lambertian shading done in the code I left out here

return color;

}

sampler TextureSampler = sampler_state { Texture = ; };

float4 GrayscalePixelShader(float2 TextureCoordinate : TEXCOORD0) : COLOR0 { // Get the color. float4 color = tex2D(TextureSampler, TextureCoordinate);

// Turn pixel to grayscale.
float grayscale = dot(color.rgb, float3(0.3, 0.59, 0.11));
color.r = grayscale;
color.g = grayscale;
color.b = grayscale;
color.a = 1.0f;

// Return the result.
return color;

}

technique Simple {

pass Pass0
{
    VertexShader = compile vs_2_0 SimpleVertexShader();
    PixelShader  = compile ps_2_0 SimplePixelShader();
}

pass Pass1
{
    PixelShader  = compile ps_2_0 GrayscalePixelShader();
}

}

As far as I could find out the problem lies here: // Get the color. float4 color = tex2D(TextureSampler, TextureCoordinate);

the whole lambertian coloring works, the grayscale seems to work properly by itself after some testing, but what doesnt seem to work is getting the red lambertian shaded model colors. Anybody any clue what Im doing wrong?

HLSL grayscale pixel shader: How to get the original colors of the model?

The following code gives me a completely black colored model, while it should be a model shaded in greyscale:

float4 SimplePixelShader(VertexShaderOutput input) : COLOR0
{
    //some lambertian shading done in the code I left out here
    
    return color;
}

sampler TextureSampler = sampler_state
{
    Texture = <ScreenTexture>;
};

float4 GrayscalePixelShader(float2 TextureCoordinate : TEXCOORD0) : COLOR0
{
    // Get the color.
    float4 color = tex2D(TextureSampler, TextureCoordinate); 
    
    // Turn pixel to grayscale.
    float grayscale = dot(color.rgb, float3(0.3, 0.59, 0.11));
    color.r = grayscale;
    color.g = grayscale;
    color.b = grayscale;
    color.a = 1.0f;
    
    // Return the result.
    return color;
}

technique Simple
{

    pass Pass0
    {
        VertexShader = compile vs_2_0 SimpleVertexShader();
        PixelShader  = compile ps_2_0 SimplePixelShader();
    }
    
    pass Pass1
    {
        PixelShader  = compile ps_2_0 GrayscalePixelShader();
    }
}

As far as I could find out the problem lies here:

// Get the color.
float4 color = tex2D(TextureSampler, TextureCoordinate);

The whole lambertian coloring works, the grayscale seems to work properly by itself after some testing, but what doesnt seem to work is getting the red lambertian shaded model colors. Anybody any clue what I'm doing wrong?

Source Link
Hasse Iona
  • 157
  • 2
  • 9

Postprocessing: HLSL grayscale pixelshader: getting original colors of the model doesnt work

The following code gives me a completely black colored model, while it should be a model shaded in greyscale:

float4 SimplePixelShader(VertexShaderOutput input) : COLOR0 { //some lambertian shading done in the code I left out here

return color;

}

sampler TextureSampler = sampler_state { Texture = ; };

float4 GrayscalePixelShader(float2 TextureCoordinate : TEXCOORD0) : COLOR0 { // Get the color. float4 color = tex2D(TextureSampler, TextureCoordinate);

// Turn pixel to grayscale.
float grayscale = dot(color.rgb, float3(0.3, 0.59, 0.11));
color.r = grayscale;
color.g = grayscale;
color.b = grayscale;
color.a = 1.0f;

// Return the result.
return color;

}

technique Simple {

pass Pass0
{
    VertexShader = compile vs_2_0 SimpleVertexShader();
    PixelShader  = compile ps_2_0 SimplePixelShader();
}

pass Pass1
{
    PixelShader  = compile ps_2_0 GrayscalePixelShader();
}

}

As far as I could find out the problem lies here: // Get the color. float4 color = tex2D(TextureSampler, TextureCoordinate);

the whole lambertian coloring works, the grayscale seems to work properly by itself after some testing, but what doesnt seem to work is getting the red lambertian shaded model colors. Anybody any clue what Im doing wrong?