FrostEffect.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using UnityEngine;
  2. [ExecuteInEditMode]
  3. [AddComponentMenu("Image Effects/Frost")]
  4. public class FrostEffect : MonoBehaviour
  5. {
  6. public float FrostAmount = 0.5f; //0-1 (0=minimum Frost, 1=maximum frost)
  7. public float EdgeSharpness = 1; //>=1
  8. public float minFrost = 0; //0-1
  9. public float maxFrost = 1; //0-1
  10. public float seethroughness = 0.2f; //blends between 2 ways of applying the frost effect: 0=normal blend mode, 1="overlay" blend mode
  11. public float distortion = 0.1f; //how much the original image is distorted through the frost (value depends on normal map)
  12. public Texture2D Frost; //RGBA
  13. public Texture2D FrostNormals; //normalmap
  14. public Shader Shader; //ImageBlendEffect.shader
  15. private Material material;
  16. private void Start()
  17. {
  18. material = new Material(Shader);
  19. material.SetTexture("_BlendTex", Frost);
  20. material.SetTexture("_BumpMap", FrostNormals);
  21. }
  22. private void OnRenderImage(RenderTexture source, RenderTexture destination)
  23. {
  24. if (!Application.isPlaying)
  25. {
  26. material.SetTexture("_BlendTex", Frost);
  27. material.SetTexture("_BumpMap", FrostNormals);
  28. EdgeSharpness = Mathf.Max(1, EdgeSharpness);
  29. }
  30. material.SetFloat("_BlendAmount", Mathf.Clamp01(Mathf.Clamp01(FrostAmount) * (maxFrost - minFrost) + minFrost));
  31. material.SetFloat("_EdgeSharpness", EdgeSharpness);
  32. material.SetFloat("_SeeThroughness", seethroughness);
  33. material.SetFloat("_Distortion", distortion);
  34. //Debug.Log("_Distortion: " + distortion);
  35. Graphics.Blit(source, destination, material);
  36. }
  37. private void OnPostRender()
  38. {
  39. //Debug.Log("rendering");
  40. }
  41. }