3dmax fx shader, vertex color

美术那边需要一个能在3dmax里用的支持diffuse纹理和顶点色的additive shader(不带光照)。

以前没搞过这个,于是从3dmax自带的vertexcolor.fx,DiffuseBump.fx拼凑出了一个,如下:

 

//-----------------------------DiffuseMapVertexcolorAdditive.fx

// 3ds max effect file
// Simple vertex color - work with the Vertex Paint tool.  The max effect parser
// allows you to define any arbitary map channel to be passed in via a texcoord.
// In this case we are interested in Vertex Color, Illumination and Alpha which
// are stored in 0,-1,-2 respectively.



// light direction (view space)

// transformations
float4x4 World      :         WORLD;
float4x4 View       :         VIEW;
float4x4 Projection :         PROJECTION;
float4x4 WorldViewProj :     WORLDVIEWPROJ;
float4x4 WorldView :         WORLDVIEW;

texture diffuseTexture : DiffuseMap<
    string name = "seafloor.dds";
    string UIName = "Diffuse Texture";
    int Texcoord = 0;
    int MapChannel = 1;
    >;
    

int texcoord1 : Texcoord
<
    int Texcoord = 1;
    int MapChannel = 0;
>;
int texcoord2 : Texcoord
<
    int Texcoord = 2;
    int MapChannel = -2;
>;




struct AppData
{
    float3 Pos  : POSITION;
    float2 TexCoord : TEXCOORD0;
    float3 col    : TEXCOORD1;
    float3 alpha :TEXCOORD2;
 
};

struct v2f
{
    float4 Pos  : POSITION;
    float4 color : COLOR;
    float2 TexCoord0 : TEXCOORD0;

};



struct f2fb {
    float4 col : COLOR;
};



v2f VS(
    AppData IN
 
)
{
    v2f Out = (v2f)0;
   
    Out.Pos  = mul(float4(IN.Pos,1),WorldViewProj);    // position (projected)
    
    float4 diff;
    diff = float4(IN.col,1);
    

   
    Out.color = diff;
        
    Out.color.a = IN.alpha.x;

    Out.TexCoord0.xy = IN.TexCoord.xy;


    return Out;
    
}
f2fb PS(v2f IN,
        uniform sampler2D DiffuseMap)
{
    f2fb OUT;

    //fetch base color
    float4 color = tex2D(DiffuseMap,IN.TexCoord0 );

    //fetch vertex color
    float4 vertexColor=IN.color;

    OUT.col = color *vertexColor;

    return OUT;
}

sampler2D diffuseSampler = sampler_state
{
    Texture = <diffuseTexture>;
    MinFilter = Linear;
    MagFilter = Linear;
    MipFilter = Linear;
    ADDRESSU = WRAP;
    ADDRESSV = WRAP;
};

technique tech
{
    pass P0
    {
        ZEnable = true;
    ZWriteEnable = false;
        AlphaBlendEnable = TRUE;
        SrcBlend         = SRCALPHA;
        DestBlend        = ONE;//InvSrcAlpha;  
        CullMode = None;
        ShadeMode = Gouraud;  
        // shaders
        
        VertexShader = compile vs_2_0 VS();
    PixelShader = compile ps_2_0 PS(diffuseSampler);
    }  
}


technique tech_cullModeCW
{
    pass P0
    {
        ZEnable = true;
    ZWriteEnable = false;
        AlphaBlendEnable = TRUE;
        SrcBlend         = SRCALPHA;
        DestBlend        = ONE;//InvSrcAlpha;  
        CullMode = CW;
        ShadeMode = Gouraud;  
        // shaders
        
        VertexShader = compile vs_2_0 VS();
    PixelShader = compile ps_2_0 PS(diffuseSampler);
    }  
}

----

补充:

1,内置的dx shader vertexcolor.fx在3dmax2012中会报错,在3dmax2010中vertexcolor.fx中是可用的。

2,仿照vertexcolor.fx,顶点色col用mapchannel 0,顶点透明度alpha用mapchannel -2。那个diffuse纹理,用一个跟它们不冲突的通道即可,我这里用的是mapchannel 1。

 

补充2:

3dmax里似乎没有办法控制渲染顺序,所以在复杂场景中,使用了blend的物体,显示结果并不一定符合预期。

posted on 2016-01-14 14:25  wantnon  阅读(2048)  评论(0编辑  收藏  举报

导航