Unity Shader 闪烁的光晕 Glow 亮瞎你的眼

Unity Shader 实现动态发光效果

效果图 :

a53846c3gw1fbklyjye2hg20am08g41j.gif

Shader006.shader

Shader "Custom/Shader006" {

    Properties {
        _Color ("Color", Color) = (1,0.6,0,1)
        _GlowColor("Glow Color", Color) = (1,1,0,1)
        _Strength("Glow Strength", Range(5.0, 1.0)) = 2.0
        _GlowRange("Glow Range", Range(0.1,1)) = 0.6
        // _MainTex ("Albedo (RGB)", 2D) = "white" {}
        // _Glossiness ("Smoothness", Range(0,1)) = 0.5
        // _Metallic ("Metallic", Range(0,1)) = 0.0
    }

    SubShader {

        Pass {
            Tags { "LightMode"="ForwardBase" }

            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag

            float4 _Color;

            float4 vert(float4 vertexPos : POSITION) : SV_POSITION {
                return mul(UNITY_MATRIX_MVP, vertexPos);
            }

            float4 frag(void) : COLOR {
                return _Color;
            }

            ENDCG
        }

        Pass {
            Tags { "LightMode"="ForwardBase" "Queue"="Transparent" "RenderType"="Transparent" }
            // Cull Front
            ZWrite Off
            Blend SrcAlpha OneMinusSrcAlpha

            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            float4 _GlowColor;
            float _Strength;
            float _GlowRange;

            struct a2v {
                float4 vertex : POSITION;
                float4 normal : NORMAL;
            };

            struct v2f {
                float4 position : SV_POSITION;
                float4 col : COLOR;
            };

            v2f vert(a2v a) {
                v2f o;
                float4x4 modelMatrix = unity_ObjectToWorld;
                float4x4 modelMatrixInverse = unity_WorldToObject;
                float3 normalDirection = normalize(mul(a.normal, modelMatrixInverse)).xyz;
                float3 viewDirection = normalize(_WorldSpaceCameraPos - mul(modelMatrix, a.vertex).xyz);
                float4 pos = a.vertex + (a.normal * _GlowRange);
                o.position = mul(UNITY_MATRIX_MVP, pos);
                float3 normalDirectionT = normalize(normalDirection);
                float3 viewDirectionT = normalize(viewDirection);
                float strength = abs(dot(viewDirectionT, normalDirectionT));
                float opacity = pow(strength, _Strength);
                float4 col = float4(_GlowColor.xyz, opacity);
                o.col = col;
                return o;
            }

            float4 frag(v2f i) : COLOR {
                return i.col;
            }

            ENDCG
        }

    }
    FallBack "Diffuse"
}

ScriptShader006.cs

a53846c3gw1fbklzyee5wj20lg0gctaz.jpg

using UnityEngine;
using System.Collections;

public class ScriptShader006 : MonoBehaviour
{

    private Material mat;
    private float value;
    float speed = 2.5f;

    void Start ()
    {
        mat = GetComponent<MeshRenderer> ().sharedMaterial;
    }

    void Update ()
    {
        value = Mathf.PingPong (Time.time * speed, 5);
        mat.SetFloat ("_Strength", value);
        Debug.Log (value);
    }
}

a53846c3gw1fbklyn3dsmg20am08ggpo.gif

End.

→ 源码 GitHub

转载于:https://www.cnblogs.com/isayes/p/6532596.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值