游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1261|回复: 1

shader显示颜色不正确

[复制链接]

52

主题

155

帖子

160

积分

注册会员

Rank: 2

积分
160
发表于 2007-4-10 14:14:00 | 显示全部楼层 |阅读模式
我在fx composer 看到颜色是正确的,有材质颜色和高光,但是放到d3d程序里看起来就没有颜色(红色),没有高光了。shader里所有的颜色是硬编码的。请问是什么原因的。

这是shader代码片断:
// light intensity
float4 LightAmbientIntensity    = { 0.8f, 0.8f, 0.8f, 1.0f };    // ambient
float4 GroundColor              = { 0.1f, 0.4f, 0.0f, 1.0f };    // ground
float4 SkyColor                 = { 0.5f, 0.5f, 0.9f, 1.0f };    // sky
float4 LightDiffuseColor        = { 1.0f, 0.9f, 0.8f, 1.0f };    // diffuse
float4 LightSpecularColor       = { 1.0f, 1.0f, 1.0f, 1.0f };    // specular

// material reflectivity
float4 MaterialAmbientIntensity = { 0.5f, 0.5f, 0.5f, 1.0f };    // ambient
float4 MaterialDiffuseColor     = { 0.4f, 0.4f, 0.4f, 1.0f };    // diffuse
float4 MaterialSpecularColor    = { 0.2f, 0.2f, 0.2f, 1.0f };    // specular
int    MaterialSpecularPower    = 32;                            // power


图示茶壶是fx composer里的,草莓水果是程序里的。
sf_2007410141414.jpg

52

主题

155

帖子

160

积分

注册会员

Rank: 2

积分
160
 楼主| 发表于 2007-4-10 14:19:00 | 显示全部楼层

完整的shader



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

// direction of light from sky (view space)
float3 DirFromSky < string UIDirectional = "Direction from Sky"; > = { 0.0f, -1.0f, 0.0f };            

// light intensity
float4 LightAmbientIntensity    = { 0.8f, 0.8f, 0.8f, 1.0f };    // ambient
float4 GroundColor              = { 0.1f, 0.4f, 0.0f, 1.0f };    // ground
float4 SkyColor                 = { 0.5f, 0.5f, 0.9f, 1.0f };    // sky
float4 LightDiffuseColor        = { 1.0f, 0.9f, 0.8f, 1.0f };    // diffuse
float4 LightSpecularColor       = { 1.0f, 1.0f, 1.0f, 1.0f };    // specular

// material reflectivity
float4 MaterialAmbientIntensity = { 0.5f, 0.5f, 0.5f, 1.0f };    // ambient
float4 MaterialDiffuseColor     = { 0.4f, 0.4f, 0.4f, 1.0f };    // diffuse
float4 MaterialSpecularColor    = { 0.2f, 0.2f, 0.2f, 1.0f };    // specular
int    MaterialSpecularPower    = 32;                            // power

// transformations
float4x4 World :WORLD;
float4x4 View :VIEW ;
float4x4 ViewProjection : VIEWPROJECTION;
float3 CameraPos : CAMERAPOSITION;   


// use the TIME semantic to inform EffectEdit to fill in the
//   current time in seconds

float Time : TIME;


texture EnvironmentMap
<
    string type = "CUBE";
    string name = "G:\test\Meshes\lobbycube.dds";
>;

samplerCUBE EnvironmentSampler = sampler_state
{
    Texture = (EnvironmentMap);
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};




//-----------------------------------------------------------------------------
// Name: CubeMapLookup
// Type: Helper function
// Desc: Performs a cubemap texture sample
//-----------------------------------------------------------------------------
float4 CubeMapLookup(float3 CubeTexcoord)
{
    // Goal 3B: Do a cubemap texture lookup
    //
    //   Suggestions:
    //     Use the provided CubeTexcoord and the texCUBE intrinsic to
    //       do the texture lookup
     
    return texCUBE(EnvironmentSampler, CubeTexcoord);
}




//-----------------------------------------------------------------------------
// Name: CalcReflectionVector
// Type: Helper function
// Desc: Calculate the reflection vector based on view and normal vectors
//-----------------------------------------------------------------------------
float3 CalcReflectionVector(float3 ViewToPos, float3 Normal)
{
    // Goal 3C: Calculate the reflection vector here   
    //
    //   The reflection vector can be used to do a cube map
    //     lookup to make a surface appear shiny
    //
    //   Suggestions:
    //     A good way to start is to just return the Normal
    //        until you make sure the cube map lookup is working
    //     Next you can use the reflect intrinsic to reflect
    //        the ViewToPos vector around the Normal vector
    //     Both the ViewToPos and Normal vectors should be
    //        normalized before caluclating reflection (which
    //        has already been done for you in the vertex shader).
   
    return reflect(ViewToPos, Normal);
}




//-----------------------------------------------------------------------------
// Name: CalcVertexAnimation
// Type: Helper function
// Desc: Calculate a per-vertex transformation offset
//-----------------------------------------------------------------------------
float4 CalcVertexAnimation(float4 Offset)
{
    return sin(Time + Offset) * 0.1;      
}




//-----------------------------------------------------------------------------
// Name: CalcHemisphere
// Type: Helper function
// Desc: Calculates the hemispheric lighting term
//-----------------------------------------------------------------------------
float4 CalcHemisphere(float3 Normal, float3 DirToSky, float Occ)
{
    float4 Hemi = MaterialAmbientIntensity;
   
    // occlusion factor
    Hemi *= (1 - Occ);
   
    // calc lerp factor here
    float LerpFactor = (dot(Normal, DirToSky) + 1) / 2;
   
    // lerp between the ground and sky color based on the LerpFactor which
    //   is calculated by the angle between the normal and the sky direction
    Hemi *= lerp(GroundColor, SkyColor, LerpFactor);

    return Hemi;
}




//-----------------------------------------------------------------------------
// Name: CalcAmbient
// Type: Helper function
// Desc: Calculates the ambient lighting term
//-----------------------------------------------------------------------------
float4 CalcAmbient()
{
    return LightAmbientIntensity * MaterialAmbientIntensity;
}




//-----------------------------------------------------------------------------
// Name: CalcDiffuse
// Type: Helper function
// Desc: Calculates the diffuse lighting term
//-----------------------------------------------------------------------------
float4 CalcDiffuse(float3 Normal, float3 DirToLight)
{
    return MaterialDiffuseColor * LightDiffuseColor * max(0, dot(Normal, DirToLight));
}




//-----------------------------------------------------------------------------
// Name: CalcSpecular
// Type: Helper function
// Desc: Calculates the specular lighting term
//-----------------------------------------------------------------------------
float4 CalcSpecular(float3 Normal, float3 DirFromLight, float3 EyeToVertex)
{
    float3 R = normalize(reflect(DirFromLight, Normal));
    return MaterialSpecularColor * LightSpecularColor * pow(max(0, dot(R, -EyeToVertex)), MaterialSpecularPower/4);
}




// vertex shader output structure
struct VS_OUTPUT
{
    float4 Pos  : POSITION;
    float4 Diff : COLOR0;
    float4 Spec : COLOR1;
    float3 CubeTexcoord : TEXCOORD0;
};

//-----------------------------------------------------------------------------
// Name: VS
// Type: Vertex shader
// Desc: Transforms the model from object to projection space and calculates
//       the per-vertex lighting components.
//-----------------------------------------------------------------------------
VS_OUTPUT VS(
    float4 InPos  : POSITION,   // Vertex position in model space
    float3 InNormal : NORMAL,   // Vertex normal in model space
    float  Occ  : TEXCOORD0,    // Occlusion factor
    uniform bool bAmbient,      // Ambient lighting toggle
    uniform bool bHemispheric,  // Hemispheric lighting toggle
    uniform bool bDiffuse,      // Diffuse lighting toggle
    uniform bool bSpecular)     // Specular lighting toggle
{
    VS_OUTPUT Out = (VS_OUTPUT)0;

    // transform the position and normal
   
    // add to position based on time calculation for vertex animation
    float4 Pos = InPos;
    //Pos += CalcVertexAnimation(Pos);   
    Pos = mul(Pos,World);            
    Pos = mul(Pos,View);
   
    float3 Normal = normalize(mul(InNormal, (float3x3)World));   // normal (view space)   
    float3 EyeToVertex = normalize(Pos - CameraPos);             // vector from vertex towards eye


    Out.Pos  = mul(Pos, ViewProjection);              // position (projected)
   
    // calculate the selected lighting terms
    if( bDiffuse )
        Out.Diff += CalcDiffuse(Normal, -DirFromLight);
        
    if( bAmbient )
        Out.Diff += CalcAmbient();
        
    if( bHemispheric )
        Out.Diff += CalcHemisphere(Normal, -DirFromSky, Occ);
        
    if( bSpecular )
        Out.Spec += CalcSpecular(Normal, DirFromLight, EyeToVertex);

    Out.CubeTexcoord = CalcReflectionVector(EyeToVertex, Normal);
   
    return Out;
}




//-----------------------------------------------------------------------------
// Name: PS
// Type: Pixel shader
// Desc: Calculates the pixel color based on interpolated vertex colors
//-----------------------------------------------------------------------------
float4 PS ( VS_OUTPUT In) : COLOR
{
    float4 OutColor;
   
    float4 Environment = CubeMapLookup(In.CubeTexcoord);
   
    // Goal 3D; Adjust reflectivity
    //
    //   Most shiny objects are not completely reflective: Some
    //     percentage of the final color is a reflection of the
    //     environment and the remaining contribution comes from the
    //     material's diffuse properties. Factor in the diffuse
    //     color to decrease reflectivity.   
    //
    //   Suggestions:
    //     Lerp between the diffuse color calculated within
    //       the vertex shader (In.Diff) and the environment
    //       map color sampled from the cubemap. A good
    //       reflectivity value for this scene is 0.25

    OutColor = lerp(In.Diff, Environment, 0.25);

    OutColor += In.Spec;

    return OutColor;
}




//-----------------------------------------------------------------------------
// Name: HemisphereDiffuseSpecular
// Type: Technique
// Desc: Render with hemispheric, diffuse, and specular lighting terms
//-----------------------------------------------------------------------------
technique HemisphereDiffuseSpecular
{
    pass P0
    {
        VertexShader = compile vs_2_0 VS(false, true, true, true);
        PixelShader  = compile ps_2_0 PS();
    }
}




//-----------------------------------------------------------------------------
// Name: HemisphereDiffuse
// Type: Technique
// Desc: Render with hemispheric and diffuse lighting terms
//-----------------------------------------------------------------------------
technique HemisphereDiffuse
{
    pass P0
    {
        VertexShader = compile vs_2_0 VS(false, true, true, false);
        PixelShader  = compile ps_2_0 PS();
    }
}




//-----------------------------------------------------------------------------
// Name: Hemisphere
// Type: Technique
// Desc: Render with hemispheric lighting term only
//-----------------------------------------------------------------------------
technique Hemisphere
{
    pass P0
    {
        VertexShader = compile vs_2_0 VS(false, true, false, false);
        PixelShader  = compile ps_2_0 PS();
    }
}




//-----------------------------------------------------------------------------
// Name: Specular
// Type: Technique
// Desc: Render with specular lighting term only
//-----------------------------------------------------------------------------
technique Specular
{
    pass P0
    {
        VertexShader = compile vs_2_0 VS(false, false, false, true);
        PixelShader  = compile ps_2_0 PS();
    }
}




//-----------------------------------------------------------------------------
// Name: Ambient
// Type: Technique
// Desc: Render with ambient lighting term only
//-----------------------------------------------------------------------------
technique Ambient
{
    pass P0
    {
        VertexShader = compile vs_2_0 VS(true, false, false, false);
        PixelShader  = compile ps_2_0 PS();
    }
}




//-----------------------------------------------------------------------------
// Name: AmbientDiffuse
// Type: Technique
// Desc: Render with ambient and diffuse lighting terms
//-----------------------------------------------------------------------------
technique AmbientDiffuse
{
    pass P0
    {
        VertexShader = compile vs_2_0 VS(true, false, true, false);
        PixelShader  = compile ps_2_0 PS();
    }
}




//-----------------------------------------------------------------------------
// Name: AmbientDiffuseSpecular
// Type: Technique
// Desc: Render with ambient, diffuse, and specular lighting terms
//-----------------------------------------------------------------------------
technique AmbientDiffuseSpecular
{
    pass P0
    {
        VertexShader = compile vs_2_0 VS(true, false, true, true);
        PixelShader  = compile ps_2_0 PS();
    }
}

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

本版积分规则

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

GMT+8, 2026-4-12 19:36

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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