PageRenderTime 4ms CodeModel.GetById 1ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/Main/src/Microsoft.Research.Visualization3D/Shaders/PerPixelLightning.fx

#
Unknown | 176 lines | 142 code | 34 blank | 0 comment | 0 complexity | ebc11a4b06f1e091d8cff2b666253b94 MD5 | raw file
  1shared float4x4 world;
  2shared float4x4 view;
  3shared float4x4 projection;
  4shared float3 cameraPosition;
  5
  6//light properties
  7shared float3 lightPosition;
  8shared float4 ambientLightColor;
  9shared float4 diffuseLightColor;
 10shared float4 specularLightColor;
 11
 12//material properties
 13shared float specularPower;
 14shared float specularIntensity;
 15
 16shared float4 mbColor;
 17
 18struct VertexShaderOutputPerPixelDiffuse
 19{
 20     float4 Position : POSITION;
 21     float3 WorldNormal : TEXCOORD0;
 22     float3 WorldPosition : TEXCOORD1;
 23     float3 Color : COLOR;
 24};
 25
 26struct PixelShaderInputPerPixelDiffuse
 27{
 28     float3 WorldNormal : TEXCOORD0;
 29     float3 WorldPosition : TEXCOORD1;
 30     float4 Color : COLOR;
 31};
 32
 33struct PixelShaderMetaballInputPerPixelDiffuse
 34{
 35     float3 WorldNormal : TEXCOORD0;
 36     float3 WorldPosition : TEXCOORD1;
 37};
 38
 39struct VertexShaderMetaballOutputPerPixelDiffuse
 40{
 41     float4 Position : POSITION;
 42     float3 WorldNormal : TEXCOORD0;
 43     float3 WorldPosition : TEXCOORD1;
 44};
 45
 46VertexShaderOutputPerPixelDiffuse PerPixelDiffuseVS(
 47     float3 position : POSITION,
 48     float3 normal : NORMAL,
 49     float3 color : COLOR )
 50{
 51     VertexShaderOutputPerPixelDiffuse output;
 52
 53     //generate the world-view-projection matrix
 54     float4x4 wvp = mul(mul(world, view), projection);
 55     
 56     //transform the input position to the output
 57     output.Position = mul(float4(position, 1.0), wvp);
 58
 59     output.WorldNormal =  mul(normal, world);
 60     float4 worldPosition =  mul(float4(position, 1.0), world);
 61     output.WorldPosition = worldPosition / worldPosition.w;
 62     
 63     output.Color = color;
 64
 65     //return the output structure
 66     return output;
 67}
 68
 69VertexShaderMetaballOutputPerPixelDiffuse PerPixelDiffuseMetaballVS(
 70     float3 position : POSITION,
 71     float3 normal : NORMAL
 72     )
 73{
 74     VertexShaderMetaballOutputPerPixelDiffuse output;
 75
 76     //generate the world-view-projection matrix
 77     float4x4 wvp = mul(mul(world, view), projection);
 78     
 79     //transform the input position to the output
 80     output.Position = mul(float4(position, 1.0), wvp);
 81
 82     output.WorldNormal =  mul(normal, world);
 83     float4 worldPosition =  mul(float4(position, 1.0), world);
 84     output.WorldPosition = worldPosition / worldPosition.w;
 85     
 86     //return the output structure
 87     return output;
 88}
 89
 90float4 DiffuseAndPhongPS(PixelShaderInputPerPixelDiffuse input) : COLOR
 91{
 92     //calculate per-pixel diffuse
 93     float3 directionToLight = normalize(lightPosition - input.WorldPosition);
 94     float diffuseIntensity = saturate( dot(directionToLight, input.WorldNormal));
 95     float4 diffuse = diffuseLightColor * diffuseIntensity;
 96
 97     //calculate Phong components per-pixel
 98     float3 reflectionVector = normalize(reflect(-directionToLight, input.WorldNormal));
 99     float3 directionToCamera = normalize(cameraPosition - input.WorldPosition);
100     
101     //calculate specular component
102     float4 specular = specularLightColor * specularIntensity * 
103                       pow(saturate(dot(reflectionVector, directionToCamera)), 
104                           specularPower);
105      
106     //all color components are summed in the pixel shader
107     float4 color = input.Color * (specular  + diffuse + ambientLightColor);
108     color.a = 1.0;
109     return color;
110}
111
112float4 DiffuseAndPhongMetaballPS(PixelShaderMetaballInputPerPixelDiffuse input) : COLOR
113{
114     //calculate per-pixel diffuse
115     float3 directionToLight = normalize(lightPosition - input.WorldPosition);
116     float diffuseIntensity = saturate( dot(directionToLight, input.WorldNormal));
117     float4 diffuse = diffuseLightColor * diffuseIntensity;
118
119     //calculate Phong components per-pixel
120     float3 reflectionVector = normalize(reflect(-directionToLight, input.WorldNormal));
121     float3 directionToCamera = normalize(cameraPosition - input.WorldPosition);
122     
123     //calculate specular component
124     float4 specular = specularLightColor * specularIntensity * 
125                       pow(saturate(dot(reflectionVector, directionToCamera)), 
126                           specularPower);
127      
128     //all color components are summed in the pixel shader
129     float4 color = mbColor * (specular  + diffuse + ambientLightColor);
130     color.a = 1.0;
131     return color;
132}
133
134technique PerPixelDiffuseAndPhong
135{
136     
137    pass P0
138    {
139          //DestBlend = ONE;
140		  //SrcBlend = ONE;
141		  //ZEnable = TRUE;
142		  //ZWriteEnable = FALSE;
143		  //CullMode = NONE;
144	          
145          //set the VertexShader state to the basic
146          //vertex shader that will set up inputs for
147          //the pixel shader
148          VertexShader = compile vs_2_0 PerPixelDiffuseVS();
149          
150          //set the PixelShader state to the complete Phong Shading
151          //implementation of the pixel shader       
152          PixelShader = compile ps_2_0 DiffuseAndPhongPS();
153    }
154}
155
156technique PerPixelDiffuseAndPhongMetaball
157{
158     
159    pass P0
160    {
161          //DestBlend = ONE;
162		  //SrcBlend = ONE;
163		  //ZEnable = TRUE;
164		  //ZWriteEnable = FALSE;
165		  //CullMode = NONE;
166	          
167          //set the VertexShader state to the basic
168          //vertex shader that will set up inputs for
169          //the pixel shader
170          VertexShader = compile vs_2_0 PerPixelDiffuseMetaballVS();
171          
172          //set the PixelShader state to the complete Phong Shading
173          //implementation of the pixel shader       
174          PixelShader = compile ps_2_0 DiffuseAndPhongMetaballPS();
175    }
176}