Advertisement
Guest User

Untitled

a guest
Jan 9th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. [RequireComponent(typeof(Camera))]
  6. public class BlackHoleEffect : MonoBehaviour {
  7. //public settings
  8. public Shader shader;
  9. public Transform blackHole;
  10. public float ratio; //aspect ratio of the screen
  11. public float radius;
  12.  
  13.  
  14. //private settings
  15. Camera cam;
  16. Material _material; // will be procedurally generated
  17.  
  18. Material material {
  19. get {
  20. if (_material == null) {
  21. _material = new Material (shader);
  22. _material.hideFlags = HideFlags.HideAndDontSave;
  23. }
  24. return _material;
  25. }
  26. }
  27.  
  28. void OnEnable(){
  29. cam = GetComponent<Camera> ();
  30. ratio = 1f / cam.aspect;
  31. }
  32. void OnDisable(){
  33. if (_material) {
  34. DestroyImmediate (_material);
  35. }
  36. }
  37.  
  38. Vector3 wtsp;
  39. Vector2 pos;
  40. void OnRenderImage(RenderTexture source, RenderTexture destination){
  41. //processing happens here
  42. if (shader && material && blackHole) {
  43. wtsp = cam.WorldToScreenPoint (blackHole.position);
  44.  
  45. //is the black hole in front of the camera
  46. if (wtsp.z > 0) {
  47. pos = new Vector2 (wtsp.x / cam.pixelWidth, (wtsp.y / cam.pixelHeight));
  48. //apply shader params
  49. _material.SetVector("_Position", pos);
  50. _material.SetFloat ("_Ratio", ratio);
  51. _material.SetFloat ("_Rad", radius);
  52. _material.SetFloat ("_Distance", Vector3.Distance (blackHole.position, transform.position));
  53.  
  54.  
  55. //apply the shader to the image
  56. Graphics.Blit(source, destination, material);
  57. }
  58. }
  59. }
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement