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

📄 simpvslighting--.fx

📁 使用shader语言在vs阶段编写光照效果的演示程序及全部源代码
💻 FX
字号:
/*********************************************************************NVMH3****
File:  $Id: //sw/devtools/FXComposer/1.8/SDK/MEDIA/HLSL/shadRPort.fx#1 $

Copyright NVIDIA Corporation 2004
TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED
*AS IS* AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL NVIDIA OR ITS SUPPLIERS
BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES
WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS,
BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR ANY OTHER PECUNIARY LOSS)
ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF NVIDIA HAS
BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

Comments:
	Simple shadow map example using FP16/FP32 textures and render ports.

	This example shows how to use explicit floating-point depth maps -- for
		hardware-accelarated PCF shadowing, see "shadRPortHW.fx"

	$Date: 2005/08/12 $
	$Author: kwhite $

******************************************************************************/

// for machines that support FP16 buffers....
#define HAS_FP16

#include <include\\Quad.fxh>

float Script : STANDARDSGLOBAL <
	string UIWidget = "none";
	string ScriptClass = "scene";
	string ScriptOrder = "postprocess";
	string ScriptOutput = "color";
	string Script = "Technique=Main;";
> = 0.8; // version #

float4 ClearColor <
	string UIWidget = "color";
	string UIName = "background";
> = {0,0,0,0.0};

float ClearDepth <string UIWidget = "none";> = 1.0;

#ifdef HAS_FP16
#define FAR 1000.0f
#else
#define FAR 1.0f
#endif

float4 ShadowClearColor <
	string UIWidget = "none";
> = {FAR,FAR,FAR,0.0};

/************* "UN-TWEAKABLES," TRACKED BY CPU APPLICATION **************/

float4x4 WorldITXf : WorldInverseTranspose <string UIWidget="None";>;
float4x4 WorldViewProjXf : WorldViewProjection <string UIWidget="None";>;
float4x4 WorldXf : World <string UIWidget="None";>;
float4x4 ViewIXf : ViewInverse <string UIWidget="None";>;
float4x4 WorldViewITXf : WorldViewInverseTranspose <string UIWidget="None";>;
float4x4 WorldViewXf : WorldView <string UIWidget="None";>;
float4x4 ViewXf : View <string UIWidget="None";>;
float4x4 ViewITXf : ViewInverseTranspose <string UIWidget="None";>;

float4x4 LampViewXf : View <
    //string UIWidget="None";
    string frustum = "light0";
>;

float4x4 LampProjXf : Projection <
    //string UIWidget="None";
    string frustum = "light0";
>;

///////////////////////////////////////////////////////////////
/// TWEAKABLES ////////////////////////////////////////////////
///////////////////////////////////////////////////////////////

////////////////////////////////////////////// spot light

float3 SpotLightPos : POSITION <
	string UIName = "Light Posistion";
	string Object = "SpotLight";
	string Space = "World";
> = {-1.0f, 1.0f, 0.0f};

float3 SpotLightColor : Diffuse <
	string UIName = "Lamp";
	string Object = "SpotLight";
	string UIWidget = "Color";
> = {0.8f, 1.0f, 0.4f};

float SpotLightIntensity <
	string UIName = "Light Intensity";
	string UIWidget = "slider";
	float UIMin = 0.0;
	float UIMax = 2;
	float UIStep = 0.1;
> = 1;

float SpotLightCone <
    string UIWidget = "slider";
    float UIMin = 0.0;
    float UIMax = 90.5;
    float UIStep = 0.1;
    string UIName = "Cone Angle";
> = 45.0f;

////////////////////////////////////////////// ambient light

float3 AmbiLightColor : Ambient
<
    string UIName = "Ambient";
> = {0.07f, 0.07f, 0.07f};

/////////////// Shadow and Aiming Parameters

float ShadBias <
    string UIWidget = "slider";
    float UIMin = 0.0;
    float UIMax = 0.3;
    float UIStep = 0.0001;
    string UIName = "Shadow Bias";
> = 0.01;

////////////////////////////////////////////// surface

float3 SurfColor : Diffuse
<
    string UIName = "Surface";
	string UIWidget = "Color";
> = {1.0f, 0.7f, 0.3f};

float Kd
<
    string UIWidget = "slider";
    float UIMin = 0.0;
    float UIMax = 1.5;
    float UIStep = 0.01;
    string UIName = "Diffuse";
> = 1.0;

float Ks
<
    string UIWidget = "slider";
    float UIMin = 0.0;
    float UIMax = 1.5;
    float UIStep = 0.01;
    string UIName = "Specular";
> = 1.0;


float SpecExpon : SpecularPower
<
    string UIWidget = "slider";
    float UIMin = 1.0;
    float UIMax = 128.0;
    float UIStep = 1.0;
    string UIName = "Specular power";
> = 12.0;


////////////////////////////////////////////////////////
/// TEXTURES ///////////////////////////////////////////
////////////////////////////////////////////////////////

#define SHADOW_SIZE 1024

//#ifdef HAS_FP16
//#define SHADOW_FMT  "a16b16g16r16f"
#define SHADOW_FMT  "r32f"
//#else /* !HAS_FP16 */
//#define SHADOW_FMT  "a8b8g8r8"
//#endif /* !HAS_FP16 */

texture ShadMap : RENDERCOLORTARGET <
	float2 Dimensions = { SHADOW_SIZE, SHADOW_SIZE };
    string Format = (SHADOW_FMT) ;
	string UIWidget = "None";
>;
sampler ShadSampler = sampler_state {
    texture = <ShadMap>;
    AddressU  = CLAMP;
    AddressV = CLAMP;
    MipFilter = NONE;
    MinFilter = POINT;
    MagFilter = POINT;
};

texture ShadDepthTarget : RENDERDEPTHSTENCILTARGET <
	float2 Dimensions = { SHADOW_SIZE, SHADOW_SIZE };
    string format = "D24S8";
    string UIWidget = "None";
>;

/////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////
/// SHADER CODE BEGINS /////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////

/* data from application vertex buffer */
struct ShadowAppData {
    float3 Position	: POSITION;
    float4 UV		: TEXCOORD0;
    float4 Normal	: NORMAL;
};

// Connector from vertex to pixel shader
struct ShadowVertexOutput {
    float4 HPosition	: POSITION;
    float2 UV		: TEXCOORD0;
    float3 LightVec	: TEXCOORD1;
    float3 WNormal	: TEXCOORD2;
    float3 WView	: TEXCOORD3;
    float4 LP		: TEXCOORD4;	// current position in light-projection space
};

// Connector from vertex to pixel shader
struct JustShadowVertexOutput {
    float4 HPosition	: POSITION;
    float4 LP		: TEXCOORD0;	// current position in light-projection space
};

////////////////////////////////////////////////////////////////////////////////
/// Vertex Shaders /////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

JustShadowVertexOutput shadVS(ShadowAppData IN,
					uniform float4x4 ShadowViewProjXf	// typically created from aimShadowProjXf()
) {
    JustShadowVertexOutput OUT = (JustShadowVertexOutput)0;
    float4 Po = float4(IN.Position.xyz,(float)1.0);	// object coordinates
    float4 Pw = mul(Po,WorldXf);			// "P" in world coordinates
    float4 Pl = mul(Pw,ShadowViewProjXf);  // "P" in light coords
    OUT.LP = Pl;		// view coords (also lightspace projection coords in this case)
    OUT.HPosition = Pl; // screen clipspace coords
    return OUT;
}

// from scene camera POV
ShadowVertexOutput mainCamVS(ShadowAppData IN,
					uniform float4x4 ShadowViewProjXf)
{
    ShadowVertexOutput OUT = (ShadowVertexOutput)0;
    OUT.WNormal = mul(IN.Normal,WorldITXf).xyz; // world coords
    float4 Po = float4(IN.Position.xyz,(float)1.0);	// "P" in object coordinates
    float4 Pw = mul(Po,WorldXf);			// "P" in world coordinates
    float4 Pl = mul(Pw,ShadowViewProjXf);  // "P" in light coords
    OUT.LP = Pl;							// ...for pixel-shader shadow calcs
    OUT.WView = normalize(ViewIXf[3].xyz - Pw.xyz);	// world coords
    OUT.HPosition = mul(Po,WorldViewProjXf);	// screen clipspace coords
    OUT.UV = IN.UV.xy;							// pass-thru
    OUT.LightVec = SpotLightPos - Pw.xyz;		// world coords
    return OUT;
}

/*********************************************************/
/*********** pixel shader ********************************/
/*********************************************************/

float4 shadPS(JustShadowVertexOutput IN) : COLOR
{
    return float4(IN.LP.zzz,1);
}

//
// Utility function for pixel shaders to use this shadow map
//
float shadow_calc(float4 LP,	// current shaded point in light-projected coordinates
	uniform sampler ShadowMapSampler,	// obvious
	uniform float ShadowBiasing
) {
    float2 nuv = float2(.5,-.5)*LP.xy/LP.w + float2(.5,.5);
    float shadMapDepth = tex2D(ShadowMapSampler,nuv).x;
    float depth = LP.z - ShadowBiasing;
    float shad = 1-(shadMapDepth<depth);
	return (shad);
}

float4 useShadowPS(ShadowVertexOutput IN,
	uniform float CosSpotAng) : COLOR
{
    float3 Nn = normalize(IN.WNormal);
    float3 Vn = normalize(IN.WView);
    float3 Ln = normalize(IN.LightVec);
    float3 Hn = normalize(Vn + Ln);
    float hdn = dot(Hn,Nn);
    float ldn = dot(Ln,Nn);
    float4 litVec = lit(ldn,hdn,SpecExpon);
    ldn = litVec.y * SpotLightIntensity;
    float dl = normalize(IN.LP.xyz).z;
    dl = max((float)0,((dl-CosSpotAng)/(((float)1.0)-CosSpotAng)));
    float3 ambiContrib = SurfColor * AmbiLightColor;
    float3 diffContrib = SurfColor*(Kd*ldn * SpotLightColor);
    float3 specContrib = ((ldn * litVec.z * Ks) * SpotLightColor);
    float3 result = diffContrib + specContrib;
    float shadowed = shadow_calc(IN.LP,ShadSampler,ShadBias);
    return float4((dl*shadowed*result)+ambiContrib,1);
    //return float4(dl.xxx,1);
}

////////////////////////////////////////////////////////////////////
/// TECHNIQUES /////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////

technique Main <
	string Script = "Pass=MakeShadow;"
					"Pass=UseShadow;";
> {
	pass MakeShadow <
		string Script = "RenderColorTarget0=ShadMap;"
						"RenderDepthStencilTarget=ShadDepthTarget;"
						"RenderPort=light0;"
						"ClearSetColor=ShadowClearColor;"
						"ClearSetDepth=ClearDepth;"
						"Clear=Color;"
						"Clear=Depth;"
						"Draw=geometry;";
	> {
		VertexShader = compile vs_2_0 shadVS(mul(LampViewXf,LampProjXf));
		ZEnable = true;
		ZWriteEnable = true;
		ZFunc = LessEqual;
		CullMode = None;
		PixelShader = compile ps_2_a shadPS();
	}
	pass UseShadow <
		string Script = "RenderColorTarget0=;"
						"RenderDepthStencilTarget=;"
						"RenderPort=;"
						"ClearSetColor=ClearColor;"
						"ClearSetDepth=ClearDepth;"
						"Clear=Color;"
						"Clear=Depth;"
						"Draw=geometry;";
	> {
		VertexShader = compile vs_2_0 mainCamVS(mul(LampViewXf,LampProjXf));
		ZEnable = true;
		ZWriteEnable = true;
		ZFunc = LessEqual;
		CullMode = None;
		PixelShader = compile ps_2_a useShadowPS(cos(radians(SpotLightCone)));
	}
}

/***************************** eof ***/

⌨️ 快捷键说明

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