using UnityEngine; public class MagnifyingGlass : PostEffectsBase { //放大的工具 public Transform tool; //是否开始放大 public bool isStartEnlarge = false; // shader private Shader myShader; //材质 private Material mat = null; public Material material { get { // 检查着色器并创建材质 mat = CheckShaderAndCreateMaterial(myShader, ref mat); return mat; } } // 放大强度 [Range(-2.0f, 2.0f), Tooltip("放大强度")] public float zoomFactor = 0.4f; // 放大镜大小 [Range(0.0f, 0.2f), Tooltip("放大镜大小")] public float size = 0.15f; // 凸镜边缘强度 [Range(0.0001f, 0.1f), Tooltip("凸镜边缘强度")] public float edgeFactor = 0.05f; // 遮罩中心位置 private Vector2 pos = Vector2.zero; void Start() { //找到对应的Shader文件 myShader = shader; if (myShader == null) myShader = Shader.Find("Custom/MagnifyingGlass"); } // 渲染屏幕 void OnRenderImage(RenderTexture source, RenderTexture destination) { if (material) { // 把鼠标坐标传递给Shader material.SetVector("_Pos", pos); material.SetFloat("_ZoomFactor", zoomFactor); material.SetFloat("_EdgeFactor", edgeFactor); material.SetFloat("_Size", size); // 渲染 //Graphics.Blit函数使用特定的UnityShader来对当前图像进行处理,再把返回的渲染纹理显示到屏幕上 Graphics.Blit(source, destination, material); } else { Graphics.Blit(source, destination); } } public void Update() { if (!isStartEnlarge) return; //if (tool == null) return; //Vector2 mousePos = Camera.main.WorldToScreenPoint(tool.position); Vector3 mousePos = Input.mousePosition; //将mousePos转化为(0,1)区间 pos = new Vector2(mousePos.x / Screen.width, mousePos.y / Screen.height); } }