📄 common_vs_fxc.h
字号:
// Compute N dot L
float4 lambertAtten = float4( dot( worldBumpBasis1, lightDir ),
dot( worldBumpBasis2, lightDir ),
dot( worldBumpBasis3, lightDir ),
dot( worldNormal, lightDir ) );
lambertAtten = max( lambertAtten, 0.0 );
// Compute angular attenuation
float exponent = cLightInfo[lightNum].spotParams.x;
float stopdot = cLightInfo[lightNum].spotParams.y;
float stopdot2 = cLightInfo[lightNum].spotParams.z;
float ooStopDotMinusStopDot2 = cLightInfo[lightNum].spotParams.w;
float lightDot = dot( -lightDir, cLightInfo[lightNum].dir );
// compute the angular attenuation based on the lights params and the position of
// the point that we are lighting.
float flAngularAtten = ( lightDot - stopdot2 ) * ooStopDotMinusStopDot2;
flAngularAtten = max( cZero, flAngularAtten );
flAngularAtten = pow( flAngularAtten, exponent );
flAngularAtten = min( flAngularAtten, cOne );
float3 color = cLightInfo[lightNum].color * flAngularAtten * distanceAtten;
color1 += color * lambertAtten.x;
color2 += color * lambertAtten.y;
color3 += color * lambertAtten.z;
normalColor += color * lambertAtten.w;
}
void AddBumpedPointLight( const float3 worldPos, int lightNum, const float3 worldBumpBasis1,
const float3 worldBumpBasis2, const float3 worldBumpBasis3, const float3 worldNormal,
inout float3 color1, inout float3 color2, inout float3 color3,
inout float3 normalColor )
{
// Get light direction
float3 lightDir = cLightInfo[lightNum].pos - worldPos;
// Get light distance squared.
float lightDistSquared = dot( lightDir, lightDir );
// Get 1/lightDistance
float ooLightDist = rsqrt( lightDistSquared );
// Normalize light direction
lightDir *= ooLightDist;
// compute distance attenuation factors.
float4 attenuationFactors;
attenuationFactors.x = 1.0f;
attenuationFactors.y = lightDistSquared * ooLightDist;
attenuationFactors.z = lightDistSquared;
attenuationFactors.w = ooLightDist;
float4 distanceAtten = 1.0f / dot( cLightInfo[lightNum].atten, attenuationFactors );
// Compute N dot L
float4 lambertAtten = float4( dot( worldBumpBasis1, lightDir ),
dot( worldBumpBasis2, lightDir ),
dot( worldBumpBasis3, lightDir ),
dot( worldNormal, lightDir ) );
lambertAtten = max( lambertAtten, 0.0 );
float3 color = cLightInfo[lightNum].color * distanceAtten;
color1 += color * lambertAtten.x;
color2 += color * lambertAtten.y;
color3 += color * lambertAtten.z;
normalColor += color * lambertAtten.w;
}
void AddBumpedDirectionalLight( int lightNum, const float3 worldBumpBasis1,
const float3 worldBumpBasis2, const float3 worldBumpBasis3, const float3 worldNormal,
inout float3 color1, inout float3 color2, inout float3 color3,
inout float3 normalColor )
{
/*
// Transform light direction into tangent space.
float3 tangentLightDirection;
tangentLightDirection.x = dot( worldBumpBasis1, -cLightInfo[lightNum].dir );
tangentLightDirection.y = dot( worldBumpBasis2, -cLightInfo[lightNum].dir );
tangentLightDirection.z = dot( worldBumpBasis3, -cLightInfo[lightNum].dir );
float4 directionalAttenuation;
directionalAttenuation.x = dot( bumpBasis[0], tangentLightDirection );
directionalAttenuation.y = dot( bumpBasis[1], tangentLightDirection );
directionalAttenuation.z = dot( bumpBasis[2], tangentLightDirection );
directionalAttenuation.w = dot( float3( 0.0f, 0.0f, 1.0f ), tangentLightDirection );
directionalAttenuation = max( directionalAttenuation, 0.0f );
color1 += directionalAttenuation.x * cLightInfo[lightNum].color;
color2 += directionalAttenuation.y * cLightInfo[lightNum].color;
color3 += directionalAttenuation.z * cLightInfo[lightNum].color;
normalColor += directionalAttenuation.w * cLightInfo[lightNum].color;
*/
float4 NDotL;
NDotL.x = dot( worldBumpBasis1, -cLightInfo[lightNum].dir );
NDotL.y = dot( worldBumpBasis2, -cLightInfo[lightNum].dir );
NDotL.z = dot( worldBumpBasis3, -cLightInfo[lightNum].dir );
NDotL.w = dot( worldNormal, -cLightInfo[lightNum].dir );
NDotL = max( 0.0f, NDotL );
color1 += NDotL.x * cLightInfo[lightNum].color;
color2 += NDotL.y * cLightInfo[lightNum].color;
color3 += NDotL.z * cLightInfo[lightNum].color;
normalColor += NDotL.w * cLightInfo[lightNum].color;
}
void AddBumpedLight( const float3 worldPos, const float3 worldNormal,
const float3 worldBumpBasis1, const float3 worldBumpBasis2,
const float3 worldBumpBasis3,
int lightNum, int lightType,
inout float3 color1, inout float3 color2, inout float3 color3,
inout float3 normalColor )
{
if( lightType == LIGHTTYPE_SPOT )
{
AddBumpedSpotLight( worldPos, lightNum, worldBumpBasis1, worldBumpBasis2, worldBumpBasis3,
worldNormal, color1, color2, color3, normalColor );
}
else if( lightType == LIGHTTYPE_POINT )
{
AddBumpedPointLight( worldPos, lightNum, worldBumpBasis1, worldBumpBasis2, worldBumpBasis3,
worldNormal, color1, color2, color3, normalColor );
}
else
{
AddBumpedDirectionalLight( lightNum, worldBumpBasis1, worldBumpBasis2, worldBumpBasis3,
worldNormal, color1, color2, color3, normalColor );
}
}
void DoBumpedLightingLinear( const float3 worldPos, const float3 worldNormal,
const float3 worldTangentS, const float3 worldTangentT,
const float3 staticLightingColor1,
const float3 staticLightingColor2,
const float3 staticLightingColor3,
const float3 staticLightingColorNormal,
const int staticLightType,
const int ambientLightType, const int localLightType0,
const int localLightType1,
out float3 color1, out float3 color2, out float3 color3, out float3 normalColor )
{
float3 worldBumpBasis1, worldBumpBasis2, worldBumpBasis3;
CalculateWorldBumpBasis( worldTangentS, worldTangentT, worldNormal,
worldBumpBasis1, worldBumpBasis2, worldBumpBasis3 );
if( staticLightType == LIGHTTYPE_STATIC )
{
// The static lighting comes in in gamma space and has also been premultiplied by $cOOOverbright
// need to get it into
// linear space so that we can do adds.
color1 = GammaToLinear( staticLightingColor1 * cOverbright );
color2 = GammaToLinear( staticLightingColor2 * cOverbright );
color3 = GammaToLinear( staticLightingColor3 * cOverbright );
normalColor = GammaToLinear( staticLightingColorNormal * cOverbright );
}
else
{
color1 = color2 = color3 = 0.0f;
normalColor = 0.0f;
}
if( ambientLightType == LIGHTTYPE_AMBIENT )
{
AddBumpedAmbientLight( worldBumpBasis1, worldBumpBasis2, worldBumpBasis3, worldNormal,
color1, color2, color3, normalColor );
}
if( localLightType0 != LIGHTTYPE_NONE )
{
AddBumpedLight( worldPos, worldNormal,
worldBumpBasis1, worldBumpBasis2, worldBumpBasis3,
0, localLightType0, color1, color2, color3, normalColor );
}
if( localLightType1 != LIGHTTYPE_NONE )
{
AddBumpedLight( worldPos, worldNormal,
worldBumpBasis1, worldBumpBasis2, worldBumpBasis3,
1, localLightType1, color1, color2, color3, normalColor );
}
}
void DoBumpedLighting( const float3 worldPos, const float3 worldNormal,
const float3 worldTangentS, const float3 worldTangentT,
const float3 staticLightingColor1,
const float3 staticLightingColor2,
const float3 staticLightingColor3,
const float3 staticLightingColorNormal,
const int staticLightType,
const int ambientLightType, const int localLightType0,
const int localLightType1,
const float modulation,
out float3 color1, out float3 color2, out float3 color3,
out float3 gammaColorNormal )
{
// special case for no lighting
if( staticLightType == LIGHTTYPE_NONE &&
ambientLightType == LIGHTTYPE_NONE &&
localLightType0 == LIGHTTYPE_NONE &&
localLightType1 == LIGHTTYPE_NONE )
{
// do nothing;
color1 = color2 = color3 = 0.0f;
gammaColorNormal = 0.0f;
}
else if( staticLightType == LIGHTTYPE_STATIC &&
ambientLightType == LIGHTTYPE_NONE &&
localLightType0 == LIGHTTYPE_NONE &&
localLightType1 == LIGHTTYPE_NONE )
{
/// special case for static lighting only
// Don't need to bother converting to linear space in this case.
color1 = staticLightingColor1;
color2 = staticLightingColor2;
color3 = staticLightingColor3;
gammaColorNormal = ( staticLightingColor1 + staticLightingColor2 +
staticLightingColor3 ) * ( 1.0f / 3.0f );
}
else
{
float3 normalColor;
DoBumpedLightingLinear( worldPos, worldNormal, worldTangentS, worldTangentT,
staticLightingColor1, staticLightingColor2, staticLightingColor3, staticLightingColorNormal,
staticLightType, ambientLightType, localLightType0, localLightType1,
color1, color2, color3, normalColor );
if (modulation != 1.0f)
{
color1 *= modulation;
color2 *= modulation;
color3 *= modulation;
}
// TODO: may want to add colorclamp here.
float3 averageColor = ( color1 + color2 + color3 ) * OO_SQRT_3;
gammaColorNormal = LinearToGamma( normalColor );
gammaColorNormal *= cOOOverbright;
float3 correctionFactor = gammaColorNormal / averageColor;
color1 *= correctionFactor;
color2 *= correctionFactor;
color3 *= correctionFactor;
}
}
void DoBumpedLightingHDR( const float3 worldPos, const float3 worldNormal,
const float3 worldTangentS, const float3 worldTangentT,
const float3 staticLightingColor1,
const float3 staticLightingColor2,
const float3 staticLightingColor3,
const float3 staticLightingColorNormal,
const int staticLightType,
const int ambientLightType, const int localLightType0,
const int localLightType1,
const float modulation,
out float3 color1, out float3 color2, out float3 color3 )
{
// special case for no lighting
if( staticLightType == LIGHTTYPE_NONE &&
ambientLightType == LIGHTTYPE_NONE &&
localLightType0 == LIGHTTYPE_NONE &&
localLightType1 == LIGHTTYPE_NONE )
{
// do nothing;
color1 = color2 = color3 = 0.0f;
}
else if( staticLightType == LIGHTTYPE_STATIC &&
ambientLightType == LIGHTTYPE_NONE &&
localLightType0 == LIGHTTYPE_NONE &&
localLightType1 == LIGHTTYPE_NONE )
{
/// special case for static lighting only
// Don't need to bother converting to linear space in this case.
color1 = GammaToLinear( staticLightingColor1 * cOverbright );
color2 = GammaToLinear( staticLightingColor2 * cOverbright );
color3 = GammaToLinear( staticLightingColor3 * cOverbright );
}
else
{
float3 normalColor;
DoBumpedLightingLinear( worldPos, worldNormal, worldTangentS, worldTangentT,
staticLightingColor1, staticLightingColor2, staticLightingColor3, staticLightingColorNormal,
staticLightType, ambientLightType, localLightType0, localLightType1,
color1, color2, color3, normalColor );
if (modulation != 1.0f)
{
color1 *= modulation;
color2 *= modulation;
color3 *= modulation;
}
}
}
float DoHeightClip( const float3 worldPos )
{
return -cClipDirection * worldPos.z + cClipDirectionTimesHeightClipZ;
}
int4 FloatToInt( in float4 floats )
{
return D3DCOLORtoUBYTE4( floats.zyxw / 255.001953125 );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -