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 Sommy1491 · Dec 08, 2017 at 11:51 AM · unity 5shadersshader programmingopenglglsl

How to pass vertex color information into GLSL shaders program

Hi, my question is how to pass per vertex color data into shader program in unity. Unlike in OpenGL c++ code, we define attributes and link it with shader program.

Example:

 float vertices []
         {
              //vertex pos                   //vertex color
             .0.0, 1.0, 0.0,                   1.0, 0.0, 0.0,
              1.0, 2.0, 1.0                    0.0, 1.0, 0.0
         }
 

and then we pass the data by parsing it based on size and offset size into vertex buffer. In unity "gl_vertex" take all vertex position data, but i want to pass color data too so that fragment shader can use it.

i want something like this,

  //vertex shader
     in vec4 color
     out vec4 Color
     void main()
     {
     gl_Position = gl_vertex //working fine
     Color = color //expecting
     }
     
     //fragment shader
     out vec4 fragcolor
     in vec4 Color
     void main()
     {
     outColor = Color
     }

Comment
Add comment · Show 1
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 meat5000 ♦ · Dec 08, 2017 at 01:16 PM 0
Share

I found the information in here

https://en.wikibooks.org/wiki/Cg_Program$$anonymous$$g/Unity

I hope its still relevant!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Dec 08, 2017 at 03:25 PM

Unity doesn't use specific vertex formats in their front-end interface to simplify the usage. The Mesh class has seperate arrays for the various vertex attributes:

  • vertices

  • normals

  • tangents

  • colors

  • uv

  • uv2

  • uv3

  • uv4

  • boneWeights


If you want to define any of those attributes you just have to assign the appropriate array. Keep in mind that the all those arrays need to have the same length as the vertices array if you want to specify them. Do no initialize any Attributes you don't need.


Also note that the above mentioned properties of the Mesh class are properties and not fields. So you can't do this:

 // this does NOT work
 mesh.vertices = new Vector3[32];
 mesh.vertices[0] = new Vector3(1, 2, 3);
 mesh.vertices[1] = new Vector3(4, 5, 6);
 // ...

When "reading" such a property Unity will actually create a new array. So those two "assignments" above (element 0 and 1) are pointless as the actual data isn't changed. To apply a change the property setter has to be invoked. So always use a temp variable or create the whole array at once

 var vertices = new Vector3[3];
 vertices[0] = new Vector3(1, 2, 3);
 vertices[1] = new Vector3(4, 5, 6);
 vertices[2] = new Vector3(7, 8, 8);
 mesh.vertices = vertices;
 var colors = new Color[vertices.Length];
 colors[0] = new Color(1,0,0);
 colors[1] = new Color(0,1,0);
 colors[2] = new Color(0,0,1);
 mesh.colors = colors;


 // Or like this:
 mesh.vertices = new Vertex3[]{
     new Vector3(1, 2, 3),
     new Vector3(4, 5, 6),
     new Vector3(7, 8, 8)
 };
 mesh.colors = new Color[]{
     new Color(1,0,0),
     new Color(0,1,0),
     new Color(0,0,1)
 };


Also keep in mind to actually define triangles with the triangles array

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

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

153 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 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 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 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 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 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 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

Compile Shader Graph to GLSL 0 Answers

Can one write glsl shaders in Unity 2017? I cant make this shader work. The expected color should be red but I get black. 0 Answers

Shader fresnel effect working in URP but not working in HDRP 0 Answers

Shader to dilate a binary image 2 Answers

How to make a plane to sphere using vertex displacement shader graph, with respect to the camera position? 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