|
| 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 | + |
| 9 | +#include <Atom/Features/ColorManagement/TransformColor.azsli> |
| 10 | +#include <Atom/Features/SrgSemantics.azsli> |
| 11 | +#include <scenesrg.srgi> |
| 12 | +#include <viewsrg.srgi> |
| 13 | + |
| 14 | +ShaderResourceGroup PerDrawSrg : SRG_PerDraw |
| 15 | +{ |
| 16 | + float3x3 m_rotation; |
| 17 | + |
| 18 | + struct StarShaderConstants |
| 19 | + { |
| 20 | + float2 m_scale; |
| 21 | + float m_exposureFactor; |
| 22 | + float m_twinkleRate; |
| 23 | + }; |
| 24 | + StarShaderConstants m_starParams; |
| 25 | +} |
| 26 | + |
| 27 | +struct VSInput |
| 28 | +{ |
| 29 | + float3 m_position : POSITION; |
| 30 | + float4 m_color : COLOR0; |
| 31 | + |
| 32 | + uint m_vertexID : SV_VertexID; |
| 33 | +}; |
| 34 | + |
| 35 | +struct VSOutput |
| 36 | +{ |
| 37 | + float4 m_position : SV_Position; |
| 38 | + float4 m_color : COLOR0; |
| 39 | + float2 m_quadPos : TEXCOORD; |
| 40 | +}; |
| 41 | + |
| 42 | +float GetFlickerAmount(in float3 pos) |
| 43 | +{ |
| 44 | + const float2 StarShimmerTab[8] = |
| 45 | + { |
| 46 | + float2(0.897907815,-0.347608525), float2(0.550299290, 0.273586675), |
| 47 | + float2(0.823885965, 0.098853070), float2(0.922739035,-0.122108860), |
| 48 | + float2(0.800630175,-0.088956800), float2(0.711673375, 0.158864420), |
| 49 | + float2(0.870537795, 0.085484560), float2(0.956022355,-0.058114540) |
| 50 | + }; |
| 51 | + |
| 52 | + const float2 hash = frac(pos.xy * 256); |
| 53 | + const float timeInSeconds = SceneSrg::m_time; |
| 54 | + float index = frac(hash.x + (hash.y + 1) * PerDrawSrg::m_starParams.m_twinkleRate * timeInSeconds); |
| 55 | + index *= 8; |
| 56 | + |
| 57 | + const float f = frac(index); |
| 58 | + const int i = int(index); |
| 59 | + return StarShimmerTab[i].x + f * StarShimmerTab[i].y; |
| 60 | +} |
| 61 | + |
| 62 | +VSOutput MainVS(VSInput IN) |
| 63 | +{ |
| 64 | + VSOutput OUT; |
| 65 | + |
| 66 | + const float4 colorAndMagnitude = IN.m_color; |
| 67 | + |
| 68 | + // Set the w component to 0 in order to project the stars out to infinity. |
| 69 | + const float4 pos = float4(mul(PerDrawSrg::m_rotation, IN.m_position), 0.0); |
| 70 | + OUT.m_position = mul(ViewSrg::m_viewProjectionMatrix, pos); |
| 71 | + |
| 72 | + // using reverse z, so push depth out to the far plane |
| 73 | + // if can gaurantee the draw order, then can disable depth check entirely |
| 74 | + OUT.m_position.z = 0.0; |
| 75 | + |
| 76 | + // apparent magnitude is how bright a star appears from earth |
| 77 | + // magnitude is a logarithmic scale and brighter objects have lower numbers |
| 78 | + // a star of magnitude 2.0 is 2.512 times as bright as a star of magnitude of 3.0 |
| 79 | + // Sirius has a magnitude of -1.46 |
| 80 | + // humans typically see stars with apparent magnitude ranging from -1.44 .. 6.5 |
| 81 | + const float humanMinMag = -1.44; |
| 82 | + const float humanMaxMag = 6.5; |
| 83 | + |
| 84 | + // scale colorAndMagnitude.w from 0..1 to 6.5..-1.44 |
| 85 | + const float apparentMagnitude = humanMaxMag + colorAndMagnitude.w * (humanMinMag - humanMaxMag); |
| 86 | + |
| 87 | + // scale the range from 6.5..-1.44 to 0..1 with a logarithmic scale approximation |
| 88 | + // e.g. 6.5 = 0.006, 1 = 0.2, 0 = 0.39, -1.44 = 1 |
| 89 | + // and dim with animated flicker amount |
| 90 | + const float brightness = GetFlickerAmount(IN.m_position) * pow(5.0, (-apparentMagnitude + humanMinMag) / 2.5); |
| 91 | + OUT.m_color = float4(brightness * colorAndMagnitude.xyz, brightness); |
| 92 | + |
| 93 | + const float2 expansion[6] = |
| 94 | + { |
| 95 | + float2( 1, 1), |
| 96 | + float2(-1, 1), |
| 97 | + float2( 1, -1), |
| 98 | + float2( 1, -1), |
| 99 | + float2(-1, 1), |
| 100 | + float2(-1, -1) |
| 101 | + }; |
| 102 | + |
| 103 | + const float2 texcoord[6] = |
| 104 | + { |
| 105 | + float2(1, 0), |
| 106 | + float2(0, 0), |
| 107 | + float2(1, 1), |
| 108 | + float2(1, 1), |
| 109 | + float2(0, 0), |
| 110 | + float2(0, 1) |
| 111 | + }; |
| 112 | + |
| 113 | + const uint vertexIndex = uint(IN.m_vertexID) % uint(6); |
| 114 | + OUT.m_position.xy += expansion[vertexIndex] * PerDrawSrg::m_starParams.m_scale; |
| 115 | + OUT.m_quadPos = texcoord[vertexIndex]; |
| 116 | + return OUT; |
| 117 | +}; |
| 118 | + |
| 119 | +struct PSOutput |
| 120 | +{ |
| 121 | + float4 m_diffuse : SV_Target0; |
| 122 | +}; |
| 123 | + |
| 124 | +PSOutput MainPS(VSOutput IN) |
| 125 | +{ |
| 126 | + PSOutput OUT; |
| 127 | + |
| 128 | + const float2 pos = IN.m_quadPos.xy; |
| 129 | + const float2 distCenter = 3.5 * pos.xy - 3.5 * float2(0.5, 0.5); |
| 130 | + const float scale = exp(-dot(distCenter, distCenter)); |
| 131 | + |
| 132 | + const float3 coolColor = IN.m_color.xyz; |
| 133 | + const float3 hotColor = IN.m_color.www; // always grayscale |
| 134 | + |
| 135 | + const float3 color = PerDrawSrg::m_starParams.m_exposureFactor * (coolColor * scale + 5.0 * hotColor * pow(scale, 10.0)); |
| 136 | + |
| 137 | + OUT.m_diffuse = float4(color, scale); |
| 138 | + |
| 139 | + return OUT; |
| 140 | +}; |
0 commit comments