hch
3 天以前 4b25281b33f386b8c86b32340b684ae34402f7e5
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
using UnityEngine;
 
[ExecuteAlways]
public class ImageMask : PostEffectsBaseEx
{
    static readonly string shaderName = "lcl/screenEffect/MaskEffect";
 
    // 遮罩大小
    [Range(0.01f, 10f), Tooltip("遮罩大小")]
    public float size = 5.0f;
    // 边缘模糊程度
    [Range(0.0001f, 0.1f), Tooltip("边缘模糊程度")]
    public float edgeBlurLength = 0.05f;
    // 遮罩中心位置
    private Vector2 pos = new Vector4(0.5f, 0.5f);
 
    void OnEnable()
    {
        shader = Shader.Find(shaderName);
    }
 
    // 渲染屏幕
    void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        if (material)
        {
            // 把鼠标坐标传递给shader
            material.SetVector("_Pos", pos);
            // 遮罩大小
            material.SetFloat("_Size", size);
            // 模糊程度
            material.SetFloat("_EdgeBlurLength", edgeBlurLength);
            // 渲染
            Graphics.Blit(source, destination, material);
        }
        else
        {
            Graphics.Blit(source, destination);
        }
    }
 
    // void Update()
    // {
    //     if (Input.GetMouseButton(0))
    //     {
    //         Vector2 mousePos = Input.mousePosition;
    //         //将mousePos转化为(0,1)区间
    //         pos = new Vector2(mousePos.x / Screen.width, mousePos.y / Screen.height);
    //     }
    // }
}