EmissiveMaterial.azsl 4.7 KB

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