Skip to content

Instantly share code, notes, and snippets.

@xiaobin83
Created October 10, 2017 03:40
Show Gist options
  • Save xiaobin83/2db2113cba2f81e84ca08590e76e9e68 to your computer and use it in GitHub Desktop.
Save xiaobin83/2db2113cba2f81e84ca08590e76e9e68 to your computer and use it in GitHub Desktop.
MAX shader of texture blending
// This is used by 3dsmax to load the correct parser
string ParamID = "0x0";
float FixedLightmapIntensity = 1.5;
int texcoord0 : Texcoord
<
int Texcoord = 0;
int MapChannel = 1;
string UIType = "None";
>;
int texcoord1 : Texcoord
<
int Texcoord = 1;
int MapChannel = 2;
string UIType = "None";
>;
int color : Texcoord
<
int Texcoord = 2;
int MapChannel = 0;
string UIType = "None";
>;
int alpha : Texcoord
<
int Texcoord = 3;
int MapChannel = -2;
string UIType = "None";
>;
bool ShowVertexColor <
string UIName = "Show Vertex Color";
> = false;
bool ShowRedChannel <
string UIName = "Show Red Channel";
> = false;
bool ShowGreenChannel <
string UIName = "Show Green Channel";
> = false;
bool ShowBlueChannel <
string UIName = "Show Blue Channel";
> = false;
bool ShowAlphaChannel <
string UIName = "Show Alpha Channel";
> = false;
bool UseVertexAlphaAsLightmap <
string UIName = "Vertex Alpha as Lightmap";
> = false;
bool UseBrightnessMap <
string UIName = "Use Brightness Map";
> = false;
bool UseLightmapRGBAsBrightness <
string UIName = "Lightmap RGB as Brightness";
> = false;
bool AlphaTestEnable <
string UIName = "AlphaTest Enable";
> = false;
float AlphaRef
<
string UIType = "FloatSpinner";
float UIMin = 0.0;
float UIMax = 1.0;
float UIStep = 0.1;
string UIName = "AlphaRef";
> = 0.1;
int BlendLayer
<
string UIType = "IntSpinner";
int UIMin = 0;
int UIMax = 4;
int UIStep = 1;
string UIName = "BlendLayer";
> = 0;
texture DiffuseMap : DiffuseMap
<
string Name = "";
string UIName = "DiffuseMap";
string ResourceType = "2D";
>;
texture LightMap : LightMap
<
string Name = "";
string UIName = "LightMap";
string ResourceType = "2D";
>;
texture BrightnessMap
<
string Name = "";
string UIName="BrightnessMap";
string ResourceType = "2D";
>;
texture DetailMap0
<
string Name = "";
string UIName = "DetailMap0";
string ResourceType = "2D";
>;
texture DetailMap1
<
string Name = "";
string UIName = "DetailMap1";
string ResourceType = "2D";
>;
texture DetailMap2
<
string Name = "";
string UIName = "DetailMap2";
string ResourceType = "2D";
>;
texture DetailMap3
<
string Name = "";
string UIName = "DetailMap3";
string ResourceType = "2D";
>;
sampler2D DiffuseMapSampler = sampler_state
{
Texture = <DiffuseMap>;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};
sampler2D LightMapSampler = sampler_state
{
Texture = <LightMap>;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};
sampler2D BrightnessMapSampler = sampler_state
{
Texture = <BrightnessMap>;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};
sampler2D DetailMap0Sampler = sampler_state
{
Texture = <DetailMap0>;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};
sampler2D DetailMap1Sampler = sampler_state
{
Texture = <DetailMap1>;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};
sampler2D DetailMap2Sampler = sampler_state
{
Texture = <DetailMap2>;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};
sampler2D DetailMap3Sampler = sampler_state
{
Texture = <DetailMap3>;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};
float LightmapIntensity
<
string UIType = "FloatSpinner";
float UIMin = 0.0;
float UIMax = 4.0;
float UIStep = 0.1;
string UIName = "LightmapIntensity";
> = 1.0;
float DetailAlphaFactor0
<
string UIType = "FloatSpinner";
float UIMin = 0.0;
float UIMax = 4.0;
float UIStep = 0.1;
string UIName = "DetailAlphaFactor0";
> = 1.0;
float DetailAlphaFactor1
<
string UIType = "FloatSpinner";
float UIMin = 0.0;
float UIMax = 4.0;
float UIStep = 0.1;
string UIName = "DetailAlphaFactor1";
> = 1.0;
float DetailAlphaFactor2
<
string UIType = "FloatSpinner";
float UIMin = 0.0;
float UIMax = 4.0;
float UIStep = 0.1;
string UIName = "DetailAlphaFactor2";
> = 1.0;
float DetailAlphaFactor3
<
string UIType = "FloatSpinner";
float UIMin = 0.0;
float UIMax = 4.0;
float UIStep = 0.1;
string UIName = "DetailAlphaFactor3";
> = 1.0;
// transformations
float4x4 WorldViewProj : WORLDVIEWPROJ;
struct VS_OUTPUT
{
float4 Pos : POSITION;
float4 Col : TEXCOORD0;
float2 Tex0 : TEXCOORD1; // diffuse
float2 Tex1 : TEXCOORD2; // lightmap, brightnessmap
};
VS_OUTPUT VS(
float3 Pos : POSITION,
float2 Tex0 : TEXCOORD0,
float2 Tex1 : TEXCOORD1,
float3 Col : TEXCOORD2,
float Alpha : TEXCOORD3
)
{
VS_OUTPUT Out = (VS_OUTPUT)0;
Out.Pos = mul(float4(Pos,1),WorldViewProj); // position (projected)
Out.Col = float4(Col.rgb, Alpha);
Out.Tex0 = Tex0;
Out.Tex1 = Tex1;
return Out;
}
float4 PS(
float4 Col : TEXCOORD0,
float2 Tex0 : TEXCOORD1,
float2 Tex1 : TEXCOORD2
) : COLOR
{
if (ShowVertexColor)
{
if (ShowRedChannel)
{
return float4(Col.rrr, 1.0);
}
if (ShowGreenChannel)
{
return float4(Col.ggg, 1.0);
}
if (ShowBlueChannel)
{
return float4(Col.bbb, 1.0);
}
if (ShowAlphaChannel)
{
return float4(Col.aaa, 1.0);
}
return float4(Col.rgb, 1.0);
}
float4 diff = tex2D(DiffuseMapSampler, Tex0);
if (AlphaTestEnable)
{
if (diff.a < AlphaRef)
{
discard;
}
}
float4 lightmap;
if (UseVertexAlphaAsLightmap)
{
lightmap = float4(Col.aaa, 1.0);
}
else
{
lightmap = tex2D(LightMapSampler, Tex1);
}
float3 brightness = float3(0.0, 0.0, 0.0);
if (UseLightmapRGBAsBrightness)
{
brightness = lightmap.rgb*2.0-1.0;
lightmap.rgb = lightmap.aaa;
}
else if (UseBrightnessMap)
{
brightness = tex2D(BrightnessMapSampler, Tex1).rgb;
brightness = brightness * 2.0 - 1.0;
}
float3 final = diff.rgb;
if (BlendLayer >= 1)
{
float4 detail = tex2D(DetailMap0Sampler, Tex0);
final = lerp(final.rgb, detail.rgb, saturate((Col.r - 1.0 + detail.a)*DetailAlphaFactor0));
}
if (BlendLayer >= 2)
{
float4 detail = tex2D(DetailMap1Sampler, Tex0);
final = lerp(final.rgb, detail.rgb, saturate((Col.g - 1.0 + detail.a)*DetailAlphaFactor1));
}
if (BlendLayer >= 3)
{
float4 detail = tex2D(DetailMap2Sampler, Tex0);
final = lerp(final.rgb, detail.rgb, saturate((Col.b - 1.0 + detail.a)*DetailAlphaFactor2));
}
if (BlendLayer >= 4)
{
float4 detail = tex2D(DetailMap3Sampler, Tex0);
final = lerp(final.rgb, detail.rgb, saturate((Col.a - 1.0 + detail.a)*DetailAlphaFactor3));
}
final += brightness;
final *= lightmap.rgb * FixedLightmapIntensity;
return float4(final, diff.a);
}
technique Default // Solid
{
pass P0
{
CullMode = CW;
AlphaBlendEnable = False;
ZWriteEnable = True;
ZEnable = True;
AlphaTestEnable = False;
VertexShader = compile vs_3_0 VS();
PixelShader = compile ps_3_0 PS();
}
}
technique Transparent
{
pass P0
{
CullMode = None;
ZWriteEnable = False;
ZEnable = True;
AlphaBlendEnable = True;
SrcBlend = SrcAlpha;
DestBlend = InvSrcAlpha;
AlphaTestEnable = False;
VertexShader = compile vs_3_0 VS();
PixelShader = compile ps_3_0 PS();
}
}
technique AlphaTest
{
pass P0
{
CullMode = None;
ZWriteEnable = True;
ZEnable = True;
AlphaTestEnable = True;
AlphaBlendEnable = False;
VertexShader = compile vs_3_0 VS();
PixelShader = compile ps_3_0 PS();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment