u3d 镜像模糊特效代码
时间: 2024-05-05 15:15:05 浏览: 121
Spells+Pack+v1.rar
以下是一个简单的U3D镜像模糊特效代码,可以将其添加到摄像机上:
```csharp
using UnityEngine;
[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
public class MirrorBlurEffect : MonoBehaviour
{
public Shader mirrorBlurShader;
[Range(0, 10)]
public float blurSize = 1.0f;
[Range(0, 1)]
public float mirrorIntensity = 0.5f;
private Material mirrorBlurMaterial;
private Camera mirrorCamera;
private RenderTexture mirrorTexture;
private void Start()
{
mirrorCamera = new GameObject("Mirror Camera").AddComponent<Camera>();
mirrorCamera.enabled = false;
}
private void OnRenderImage(RenderTexture source, RenderTexture destination)
{
if (mirrorBlurMaterial == null)
{
mirrorBlurMaterial = new Material(mirrorBlurShader);
mirrorBlurMaterial.hideFlags = HideFlags.HideAndDontSave;
}
if (mirrorTexture == null || mirrorTexture.width != source.width || mirrorTexture.height != source.height)
{
if (mirrorTexture != null)
DestroyImmediate(mirrorTexture);
mirrorTexture = new RenderTexture(source.width, source.height, 0);
mirrorTexture.hideFlags = HideFlags.HideAndDontSave;
}
mirrorCamera.CopyFrom(GetComponent<Camera>());
mirrorCamera.transform.SetPositionAndRotation(transform.position, transform.rotation * Quaternion.Euler(0, 180, 0));
mirrorCamera.Render();
mirrorBlurMaterial.SetFloat("_BlurSize", blurSize);
mirrorBlurMaterial.SetFloat("_MirrorIntensity", mirrorIntensity);
mirrorBlurMaterial.SetTexture("_MirrorTex", mirrorTexture);
Graphics.Blit(source, destination, mirrorBlurMaterial);
}
private void OnDisable()
{
if (mirrorBlurMaterial != null)
DestroyImmediate(mirrorBlurMaterial);
if (mirrorTexture != null)
DestroyImmediate(mirrorTexture);
if (mirrorCamera != null)
DestroyImmediate(mirrorCamera.gameObject);
}
}
```
使用方法:
1. 将脚本添加到摄像机上。
2. 在Inspector窗口中设置Shader和其他参数。
3. 运行游戏,即可看到镜像模糊效果。
注意事项:
1. 该脚本需要在摄像机上执行,否则无法生成镜像。
2. Shader需要支持镜像模糊效果。
3. 在使用该脚本时,建议将Camera的Clear Flags设置为Solid Color,并将背景色设置为黑色,以避免出现不必要的镜像。
阅读全文