【ShaderToy】搬运 旋转光环

照例先放个效果

shader:

Shader "Hidden/ColorHalo"
{
    Properties
    {
        _Color1("Color1",Color) = (1,1,1,1)
        _Color2("Color3",Color) = (1,1,1,1)
        _Color3("Color3",Color) = (1,1,1,1)
        _InnerRadius("_innerRadius",float) = 0.6
        _OuterRadrid("_outerRadrid",float) = 1
        _NoiseScale ("_noiseScale",float) = 0.65
        _HightLightSepeed ("_hightLightSepeed",float) = -1
        _ColorLightSpeed ("_ColorLightSpeed",float)  = 2
        _RingScale ("_ringScale",float)  = 1.2
    }
    SubShader
    {
        // No culling or depth
        Cull Off
        Blend SrcAlpha OneMinusSrcAlpha
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"
            
            float light1(float intensity,float attenuation,float dist)
            {
              return intensity / (1.0 + dist * attenuation);
            }
            
            float light2(float intensity,float attenuation,float dist)
            {
              return intensity / (1.0 + dist * dist * attenuation);
            }
            
            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            float4 _Color1;
            float4 _Color2;
            float4 _Color3;
            float _InnerRadius;
            float _OuterRadrid;
            float _NoiseScale;
            float _HightLightSepeed;
            float _ColorLightSpeed;
            float _RingScale;
            float3 hash33(float3 p3)
            {
                 p3 = frac(p3 * float3(0.1031,0.11369,0.13787));
                 p3 += dot(p3,p3.yxz + 19.19);
                 return -1 + 2 * frac(float3(p3.x+p3.y, p3.x+p3.z, p3.y+p3.z)*p3.zyx);
            }
        
            float snoise3(float3 p)
            {
                const float K1 = 0.333333333;
                const float K2 = 0.166666667;
    
                float3 i = floor(p + (p.x + p.y + p.z) * K1);
                float3 d0 = p - (i - (i.x + i.y + i.z) * K2);
    
                float3 e = step(0, d0 - d0.yzx);
	            float3 i1 = e * (1.0 - e.zxy);
	            float3 i2 = 1.0 - e.zxy * (1.0 - e);
    
                float3 d1 = d0 - (i1 - K2);
                float3 d2 = d0 - (i2 - K1);
                float3 d3 = d0 - 0.5;
    
                float4 h = max(0.6 - float4(dot(d0, d0), dot(d1, d1), dot(d2, d2), dot(d3, d3)), 0.0);
                float4 n = h * h * h * h * float4(dot(d0, hash33(i)), dot(d1, hash33(i + i1)), dot(d2, hash33(i + i2)), dot(d3, hash33(i + 1.0)));
    
                return dot(float4(31.316,31.316,31.316,31.316), n);
            }
        
            float4 Draw(float2 vUv)
            {
                float2 uv = vUv;
                float angle = atan2(uv.y,uv.x);
                float len = length(uv);
                float v0,v1,v2,v3,c1;
                float r0,d0,n0;
                float r,d;
            
                //ring
                n0 = snoise3(float3(uv * _NoiseScale,_Time.y * 0.5)) * 0.5 + 0.5;
                r0 = lerp(lerp(_OuterRadrid,1.0,0.4),lerp(_InnerRadius,1.0,0.6),0);
                d0 = distance(uv,r0 / len * uv);
                v0 = light1(1.0,10.0,d0);
                v0 *= smoothstep(r0 * 1.05,r0,len);
                c1 = cos(angle +_Time.y * _ColorLightSpeed) * 0.5 + 0.5;
            
                //high light1
                float a = _Time.y * _HightLightSepeed;
                float2 pos = float2(cos(a),sin(a)) * r0;
                d = distance(uv,pos);
                v1 = light2(1.5,5.0,d);
                v1 *= light1(1.0,50.0,d0);
            
                //back ring
                v2 = smoothstep(1.0,lerp(_InnerRadius,1.0,n0 * 0.5),len);
            
                //hole
                v3 = smoothstep(_InnerRadius,_InnerRadius * 1.1, len);
            
                //color
                float3 c = lerp(_Color1,_Color2,c1);
                float4 col;
                col.rgb = lerp(_Color3,c,v0);
                col.rgb += float3(v1,v1,v1);
                col.a = max(v0,max(v1,v2) * v3);
                col = clamp(col,0,1);
            
                return col;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = Draw((i.uv * 2 - float2(1,1)) * _RingScale);
                float3 bg = float3(0,0,0);
                col.rgb = lerp(bg,col.rgb,col.a);  //normal
                return col;
            }
            
 
            ENDCG
        }
    }
}

原文地址:https://www.shadertoy.com/view/3tBGRm

偶然在shadertoy上看到的着色器效果,所以学习一下 顺带翻译。

猜你喜欢

转载自blog.csdn.net/chooselove/article/details/106210764
今日推荐