I cannot use skybox as my camera angle is top down, so the player would never see the horizon and thus never see the gradient.
I just need a static 2d gradient color background which doesn’t change when the camera moves. If a shader script is needed, I need some guidance as I am new to shaders.
3 Answers
3
you can simply use a 2d plane with your background assigned as a texture. just parent it to the camera
For people that stumbled upon here, you can try this script to test a gradient color background
// GradientBg.cs
// This script demonstrates how to create a gradient color background for the main camera.
// to use this script:
// 1. have Main Camera is in the scene
// 2. create an Empty GameObject in the scene
// 3. attach this script to the Empty GameObject
// 4. press play
// 5. if you see a green to cyan box, good, now just change the BackgroundPlaneVerteices to fill your camera viewport
using UnityEngine;
using UnityEngine.Rendering;
public class GradientBg : MonoBehaviour
{
void Start()
{
// put the backgorund plane in front of camera (you can change this later);
this.transform.position = new Vector3(0, 0, 1f);
// position and orient main camera and the background plane
this.transform.rotation = Quaternion.identity;
Camera.main.transform.position = Vector3.zero;
Camera.main.transform.rotation = Quaternion.identity;
Camera.main.clearFlags = CameraClearFlags.SolidColor;
//set background's plane transform parent to be the camera's transform, so the plane doesnt move as camera moves
this.transform.parent = Camera.main.transform;
// create the background plane
MeshFilter mf = this.gameObject.AddComponent<MeshFilter>();
MeshRenderer mr = this.gameObject.AddComponent<MeshRenderer>();
Material mat = new Material(Shader.Find("Sprites/Default"));
// set the proper renderering order for the background plane
mat.renderQueue = ((int)RenderQueue.Background);
mat.color = Color.white;
mr.material = mat;
mr.enabled = true;
// setting the background plane's 4 corner positions ( you def want to change them later )
Vector3[] BackgroundPlaneVerteices = new Vector3[4];
BackgroundPlaneVerteices[0] = new Vector3(0, 0, 0) * 0.25f;
BackgroundPlaneVerteices[1] = new Vector3(1, 0, 0) * 0.25f;
BackgroundPlaneVerteices[2] = new Vector3(1, 1, 0) * 0.25f;
BackgroundPlaneVerteices[3] = new Vector3(0, 1, 0) * 0.25f;
//define the order in which the vertices in the BackgroundPlaneVerteices shoudl be used to draw the triangle
int[] trianglesArray = new int[6];
trianglesArray[0] = 0;
trianglesArray[1] = 1;
trianglesArray[2] = 2;
trianglesArray[3] = 2;
trianglesArray[4] = 3;
trianglesArray[5] = 0;
mf.mesh.vertices = BackgroundPlaneVerteices;
mf.mesh.triangles = trianglesArray;
// here to create gradient color
Color[] colors = new Color[mf.mesh.vertices.Length];
colors[0] = Color.green;
colors[1] = Color.green;
colors[2] = Color.cyan;
colors[3] = Color.cyan;
mf.mesh.colors = colors;
}
}
you can simply use a 2d plane with your background assigned as a texture. just parent it to the camera
– toddisarockstar