custom shader

 avatar
unknown
c_cpp
13 days ago
4.9 kB
11
Indexable
in a year
[Include_Library Shadow d6294fcb-c64d-413e-8270-ff771b955b06]
[Include_Library LightingModels 910dd05a-3f80-4650-ad5c-22f688c3a66f]

[Begin_ResourceLayout]

    [Directives:Normal NORMAL_OFF NORMAL]
	[Directives:LightModel LIT_OFF PHONG, BLINN, COOKTORRANCE, ORENNAYAR]

	cbuffer PerDrawCall : register(b0)
	{
		float4x4 WorldViewProj	: packoffset(c0);	[WorldViewProjection]
		float4x4 World			: packoffset(c4);   [World]
		float4x4 WorldInvTrans	: packoffset(c8);   [WorldInverseTranspose]		
	};	
	
	cbuffer ShadowMapViewBuffer 	: register (b1)
	{
		float4x4 ShadowViewProjectionArray[64]	: packoffset(c0.x); [ShadowViewProjectionBuffer]
	};

	cbuffer LightBuffer : register(b2)
	{		
		LightProperties Lights[64]	: packoffset(c0.x); [LightBuffer]
	};

	cbuffer Parameters : register(b3)
	{
		float4 AmbientColor         : packoffset(c0);   [Default(0.1,0.1,0.1,1)]
		float4 DiffuseColor			: packoffset(c1);   [Default(1.0, 1.0, 1.0, 1.0)]		
		float3 SunDirection			: packoffset(c2);	[SunDirection]
		float3 SunColor				: packoffset(c3);   [SunColor]
		float3 CameraPosition		: packoffset(c4);   [CameraPosition]
		float Glossiness			: packoffset(c4.w); [Default(256)]
		float4 SpecularColor		: packoffset(c5);   [Default(1,1,1.0, 1)]		
	};
	
	Texture2D ColorTexture : register(t0);
	SamplerState ColorSampler : register(s0);
	
	Texture2D NormalTexture				: register(t1);
	SamplerState NormalSampler	: register(s1);
	
	Texture2DArray PunctualShadowMap					: register(t2); [PunctualShadowMap]
	SamplerComparisonState PunctualShadowMapSampler		: register(s2);

[End_ResourceLayout]

[Begin_Pass:ShadowMap]
	[Profile 12_1]
	[Entrypoints VS=VS PS=PS]
	[DepthClipEnable False]
	
	struct VS_IN
	{
		float4 position : POSITION0;
	};
	
	struct PS_IN
	{
		float4 position : SV_POSITION;	
	};
	
	PS_IN VS(VS_IN input)
	{
		PS_IN output = (PS_IN)0;
		
		output.position = mul(input.position, WorldViewProj);		 	
	 	
		return output;
	}	

	void PS(PS_IN input)
	{			
	}

[End_Pass]

[Begin_Pass:Default]
	[Profile 11_0]
	[Entrypoints VS=VS PS=PS]

	struct VS_IN
	{
		float4 position : POSITION0;
		float3 normal	: NORMAL0;
	#if NORMAL
		float3 tangent  : TANGENT;
	#endif
		float2 texCoord : TEXCOORD0;
	};

	struct PS_IN
	{
		float4 position : SV_POSITION;		
		float2 texCoord : TEXCOORD0;
		float3 positionWS : TEXCOORD1;
		float3 normalWS	: TEXCOORD2;
		float3 tangentWS : TEXCOORD3;
		float3 binormalWS : TEXCOORD4;
		float3 lightVec : TEXCOORD5;
		float3 eyeVec   : TEXCOORD6;
	};

	PS_IN VS(VS_IN input)
	{
		PS_IN output = (PS_IN)0;

		output.position = mul(input.position, WorldViewProj);	
		output.positionWS = mul(input.position, World);
		output.texCoord = input.texCoord;
		output.normalWS = mul(input.normal, WorldInvTrans);
	#if NORMAL
		output.tangentWS = normalize(mul(input.tangent, WorldInvTrans));	
		output.binormalWS = normalize(cross(output.normalWS, output.tangentWS));
	#endif
		
		float3 positionWS = mul(input.position, World).xyz;
		output.lightVec = Lights[1].Position - positionWS;
		
		output.eyeVec = CameraPosition - positionWS;

		return output;
	}
	
	
	float4 PS(PS_IN input) : SV_Target
	{
		float4 colorTexture = ColorTexture.Sample(ColorSampler, input.texCoord);
		
		Surface surface;
		surface.L = normalize(input.lightVec);
		surface.V = normalize(input.eyeVec);
		
		#if NORMAL
			float3x3 tbn = float3x3(input.tangentWS, input.binormalWS, input.normalWS);			
			float4 normalTexture = NormalTexture.Sample(NormalSampler, input.texCoord) * 2.0 - 1.0;		
			surface.N = normalize(mul(normalTexture, tbn));
		#else
			surface.N = normalize(input.normalWS);
		#endif
		
		surface.LightColor = float4(Lights[1].Color, 1);
		
		Pixel pixel;
		pixel.AmbientColor = AmbientColor;
		pixel.BaseColor = DiffuseColor * colorTexture;
		pixel.SpecularColor = SpecularColor;
		pixel.Glossiness = Glossiness;		
		pixel.Roughness = 0.2f;
		pixel.SpecularRoughness = 0.2f;
		pixel.FresnelRoughness = 0.2f;
	
		float4 finalColor = AmbientColor * DiffuseColor * colorTexture;				
		
		float4 lightingColor = float4(0,0,0,0);
	#if PHONG
		lightingColor = PhongBRDF(surface, pixel);
	#elif BLINN
		lightingColor = BlinnBRDF(surface, pixel);
	#elif COOKTORRANCE
		lightingColor = CookTorranceBRDF(surface, pixel); 
	#elif ORENNAYAR
		lightingColor = OrenNayarBRDF(surface, pixel);
	#endif			
			
	float3 shadowTerm = 1;
	
	#if SHADOW_SUPPORTED
		float3 positionToLight = Lights[1].Position - input.positionWS;
		shadowTerm = PointLightShadow(Lights[1], 
							input.positionWS,  
							positionToLight,
							ShadowViewProjectionArray,
							PunctualShadowMap,
							PunctualShadowMapSampler);
	#endif
		
		finalColor.xyz += (shadowTerm * lightingColor);
		
		return finalColor;	
	}

[End_Pass]
Editor is loading...
Leave a Comment