标签:
#ifndef _COMMON_FXH #define _COMMON_FXH /************* Constants *************/ #define FLIP_TEXTURE_Y 1 /************* Data Structures *************/ struct POINT_LIGHT { float3 Position; float LightRadius; float4 Color; }; struct LIGHT_CONTRIBUTION_DATA { float4 Color; float3 Normal; float3 ViewDirection; float4 LightColor; float4 LightDirection; float4 SpecularColor; float SpecularPower; }; /************* Utility Functions *************/ float2 get_corrected_texture_coordinate(float2 textureCoordinate) { #if FLIP_TEXTURE_Y return float2(textureCoordinate.x, 1.0 - textureCoordinate.y); #else return textureCoordinate; #endif } float3 get_vector_color_contribution(float4 light, float3 color) { // Color (.rgb) * Intensity (.a) return light.rgb * light.a * color; } float3 get_scalar_color_contribution(float4 light, float color) { // Color (.rgb) * Intensity (.a) return light.rgb * light.a * color; } float4 get_light_data(float3 lightPosition, float3 worldPosition, float lightRadius) { float4 lightData; float3 lightDirection = lightPosition - worldPosition; lightData.xyz = normalize(lightDirection); lightData.w = saturate(1.0f - length(lightDirection) / lightRadius); // Attenuation return lightData; } float3 get_light_contribution(LIGHT_CONTRIBUTION_DATA IN) { float3 lightDirection = IN.LightDirection.xyz; float n_dot_l = dot(IN.Normal, lightDirection); float3 halfVector = normalize(lightDirection + IN.ViewDirection); float n_dot_h = dot(IN.Normal, halfVector); float4 lightCoefficients = lit(n_dot_l, n_dot_h, IN.SpecularPower); float3 diffuse = get_vector_color_contribution(IN.LightColor, lightCoefficients.y * IN.Color.rgb) * IN.LightDirection.w; float3 specular = get_scalar_color_contribution(IN.SpecularColor, min(lightCoefficients.z, IN.Color.w)) * IN.LightDirection.w * IN.LightColor.w; return (diffuse + specular); } #endif /* _COMMON_FXH */
#include "include\\Common.fxh" /************* Resources *************/ #define NUM_LIGHTS 4 cbuffer CBufferPerFrame { POINT_LIGHT PointLights[NUM_LIGHTS]; float4 AmbientColor : AMBIENT < string UIName = "Ambient Light"; string UIWidget = "Color"; > = {1.0f, 1.0f, 1.0f, 0.0f}; float3 CameraPosition : CAMERAPOSITION < string UIWidget="None"; >; } cbuffer CBufferPerObject { float4x4 WorldViewProjection : WORLDVIEWPROJECTION < string UIWidget="None"; >; float4x4 World : WORLD < string UIWidget="None"; >; float4 SpecularColor : SPECULAR < string UIName = "Specular Color"; string UIWidget = "Color"; > = {1.0f, 1.0f, 1.0f, 1.0f}; float SpecularPower : SPECULARPOWER < string UIName = "Specular Power"; string UIWidget = "slider"; float UIMin = 1.0; float UIMax = 255.0; float UIStep = 1.0; > = {25.0f}; } Texture2D ColorTexture < string ResourceName = "default_color.dds"; string UIName = "Color Texture"; string ResourceType = "2D"; >; SamplerState ColorSampler { Filter = MIN_MAG_MIP_LINEAR; AddressU = WRAP; AddressV = WRAP; }; RasterizerState DisableCulling { CullMode = NONE; }; /************* Data Structures *************/ struct VS_INPUT { float4 ObjectPosition : POSITION; float2 TextureCoordinate : TEXCOORD; float3 Normal : NORMAL; }; struct VS_OUTPUT { float4 Position : SV_Position; float3 WorldPosition : POSITION; float3 Normal : NORMAL; float2 TextureCoordinate : TEXCOORD0; }; /************* Vertex Shader *************/ VS_OUTPUT vertex_shader(VS_INPUT IN) { VS_OUTPUT OUT = (VS_OUTPUT)0; OUT.Position = mul(IN.ObjectPosition, WorldViewProjection); OUT.WorldPosition = mul(IN.ObjectPosition, World).xyz; OUT.TextureCoordinate = get_corrected_texture_coordinate(IN.TextureCoordinate); OUT.Normal = normalize(mul(float4(IN.Normal, 0), World).xyz); return OUT; } /************* Pixel Shader *************/ float4 pixel_shader(VS_OUTPUT IN, uniform int lightCount) : SV_Target { float4 OUT = (float4)0; float3 normal = normalize(IN.Normal); float3 viewDirection = normalize(CameraPosition - IN.WorldPosition); float4 color = ColorTexture.Sample(ColorSampler, IN.TextureCoordinate); float3 ambient = get_vector_color_contribution(AmbientColor, color.rgb); LIGHT_CONTRIBUTION_DATA lightContributionData; lightContributionData.Color = color; lightContributionData.Normal = normal; lightContributionData.ViewDirection = viewDirection; lightContributionData.SpecularColor = SpecularColor; lightContributionData.SpecularPower = SpecularPower; float3 totalLightContribution = (float3)0; [unroll] for (int i = 0; i < lightCount; i++) { lightContributionData.LightDirection = get_light_data(PointLights[i].Position, IN.WorldPosition, PointLights[i].LightRadius); lightContributionData.LightColor = PointLights[i].Color; totalLightContribution += get_light_contribution(lightContributionData); } OUT.rgb = ambient + totalLightContribution; OUT.a = 1.0f; return OUT; } /************* Techniques *************/ technique10 Lights1 { pass p0 { SetVertexShader(CompileShader(vs_4_0, vertex_shader())); SetGeometryShader(NULL); SetPixelShader(CompileShader(ps_4_0, pixel_shader(1))); SetRasterizerState(DisableCulling); } } technique10 Lights2 { pass p0 { SetVertexShader(CompileShader(vs_4_0, vertex_shader())); SetGeometryShader(NULL); SetPixelShader(CompileShader(ps_4_0, pixel_shader(2))); SetRasterizerState(DisableCulling); } } technique10 Lights3 { pass p0 { SetVertexShader(CompileShader(vs_4_0, vertex_shader())); SetGeometryShader(NULL); SetPixelShader(CompileShader(ps_4_0, pixel_shader(3))); SetRasterizerState(DisableCulling); } } technique10 Lights4 { pass p0 { SetVertexShader(CompileShader(vs_4_0, vertex_shader())); SetGeometryShader(NULL); SetPixelShader(CompileShader(ps_4_0, pixel_shader(4))); SetRasterizerState(DisableCulling); } }
标签:
原文地址:http://blog.csdn.net/chenjinxian_3d/article/details/51865020