Unity 5 Gradient Shader

Hi,

I have been using the gradient shader script below, but after upgrading to Unity 5 is only seems to display a solid color. Anyone have any suggestions as to what might be wrong, or some other solution for getting a gradient background (without resorting to a texture)?

http://wiki.unity3d.com/index.php/CameraGradientBackground

Thanks,
Mark

The script you are using is more than three and a half years old and seems to be written for Unity3.1.

You should consider finding a more up-to-date script. This page from Unity Answers might point you to some better suited alternatives. There’s also a whole topic for gradient on Unity Answers found here.

Please note, that Gradients are now a Unity built-in feature. You might want to read the documentation to find out more.

as for the C# part of the original script, there are a number of warnings and errors caused by deprecated code:

  • warning CS0618: UnityEngine.Material.Material(string)' is obsolete: Creating materials from shader source string will be removed in the future. Use Shader assets instead.’
  • error CS0619: UnityEngine.Component.camera' is obsolete: Property camera has been deprecated. Use GetComponent() instead. (UnityUpgradable)’
  • error CS0619: UnityEngine.GameObject.renderer' is obsolete: Property renderer has been deprecated. Use GetComponent() instead. (UnityUpgradable)’

Running the Unity API Updater fixes most of the problems except the Material one. If you are willing to fix and share the code, I’m sure some people will thank you.

Done.

using UnityEngine;

[RequireComponent(typeof(Camera))]
public class GradientBackground : MonoBehaviour
{
   
    public Color topColor = Color.blue;
    public Color bottomColor = Color.white;
    public Shader Shader;
    public int gradientLayer = 7;
   
    void Awake ()
    {   
        Camera camera = this.gameObject.GetComponent<Camera>();
        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)).GetComponent<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);

        GameObject gradientPlane = new GameObject("Gradient Plane", typeof(MeshFilter), typeof(MeshRenderer));
       
        ((MeshFilter)gradientPlane.GetComponent(typeof(MeshFilter))).mesh = mesh;
        Renderer gradRend = gradientPlane.GetComponent<Renderer>();
        gradRend.material = mat;
        gradientPlane.layer = gradientLayer;
    }
   
}

And shader

Shader "Basic Color"
{
    Properties
    {
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200
       
        Pass {
            CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
                #include "UnityCG.cginc"
                struct vertexInput
                {
                    float4 vertex : POSITION;
                    fixed4 color : COLOR;
                };
               
                struct v2f {
                    float4 pos : SV_POSITION;
                    fixed4 color : COLOR;
                };
           
           
                v2f vert(vertexInput v)
                {
                    v2f o;
                    o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                    o.color = v.color;
                    return o;
                }
           
                sampler2D _MainTex;
           
                float4 frag(v2f IN) : COLOR
                {
                    return IN.color;
                }
            ENDCG
        }
    }
}
1 Like