unity shader 入门之路(八)边缘自发光-Glow

unity5.5以后的镜头后期都被整合进了Post-processing Stack,可以在Asset Store中搜索导入。

想实现Glow效果,先要做一些准备工作:


1.导入Post-processing Stack

2.新建Post-processing文件

3.在Main Camera添加Post-processing组件

4.把新建的文件拖进去

以上都不是重点,重点是shader怎么写:

1.New一个新的shader文件,格式随意(不知道怎么做的去看上一篇)。

2.首先我们需要一个Texture


3.其次我们需要一个菲涅耳聚光灯(Fresnel)节点


4.添加一个拾色器


5.数学运算

将Fresnel节点与拾色器节点相乘,

将Texture与之相加。

6.需要注意一点,拾色器需要设置为HDR模式



7.最终效果


这种做法最大的好处就是可以实现单独物体的Glow效果,而非之前全局Glow。

最后贴上Shader:

Shader "Shader Forge/Glow" {
    Properties {
        _node_8448 ("node_8448", 2D) = "gray" {}
        [HDR]_node_708 ("node_708", Color) = (0.2214533,0.64336,0.9411765,1)
        _node_4610 ("node_4610", Range(0, 10)) = 1.880342
    }
    SubShader {
        Tags {
            "RenderType"="Opaque"
        }
        Pass {
            Name "FORWARD"
            Tags {
                "LightMode"="ForwardBase"
            }
            
            
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #define UNITY_PASS_FORWARDBASE
            #include "UnityCG.cginc"
            #pragma multi_compile_fwdbase_fullshadows
            #pragma multi_compile_fog
            #pragma only_renderers d3d9 d3d11 glcore gles 
            #pragma target 3.0
            uniform sampler2D _node_8448; uniform float4 _node_8448_ST;
            uniform float4 _node_708;
            uniform float _node_4610;
            struct VertexInput {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
                float2 texcoord0 : TEXCOORD0;
            };
            struct VertexOutput {
                float4 pos : SV_POSITION;
                float2 uv0 : TEXCOORD0;
                float4 posWorld : TEXCOORD1;
                float3 normalDir : TEXCOORD2;
                UNITY_FOG_COORDS(3)
            };
            VertexOutput vert (VertexInput v) {
                VertexOutput o = (VertexOutput)0;
                o.uv0 = v.texcoord0;
                o.normalDir = UnityObjectToWorldNormal(v.normal);
                o.posWorld = mul(unity_ObjectToWorld, v.vertex);
                o.pos = UnityObjectToClipPos( v.vertex );
                UNITY_TRANSFER_FOG(o,o.pos);
                return o;
            }
            float4 frag(VertexOutput i) : COLOR {
                i.normalDir = normalize(i.normalDir);
                float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
                float3 normalDirection = i.normalDir;
////// Lighting:
                float4 _node_8448_var = tex2D(_node_8448,TRANSFORM_TEX(i.uv0, _node_8448));
                float3 finalColor = (_node_8448_var.rgb+(pow(1.0-max(0,dot(normalDirection, viewDirection)),_node_4610)*_node_708.rgb));
                fixed4 finalRGBA = fixed4(finalColor,1);
                UNITY_APPLY_FOG(i.fogCoord, finalRGBA);
                return finalRGBA;
            }
            ENDCG
        }
    }
    FallBack "Diffuse"
    CustomEditor "ShaderForgeMaterialInspector"
}

猜你喜欢

转载自blog.csdn.net/testiness_wind/article/details/79785681