Sign in to follow this  
rdeboer

Fade object out per pixel

Recommended Posts

rdeboer    122
I am trying to fade out an object at a per pixel level. I haven't done many pixel shaders and I can't find much information on how to achieve this. The texdepth instruction is for ps1.4, I am using HLSL and ps2 There is a Depth register. struct PS_OUTPUT { float4 Color[4] : COLOR0; float Depth : DEPTH; }; PS_OUTPUT main(void) { PS_OUTPUT out = (PS_OUTPUT)0; // Shader statements ... // Write up to four pixel shader output colors out.Color[0] = ... out.Color[1] = ... out.Color[2] = ... out.Color[3] = ... // Write pixel depth out.Depth = ... return out; } But thats just writing to the depth register. I need to set the colour to the existing colour and alpha to a value based on the depth or z-buffer value.

Share this post


Link to post
Share on other sites
mattnewport    1038
If you want to access the position from a pixel shader you need to pass it in as an interpolated value through a texture coordinate. In your vertex shader write out the position (or just the z value if that's all you need) to a tex coord output and then you can use it in your pixel shader. Here's an example - I've just modified simple.fx from the DX9 SDK, it should load into EffectEdit:

//
// Simple Lighting Model
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Note: This effect file works with EffectEdit.
//

string XFile = "tiger\\tiger.x"; // model
int BCLR = 0xff202080; // background

// light direction (view space)
float3 lightDir < string UIDirectional = "Light Direction"; > = {0.577, -0.577, 0.577};

// light intensity
float4 I_a = { 0.1f, 0.1f, 0.1f, 1.0f }; // ambient
float4 I_d = { 1.0f, 1.0f, 1.0f, 1.0f }; // diffuse
float4 I_s = { 1.0f, 1.0f, 1.0f, 1.0f }; // specular

// material reflectivity
float4 k_a : MATERIALAMBIENT = { 1.0f, 1.0f, 1.0f, 1.0f }; // ambient
float4 k_d : MATERIALDIFFUSE = { 1.0f, 1.0f, 1.0f, 1.0f }; // diffuse
float4 k_s : MATERIALSPECULAR= { 1.0f, 1.0f, 1.0f, 1.0f }; // specular
float n : MATERIALPOWER = 32.0f; // power

// texture
texture Tex0 < string name = "tiger\\tiger.bmp"; >;

// transformations
float4x4 World : WORLD;
float4x4 View : VIEW;
float4x4 Projection : PROJECTION;

struct VS_OUTPUT
{
float4 Pos : POSITION;
float4 Diff : COLOR0;
float4 Spec : COLOR1;
float2 Tex : TEXCOORD0;
float1 ZPos : TEXCOORD1;
};

VS_OUTPUT VS(
float3 Pos : POSITION,
float3 Norm : NORMAL,
float2 Tex : TEXCOORD0)
{
VS_OUTPUT Out = (VS_OUTPUT)0;

float3 L = -lightDir;

float4x4 WorldView = mul(World, View);

float3 P = mul(float4(Pos, 1), (float4x3)WorldView); // position (view space)
float3 N = normalize(mul(Norm, (float3x3)WorldView)); // normal (view space)

float3 R = normalize(2 * dot(N, L) * N - L); // reflection vector (view space)
float3 V = -normalize(P); // view direction (view space)

Out.Pos = mul(float4(P, 1), Projection); // position (projected)
Out.Diff = I_a * k_a + I_d * k_d * max(0, dot(N, L)); // diffuse + ambient
Out.Spec = I_s * k_s * pow(max(0, dot(R, V)), n/4); // specular
Out.Tex = Tex;
Out.ZPos = 1 - (P.z / 10);

return Out;
}

sampler Sampler = sampler_state
{
Texture = (Tex0);
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
};

float4 PS(
float4 Diff : COLOR0,
float4 Spec : COLOR1,
float2 Tex : TEXCOORD0,
float1 ZPos : TEXCOORD1) : COLOR
{
float4 color = tex2D(Sampler, Tex) * Diff + Spec;
color.a = ZPos;
return color;
}

technique TVertexAndPixelShader
{
pass P0
{
AlphaBlendEnable = TRUE;
SrcBlend = SRCALPHA;
DestBlend = INVSRCALPHA;
// shaders
VertexShader = compile vs_2_0 VS();
PixelShader = compile ps_2_0 PS();
}
}


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this