2
0

BsLightRendering.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "BsLightRendering.h"
  2. #include "BsMaterial.h"
  3. #include "BsShader.h"
  4. #include "BsRenderBeast.h"
  5. #include "BsRenderTargets.h"
  6. #include "BsGpuParams.h"
  7. #include "BsLight.h"
  8. namespace BansheeEngine
  9. {
  10. void PerLightParams::setParameters(const LightCore* light)
  11. {
  12. // Note: I could just copy the data directly to the parameter buffer if I ensured the parameter
  13. // layout matches
  14. Vector4 positionAndType = (Vector4)light->getPosition();
  15. switch (light->getType())
  16. {
  17. case LightType::Directional:
  18. positionAndType.w = 0;
  19. break;
  20. case LightType::Point:
  21. positionAndType.w = 0.3f;
  22. break;
  23. case LightType::Spot:
  24. positionAndType.w = 0.8f;
  25. break;
  26. }
  27. mBuffer.gLightPositionAndType.set(positionAndType);
  28. Vector4 colorAndIntensity;
  29. colorAndIntensity.x = light->getColor().r;
  30. colorAndIntensity.y = light->getColor().g;
  31. colorAndIntensity.z = light->getColor().b;
  32. colorAndIntensity.w = light->getIntensity();
  33. mBuffer.gLightColorAndIntensity.set(colorAndIntensity);
  34. Radian spotAngle = Math::clamp(light->getSpotAngle() * 0.5f, Degree(1), Degree(90));
  35. Radian spotFalloffAngle = Math::clamp(light->getSpotFalloffAngle() * 0.5f, Degree(1), (Degree)spotAngle);
  36. Vector3 spotAngles;
  37. spotAngles.x = spotAngle.valueRadians();
  38. spotAngles.y = Math::cos(spotAngles.x);
  39. spotAngles.z = 1.0f / (Math::cos(spotFalloffAngle) - spotAngles.y);
  40. mBuffer.gLightSpotAngles.set(spotAngles);
  41. mBuffer.gLightDirection.set(-light->getRotation().zAxis());
  42. Vector4 lightGeometry;
  43. lightGeometry.x = light->getType() == LightType::Spot ? (float)LightCore::LIGHT_CONE_NUM_SIDES : 0;
  44. lightGeometry.y = (float)LightCore::LIGHT_CONE_NUM_SLICES;
  45. lightGeometry.z = light->getBounds().getRadius();
  46. float coneRadius = Math::sin(spotAngle) * light->getRange();
  47. lightGeometry.w = coneRadius;
  48. mBuffer.gLightGeometry.set(lightGeometry);
  49. Matrix4 transform = Matrix4::TRS(light->getPosition(), light->getRotation(), Vector3::ONE);
  50. mBuffer.gMatConeTransform.set(transform);
  51. }
  52. const SPtr<GpuParamBlockBufferCore>& PerLightParams::getBuffer() const
  53. {
  54. return mBuffer.getBuffer();
  55. }
  56. DirectionalLightMat::DirectionalLightMat()
  57. {
  58. mMaterial->setParamBlockBuffer("PerLight", mParams.getBuffer());
  59. auto& texParams = mMaterial->getShader()->getTextureParams();
  60. for (auto& entry : texParams)
  61. {
  62. if (entry.second.rendererSemantic == RPS_GBufferA)
  63. mGBufferA = mMaterial->getParamTexture(entry.second.name);
  64. else if (entry.second.rendererSemantic == RPS_GBufferB)
  65. mGBufferB = mMaterial->getParamTexture(entry.second.name);
  66. else if (entry.second.rendererSemantic == RPS_GBufferDepth)
  67. mGBufferDepth = mMaterial->getParamTexture(entry.second.name);
  68. }
  69. }
  70. void DirectionalLightMat::setStaticParameters(const SPtr<RenderTargets>& gbuffer, const SPtr<GpuParamBlockBufferCore>& perCamera)
  71. {
  72. mGBufferA.set(gbuffer->getTextureA());
  73. mGBufferB.set(gbuffer->getTextureB());
  74. mGBufferDepth.set(gbuffer->getTextureDepth());
  75. mMaterial->setParamBlockBuffer("PerCamera", perCamera);
  76. }
  77. void DirectionalLightMat::setParameters(const LightCore* light)
  78. {
  79. mParams.setParameters(light);
  80. }
  81. PointLightMat::PointLightMat()
  82. {
  83. mMaterial->setParamBlockBuffer("PerLight", mParams.getBuffer());
  84. auto& texParams = mMaterial->getShader()->getTextureParams();
  85. for (auto& entry : texParams)
  86. {
  87. if (entry.second.rendererSemantic == RPS_GBufferA)
  88. mGBufferA = mMaterial->getParamTexture(entry.second.name);
  89. else if (entry.second.rendererSemantic == RPS_GBufferB)
  90. mGBufferB = mMaterial->getParamTexture(entry.second.name);
  91. else if (entry.second.rendererSemantic == RPS_GBufferDepth)
  92. mGBufferDepth = mMaterial->getParamTexture(entry.second.name);
  93. }
  94. }
  95. void PointLightMat::setStaticParameters(const SPtr<RenderTargets>& gbuffer, const SPtr<GpuParamBlockBufferCore>& perCamera)
  96. {
  97. mGBufferA.set(gbuffer->getTextureA());
  98. mGBufferB.set(gbuffer->getTextureB());
  99. mGBufferDepth.set(gbuffer->getTextureDepth());
  100. mMaterial->setParamBlockBuffer("PerCamera", perCamera);
  101. }
  102. void PointLightMat::setParameters(const LightCore* light)
  103. {
  104. mParams.setParameters(light);
  105. }
  106. }