|
|

楼主 |
发表于 2005-3-20 22:11:00
|
显示全部楼层
Re:想了解一下用cubic environment map实现单位向量的查找.
有谁是用effect edit写shader的?
我贴一下这个shader,大家给看看问题出哪了:
struct VS_INPUT
{
float4 Pos : POSITION;
float3 Normal : NORMAL;
float3 Tex0 : TEXCOORD0;
float3 Tangent : TANGENT;
};
struct VS_OUTPUT
{
float4 Pos : POSITION;
float3 tLight : COLOR;
float3 tNormal : TEXCOORD0;
float3 tNormalize : TEXCOORD1;
//float3 Tex0 : TEXCOORD4;
float3 tAttenuation :TEXCOORD2;
float3 tDist :TEXCOORD3;
};
// Global Matrices
float4x4 matTotal : WorldViewProjection;
float4x4 matWorld : World;
float3 vLight = {0.0f, 0.0f, 0.0f}; // Position of the Omni light
float range = 80.0f; // Range of the Omni light
texture texBasis < string name = "fieldstone.bmp"; >;
texture texNormalize < string name = "lobbycube.dds" ;>;
texture texNormal < string name = "fieldstonebumpdot3.tga"; >;
texture texAtten< string name = "atten.dds"; >;
//texture texDist< string name = "DistAtten.dds";>;
vector vCPS = {0.0f, 1.0f, -1.0f, 1.0f};
float4 vecEye :CAMERAPOSITION;
string XFile = "chamber.x";
int BCLR = 0xffffffff;
VS_OUTPUT VShader(VS_INPUT i)
{
VS_OUTPUT o;
float3 wNormal, wTangent, wBinormal, tLight;
float3 aLight;
float a;
float4 tPos = mul( i.Pos, matWorld );
//vLight = mul( vLight, matWorld );
tLight = tPos - vLight;
a = clamp( range / length( tLight ), 0, 1.0 );
aLight = tLight / range;
tLight = normalize( tLight );
aLight.xy = 0.5*aLight.xy+0.5 ;
//aLight.xyz = aLight.xyz;
o.tAttenuation = aLight;
o.tDist = 0.5*(aLight.z)+0.5;
o.Pos = mul( i.Pos, matTotal );
wNormal = mul( i.Normal, matWorld );
wTangent = mul( i.Tangent, matWorld );
wBinormal = cross( wTangent, wNormal );
float3x3 tangentMatrix = { wTangent, wBinormal, wNormal };
tangentMatrix = transpose( tangentMatrix );
tLight = mul( -tLight, tangentMatrix );
o.tLight = (tLight * 0.5f + 0.5f);
// Copy UV coordinates
//o.Tex0 = i.Tex0;
o.tNormal = i.Tex0;
o.tNormalize = 0.5*normalize(mul(i.Pos,matWorld)-vLight)+0.5;
return o;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
uniform sampler sampler_diffuse = sampler_state
{
Texture = <texBasis>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};
uniform sampler sampler_bump = sampler_state
{
Texture = <texNormal>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};
uniform sampler sampler_Atten = sampler_state
{
Texture = <texAtten>;
BORDERCOLOR = 0x00000000;
AddressU = Border;
AddressV = Border;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};
uniform sampler sampler_Dist = sampler_state
{
Texture = <texAtten>;
BORDERCOLOR = 0x00000000;
ADDRESSU = Border;
ADDRESSV = Border;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};
uniform samplerCUBE sampler_Normalize = sampler_state
{
Texture = <texNormalize>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Point;
AddressU = clamp;
AddressV = clamp;
AddressW = clamp;
};
struct PS_INPUT
{
float4 tLight : COLOR;
float3 tNormal : TEXCOORD0;
float3 Tex0 : TEXCOORD1;
float3 tAttenuation : TEXCOORD2;
float3 tDist : TEXCOORD3;
float3 tNormalize : TEXCOORD1;
};
struct PS_OUTPUT
{
float4 Col : COLOR;
};
PS_OUTPUT PShader( PS_INPUT i )
{
PS_OUTPUT o;
float4 t0, Atten, t4;
float4 t2, t3;
float3 t1, t5, tLight, aLight;
float cosang, sub;
t0 = tex2D( sampler_diffuse, i.Tex0 );
t1 = 2 * tex2D( sampler_bump, i.tNormal ) - 1;
t2 = tex2D( sampler_Atten, i.tAttenuation ) ;
t3 = tex2D( sampler_Dist, i.tDist );
t4 = texCUBE( sampler_Normalize, i.tNormalize );
Atten = 1 - t2 - t3;
tLight = 2 * i.tLight - 1;
float tmp = dot(tLight,tLight);
float3 vtmp = tLight*(1-tmp)+tLight;
tLight = vtmp;
//tLight = 2 * i.tLight - 1;
//cosang = saturate( dot( t1, tLight ) ) ;
cosang = saturate( dot( t1.xyz, t4.xyz ) );
//o.Col = (-Atten-0.1)*cosang+cosang*0.1;
//o.Col = t0*cosang+saturate(cosang);
o.Col = t4;
return o;
}
technique DiffuseBump
{
pass P0
{
VertexShader = compile vs_1_1 VShader();
PixelShader = compile ps_1_3 PShader();
Lighting = false;
ColorVertex = false;
ZEnable = true;
AlphaBlendEnable = false;
//shademode = flat;
}
}
我发现若用effect edit里面的Solid fill, no texture功能,渲染出来的是我想要的cubic environment效果,但一用自己的sampler就不对了.为啥? |
|