Env- & Specular Bumpmapping DX9

Posted By: oliver2s

Env- & Specular Bumpmapping DX9 - 09/07/04 21:15

Here is a shader adaption from ATI's Rendermonkey. Its an Environment Bumpmapping shader and a Specular Bumpmapping shader. I know, the most of you can't use this now because the DX9 interface is only non-public beta. The shader needs a DX9 card with VS.2.0 & PS.2.0.
Here's a shot:


and here's a testlevel with the resources (only betatesters can use this at moment):
http://web108.webbox180.server-home.org/Bilder/shader_test.zip
Posted By: TimeOut

Re: Env- & Specular Bumpmapping DX9 - 09/07/04 21:55

Looks very very good. When came 6.3 for the other users?
Posted By: Nadester

Re: Env- & Specular Bumpmapping DX9 - 09/08/04 04:18

You need version 6.30.1 for the DirectX 9 interface - The current public beta is 6.30.0. This effect is quite similiar to the normal mapping shader (the specular bumpmapping at least). Nice job Oliver!
Posted By: Whisper

Re: Env- & Specular Bumpmapping DX9 *DELETED* *DELETED* - 09/08/04 11:21

Post deleted by whisper
Posted By: RAFU

Re: Env- & Specular Bumpmapping DX9 - 09/08/04 12:34

works wonderful.. keep up the great work
Posted By: Nardulus

Re: Env- & Specular Bumpmapping DX9 - 09/08/04 12:37

It works great on my ATI 9700 development machine and my FX Go5200 on my portable.

It looks like the HLSL and DX9 decision is overcoming some of the NVidia problems.....


Ken
Posted By: William

Re: Env- & Specular Bumpmapping DX9 - 09/09/04 11:45

Looks great!
Posted By: Rhuarc

Re: Env- & Specular Bumpmapping DX9 - 09/09/04 11:50

PERDY!



Being able to use HLSL will be very nice .

-Rhuarc
Posted By: Matt_Aufderheide

Re: Env- & Specular Bumpmapping DX9 - 11/21/04 10:12

can anyone explain how this can be converted to use point lights..
Posted By: NMS

Re: Env- & Specular Bumpmapping DX9 - 08/22/05 08:48

Link's broken...
Posted By: Christoph_B

Re: Env- & Specular Bumpmapping DX9 - 08/22/05 10:20

this thread is almost one year old...
Posted By: SFMAT4

Re: Env- & Specular Bumpmapping DX9 - 08/22/05 16:11

Hi,

I have a question about these shaders. Is there a possibillity to insert the alpha channel from the diffuse map? Maybe for using these shader on tga plants or somthing else. Thanks for help!

best regards
SF

Shader:

for main wdl file.
Code:
 
STARTER load_fx
{
effect_load(mat_env_bump,"env_bump.fx");
effect_load(mat_spec_bump,"spec_bump.fx");
}

ACTION test_ent1
{
my.material = mat_env_bump;
}



specular bumpmapping
Code:
 
float bumpiness = { 1.0f }; //Stärke des Bumpmappings
float specular_power = { 128.0f }; //Stärke des Lichts (1 = sehr stark, über 1 schwächer)
float Ka = { 1.0f }; //Gesamthelligkeit
float4 specular = { 1.0f, 0.956737f, 0.670380f, 1.0f }; //Specular Farbe
float4 ambient = { 0.37647f, 0.37647f, 0.24314f, 1.0f }; //Ambient Farbe alt: alles 1.0f
float4 diffuse = { 0.37647f, 0.37647f, 0.24314f, 1.0f }; //Diffuse Farbe alt: 0.839216f, 0.862341f, 0.890411f, 1.0f


float Kd = { 1.0f };
float Ks = { 1.0f };
float4 light_position = { -100.0f, 100.0f, -100.0f, 1.0f };
float4 eye_position = { 0.0f, 0.0f, -10.0f, 1.0f };
float4 vecViewPos;
float4 vecSunDir;
float4 vecAmbient;

float4x4 matWorldViewProj: register(c0);
float4x4 matMtl;

texture entSkin1;
texture entSkin2;

struct VS_INPUT_STRUCT
{
float4 position: POSITION;
float3 normal: NORMAL;
float2 texcoord0: TEXCOORD0;
float3 binormal: TEXCOORD1;
float3 tangent: TEXCOORD2;
};

struct VS_OUTPUT_STRUCT
{
float4 position: POSITION;
float2 bump_map: TEXCOORD0;
float3 light_vector: TEXCOORD1;
float3 half_angle: TEXCOORD2;
};

VS_OUTPUT_STRUCT VS( VS_INPUT_STRUCT vsInStruct )
{
VS_OUTPUT_STRUCT vsOutStruct; //** Declare the output struct

//**-----------------------------------------------------------
//** Calculate the pixel position using the perspective matrix.
//**-----------------------------------------------------------
vsOutStruct.position = mul(vsInStruct.position, matWorldViewProj );

//**----------------------------------------------
//** Pass the bump and base texture coords through
//**----------------------------------------------
vsOutStruct.bump_map = vsInStruct.texcoord0;

//**--------------------------------------------
//** Calculate the light vector in object space,
//** and then transform it into texture space.
//**--------------------------------------------
float3 temp_light_position = mul( vecAmbient, matMtl );
float3 temp_light_vector = temp_light_position - vsInStruct.position;
vsOutStruct.light_vector.x = dot( temp_light_vector, vsInStruct.tangent );
vsOutStruct.light_vector.y = dot( temp_light_vector, vsInStruct.binormal );
vsOutStruct.light_vector.z = dot( temp_light_vector, vsInStruct.normal );

//**-------------------------------------------
//** Calculate the view vector in object space,
//** and then transform it into texture space.
//**-------------------------------------------
float3 temp_eye_position = mul( vecViewPos, matMtl );
float3 temp_view_vector = temp_eye_position - vsInStruct.position;
float3 temp_view_vector2;
temp_view_vector2.x = dot( temp_view_vector, vsInStruct.tangent );
temp_view_vector2.y = dot( temp_view_vector, vsInStruct.binormal );
temp_view_vector2.z = dot( temp_view_vector, vsInStruct.normal );

//**-------------------------
//** Calculate the half angle
//**-------------------------
vsOutStruct.half_angle = vsOutStruct.light_vector + temp_view_vector2;

return vsOutStruct; //** Return the resulting output struct
}

sampler base_map = sampler_state
{
texture=(entSkin1);
MAGFILTER = LINEAR;
MINFILTER = LINEAR;
MIPFILTER = LINEAR;
};

sampler bump_map = sampler_state
{
texture=(entSkin2);
MAGFILTER = LINEAR;
MINFILTER = LINEAR;
MIPFILTER = LINEAR;
};

struct PS_INPUT_STRUCT
{
float2 bump_map: TEXCOORD0;
float3 light_vector: TEXCOORD1;
float3 half_angle: TEXCOORD2;
};

struct PS_OUTPUT_STRUCT
{
float4 color0: COLOR0;
};

//**---------------------------------------------------------
//** Function: main
//** Description: Declare the main entry point for the shader
//** Input: PS_INPUT_STRUCT, derived from the output of
//** the associated vertex shader
//** Returns: PS_OUTPUT_STRUCT
//**---------------------------------------------------------
PS_OUTPUT_STRUCT PS( PS_INPUT_STRUCT psInStruct )
{
PS_OUTPUT_STRUCT psOutStruct; //** Declare the output struct

//**------------------------------------------------------
//** Retreive the base color and bump components from the
//** respective textures, based on the passed bump coords.
//**------------------------------------------------------
float3 base = tex2D( base_map, psInStruct.bump_map );
float3 bump = tex2D( bump_map, psInStruct.bump_map );

//**----------------------------------------------------
//** Normalize the passed vectors from the vertex shader
//**----------------------------------------------------
float3 normalized_light_vector = normalize( psInStruct.light_vector );
float3 normalized_half_angle = normalize( psInStruct.half_angle );

//**--------------------------------------------------------
//** "Smooth out" the bump based on the bumpiness parameter.
//** This is simply a linear interpolation between a "flat"
//** normal and a "bumped" normal. Note that this "flat"
//** normal is based on the texture space coordinate basis.
//**--------------------------------------------------------
float3 smooth = { 0.5f, 0.5f, 1.0f };
bump = lerp( smooth, bump, bumpiness );
bump = normalize( ( bump * 2.0f ) - 1.0f );

//**---------------------------------------------------------
//** These dot products are used for the lighting model
//** equations. The surface normal dotted with the light
//** vector is denoted by n_dot_l. The normal vector
//** dotted with the half angle vector is denoted by n_dot_h.
//**---------------------------------------------------------
float4 n_dot_l = dot( bump, normalized_light_vector );
float4 n_dot_h = dot( bump, normalized_half_angle );

//**--------------------------------------
//** Calculate the resulting pixel color,
//** based on our lighting model.
//** Ambient + Diffuse + Specular
//**--------------------------------------
psOutStruct.color0.rgb =
( base * ambient * Ka ) +
( base * diffuse * Kd * max( 0, n_dot_l ) ) +
( specular * Ks * pow( max( 0, n_dot_h ), specular_power ) );
psOutStruct.color0.a = 1.0f; //** Set the alpha component manually

return psOutStruct; //** Return the resulting output struct
}


technique spec_bump
{
pass Single_Pass
{
VertexShader = compile vs_2_0 VS();
PixelShader = compile ps_2_0 PS();
}
}



environment bumpmapping:
Code:
 
float Ka = { 0.9f }; //Gesamthelligkeit
float reflectance = { 0.5f }; //Stärke des Environment Mappings
float specular_power = { 64.0f }; //Stärke des Lichts (1 = stark, über 1 = schwächer)
float bumpiness = { 0.8f }; //Stärke des Bump Mappings
float4 ambient = { 1.0f, 1.0f, 1.0f, 1.0f }; //Ambient Farbe
float4 diffuse = { 0.839216f, 0.863241f, 0.890411f, 1.0f }; //Diffuse Farbe
float4 specular = { 1.0f, 0.956737f, 0.670380f, 1.0f}; //Specular Farbe


float Kd = { 1.0f };
float Ks = { 1.0f };
float reflection_clarity = { 3.0f };

texture mtlSkin1;
texture mtlSkin2;

texture entSkin1;
texture entSkin2;

float4x4 matWorldViewProj: register(c0);
float4 vecSunDir: register(c8);
float4 vecViewPos: register(c9);
float4x4 matWorldView: register(c10);
float4x4 matViewInv;
float4x4 matMtl;
float4 vecAmbient;


struct VS_INPUT_STRUCT
{
float4 position: POSITION;
float3 normal: NORMAL;
float2 texcoord0: TEXCOORD0;
float3 binormal: TEXCOORD1;
float3 tangent: TEXCOORD2;
};

struct VS_OUTPUT_STRUCT
{
float4 position: POSITION;
float2 bump_map: TEXCOORD0;
float3 light_vector: TEXCOORD1;
float3 half_angle: TEXCOORD2;
float3 basis1: TEXCOORD3;
float3 basis2: TEXCOORD4;
float3 basis3: TEXCOORD5;
};

//**---------------------------------------------------------
//** Function: main
//** Description: Declare the main entry point for the shader
//** Input: VS_INPUT_STRUCT, derived from the stream
//** mapping parameters defined in the workspace
//** Returns: VS_OUTPUT_STRUCT
//**---------------------------------------------------------
VS_OUTPUT_STRUCT VS( VS_INPUT_STRUCT vsInStruct )
{
VS_OUTPUT_STRUCT vsOutStruct; //** Declare the output struct

vsOutStruct.position = mul(vsInStruct.position, matWorldViewProj );
float3 position = mul( vsInStruct.position, matViewInv );

//**----------------------------------------------
//** Pass the bump and base texture coords through
//**----------------------------------------------
vsOutStruct.bump_map = vsInStruct.texcoord0;

//**--------------------------------------------
//** Calculate the light vector in object space,
//** and then transform it into texture space.
//**--------------------------------------------
float3 temp_light_position = mul( vecAmbient, matMtl );
float3 temp_light_vector = temp_light_position - vsInStruct.position;
vsOutStruct.light_vector.x = dot( temp_light_vector, vsInStruct.tangent );
vsOutStruct.light_vector.y = dot( temp_light_vector, vsInStruct.binormal );
vsOutStruct.light_vector.z = dot( temp_light_vector, vsInStruct.normal );

//**-------------------------------------------
//** Calculate the view vector in object space,
//** and then transform it into texture space.
//**-------------------------------------------
float3 temp_eye_position = mul( vecViewPos, matMtl );
float3 temp_view_vector = temp_eye_position - vsInStruct.position;
float3 temp_view_vector2;
temp_view_vector2.x = dot( temp_view_vector, vsInStruct.tangent );
temp_view_vector2.y = dot( temp_view_vector, vsInStruct.binormal );
temp_view_vector2.z = dot( temp_view_vector, vsInStruct.normal );

//**-------------------------
//** Calculate the half angle
//**-------------------------
vsOutStruct.half_angle = vsOutStruct.light_vector + temp_view_vector2;

//**-----------------------------------------
//** Construct the transpose of the tangent
//** space basis vectors, so we can transform
//** the reflection vector from texture space
//** into view space
//**-----------------------------------------
vsOutStruct.basis1.x = vsInStruct.tangent.x;
vsOutStruct.basis1.y = vsInStruct.binormal.x;
vsOutStruct.basis1.z = vsInStruct.normal.x;
vsOutStruct.basis2.x = vsInStruct.tangent.y;
vsOutStruct.basis2.y = vsInStruct.binormal.y;
vsOutStruct.basis2.z = vsInStruct.normal.y;
vsOutStruct.basis3.x = vsInStruct.tangent.z;
vsOutStruct.basis3.y = vsInStruct.binormal.z;
vsOutStruct.basis3.z = vsInStruct.normal.z;

return vsOutStruct; //** Return the resulting output struct
}


sampler base_map = sampler_state
{
texture=(entSkin1);
MAGFILTER = LINEAR;
MINFILTER = LINEAR;
MIPFILTER = LINEAR;
};

sampler bump_map = sampler_state
{
texture=(entSkin2);
MAGFILTER = LINEAR;
MINFILTER = LINEAR;
MIPFILTER = LINEAR;
};

sampler environment_map = sampler_state
{
texture=(mtlSkin2);
};

struct PS_INPUT_STRUCT
{
float2 bump_map: TEXCOORD0;
float3 light_vector: TEXCOORD1;
float3 half_angle: TEXCOORD2;
float3 basis1: TEXCOORD3;
float3 basis2: TEXCOORD4;
float3 basis3: TEXCOORD5;
};

struct PS_OUTPUT_STRUCT
{
float4 color0: COLOR0;
};

//**---------------------------------------------------------
//** Function: main
//** Description: Declare the main entry point for the shader
//** Input: PS_INPUT_STRUCT, derived from the output of
//** the associated vertex shader
//** Returns: PS_OUTPUT_STRUCT
//**---------------------------------------------------------
float4x4 matWorldViewInv;

PS_OUTPUT_STRUCT PS( PS_INPUT_STRUCT psInStruct )
{
PS_OUTPUT_STRUCT psOutStruct;

//**----------------------------------------
//** Get the base and bump map texture coord
//**----------------------------------------
float4 bump_coord = { psInStruct.bump_map, 0.0f, reflection_clarity };

//**------------------------------------------------------
//** Retreive the base color and bump components from the
//** respective textures, based on the passed bump coords.
//**------------------------------------------------------
float3 base = tex2D( base_map, bump_coord );
float3 bump = tex2D( bump_map, bump_coord );

//**--------------------------------------------------
//** Includes MIP bias to help clairify the reflection
//**--------------------------------------------------
float3 reflection_bump = tex2Dbias( bump_map, bump_coord );

//**----------------------------------------------------
//** Normalize the passed vectors from the vertex shader
//**----------------------------------------------------
float3 normalized_light_vector = normalize( psInStruct.light_vector );
float3 normalized_half_angle = normalize( psInStruct.half_angle );

//**--------------------------------------------------------
//** "Smooth out" the bumps based on the bumpiness parameter.
//** This is simply a linear interpolation between a "flat"
//** normal and a "bumped" normal. Note that this "flat"
//** normal is based on the texture space coordinate basis.
//**--------------------------------------------------------
float3 smooth;
smooth.r = 0.5f;
smooth.g = 0.5f;
smooth.b = 1.0f;
bump = lerp( smooth, bump, bumpiness );
bump = normalize( ( bump * 2.0f ) - 1.0f );
reflection_bump = lerp( smooth, reflection_bump, bumpiness );
reflection_bump = normalize( ( reflection_bump * 2.0f ) - 1.0f );

//**---------------------------------------------
//** Translate the reflection normal from texture
//** space into view space, so we can case the
//** reflection vector into an environment map.
//**---------------------------------------------
float3 reflection = reflection_bump;
reflection.x = dot( reflection_bump, psInStruct.basis1 );
reflection.y = dot( reflection_bump, psInStruct.basis2 );
reflection.z = dot( reflection_bump, psInStruct.basis3 );
float3 reflection_vector = mul( matMtl, reflection );
reflection_vector = normalize( reflection_vector );

//**------------------------------------------
//** Calculate the resulting reflection color.
//**------------------------------------------
float3 reflection_color = texCUBE( environment_map, reflection_vector );

//**----------------------------------------------------------
//** The following modifiers are used to enhance the effect of
//** the basic reflection idea. Normally, specular / gloss
//** maps will take the place of these modifiers.
//**----------------------------------------------------------
float3 result_color = lerp( base, reflection_color, base * reflectance );
float3 modified_specular_color = specular * base;
float3 modified_specular_coefficient = Ks * base;

//**---------------------------------------------------------
//** These dot products are used for the lighting model
//** equations. The surface normal dotted with the light
//** vector is denoted by n_dot_l. The normal vector
//** dotted with the half angle vector is denoted by n_dot_h.
//**---------------------------------------------------------
float3 n_dot_l = dot( bump, normalized_light_vector );
float3 n_dot_h = dot( bump, normalized_half_angle );

//**--------------------------------------
//** Calculate the resulting base color,
//** based on our lighting model.
//** Ambient + Diffuse + Specular
//**--------------------------------------
psOutStruct.color0.rgb =
( result_color * ambient * Ka ) +
( result_color * diffuse * Kd * max( 0, n_dot_l ) ) +
( modified_specular_color * modified_specular_coefficient * pow( max( 0, n_dot_h ), specular_power ) );

//**------------------------------------
//** Interpolate the resulting color
//** based on the reflectance parameter.
//**------------------------------------
psOutStruct.color0.a = 1.0f; //** Set the alpha component manually

return psOutStruct; //** Return the resulting output struct
}

technique env_bump
{
pass Single_Pass
{
VertexShader = compile vs_2_0 VS();
PixelShader = compile ps_2_0 PS();
}
}


© 2024 lite-C Forums