⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hlsl.fx

📁 3d 游戏 入门教程之例子源码-图像渲染
💻 FX
字号:
// -------------------------------------------------------------
// Diffuse and specular shader using Phong specular
// 
// Copyright (c) 2003 Wolfgang F. Engel (wolf@direct3d.net)
// All rights reserved.
// -------------------------------------------------------------

// -------------------------------------------------------------
// variables that are provided by the application
// -------------------------------------------------------------
float4x4 matWorldViewProj;	
float4x4 matWorld;	
float4 vecLightDir;
float4 vecEye;

texture ColorMap;
sampler ColorMapSampler = sampler_state
{
   Texture = <ColorMap>;
   MinFilter = Linear;
   MagFilter = Linear;
   MipFilter = Linear;   
   AddressU  = Clamp;
   AddressV  = Clamp;
};

texture BumpMap;
sampler BumpMapSampler = sampler_state
{
   Texture = <BumpMap>;
   MinFilter = Linear;
   MagFilter = Linear;
   MipFilter = Linear;   
   AddressU  = Clamp;
   AddressV  = Clamp;
};

// -------------------------------------------------------------
// Output channels
// -------------------------------------------------------------
struct VS_OUTPUT
{
    float4 Pos  : POSITION;
    float2 Tex : TEXCOORD0;
    float3 Light : TEXCOORD1;
    float3 View : TEXCOORD2;
    float3 Att : TEXCOORD3;
};

// -------------------------------------------------------------
// vertex shader function (input channels)
// -------------------------------------------------------------
VS_OUTPUT VS(float4 Pos : POSITION, float2 Tex : TEXCOORD, float3 Normal : NORMAL, float3 Tangent : TANGENT  )
{
    VS_OUTPUT Out = (VS_OUTPUT)0;      
    Out.Pos = mul(Pos, matWorldViewProj);	// transform Position
    
    // compute the 3x3 tranform matrix 
    // to transform from world space to tangent space
    float3x3 worldToTangentSpace;
    worldToTangentSpace[0] = mul(Tangent, matWorld);
    worldToTangentSpace[1] = mul(cross(Tangent, Normal), matWorld);
    worldToTangentSpace[2] = mul(Normal, matWorld);
        
    Out.Tex = Tex.xy;

    float3 PosWorld = normalize(mul(Pos, matWorld));

    float3 Light = vecLightDir - PosWorld; 
    Out.Light.xyz = mul(worldToTangentSpace, Light);	// L

    float3 Viewer = vecEye - PosWorld;						
    Out.View = mul(worldToTangentSpace, Viewer);		// V
    
    float LightRange = 0.5;    
    Out.Att = vecLightDir * LightRange;				// Point light
    
   return Out;
}

// -------------------------------------------------------------
// Pixel Shader (input channels):output channel
// -------------------------------------------------------------
float4 PS(float2 Tex: TEXCOORD0, float3 Light : TEXCOORD1, float3 View : TEXCOORD2, float3 Att : TEXCOORD3) : COLOR
{
   
    float4 color = tex2D(ColorMapSampler, Tex);					// fetch color map
    float3 bumpNormal = 2 * (tex2D(BumpMapSampler, Tex) - 0.5); // fetch bump map
	
    float3 LightDir = normalize(Light);
    float3 ViewDir = normalize(View);
        
    float4 diff = saturate(dot(bumpNormal, LightDir));    // diffuse component
    
    // compute self-shadowing term
    float shadow = saturate(4 * diff);
    
    float3 Reflect = normalize(2 * diff * bumpNormal - LightDir);  // R
 	
	// gloss map in color.w used to restrict spec reflection
    float4 spec = min(pow(saturate(dot(Reflect, ViewDir)), 15), color.w);
    
    // attenuation
    float4 Attenuation = saturate(dot(Att, Att));
    return  0.2 * color + (shadow * (color * diff + spec) * (1 -Attenuation));	    
}


// -------------------------------------------------------------
// 
// -------------------------------------------------------------
technique TShader
{
    pass P0
    {
        Sampler[0] = (ColorMapSampler);		
        Sampler[1] = (BumpMapSampler);		
    
        // compile shaders
        VertexShader = compile vs_1_1 VS();
        PixelShader  = compile ps_2_0 PS();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -