- Home /
What vertex shader should I be using?
Hi, I'm completely new to shaders and was wondering if someone could help me out. My end goal is to make a background gradient that can be seen form every angle. Additionally, I would like to be able to change the colors that the gradient consists of during run time. I found some code to help me with this and updated it, but I am still clueless as to what vertex shader I should be using. The ones I tried did not work for me.
In this code a plane with the gradient colors is created, which only the background camera can see:
using UnityEngine;
public class GradientBackground : MonoBehaviour {
public Color topColor = Color.blue;
public Color bottomColor = Color.white;
public int gradientLayer = 7;
public Shader myShader;
void Awake () {
gradientLayer = Mathf.Clamp(gradientLayer, 0, 31);
transform.GetComponent<Camera>().clearFlags = CameraClearFlags.Depth;
transform.GetComponent<Camera>().cullingMask = transform.GetComponent<Camera>().cullingMask & ~(1 << gradientLayer);
Camera gradientCam = new GameObject("Gradient Cam",typeof(Camera)).GetComponent<Camera>();
gradientCam.depth = transform.GetComponent<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 (myShader);
GameObject gradientPlane = new GameObject("Gradient Plane", typeof(MeshFilter), typeof(MeshRenderer));
((MeshFilter)gradientPlane.GetComponent(typeof(MeshFilter))).mesh = mesh;
gradientPlane.GetComponent<Renderer>().material = mat;
gradientPlane.layer = gradientLayer;
}
}
Help is greatly appreciated!
$$anonymous$$inda hard to help with a shader if you don't actually post the shader. I assume you would need to be accessing vertex colours, etc. But would you $$anonymous$$d posting the shader too?
Shader "mobile_VertexBlend_2tex_lightmap" {
Properties {
_$$anonymous$$ainTex ("Texture 1 (paint 0 alpha)", 2D) = ""
_Texture2 ("Texture 2 (paint 1 alpha)", 2D) = ""
}
SubShader { // Realtime lighting (vertex)
Pass {
BindChannels {
Bind "vertex", vertex
Bind "color", color
Bind "texcoord", texcoord
}
SetTexture[_$$anonymous$$ainTex]
SetTexture[_Texture2] {Combine texture Lerp(primary) previous}
}
Pass {
Blend DstColor SrcColor
Lighting On $$anonymous$$aterial {Ambient(1,1,1) Diffuse(1,1,1)}
}
}
SubShader {
Pass {
Tags {"Light$$anonymous$$ode"="VertexL$$anonymous$$"}
GLSLPROGRA$$anonymous$$
varying lowp float blend;
varying lowp vec4 uv1and2;
#ifdef VERTEX
uniform lowp mat4 unity_Lightmap$$anonymous$$atrix;
void main() {
gl_Position = gl_$$anonymous$$odelViewProjection$$anonymous$$atrix * gl_Vertex;
blend = gl_Color.a;
uv1and2 = vec4(gl_$$anonymous$$ultiTexCoord0.xy,
vec2(unity_Lightmap$$anonymous$$atrix[0].x, unity_Lightmap$$anonymous$$atrix[1].y) * gl_$$anonymous$$ultiTexCoord1.xy
+ unity_Lightmap$$anonymous$$atrix[3].xy);
}
#endif
#ifdef FRAG$$anonymous$$ENT
uniform lowp sampler2D unity_Lightmap, _$$anonymous$$ainTex, _Texture2;
void main() {
gl_FragColor = vec4(texture2D(unity_Lightmap, uv1and2.zw).rgb * 2.
* mix(texture2D(_$$anonymous$$ainTex, uv1and2.xy).rgb, texture2D(_Texture2, uv1and2.xy).rgb, blend), 1);
}
#endif
ENDGLSL
}
Pass {
Tags {"Light$$anonymous$$ode"="VertexL$$anonymous$$RGB$$anonymous$$"}
BindChannels {
Bind "vertex", vertex
Bind "color", color
Bind "texcoord", texcoord
Bind "texcoord1", texcoord2
}
SetTexture[_$$anonymous$$ainTex]
SetTexture[_Texture2] {Combine texture Lerp(primary) previous}
SetTexture[unity_Lightmap] {$$anonymous$$atrix[unity_Lightmap$$anonymous$$atrix] Combine previous * texture Double}
}
}
SubShader {
Tags {"Light$$anonymous$$ode"="VertexL$$anonymous$$"}
Pass {
BindChannels {
Bind "vertex", vertex
Bind "color", color
Bind "texcoord", texcoord
}
SetTexture[_$$anonymous$$ainTex]
SetTexture[_Texture2] {Combine texture Lerp(primary) previous}
}
Pass {
Blend DstColor SrcColor
BindChannels {
Bind "vertex", vertex
Bind "texcoord1", texcoord0
}
SetTexture[unity_Lightmap] {$$anonymous$$atrix[unity_Lightmap$$anonymous$$atrix]}
}
}
}
This is the shader I was trying to use. Like I said, I don't know what I'm doing when it comes to shaders. How would I make this shader work for my GradientBackground script?
Your answer
![](https://koobas.hobune.stream/wayback/20220612113805im_/https://answers.unity.com/themes/thub/images/avi.jpg)