2 registered members (AndrewAMD, ozgur),
1,526
guests, and 8
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Problems with Bump Mapping Shader
#80528
07/06/06 18:55
07/06/06 18:55
|
Joined: Oct 2004
Posts: 150 Germany
tkunze
OP
Member
|
OP
Member
Joined: Oct 2004
Posts: 150
Germany
|
Hello, currently i try to port an bumpmapping shader but i am not able to get a good result. My problem are the 2 lines in the vertex shader (marked in red)where in the original implementation vecLightDir and vecEye is used. I tried to map this to vecSunDir, vecViewDir ... but i always get a result like shown in the pictures above. The next problem is the roof of the house model. It is tiled into 6 surfaces, pointing to the same area in the texture. The shader calculates the light impact for each area and not for the whole roof (The lightning is for each surface dark on the upper right side and bright on the lower left side and not for the whole roof). Is there something what can be changed in the shader or is it necessary that the roof is only one surface to get the proper light calculation. The demo project can be downloaded with Link to BumpMapping Shader (10 MB). Any help is appreciated. Code:
material bumpmapping_version2 { effect " ////////////////////////////////////////////////////////////////////////////////////////////////////// // Define your needed values float4x4 matWorldViewProj; float4x4 matWorld; float4 vecLight; float4 vecSunDir; float4 vecViewDir; float4 vecViewPos; // Define your textures texture entSkin1; texture entSkin2; ////////////////////////////////////////////////////////////////////////////////////////////////////// // Texture settings
sampler ColorMapSampler = sampler_state { Texture = <entSkin1>; MipFilter = Linear; MinFilter = Linear; MagFilter = Linear; AddressU = Clamp; AddressV = Clamp; };
sampler BumpMapSampler = sampler_state { Texture = <entSkin2>; MipFilter = Linear; MinFilter = Linear; MagFilter = Linear; AddressU = Clamp; AddressV = Clamp; };
// Define the output of your vertexshader struct VS_OUT { float4 Pos : POSITION; float2 Tex : TEXCOORD0; float3 Light : TEXCOORD1; float3 View : TEXCOORD2; };
struct VS_IN { float4 Pos : POSITION; float2 Tex : TEXCOORD; float3 Normal : NORMAL; float3 Tangent : TANGENT; };
////////////////////////////////////////////////////////////////////////////////////////////////////// // vertexshader VS_OUT vs( VS_IN In ) { VS_OUT Out = ( VS_OUT ) 0; // Declare your output Out.Pos = mul( In.Pos, matWorldViewProj ); // Transform the ouput to the view float3x3 worldToTangentSpace; worldToTangentSpace[0]=mul(In.Tangent, matWorld); worldToTangentSpace[1]=mul(cross(In.Tangent,In.Normal), matWorld); worldToTangentSpace[2]=mul(In.Normal, matWorld); Out.Tex=In.Tex; float4 PosWorld = mul(In.Pos, matWorld); Out.Light = mul(worldToTangentSpace, vecLight); // In the Original Shader: Out.Light = mul(worldToTangentSpace, vecLightDir); Out.View = mul(worldToTangentSpace, vecViewPos - PosWorld); // In the Original Shader: Out.View = mul(worldToTangentSpace, vecEye - PosWorld); return Out; }
float4 ps( float2 Tex : TEXCOORD0, float3 Light: TEXCOORD1, float3 View : TEXCOORD2 ) : COLOR { float4 Color = tex2D(ColorMapSampler, Tex); float3 Normal = (2*(tex2D(BumpMapSampler,Tex)))-1.0; float3 LightDir = normalize(Light); float3 ViewDir = normalize(View); float Diffuse=saturate(dot(Normal,LightDir)); float3 Reflect = normalize(2*Diffuse * Normal - LightDir); float Specular = pow(saturate(dot(Reflect,ViewDir)),3); return 0.2 * Color + Color * Diffuse + Specular; } ////////////////////////////////////////////////////////////////////////////////////////////////////// // technique mytechnique { // Define your passes pass p0 { VertexShader = compile vs_2_0 vs(); PixelShader = compile ps_2_0 ps(); } } "; }
|
|
|
Re: Problems with Bump Mapping Shader
[Re: versus2]
#80533
08/12/06 12:31
08/12/06 12:31
|
Joined: Jul 2006
Posts: 783 London, UK
sheefo
User
|
User
Joined: Jul 2006
Posts: 783
London, UK
|
Try this code, I took it from the normalmap shaders and took away everything apart from the bumpmapping stuff: Code:
float4x4 matWorldViewProj; float4x4 matWorld; float4 vecLightPos[8]; //light position float4 vecLightColor[8]; //light position float4 vecViewPos;
texture entSkin1; texture entSkin2;
sampler ColorMapSampler = sampler_state { Texture = <entSkin1>; MinFilter = Linear; MagFilter = Linear; MipFilter = Linear; AddressU = wrap; AddressV = wrap; }; sampler BumpMapSampler = sampler_state { Texture = <entSkin2>; MinFilter = Linear; MagFilter = Linear; MipFilter = Linear; AddressU = wrap; AddressV = wrap; };
struct VS_OUTPUT { float4 Pos : POSITION; float2 Tex : TEXCOORD0; float3 Light : TEXCOORD2; float3 View : TEXCOORD3; float3 Att : TEXCOORD4; }; VS_OUTPUT vs(float4 Pos : POSITION, float2 texcoord0 : TEXCOORD0, float3 Normal : NORMAL, float3 Tangent : TEXCOORD0) { VS_OUTPUT Out = (VS_OUTPUT) 0; Out.Pos = mul(Pos, matWorldViewProj); // transform Position
// compute the 3x3 tranform matrix // to transform from world space to tangent space float3x3 worldToTangentSpace; worldToTangentSpace[0] = mul(Tangent, matWorld); worldToTangentSpace[1] = mul(cross(Tangent, Normal), matWorld); worldToTangentSpace[2] = mul(Normal, matWorld); Out.Tex = texcoord0.xy;
float3 PosWorld = mul(Pos, matWorld); float LightRange = 0.00001; //light 1 float3 Light = PosWorld - vecLightPos[1] ; Out.Light.xyz = mul(worldToTangentSpace, -Light); // L float3 Viewer1 = PosWorld - vecViewPos; Out.View = mul(worldToTangentSpace, -Viewer1); // V Out.Att = Light * LightRange; // Point light return(Out); } struct PS_INPUT { float2 Tex : TEXCOORD0; float3 Light : TEXCOORD2; float3 View : TEXCOORD3; float3 Att : TEXCOORD4; }; float4 ps(PS_INPUT psInStruct) : COLOR { float4 color = tex2D(ColorMapSampler, psInStruct.Tex); // fetch color map float3 bumpNormal = 2 * (tex2D(BumpMapSampler, psInStruct.Tex) - 0.5); // fetch bump map float4 gloss = tex2D(BumpMapSampler, psInStruct.Tex); //light1 float3 LightDir1 = normalize(psInStruct.Light); float3 ViewDir1 = normalize(psInStruct.View); float4 diff1 = saturate(dot(bumpNormal, LightDir1)); // diffuse component float shadow1 = saturate(4 * diff1); float3 Reflect1 = normalize(2 * diff1 * bumpNormal - LightDir1); // R float4 spec1 = pow(saturate(dot(Reflect1, ViewDir1)), 15); float4 Attenuation1 = saturate(dot(psInStruct.Att, psInStruct.Att));
return((0.3 * color) + ((shadow1 * (color * diff1 + (spec1 * gloss.w)) * (1 - Attenuation1)) * vecLightColor[1])); } //////////////////////////////////////////////////////////// // Bumpmap //////////////////////////////////////////////////////////// technique bumpmap { pass p0 { AlphaBlendEnable = false; SrcBlend = zero;
VertexShader = compile vs_2_0 vs(); PixelShader = compile ps_2_0 ps(); } }
|
|
|
Re: Problems with Bump Mapping Shader
[Re: sheefo]
#80534
08/17/06 19:06
08/17/06 19:06
|
Joined: Jun 2006
Posts: 11
versus2
Newbie
|
Newbie
Joined: Jun 2006
Posts: 11
|
|
|
|
|