EmissiveMaterial.azsl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #include <viewsrg.srgi>
  9. #include <Atom/Features/PBR/AlphaUtils.azsli>
  10. #include <Atom/Features/PBR/DefaultObjectSrg.azsli>
  11. #include <Atom/Features/PBR/ForwardPassSrg.azsli>
  12. #include <Atom/Features/PBR/ForwardPassOutput.azsli>
  13. #include <Atom/Features/ColorManagement/TransformColor.azsli>
  14. #include <Atom/Features/SrgSemantics.azsli>
  15. #include <Atom/Features/PBR/Lighting/StandardLighting.azsli>
  16. #include <Atom/Features/PBR/Decals.azsli>
  17. ShaderResourceGroup MaterialSrg : SRG_PerMaterial
  18. {
  19. float3 m_emissiveColor;
  20. float m_emissiveIntensity;
  21. Texture2D m_emissiveMap;
  22. Sampler m_sampler
  23. {
  24. AddressU = Wrap;
  25. AddressV = Wrap;
  26. MinFilter = Linear;
  27. MagFilter = Linear;
  28. MipFilter = Linear;
  29. };
  30. }
  31. struct VSInput
  32. {
  33. float3 m_position : POSITION;
  34. float3 m_normal : NORMAL;
  35. float4 m_tangent : TANGENT;
  36. float3 m_bitangent : BITANGENT;
  37. float2 m_uv : UV0;
  38. };
  39. struct VSOutput
  40. {
  41. precise float4 m_position : SV_Position;
  42. float3 m_normal: NORMAL;
  43. float3 m_tangent : TANGENT;
  44. float3 m_bitangent : BITANGENT;
  45. float3 m_worldPosition : UV0;
  46. float2 m_uv : UV1;
  47. float3 m_shadowCoords[ViewSrg::MaxCascadeCount] : UV3;
  48. };
  49. #include <Atom/Features/Vertex/VertexHelper.azsli>
  50. option bool o_emissive_useTexture;
  51. VSOutput MainVS(VSInput IN)
  52. {
  53. VSOutput OUT;
  54. float3 worldPosition = mul(ObjectSrg::GetWorldMatrix(), float4(IN.m_position, 1.0)).xyz;
  55. VertexHelper(IN, OUT, worldPosition);
  56. OUT.m_uv = IN.m_uv;
  57. return OUT;
  58. }
  59. ForwardPassOutput MainPS(VSOutput IN)
  60. {
  61. // ------- Surface -------
  62. Surface surface;
  63. // Position, Normal, Roughness
  64. surface.position = IN.m_worldPosition.xyz;
  65. surface.normal = surface.vertexNormal = normalize(IN.m_normal);
  66. surface.roughnessLinear = 1.0f;
  67. surface.CalculateRoughnessA();
  68. // Albedo, SpecularF0
  69. const float3 baseColor = float3(0.05f, 0.05f, 0.05f);
  70. const float metallic = 0.0f;
  71. const float specularF0Factor = 0.5f;
  72. surface.SetAlbedoAndSpecularF0(baseColor, specularF0Factor, metallic);
  73. // Clear Coat
  74. surface.clearCoat.InitializeToZero();
  75. // ------- LightingData -------
  76. LightingData lightingData;
  77. // Light iterator
  78. lightingData.tileIterator.Init(IN.m_position, PassSrg::m_lightListRemapped, PassSrg::m_tileLightData);
  79. lightingData.Init(surface.position, surface.normal, surface.roughnessLinear);
  80. // Shadow, Occlusion
  81. lightingData.shadowCoords = IN.m_shadowCoords;
  82. // Emissive
  83. float3 emissive = MaterialSrg::m_emissiveColor.rgb * MaterialSrg::m_emissiveIntensity;
  84. if (o_emissive_useTexture)
  85. {
  86. float4 sampledValue = MaterialSrg::m_emissiveMap.Sample(MaterialSrg::m_sampler, IN.m_uv);
  87. emissive *= TransformColor(sampledValue.rgb, ColorSpaceId::LinearSRGB, ColorSpaceId::ACEScg);
  88. }
  89. lightingData.emissiveLighting = emissive;
  90. // Diffuse and Specular response
  91. lightingData.specularResponse = FresnelSchlickWithRoughness(lightingData.NdotV, surface.specularF0, surface.roughnessLinear);
  92. lightingData.diffuseResponse = 1.0f - lightingData.specularResponse;
  93. const float alpha = 1.0f;
  94. // ------- Lighting Calculation -------
  95. // Apply Decals
  96. ApplyDecals(lightingData.tileIterator, surface);
  97. // Apply Direct Lighting
  98. ApplyDirectLighting(surface, lightingData);
  99. // Apply Image Based Lighting (IBL)
  100. ApplyIBL(surface, lightingData);
  101. // Finalize Lighting
  102. lightingData.FinalizeLighting();
  103. PbrLightingOutput lightingOutput = GetPbrLightingOutput(surface, lightingData, alpha);
  104. ForwardPassOutput OUT;
  105. OUT.m_diffuseColor = lightingOutput.m_diffuseColor;
  106. OUT.m_diffuseColor.w = -1; // Subsurface scattering is disabled
  107. OUT.m_specularColor = lightingOutput.m_specularColor;
  108. OUT.m_specularF0 = lightingOutput.m_specularF0;
  109. OUT.m_albedo = lightingOutput.m_albedo;
  110. OUT.m_normal = lightingOutput.m_normal;
  111. return OUT;
  112. }