ColorblindnessSimulation.azsl 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 <Atom/Features/SrgSemantics.azsli>
  9. #include <Atom/Features/PostProcessing/FullscreenPixelInfo.azsli>
  10. #include <Atom/Features/PostProcessing/FullscreenVertex.azsli>
  11. #include <Atom/Features/PostProcessing/PostProcessUtil.azsli>
  12. ShaderResourceGroup PassSrg : SRG_PerPass
  13. {
  14. Texture2D<float4> m_framebuffer;
  15. Sampler LinearSampler
  16. {
  17. MinFilter = Linear;
  18. MagFilter = Linear;
  19. MipFilter = Linear;
  20. AddressU = Clamp;
  21. AddressV = Clamp;
  22. AddressW = Clamp;
  23. };
  24. //float4x4 m_simulationMatrix;
  25. //float4 m_simulationParams;
  26. }
  27. #define BLINDNESS_STRENGTH 1.0h //PassSrg.m_simulationParams.x
  28. PSOutput MainPS(VSOutput IN)
  29. {
  30. float3x4 simulationMatrix = float3x4(0, 1.05118294, -0.05116099, 0,
  31. 0, 1, 0, 0,
  32. 0, 0, 1, 0);
  33. PSOutput OUT;
  34. // This texture sample will be in sRGB since HDR post process should happen before this
  35. half3 srgbColor = half3(PassSrg::m_framebuffer.Sample(PassSrg::LinearSampler, IN.m_texCoord).rgb);
  36. half3 linearColor = half3(SRGBToLinear(srgbColor));
  37. // LMS Transformation matrix and inverse described in http://ixora.io/projects/colorblindness/color-blindness-simulation-research/
  38. half3x3 lmsTransformation = half3x3(0.31399022, 0.63951294, 0.04649755,
  39. 0.15537241, 0.75789446, 0.08670142,
  40. 0.01775239, 0.10944209, 0.87256922);
  41. half3x3 inverseLmsTransformation = half3x3(5.47221206, -4.6419601, 0.16963708,
  42. -1.1252419, 2.29317094, -0.1678952,
  43. 0.02980165, -0.19318073, 1.16364789);
  44. // Linear to LMS
  45. half3 lmsColor = mul(lmsTransformation, linearColor);
  46. // Apply given simulation matrix to adjust colors to be what a colorblind person would see
  47. lmsColor = half3(mul(simulationMatrix, float4(lmsColor, 1.000)).rgb);
  48. // LMS back into Linear
  49. half3 simulatedLinearColor = mul(inverseLmsTransformation, lmsColor);
  50. // Linear interpolation between true color and color blindness based on blindness strength
  51. simulatedLinearColor = lerp(linearColor, simulatedLinearColor, BLINDNESS_STRENGTH);
  52. // Linear to sRGB
  53. float3 simulatedsRGBColor = LinearToSRGB(simulatedLinearColor);
  54. OUT.m_color.rgb = simulatedsRGBColor;
  55. OUT.m_color.w = 1;
  56. return OUT;
  57. }