Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by aronanders · Mar 25, 2017 at 08:52 AM · shadersbackgroundgradient

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!

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Namey5 · Mar 25, 2017 at 10:08 PM 0
Share

$$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?

avatar image aronanders · Mar 27, 2017 at 05:45 PM 0
Share
 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]}
     }
 }
  
 }
avatar image aronanders aronanders · Mar 27, 2017 at 05:47 PM 0
Share

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?

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Rendering full-screen gradient background without overdraw? 1 Answer

How to create a shader for a dynamic linear gradient 0 Answers

Unlit Shader with Shadows from point lights. 1 Answer

Gradient shader on a 3D plane 0 Answers

Gradient Background Color,Gradient Background color 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges