SkyBoxFeatureProcessor.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <Atom/RPI.Public/Buffer/Buffer.h>
  10. #include <Atom/RPI.Public/FeatureProcessor.h>
  11. #include <Atom/RPI.Reflect/Image/Image.h>
  12. #include <Atom/RPI.Public/Shader/ShaderResourceGroup.h>
  13. #include <Atom/Feature/SkyBox/SkyBoxFeatureProcessorInterface.h>
  14. #include <Atom/Feature/SkyBox/SkyboxConstants.h>
  15. namespace AZ
  16. {
  17. namespace Render
  18. {
  19. // default SunParameters is set to radius of earth's sun, distance from sun -> earth, and cos(angularDiameter) of the sun
  20. struct SunParameters
  21. {
  22. float m_radius = PhysicalSunRadius; // Sun physical radius, unit is millions of km
  23. float m_distance = PhysicalSunDistance; // Sun distance to planet, unit is millions of km
  24. float m_cosAngularDiameter = PhysicalSunCosAngularDiameter; // Cosine angular diameter of the sun, unit is radians
  25. };
  26. struct HosekSky
  27. {
  28. AZ::Vector3 a; // Darkening or brightening of the horizon. Negative is brighter relative to the zenith luminance.
  29. AZ::Vector3 b; // Smoothness of the gradient that is caused by darkening or brightening of the horizon. Higher values result in a more gradual transition.
  30. AZ::Vector3 c; // Added in the extended formula due to the complication arose by anisotropic term, not exist in the original Perez formula.
  31. AZ::Vector3 d; // Relative intensity of the area near the sun. Higher values result in higher luminance.
  32. AZ::Vector3 e; // The width of the region described above by D is modulated by E. Higher values result in a more gradual transition.
  33. AZ::Vector3 f; // Relative intensity of back-scattered light - in other words, the light reflected back from the ground. Higher values result in more reflected light.
  34. AZ::Vector3 g; // Relative intensity of the aureole (the area around the sun).
  35. AZ::Vector3 h; // Size of the aureole.
  36. AZ::Vector3 i; // Smooth gradient around zenith.
  37. AZ::Vector3 z; // Absolute luminance at zenith.
  38. };
  39. class SkyBoxFeatureProcessor final
  40. : public SkyBoxFeatureProcessorInterface
  41. {
  42. public:
  43. AZ_CLASS_ALLOCATOR(SkyBoxFeatureProcessor, AZ::SystemAllocator)
  44. AZ_RTTI(AZ::Render::SkyBoxFeatureProcessor, "{CB7D1F95-2A02-4152-86F1-BB29DC802CF7}", AZ::Render::SkyBoxFeatureProcessorInterface);
  45. static void Reflect(AZ::ReflectContext* context);
  46. SkyBoxFeatureProcessor();
  47. virtual ~SkyBoxFeatureProcessor() = default;
  48. //! FeatureProcessor
  49. void Activate() override;
  50. void Deactivate() override;
  51. void Simulate(const FeatureProcessor::SimulatePacket& packet) override;
  52. void Render(const FeatureProcessor::RenderPacket& packet) override;
  53. // SkyBoxFeatureProcessorInterface overrides ...
  54. void Enable(bool enable) override;
  55. bool IsEnabled() override;
  56. void SetSkyboxMode(SkyBoxMode mode) override;
  57. void SetFogSettings(const SkyBoxFogSettings& fogSettings) override;
  58. void SetFogEnabled(bool enable) override;
  59. bool IsFogEnabled() override;
  60. void SetFogColor(const AZ::Color& color) override;
  61. void SetFogTopHeight(float topHeight) override;
  62. void SetFogBottomHeight(float bottomHeight) override;
  63. void SetCubemapRotationMatrix(AZ::Matrix4x4 matrix) override;
  64. void SetCubemap(Data::Instance<RPI::Image> cubemap) override;
  65. void SetCubemapExposure(float exposure) override;
  66. void SetSunPosition(SunPosition sunPosition) override;
  67. void SetSunPosition(float azimuth, float altitude) override;
  68. void SetTurbidity(int turbidity) override;
  69. void SetSkyIntensity(float intensity, PhotometricUnit unit) override;
  70. void SetSunIntensity(float intensity, PhotometricUnit unit) override;
  71. void SetSunRadiusFactor(float factor) override;
  72. private:
  73. static constexpr const char* FeatureProcessorName = "SkyBoxFeatureProcessor";
  74. // Parameters definition written above
  75. struct PhysicalSkyData
  76. {
  77. AZStd::array<float, 4> m_physicalSkyParameterA;
  78. AZStd::array<float, 4> m_physicalSkyParameterB;
  79. AZStd::array<float, 4> m_physicalSkyParameterC;
  80. AZStd::array<float, 4> m_physicalSkyParameterD;
  81. AZStd::array<float, 4> m_physicalSkyParameterE;
  82. AZStd::array<float, 4> m_physicalSkyParameterF;
  83. AZStd::array<float, 4> m_physicalSkyParameterG;
  84. AZStd::array<float, 4> m_physicalSkyParameterH;
  85. AZStd::array<float, 4> m_physicalSkyParameterI;
  86. AZStd::array<float, 4> m_physicalSkyParameterZ;
  87. AZStd::array<float, 4> m_physicalSkySunParameters;
  88. AZStd::array<float, 4> m_physicalSkySunDirection;
  89. AZStd::array<float, 4> m_physicalSkySunRGB;
  90. AZStd::array<float, 4> m_physicalSkyAndSunIntensity;
  91. };
  92. SkyBoxFeatureProcessor(const SkyBoxFeatureProcessor&) = delete;
  93. void LoadDefaultCubeMap();
  94. void InitBuffer();
  95. HosekSky ComputeHosekSky();
  96. //! Sun color is based on Preetham's paper
  97. //! https://www.cs.utah.edu/~shirley/papers/sunsky/sunsky.pdf
  98. AZ::Vector4 ComputeSunRGB();
  99. //! Sample function for the look up table
  100. double SampleLUT(const double* dataset, size_t stride, int turbidity, float albedo, float inverseAltitude);
  101. double EvaluateSpline(const double* spline, size_t stride, double value);
  102. AZ::Vector3 ComputeSpherical(float altitude, float azimuth) const;
  103. //! Irradiance to CIE XYZ
  104. //! http://jcgt.org/published/0002/02/01/paper.pdf
  105. AZ::Vector3 EvaluateCIEXYZ(int lambda);
  106. //--------------------------------------------------------------------------
  107. //! Evaluates the Perez (extended) formula to find the luminance, in RGB, of the sky
  108. //! https://cgg.mff.cuni.cz/projects/SkylightModelling/HosekWilkie_SkylightModel_SIGGRAPH2012_Preprint_lowres.pdf
  109. //!
  110. //! \param cosTheta Viewing angle on Y axis in radians
  111. //! \param gamma Angle, in radians, between view direction and sun direction
  112. //! \param cosGamma Float dot product of view direction and sun direction
  113. //! \param hosek Sky model parameters
  114. //--------------------------------------------------------------------------
  115. AZ::Vector3 EvaluateHosek(float cosTheta, float gamma, float cosGamma,const HosekSky& hosek);
  116. AZ::Vector3 Vector3Pow(AZ::Vector3 a, AZ::Vector3 b);
  117. AZ::Vector3 Vector3Exp(AZ::Vector3 a);
  118. Data::Instance<RPI::Buffer> m_buffer;
  119. PhysicalSkyData m_physicalSkyData;
  120. RHI::ShaderInputNameIndex m_skyboxEnableIndex = "m_enable";
  121. RHI::ShaderInputNameIndex m_physicalSkyBufferIndex = "m_physicalSkyData";
  122. RHI::ShaderInputNameIndex m_physicalSkyIndex = "m_physicalSky";
  123. RHI::ShaderInputNameIndex m_cubemapIndex = "m_skyboxCubemap";
  124. RHI::ShaderInputNameIndex m_cubemapRotationMatrixIndex = "m_cubemapRotationMatrix";
  125. RHI::ShaderInputNameIndex m_cubemapExposureIndex = "m_cubemapExposure";
  126. RHI::ShaderInputNameIndex m_fogEnableIndex = "m_fogEnable";
  127. RHI::ShaderInputNameIndex m_fogColorIndex = "m_fogColor";
  128. RHI::ShaderInputNameIndex m_fogTopHeightIndex = "m_fogTopHeight";
  129. RHI::ShaderInputNameIndex m_fogBottomHeightIndex = "m_fogBottomHeight";
  130. bool m_skyNeedUpdate = true;
  131. bool m_sunNeedUpdate = true;
  132. bool m_mapBuffer = true;
  133. bool m_enable = false;
  134. SkyBoxMode m_skyboxMode = SkyBoxMode::None;
  135. SkyBoxFogSettings m_fogSettings;
  136. Data::Instance<RPI::ShaderResourceGroup> m_sceneSrg = nullptr;
  137. Data::Instance<RPI::Image> m_cubemapTexture = nullptr;
  138. float m_cubemapExposure = 0;
  139. AZ::Matrix4x4 m_cubemapRotationMatrix = AZ::Matrix4x4::CreateIdentity();
  140. int m_turbidity = 1; // A measure of the aerosol content in the air, it is not linearly interpolated as a float due to numerical instability, but rather treated as integer steps
  141. SunPosition m_sunPosition; // Sun position in the Sky( Azimuth, Altitude)
  142. SunParameters m_sunParameters; // Sun physical parameters
  143. AZ::Vector3 m_sunDirection;
  144. PhotometricValue m_sunIntensity;
  145. PhotometricValue m_skyIntensity;
  146. Data::Instance<RPI::Image> m_defaultCubemapTexture;
  147. };
  148. } // namespace Render
  149. } // namespace AZ