Unity 中的黑洞失真着色器

Black hole distortion shader in Unity

我发现着色器代码具有围绕特定点扭曲 space 的效果。这是一个很酷的效果,但是它缺少一些动画,所以我添加了一些东西:

Shader "Marek/BlackHoleDistortion"
{
    Properties {
        _DistortionStrength ("Distortion Strength", Range(0, 10)) = 0
        _Timer("Timer", Range(0, 10)) = 0
        _HoleSize ("Hole Size", Range(0, 1)) = 0.1736101
        _HoleEdgeSmoothness ("Hole Edge Smoothness", Range(1, 4)) = 4
        _ObjectEdgeArtifactFix ("Object Edge Artifact Fix", Range(1, 10)) = 1
    }
    SubShader {
        Tags {
            "IgnoreProjector"="True"
            "Queue"="Transparent"
            "RenderType"="Transparent"
        }
        GrabPass{ }
        Pass {
            Name "FORWARD"
            Tags {
                "LightMode"="ForwardBase"
            }
            ZWrite Off

            CGPROGRAM
            #include "UnityCG.cginc"
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_fwdbase
            #pragma only_renderers d3d9 d3d11 glcore gles 
            #pragma target 3.0
            uniform sampler2D _GrabTexture;
            uniform float _DistortionStrength;
            uniform float _HoleSize;
            uniform float _HoleEdgeSmoothness;
            uniform float _ObjectEdgeArtifactFix;
            uniform float _Timer;

            struct VertexInput {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
            };

            struct VertexOutput {
                float4 pos : SV_POSITION;
                float4 posWorld : TEXCOORD0;
                float3 normalDir : TEXCOORD1;
                float4 projPos : TEXCOORD2;
            };

            VertexOutput vert (VertexInput v) {
                VertexOutput o = (VertexOutput)0;
                o.normalDir = UnityObjectToWorldNormal(v.normal);
                o.posWorld = mul(unity_ObjectToWorld, v.vertex);
                o.pos = UnityObjectToClipPos(v.vertex);
                o.projPos = ComputeScreenPos(o.pos);

                COMPUTE_EYEDEPTH(o.projPos.z);

                return o;
            }

            float4 frag(VertexOutput i) : COLOR {
                i.normalDir = normalize(i.normalDir);

                float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
                float3 normalDirection = i.normalDir;
                float2 sceneUVs = (i.projPos.xy / i.projPos.w);
                float node_9892 = (_HoleSize * -1.0 + 1.0);
                float node_3969 = (1.0 - pow(1.0 - max(0, dot(normalDirection, viewDirection)), clamp(_DistortionStrength - _Timer, 0, _DistortionStrength)));
                float node_9136 = (length(float2(ddx(node_3969), ddy(node_3969))) * _HoleEdgeSmoothness);
                float node_4918 = pow(node_3969, 6.0);
                float node_1920 = (1.0 - smoothstep((node_9892 - node_9136), (node_9892 + node_9136), node_4918));
                float3 finalColor = (
                    lerp(
                        float4(node_1920, node_1920, node_1920, node_1920), 
                        float4(1, 1, 1, 1), 
                        pow(
                            pow(1.0 - max(0, dot(normalDirection, viewDirection)), 1.0), 
                            _ObjectEdgeArtifactFix
                        )
                    ) * tex2D(_GrabTexture, ((node_4918 * (sceneUVs.rg * _Time * -2.0 + 1.0)) + sceneUVs.rg)).rgb).rgb;

                return fixed4(finalColor, 1);
            }
            ENDCG
        }
    }

    FallBack "Diffuse"

}

现在的问题是,为了让失真在一定时间后消失,我需要在等式中加入一些变量——这里我称之为 _Timer。由于显而易见的原因,我没有使用内置的 _Time - 它是一个不断增长的值,每次使用此着色器的对象被激活时,我都需要从 0 开始的东西。传递该参数的 C# 代码处理如下所示:

public void Update() {
    _timeElapsed += Time.deltaTime;

    _renderer.material.SetFloat("_Timer", _timeElapsed);
}

问题是 - 我能做得更好吗?我希望这个着色器的代码更像是一个独立的东西——不需要从 cs 脚本向它传递参数。

Unity provides a handful of built-in values for your shaders: things like current object’s transformation matrices, time etc.

You just use them in ShaderLab like you’d use any other property, the only difference is that you don’t have to declare it somewhere - they are “built in”.

https://docs.unity3d.com/455/Documentation/Manual/SL-BuiltinValues.html

有一种聪明的方法可以为您提供 4 个值的变化,可能会为每个正在渲染的像素节省 re-using 和 pre-multiplied 值的乘法运算。有 4 个值可用。

 _Time.x = time / 20
 _Time.y = time
 _Time.z = time * 2
 _Time.w = time * 3

这是一个向您展示其工作原理的简单示例:

Shader "Example/Circle"
{
    Properties
    {
    }
    SubShader
    {
        Cull Off 

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            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;
            }



            float circle(in float2 _st, in float _radius){
                float2 dist = distance(_st,float2(0.5,0.5));
                float result = step(dist,_radius);
                return result;
            }

            fixed4 frag (v2f i) : SV_Target
            {
            float WaveTime = sin(_Time.z);
            float3 color = float3(1,1,1)*circle(i.uv,WaveTime);

            return float4( color, 1.0 );
            }
            ENDCG
        }
    }
}

您在评论中提到启用时需要重置时间值,因此此处需要使用脚本初始化时间值。

所以你应该在着色器中使用你自己的时间:

Properties
{
    _Timer("Timer",Float) = 0
}

float WaveTime = sin(_Timer);

using System.Collections;
using UnityEngine;

public class Circle : MonoBehaviour {
    public float _timeElapsed;


    void OnEnable(){
        _timeElapsed = 0;
    }

public void Update() {
    _timeElapsed += Time.deltaTime;
    var _renderer = GetComponent<MeshRenderer>();
    _renderer.material.SetFloat("_Timer", _timeElapsed);
}
}

Can I do it better?

In-short,是也不是。如果您希望着色器根据 material 表现不同,您根本无法避免从 C# 传递 属性。但是,您可以通过传递 start 时间并在着色器中计算 elapse 时间来避免在 Update 中执行此操作。

C#

void OnEnable ()
{
    _renderer.material.SetFloat("_StartTime", Time.timeSinceLevelLoad);
}

着色器

uniform float _StartTime;

float4 frag(VertexOutput i) : COLOR
{
   float elapse = _Time.y - _StartTime;
}

现在,虽然这将直接与您当前使用的设置相关联,但应该注意访问 .material 属性 将克隆 material(这可能会破坏批处理, 除其他事项外)。 最近引入 MaterialPropertyBlocks 可以避免这种情况。