본문 바로가기
개발/Unity3D

[shader] 오로라

by 비트-바이트 2024. 7. 1.
반응형

 

Shader "Custom/HDRPAurora"
{
    Properties
    {
        _NoiseTexture ("Noise Texture", 2D) = "white" {}
    }

    HLSLINCLUDE
    #pragma target 4.5
    #pragma only_renderers d3d11 playstation xboxone xboxseries vulkan metal switch

    #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
    #include "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl"

    TEXTURE2D(_NoiseTexture);
    SAMPLER(sampler_NoiseTexture);

    ENDHLSL

    SubShader
    {
        Tags { "RenderType"="Opaque" "RenderPipeline"="HDRenderPipeline" }

        Pass
        {
            Name "AuroraPass"
            Tags { "LightMode" = "ForwardOnly" }

            HLSLPROGRAM
            #pragma vertex Vert
            #pragma fragment Frag

            struct Attributes
            {
                float4 positionOS : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct Varyings
            {
                float4 positionCS : SV_POSITION;
                float2 uv : TEXCOORD0;
            };

            Varyings Vert(Attributes input)
            {
                Varyings output;
                output.positionCS = TransformObjectToHClip(input.positionOS.xyz);
                output.uv = input.uv;
                return output;
            }

            float4 Frag(Varyings input) : SV_Target
            {
                float2 F = input.uv * _ScreenSize.xy;
                float3 A = _ScreenSize;
                float4 O = float4(0, 0, 0, 0);
                float3 p;
                float u = 0, R = 0, o, r, a = _Time.y;

                for (; u < 44.; u++) {
                    p = R * normalize(float3(F + F - A.xy, A.y));
                    
                    p.z -= 2.;
                    r = length(p);
                    p /= r * 0.1;
                    
                    p.xz = mul(p.xz, float2x2(cos(a * 0.2), sin(a * 0.2), -sin(a * 0.2), cos(a * 0.2)));
                    R += o = min(r - 0.3, SAMPLE_TEXTURE2D(_NoiseTexture, sampler_NoiseTexture, F / 1024.).r * 0.1) + 0.1;
                    
                    O += 0.05 / (0.4 + o) 
                         * lerp(smoothstep(0.5, 0.7, sin(p.x + cos(p.y) * cos(p.z)) * sin(p.z + sin(p.y) * cos(p.x + a))), 
                                1., 0.15 / r / r) 
                         * smoothstep(5., 0., r)
                         * (1. + cos(R * 3. + float4(0, 1, 2, 0)));
                }

                return O;
            }
            ENDHLSL
        }
    }
}

 

반응형