Deozaan

GradientBackground.cs

Mar 10th, 2013
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.73 KB | None | 0 0
  1. // GradientBackground.cs
  2. // By Eric Haines (Eric5h5)
  3. // From http://wiki.unity3d.com/index.php/CameraGradientBackground
  4. // Slightly modified by Roidz and Deozaan
  5.  
  6. using UnityEngine;
  7.  
  8. public class GradientBackground : MonoBehaviour {
  9.  
  10.     public Color topColor = Color.blue;
  11.     public Color bottomColor = Color.white;
  12.     public int gradientLayer = 7;
  13.  
  14.     void Awake() {
  15.         gradientLayer = Mathf.Clamp(gradientLayer, 0, 31);
  16.         if (!camera) {
  17.             Debug.LogError("Must attach GradientBackground script to the camera");
  18.             return;
  19.         }
  20.  
  21.         Transform container = new GameObject("Gradient").transform;
  22.         container.parent = transform;
  23.  
  24.         camera.clearFlags = CameraClearFlags.Depth;
  25.         camera.cullingMask = camera.cullingMask & ~(1 << gradientLayer);
  26.         Camera gradientCam = new GameObject("Gradient Cam", typeof(Camera)).camera;
  27.         gradientCam.depth = camera.depth - 1;
  28.         gradientCam.cullingMask = 1 << gradientLayer;
  29.         gradientCam.transform.parent = container;
  30.  
  31.         Mesh mesh = new Mesh();
  32.         mesh.vertices = new Vector3[4] {
  33.             new Vector3(-100f, .577f, 1f),
  34.             new Vector3(100f, .577f, 1f),
  35.             new Vector3(-100f, -.577f, 1f),
  36.             new Vector3(100f, -.577f, 1f)
  37.         };
  38.  
  39.         mesh.colors = new Color[4] { topColor, topColor, bottomColor, bottomColor };
  40.         mesh.triangles = new int[6] { 0, 1, 2, 1, 3, 2 };
  41.  
  42.         Material mat = new Material("Shader \"Vertex Color Only\"{Subshader{BindChannels{Bind \"vertex\", vertex Bind \"color\", color}Pass{}}}");
  43.         GameObject gradientPlane = new GameObject("Gradient Plane", typeof(MeshFilter), typeof(MeshRenderer));
  44.  
  45.         ((MeshFilter)gradientPlane.GetComponent(typeof(MeshFilter))).mesh = mesh;
  46.         gradientPlane.renderer.material = mat;
  47.         gradientPlane.layer = gradientLayer;
  48.         gradientPlane.transform.parent = container;
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment