VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > Objective-C编程 >
  • Unity Shader实现黑幕过场效果

这篇文章主要为大家详细介绍了Unity Shader实现黑幕过场效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

 

本文实例为大家分享了Unity Shader实现黑幕过场效果的具体代码,供大家参考,具体内容如下

一、效果演示

二、实现

Shader:黑幕过场着色器

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

//黑幕过场着色器

 

Shader "Custom/BlackScreenSpread"

{

 Properties

 {

  _Color("Main Color", Color) = (1,1,1,1)

  _MainTex("Base (RGB)", 2D) = "white" {}

  _Radius("Radius",float)=1.5

  _Center_X("Center_X", float) =0.5

  _Center_Y("Center_Y", float) =0.5

  _Sharp("Sharp", float) = 100

 }

 

 SubShader

 {

  Pass

  {

   ZTest Always Cull Off ZWrite Off

   Fog{ Mode off

  }

   

  CGPROGRAM

  #pragma vertex vert_img       

  #pragma fragment frag            

  #include "UnityCG.cginc" 

 

  fixed4 _Color;

  sampler2D _MainTex;

  float _Radius;

  float _Center_X;

  float _Center_Y;

  float _Sharp;

 

  float _tanh(float x)

  {

   return 2.0f / (1.0f + exp(-2.0f * x)) - 1.0f;

  }

 

  float4 frag(v2f_img i) : COLOR

  

   _Center_X=_Center_X*(_ScreenParams.x / _ScreenParams.y);

   float x = i.uv.x*(_ScreenParams.x / _ScreenParams.y);

   float y = i.uv.y;

 

   float dis = sqrt((x - _Center_X)*(x - _Center_X) + (y - _Center_Y)*(y - _Center_Y));

   float t = _Radius - dis;

   float rt = 0.5f + _tanh(t * _Sharp) * 0.5f;

   float col = float4(rt, rt, rt, rt);

   return tex2D(_MainTex, i.uv) * col * _Color;

  }

  ENDCG

 }

 }

 Fallback off

}

CS:后处理的基类

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

using UnityEngine;

 

/// <summary>

/// 后处理的基类

/// </summary>

[ExecuteInEditMode]

[RequireComponent(typeof(Camera))]

public class PostEffectBase : MonoBehaviour

{

    [Header("后处理着色器")]

    public Shader shader = null;

 

    private Material _material = null;

 

    public Material material

    {

        get

        {

            if (_material == null)

            {

                _material = GenerateMaterial(shader);

            }

            return _material;

        }

    }

 

    protected Material GenerateMaterial(Shader shader)

    {

        if (shader == null || shader.isSupported == false)

        {

            return null;

        }

        Material material = new Material(shader);

        material.hideFlags = HideFlags.DontSave;

        if (material)

        {

            return material;

        }

        return null;

    }

}

CS:黑幕过场效果(挂载到渲染的相机上)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

using UnityEngine;

 

/// <summary>

/// 黑幕过场效果

/// </summary>

[RequireComponent(typeof(Camera))]

public class BlackScreenSpread : PostEffectBase

{

    [Header("黑幕半径")]

    [Space(25)]

    public float radius;

 

    [Header("黑幕中心")]

    public Vector2 center;

 

    [Header("使用边缘模糊")]

    public bool useEdgeBlur;

    [Header("边缘模糊值")]

    public float blur = 10;

 

    public void OnRenderImage(RenderTexture source, RenderTexture destination)

    {

        if (material)

        {

            material.SetFloat("_Center_X", center.x);

            material.SetFloat("_Center_Y", center.y);

            material.SetFloat("_Radius", radius);

            material.SetFloat("_Sharp", useEdgeBlur ? blur : 200);

            Graphics.Blit(source, destination, material);

        }

        else

        {

            Graphics.Blit(source, destination);

        }

    }

}

——设置属性

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

 

原文链接:https://liuhaowen.blog.csdn.net/article/details/109529957


相关教程