Tuesday, January 29, 2013

Transparency Shader


Transparency Shader

Thank you to RB Whitaker his fabulous blog I wrote a transparency shader for XNA:

I am going to use this in a game I am working for XBLIG called Ninja Avatar: The Trials. We want to give the player the ability to look through buildings and find something. I will use this shader two-fold. I will give the unwanted buildings a regular transparency, then I will give the object that the player wants a pinkish glow.  I am very excited!



float4x4 World;
float4x4 View;
float4x4 Projection;
float4x4 WorldInverseTranspose;

float4 AmbientColor = float4(1, 1, 1, 1);
float AmbientIntensity = 0.5;

float3 DiffuseLightDirection = float3(1, 0, 0);
float4 DiffuseColor = float4(6, 1, 1, 1);
float DiffuseIntensity = 1.0;

float Shininess = 100;
float4 SpecularColor = float4(1, 1, 1, 1);
float SpecularIntensity = .1;
float3 ViewVector = float3(1, 0, 0);

float Transparency = 0.5;

texture ModelTexture;
sampler2D textureSampler = sampler_state {
    Texture = (ModelTexture);
    MinFilter = Linear;
    MagFilter = Linear;
    AddressU = Wrap;
    AddressV = Wrap;
};

struct VertexShaderInput
{
    float4 Position : POSITION0;
    float4 Normal : NORMAL0;
    float2 TextureCoordinate : TEXCOORD0;
};

struct VertexShaderOutput
{
    float4 Position : POSITION0;
    float4 Color : COLOR0;
    float3 Normal : TEXCOORD0;
    float2 TextureCoordinate : TEXCOORD1;
};

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
    VertexShaderOutput output;

    float4 worldPosition = mul(input.Position, World);
    float4 viewPosition = mul(worldPosition, View);
    output.Position = mul(viewPosition, Projection);

    float4 normal = normalize(mul(input.Normal, WorldInverseTranspose));
    float lightIntensity = dot(normal, DiffuseLightDirection);
    output.Color = saturate(DiffuseColor * DiffuseIntensity * lightIntensity);

    output.Normal = normal;

    output.TextureCoordinate = input.TextureCoordinate;
    return output;
}

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
    float3 light = normalize(DiffuseLightDirection);
    float3 normal = normalize(input.Normal);
    float3 r = normalize(2 * dot(light, normal) * normal - light);
    float3 v = normalize(mul(normalize(ViewVector), World));
    float dotProduct = dot(r, v);

    float4 specular = SpecularIntensity * SpecularColor * max(pow(dotProduct, Shininess), 0) * length(input.Color);

    float4 textureColor = tex2D(textureSampler, input.TextureCoordinate);
    textureColor.a = 1;

    float4 color = saturate(textureColor * (input.Color) + AmbientColor * AmbientIntensity + specular);
    color.a = Transparency;
    return color;
}

technique Textured
{
    pass Pass1
    {
        AlphaBlendEnable = TRUE;
        DestBlend = INVSRCALPHA;
        SrcBlend = SRCALPHA;
        VertexShader = compile vs_2_0 VertexShaderFunction();
        PixelShader = compile ps_2_0 PixelShaderFunction();
    }
}

Better Late Than Never!

I am finally posting after a cold winter break. I spent a lot of time playing Dota2, steam sales, building my portfolio, looking for an intership, writing shaders, and particle systems. I have found over the break that companies are really looking for strong skills. I have skills with a variety of things from computer science to film to shaders to animations, but they really wanted to see what I was good at. I am taking that advice and working sharders and special effects for this semester. I was able to use programs like FX composer and visual studio to help write shaders and special effects.

Currently I have an ambient, diffuse, normal map, specular, and transparent shaders. I have also made a cool cartoon like fire effect. I am also working on directing the art team with animations, sounds, and level design. This is going to be an exciting semester.

I recently went to Disneyland for my wife's half marathon. It was a lot of fun! It is such a refreshing experience for an artist or animator to walk around the park and see all the time put it into to make you feel immersed in the experience.