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