[1]: /storage/temp/93205-sketch3.png
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Camera))]
public class BlackHole : MonoBehaviour {
//public settings
public Shader shader;
public Transform blackHole;
public float ratio; //aspect ratio of the screen
public float radius; //size of black hone in scene
//private settings
Camera cam;
Material _material; //will be procedurally generated
Material material {
get {
if (_material == null) {
_material = new Material (shader);
_material.hideFlags = HideFlags.HideAndDontSave;
}
return _material;
}
}
void onenable(){
cam = GetComponent ();
ratio = 1f / cam.aspect;
}
void ondisable(){
if (_material) {
DestroyImmediate (_material);
}
}
Vector3 wtsp;
Vector2 pos;
void OnRenderImage(RenderTexture source, RenderTexture destination){
//processing happens here
if (shader && material && blackHole){
wtsp = cam.WorldToScreenPoint (blackHole.position);
//is the Black Hole in front of the camera
if (wtsp.z > 0) {
pos = new Vector2 (wtsp.x / cam.pixelWidth, 1 - (wtsp.y / cam.pixelHeight));
//apply shader parametrs
_material.SetVector ("_position", pos);
_material.SetFloat ("_ratio", ratio);
_material.SetFloat ("_rad", radius);
_material.SetFloat ("_distance", Vector3.Distance (blackHole.position, transform.position));
//apply the shader to the image
Graphics.Blit(source, destination, material);
}
}
}
}
↧