CameraGradientBackground

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Text replace - "<javascript>" to "<syntaxhighlight lang="javascript">")
m (Text replace - "</javascript>" to "</syntaxhighlight>")
 
Line 35: Line 35:
 
gradientPlane.renderer.material = mat;
 
gradientPlane.renderer.material = mat;
 
gradientPlane.layer = gradientLayer;
 
gradientPlane.layer = gradientLayer;
}</javascript>
+
}</syntaxhighlight>
  
 
== GradientBackground.cs ==
 
== GradientBackground.cs ==

Latest revision as of 20:52, 10 January 2012

Author: Eric Haines (Eric5h5)

Contents

[edit] Description

Creates a background for the camera, which is a simple gradient blend between two colors.

[edit] Usage

Attach this script to your camera, and change the top and bottom colors in the inspector as desired. When run, the clear flags for your camera is set to Depth Only, allowing a newly created background camera to show through. A plane with the gradient colors is created, which only the background camera can see. This is done with GradientLayer, which is the only layer that the background camera sees, and your camera is set to ignore. The default, 7, is a built-in (non-user-editable) layer that's not used for anything as of Unity 3.1. It can be changed as necessary.

[edit] GradientBackground.js

var topColor = Color.blue;
var bottomColor = Color.white;
var gradientLayer = 7;
 
function Awake () {
	gradientLayer = Mathf.Clamp(gradientLayer, 0, 31);
	if (!camera) {
		Debug.LogError ("Must attach GradientBackground script to the camera");
		return;
	}
 
	camera.clearFlags = CameraClearFlags.Depth;
	camera.cullingMask = camera.cullingMask & ~(1 << gradientLayer);
	var gradientCam = new GameObject("Gradient Cam", Camera).camera;
	gradientCam.depth = camera.depth-1;
	gradientCam.cullingMask = 1 << gradientLayer;
 
	var mesh = new Mesh();
	mesh.vertices = [Vector3(-100, .577, 1), Vector3(100, .577, 1), Vector3(-100, -.577, 1), Vector3(100, -.577, 1)];
	mesh.colors = [topColor, topColor, bottomColor, bottomColor];
	mesh.triangles = [0, 1, 2, 1, 3, 2];
 
	var mat = new Material("Shader \"Vertex Color Only\"{Subshader{BindChannels{Bind \"vertex\", vertex Bind \"color\", color}Pass{}}}");
	var gradientPlane = new GameObject("Gradient Plane", MeshFilter, MeshRenderer);
	(gradientPlane.GetComponent(MeshFilter) as MeshFilter).mesh = mesh;
	gradientPlane.renderer.material = mat;
	gradientPlane.layer = gradientLayer;
}

[edit] GradientBackground.cs

using UnityEngine;
 
public class GradientBackground : MonoBehaviour {
 
	public Color topColor = Color.blue;
	public Color bottomColor = Color.white;
	public int gradientLayer = 7;
 
	void Awake () {	
		gradientLayer = Mathf.Clamp(gradientLayer, 0, 31);
   		if (!camera) {
        	Debug.LogError ("Must attach GradientBackground script to the camera");
        	return;
    	}
 
    	camera.clearFlags = CameraClearFlags.Depth;
    	camera.cullingMask = camera.cullingMask & ~(1 << gradientLayer);
    	Camera gradientCam = new GameObject("Gradient Cam",typeof(Camera)).camera;
    	gradientCam.depth = camera.depth-1;
    	gradientCam.cullingMask = 1 << gradientLayer;
 
		Mesh mesh = new Mesh();
		mesh.vertices = new Vector3[4]
						{new Vector3(-100f, .577f, 1f), new Vector3(100f, .577f, 1f), new Vector3(-100f, -.577f, 1f), new Vector3(100f, -.577f, 1f)};
 
		mesh.colors = new Color[4] {topColor,topColor,bottomColor,bottomColor};
 
		mesh.triangles = new int[6] {0, 1, 2, 1, 3, 2};
 
		Material mat = new Material("Shader \"Vertex Color Only\"{Subshader{BindChannels{Bind \"vertex\", vertex Bind \"color\", color}Pass{}}}");
    	GameObject gradientPlane = new GameObject("Gradient Plane", typeof(MeshFilter), typeof(MeshRenderer));
 
		((MeshFilter)gradientPlane.GetComponent(typeof(MeshFilter))).mesh = mesh;
    	gradientPlane.renderer.material = mat;
    	gradientPlane.layer = gradientLayer;
	}
 
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox