Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- varying vec2 v_vTexcoord;//Texture coordinates
- varying vec4 v_vColour;//Vertex color
- varying vec3 v_vPosition;//Vertex position
- varying vec3 v_vNormal;//Vertex normals
- varying float fog;//Fog amount#
- */
- struct PixelShaderInput{
- float4 Position : SV_POSITION;
- float3 Normal : NORMAL;
- float4 Colour : COLOR0;
- float2 Texcoord : TEXCOORD0;
- float Fog : FOG;
- };
- /*
- #define LN 8 //Number of lights
- #define DL 8 //Number of directional lights
- */
- static const int LN = 8;
- static const int DL = 8;
- /*
- uniform sampler2D normalmap; //normalmap
- */
- Texture2D normal_map : register(t1);
- SamplerState normal_map_sampler : register(s1);
- uniform float3 ambience;
- uniform float light[LN*4];//x,y,z, and range for point lights
- uniform float lcolor[LN*3];//r,g,b for point lights
- uniform float dlight[DL*4];//x,y,z, and range for directional lights
- uniform float dcolor[DL*3];//r,g,b for directional lights
- uniform float3 fogcolor;
- float3 lighting(float3 pos, float3 col, float3 norm, float3 lpos,float lrange, float3 lcol)
- {
- float N = normalize(lpos-pos);//Light vector
- float L = max(1.0-length(pos-lpos)/lrange,0.0);//Light range
- return (col * lcol * L * pow(max(dot(norm,N),0.0),4.0));//Brightness calculation
- }
- float3 dlighting(float3 col, float3 norm, float3 lpos, float3 lcol)
- {
- float3 N = normalize(lpos);//Light vector
- return (col * lcol * pow(max(dot(norm,N),0.0),4.0));//Brightness calculation
- }
- float4 main(PixelShaderInput INPUT) : SV_TARGET {
- float4 Col = INPUT.Colour * tex2D(gm_BaseTexture, INPUT.Texcoord);//Pixel color
- float3 normalvector = normalize(normal_map.Sample(normal_map_sampler,INPUT.Texcoord).rgb*2.0-1.0);
- // derivations of the fragment position
- float3 pos_dx = ddx(INPUT.Position);
- float3 pos_dy = ddy(INPUT.Position);
- // derivations of the texture coordinate
- float2 texC_dx = ddx(INPUT.Texcoord);
- float2 texC_dy = ddy(INPUT.Texcoord);
- // tangent vector and binormal vector
- float3 tangent = texC_dy.y * pos_dx - texC_dx.y * pos_dy;
- float3 bitangent = texC_dx.x * pos_dy - texC_dy.x * pos_dx;
- tangent = cross( cross( INPUT.Normal, tangent ), tangent ); // orthonormalization of the tangent vector
- bitangent = cross( bitangent, cross( bitangent, INPUT.Normal ) ); // orthonormalization of the binormal vectors to the normal vector
- bitangent = cross( cross( tangent, bitangent ), tangent ); // orthonormalization of the binormal vectors to the tangent vector
- float3x3 tbn = float3x3( normalize(tangent), normalize(bitangent), INPUT.Normal);
- normalvector = mul(normalvector,tbn);
- float3 total = float3(0.0,0.0,0.0);
- for(int i = 0;i < LN;i++)//Loop for all point lights
- {
- total += lighting(INPUT.Position, Col.rgb,normalvector,float3(light[i*4],light[i*4+1],light[i*4+2]),light[i*4+3],
- abs(float3(lcolor[i*3],lcolor[i*3+1],lcolor[i*3+2])));
- }
- for(int i = 0;i < DL;i++)//Loop for all directional lights
- {
- total += dlighting(Col.rgb,normalvector,float3(dlight[i*3],dlight[i*3+1],dlight[i*3+2]),
- abs(float3(dcolor[i*3],dcolor[i*3+1],dcolor[i*3+2])));
- }
- return float4(lerp(total+Col.rgb*ambience,fogcolor,clamp(INPUT.Fog,0.0,1.0)),Col.a);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement