Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 slek120 · Mar 24, 2014 at 05:30 PM · shadermaterialcolorlinesgl

How do I change GL LINES color?

I want to draw each line with a different color. I created a material with the Diffuse shader and use GL.Color like the script reference says but pass 0 is just black, pass 1 is nothing, pass 2 is grey, and pass 3 is white no matter what color I use.

     Material mat;
     public Color[] colors = new Color[6] {
         Color.blue,
         Color.cyan,
         Color.green,
         Color.magenta,
         Color.red,
         Color.yellow
     };

     void Awake ()
     {
         mat = new Material (Shader.Find ("Diffuse"));
     }
 
     void OnPostRender ()
     {
         GL.PushMatrix ();
         mat.SetPass (0);
         GL.LoadOrtho ();
         GL.Begin (GL.LINES);
         for (int i = 0; i < 12; i++) {
             GL.Color (colors [i % 6]);
             GL.Vertex3 (0, i / 12f, 0);
             GL.Vertex3 (1, i / 12f, 0);
         }
         GL.End ();
         GL.PopMatrix ();
     }

What am I doing wrong?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by zharik86 · Mar 24, 2014 at 08:28 PM

Create the new material for GL, see below:

  private static Material mat;

  void Awake() {
   mat = new Material( "Shader \"Lines/Colored Blended\" {" +
     "SubShader { Pass { " +
     "    Blend SrcAlpha OneMinusSrcAlpha " +
     "    ZWrite Off Cull Off Fog { Mode Off } " +
     "    BindChannels {" +
     "      Bind \"vertex\", vertex Bind \"color\", color }" +
     "} } }" );
   mat.hideFlags = HideFlags.HideAndDontSave;
   mat.shader.hideFlags = HideFlags.HideAndDontSave;
  }

  void OnPostRender() {
   ...
  }

  void OnApplicationQuit() {
   DestroyImmediate(mat);
  }
Comment
Add comment · Show 4 · Share
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 slek120 · Mar 25, 2014 at 02:12 AM 0
Share

Thanks. This works great. Is there a reason for mat to be static? And why do you explicitly destroy mat on quit?

avatar image zharik86 · Mar 25, 2014 at 06:14 AM 0
Share

@slek120 The static material becomes in order that it could be used in other scripts without creating again. Therefore that it also should be destroyed when closing application, as it static. Certainly, you can use not static variable. Then everything remains the same, but thus to you, most likely, not to be necessary the OnAplicationQuit function. Therefore it is possible to delete it.

avatar image Caspar_ · Mar 06, 2016 at 11:00 AM 0
Share

Help! Why I only got Pink lines according your method? This question has puzzled for long time ...........

alt text

thank you in advance~

color_line code:


using UnityEngine; using System.Collections;

public class color_line : $$anonymous$$onoBehaviour { private static $$anonymous$$aterial mat; public Color[] colors = new Color[6] { Color.blue, Color.cyan, Color.green, Color.magenta, Color.red, Color.yellow };

 void Awake()
 {
     mat = new $$anonymous$$aterial("Shader \"Lines/Colored Blended\" {" +
       "SubShader { Pass { " +
       "    Blend SrcAlpha One$$anonymous$$inusSrcAlpha " +
       "    ZWrite Off Cull Off Fog { $$anonymous$$ode Off } " +
       "    BindChannels {" +
       "      Bind \"vertex\", vertex Bind \"color\", color }" +
       "} } }");
     mat.hideFlags = HideFlags.HideAndDontSave;
     mat.shader.hideFlags = HideFlags.HideAndDontSave;
 }

 void OnPostRender()
 {
     GL.Push$$anonymous$$atrix();
     mat.SetPass(0);
     GL.LoadOrtho();
     GL.Begin(GL.LINES);
     for (int i = 0; i < 12; i++)
     {
         GL.Color(colors[i % 6]);
         GL.Vertex3(0, i / 12f, 0);
         GL.Vertex3(1, i / 12f, 0);
     }
     GL.End();
     GL.Pop$$anonymous$$atrix();
 }

 void OnApplicationQuit()
 {
     DestroyImmediate(mat);
 }


 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
 
 }

}


无标题.png (224.5 kB)
avatar image ChrissTman Caspar_ · Nov 14, 2016 at 03:29 PM 0
Share

Try just choose existing material with shader named TextShader ( GUI/TextShader) :)

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

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Material doesn't have a color property '_Color' 4 Answers

Changing two different objects renderer colour 1 Answer

Is there a way to blend textures into a material based on a noise map? 1 Answer

Standard Shader Decals? 1 Answer

Car paint looks not good? 1 Answer


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