游戏开发论坛

 找回密码
 立即注册
搜索
查看: 7443|回复: 9

高光贴图+法线贴图

[复制链接]

25

主题

145

帖子

341

积分

中级会员

Rank: 3Rank: 3

积分
341
发表于 2006-2-27 16:09:00 | 显示全部楼层 |阅读模式
通过网上资料及高手指点得出:
sf_200622716849.jpg

25

主题

145

帖子

341

积分

中级会员

Rank: 3Rank: 3

积分
341
 楼主| 发表于 2006-2-27 16:10:00 | 显示全部楼层

Re: 高光贴图+法线贴图

a
sf_200622716946.jpg

38

主题

1581

帖子

1614

积分

金牌会员

Rank: 6Rank: 6

积分
1614
QQ
发表于 2006-2-27 16:33:00 | 显示全部楼层

Re:高光贴图+法线贴图

不错!
高光不是很明显啊

38

主题

275

帖子

281

积分

中级会员

Rank: 3Rank: 3

积分
281
QQ
发表于 2006-2-27 19:14:00 | 显示全部楼层

Re:高光贴图+法线贴图

~~不懂~~~偶还没有开始学3D

64

主题

855

帖子

856

积分

高级会员

Rank: 4

积分
856
QQ
发表于 2006-2-28 10:22:00 | 显示全部楼层

Re:高光贴图+法线贴图

嗯,再加上OffsetMap和BumpShadow就更好了

25

主题

145

帖子

341

积分

中级会员

Rank: 3Rank: 3

积分
341
 楼主| 发表于 2006-3-2 11:20:00 | 显示全部楼层

Re: 高光贴图+法线贴图

加入了shadowmap
sf_200632112027.jpg

25

主题

145

帖子

341

积分

中级会员

Rank: 3Rank: 3

积分
341
 楼主| 发表于 2006-5-17 09:48:00 | 显示全部楼层

Re: 高光贴图+法线贴图

最近整理好了.fx文件,公布一下
float       SpecalurPow = 8;
float4      posLight : POSITION = { 100, 1000, -1000, 1.0 }; // light(world space)
float4      colorLight = {1.0, 1.0, 1.0, 1.0 };    // light's color
float4      colorAmbient = {0.2, 0.2, 0.2, 0.2 };  // ambient light color
float3      posCamera : CAMERAPOSITION;            // camera position
float4x4    matWorld : WORLDMATRIXARRAY;
float4x4    matInvWorld;// : MATRIX;
float4x4        matViewProj : WORLDVIEWPROJECTION;

texture tex_basetex;
texture tex_normalmap;
texture tex_specularmap;

///////////////////////////////////////////////////////
struct VS_INPUT_SN
{
    float3  Pos             : POSITION;
    float3  Normal          : NORMAL;
    float2  Tex0            : TEXCOORD0;
    float3  Tangent                        : TANGENT;
    float3  BiNormal                : BINORMAL;
};


struct VS_OUTPUT_SN
{
    float4  Pos             : POSITION;
    float2  Tex0            : TEXCOORD0;
    float3  LightDir                : TEXCOORD1;
    float3  Eye                                : TEXCOORD2;
    float3  HalfAngle                : TEXCOORD3;
};


sampler BaseMapSampler=sampler_state
{
        texture=(tex_basetex);
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};

sampler SpecularMapSampler=sampler_state
{
        texture=(tex_specularmap);
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};

sampler NormalMapSampler=sampler_state
{
        texture=(tex_normalmap);
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};

///////////////////////////////////////////////////////
// specular + normalmap
VS_OUTPUT_SN vshade_sn(VS_INPUT_SN In)
{
    VS_OUTPUT_SN   Out;
   
    float3 Normal = normalize( In.Normal );
    float3 Tangent = normalize( In.Tangent );
    float3 BiNormal = normalize( In.BiNormal );
   
    // TBN 坐标系
    half3x3 objTangentXf;
    objTangentXf[0] = Tangent.xyz;
    objTangentXf[1] = BiNormal.xyz;
    objTangentXf[2] = Normal.xyz;
   
    half4 objSpaceLightPos = mul( posLight, matInvWorld );
    half3 objLightVec = objSpaceLightPos.xyz - In.Pos.xyz;
   
    Out.LightDir.xyz = mul( objTangentXf, objLightVec );
   
    half4 objSpaceEyePos = mul( posCamera, matInvWorld );
    half3 objEyeVec = objSpaceEyePos.xyz - In.Pos.xyz;
    Out.Eye = mul(objTangentXf, objEyeVec);
    Out.HalfAngle = normalize(Out.Eye)+normalize(Out.LightDir.xyz);
    Out.Tex0 = In.Tex0.xy;
   
    Out.Pos = mul( float4(In.Pos, 1.0f), matViewProj );

    return Out;
}


float4 pshade_sn(VS_OUTPUT_SN In):COLOR
{
    float4 baseTexture   = tex2D(BaseMapSampler, In.Tex0);
    float4 specTexture   = tex2D(SpecularMapSampler, In.Tex0);
    float4 bumpTexture   = tex2D(NormalMapSampler, In.Tex0);
    float3 bump = 2*bumpTexture.xyz - 1;
   
    float3 L = normalize( In.LightDir.xyz );                                // light dir
    float3 N = normalize( bump );
    float3 E = normalize( In.Eye.xyz );                                // eye
    float3 H = normalize( In.HalfAngle.xyz );                                // halfangle vector
   
    float NdotL = saturate( dot(N, L) );
    float NdotH = saturate( dot(N, H) );
    float NdotE = saturate( dot(N, E) );
    float EdotH = saturate( dot(E, H) );
   
    float3 Lighting = lit( NdotL, NdotH, SpecalurPow );
    float3 specular = Lighting.z*specTexture;
   
    float4 Out;
    Out.xyz = saturate( colorAmbient.xyz + Lighting.y*colorLight )*baseTexture;
    Out.xyz += specular;
    Out.a = baseTexture.a;
    return Out;
}



technique model_specular_normalmap
{
    pass p0
    {
        vertexshader = compile vs_2_0 vshade_sn();
        pixelshader = compile ps_2_0 pshade_sn();
    }
}

15

主题

1268

帖子

1268

积分

金牌会员

Rank: 6Rank: 6

积分
1268
发表于 2006-5-17 12:43:00 | 显示全部楼层

Re: 高光贴图+法线贴图

gosix
[em3] 太美了,能不能把模型换成生或死3沙滩排球的,这样高光可能明显一些 [em3]

25

主题

145

帖子

341

积分

中级会员

Rank: 3Rank: 3

积分
341
 楼主| 发表于 2006-5-19 08:49:00 | 显示全部楼层

Re:高光贴图+法线贴图

To Civilian, 想法倒是8错啊

25

主题

145

帖子

341

积分

中级会员

Rank: 3Rank: 3

积分
341
 楼主| 发表于 2006-5-19 08:51:00 | 显示全部楼层

Re:高光贴图+法线贴图

加入了shadowmap

//float4      mtrlSpecalur = {1.0, 1.0, 1.0, 1.0 };
int                        bSpecularNormal = 1;                // 是否使用高光贴图+法线贴图
float       SpecalurPow = 8;
float4      posLight : POSITION = { 0.577, 0.577, -0.577, 1.0 }; // light(world space)
float4      colorLight = {1.0, 1.0, 1.0, 1.0 };    // light's color
float4      colorAmbient = {0.2, 0.2, 0.2, 0.2 };  // ambient light color
float3      posCamera : CAMERAPOSITION;            // camera position
//float3      posLookAt : POSITION;
//float4x4    matWorld : WORLDMATRIXARRAY;
float4x4        matWorldView;
float4x4    matInvWorld;// : MATRIX;
//float4x4    matView : WORLDVIEW;
float4x4        matWorldViewProj : WORLDVIEWPROJECTION;

// shadow map
#define                SHADOW_EPSILON 0.0005f
#define                SHADOW_SMAP_SIZE 512
float4x4        matViewToLightProj;
float4x4        matLightViewProj;

texture tex_basetex< string name = "rockwall.dds"; >;
texture tex_normalmap< string name = "aaa.nmf"; >;
texture tex_specularmap< string name = "aaa.smf"; >;
texture tex_shadowmap;

///////////////////////////////////////////////////////
struct VS_INPUT_SN
{
    float3  Pos             : POSITION;
    float3  Normal          : NORMAL;
    float2  Tex0            : TEXCOORD0;
    float3  Tangent                        : TANGENT;
    float3  BiNormal                : BINORMAL;
};


struct VS_OUTPUT_SN
{
    float4  Pos             : POSITION;
    float2  Tex0            : TEXCOORD0;
    float3  LightDir                : TEXCOORD1;
    float3  Eye                                : TEXCOORD2;
    float3  HalfAngle                : TEXCOORD3;
    // shadow map
    float4  PosLight                : TEXCOORD4;
};


sampler BaseMapSampler=sampler_state
{
        texture=<tex_basetex>;
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};

sampler SpecularMapSampler=sampler_state
{
        texture=<tex_specularmap>;
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};

sampler NormalMapSampler=sampler_state
{
        texture=<tex_normalmap>;
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};

sampler ShadowMapSampler=sampler_state
{
        texture=<tex_shadowmap>;
    MinFilter = Point;
    MagFilter = Point;
    MipFilter = Point;
    AddressU = Clamp;
    AddressV = Clamp;
};

///////////////////////////////////////////////////////
// specular + normalmap
VS_OUTPUT_SN vshade_sn(VS_INPUT_SN In)
{
    VS_OUTPUT_SN   Out;
   
    if( bSpecularNormal )                                        // 是否使用高光贴图+法线贴图
    {
                float3 Normal = normalize( In.Normal );
                float3 Tangent = normalize( In.Tangent );
                float3 BiNormal = normalize( In.BiNormal );
            
                // TBN 坐标系
                half3x3 objTangentXf;
                objTangentXf[0] = Tangent.xyz;
                objTangentXf[1] = BiNormal.xyz;
                objTangentXf[2] = Normal.xyz;
            
                half4 objSpaceLightPos = mul( posLight, matInvWorld );
                half3 objLightVec = objSpaceLightPos.xyz - In.Pos.xyz;
            
                Out.LightDir.xyz = mul( objTangentXf, objLightVec );
            
                half4 objSpaceEyePos = mul( posCamera, matInvWorld );
                half3 objEyeVec = objSpaceEyePos.xyz - In.Pos.xyz;
                Out.Eye = mul(objTangentXf, objEyeVec);
                Out.HalfAngle = normalize(Out.Eye)+normalize(Out.LightDir.xyz);
        }
        else
        {
                Out.LightDir = normalize( In.Normal );
                //Out.LightDir = normalize( mul( float4(In.Normal,1.0f), matWorld ) );                        // 记录normal
                Out.Eye = 0.0f;
                Out.HalfAngle = 0.0f;
        }
       
        // shadow map
        Out.PosLight = mul( float4(In.Pos, 1.0f), matWorldView );
        Out.PosLight = mul( Out.PosLight, matViewToLightProj );
       
        Out.Tex0 = In.Tex0.xy;   
    Out.Pos = mul( float4(In.Pos, 1.0f), matWorldViewProj );

    return Out;
}


float4 pshade_sn(VS_OUTPUT_SN In):COLOR
{
    float4 baseTexture   = tex2D(BaseMapSampler, In.Tex0);
   
    // shadow map
        //transform from RT space to texture space.
        float2 ShadowTexC    = 0.5 * In.PosLight.xy / In.PosLight.w + float2( 0.5, 0.5 );
        ShadowTexC.y = 1.0f - ShadowTexC.y;
        float depth = In.PosLight.z / In.PosLight.w;
        // shadow虚化
        float diffuse = (tex2D(ShadowMapSampler, ShadowTexC) + SHADOW_EPSILON < depth )? 1.0f:saturate(0.05f*depth);
   
        float4 Out;
   
    if( bSpecularNormal )                                        // 是否使用高光贴图+法线贴图
    {
                float4 specTexture   = tex2D(SpecularMapSampler, In.Tex0);
                float4 bumpTexture   = tex2D(NormalMapSampler, In.Tex0);
                float3 bump = 2*bumpTexture.xyz - 1;
            
                float3 L = normalize( In.LightDir.xyz );                                // light dir
                float3 N = normalize( bump );
                float3 E = normalize( In.Eye.xyz );                                // eye
                float3 H = normalize( In.HalfAngle.xyz );                                // halfangle vector
            
                float NdotL = saturate( dot(N, L) );
                float NdotH = saturate( dot(N, H) );
                float NdotE = saturate( dot(N, E) );
                float EdotH = saturate( dot(E, H) );
            
                float3 Lighting = lit( NdotL, NdotH, SpecalurPow );
                float3 specular = Lighting.z*specTexture;
            
                Out.xyz = saturate( colorAmbient.xyz + Lighting.y*colorLight )*baseTexture.xyz;
                Out.xyz += specular;
                Out.a = baseTexture.a;
        }
        else
        {
                float3 L = normalize( posLight.xyz );                                // light dir
                float3 N = normalize( In.LightDir );
                float NdotL = saturate( dot(N, L) );
                                 
                Out.xyz = saturate(colorAmbient.xyz + (1-colorAmbient)*NdotL*colorLight*diffuse)*baseTexture.xyz;
                Out.a = baseTexture.a;
        }
    return Out;
}



///////////////////////////////////////////////////////
// shadow map
void VertShadow( float4 Pos : POSITION,
                 float3 Normal : NORMAL,
                 out float4 oPos : POSITION,
                 out float2 Depth : TEXCOORD0 )
{
    // Compute the projected coordinates
    oPos = mul( Pos, matLightViewProj );

    // Store z and w in our spare texcoord
    Depth.xy = oPos.zw;
}

void PixShadow( float2 Depth : TEXCOORD0,
                out float4 Color : COLOR )
{
    // Depth is z / w
    Color = Depth.x / Depth.y;
}


///////////////////////////////////////////////////////
// specular + normalmap
technique model_specular_normalmap
{
    pass p0
    {
        vertexshader = compile vs_2_0 vshade_sn();
        pixelshader = compile ps_2_0 pshade_sn();
    }
}


///////////////////////////////////////////////////////
// shadow map
technique model_shadowmap
{
        pass p0
        {
        vertexshader = compile vs_1_1 VertShadow();
        pixelshader = compile ps_2_0 PixShadow();
        }
}


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-24 12:53

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表