Gradient background

Hello guys,

I want to achieve gradient backgrounds (linear and radial) with colors that are changeable from script. These backgrounds do not neccessarily need to be 3D backgrounds… I want something similar to after effects like when you have a 3D scene but with a 2D background that does not change with the camera movement. Is this even possible?
Could you lead me to the right direction, please?
It is for a scene that focuses on the objects. A camera can rotate around these objects and I want to have nicely colored backgrounds.
I thought of a radial (or linear) alpha texture that I would blend with a solid color plane but I don’t know how to begin as I am new to procedural textures.

Do you think working with skyboxes would be easier in this case? How would I achieve transition-free gradients then?

Thanks in advance!

Creating a linear gradient as a texture is trivial…all you need is to make a two-pixel texture, using the two colors in the gradient, and make sure bilinear filtering is on. The bilinear filtering creates the gradient for you.

As with most things in Unity, there are a variety of ways of creating gradients.

  1. From a performance standpoint, the best way is probably a shader. Take a look at this package in the Asset Store:

https://www.assetstore.unity3d.com/#/content/9403

If you poke around you may be able to find free version of the two shaders you need.

  1. If your gradients are one color…light to dark…you can use any shader that _Color property and a grayscale texture of the gradient.

  2. For a horizontal or vertical gradient only, you can use vertex colors. Vertex colors require a shader that supports Vertex colors. Few of the shaders that come with Unity support vertex colors, but there are a number in the Unity Wiki. Or for an absolutely minimal shader you can use this one:

    Shader “Custom/Vertex Colored” {
    Properties {
    }
    SubShader {
    Pass {
    ColorMaterial AmbientAndDiffuse
    }
    }
    }

Create Unity Quad (GameObject > CreateOther > Quad). Attach a material with a vertex shader to the quad and then attach this script:

#pragma strict

var startColor = Color.red;
var endColor = Color.blue;

function Start () {
	var mesh = GetComponent(MeshFilter).mesh;
	var colors = new Color[mesh.vertices.Length];
	colors[0] = startColor;
	colors[1] = endColor;
	colors[2] = startColor;
	colors[3] = endColor;
	mesh.colors = colors;
}
  1. You can create a procedural texture…that is generate the bitmap at runtime. Here is a link to get you started.

http://answers.unity3d.com/questions/589329/how-do-i-create-a-radial-gradient-texture-mask-usi.html

Eric5h5 - this works wonderfully,

How could I fade this imported image gradient, into another gradient.

A continuous loop of gradient (dark blue and teal for example) fading slowly into another gradient (light blue and bright green).

Game slowly changes colors? Possible in C# ??

For people that stumbled upon here, you can try this script to create 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 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;

        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;


    }


}