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 Matthew Scott · Mar 14, 2014 at 12:28 AM · shadersvertexsurfacecolors

Can vertex colours be SET in a surface shader? Or must it be done through code?

As i said, I would very much like to know if this is at all possible, I have researched thoroughly and have only found shader examples which READ vertex colours and do something like multiply them with the diffuse.

I want to do the opposite, and SET vertex colours in my surface shader after some appropriate math has been done, then read the new value to multiply it with my diffuse.

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 Dep · Mar 14, 2014 at 01:02 AM 0
Share

Specify do you want to save the new colors to be used elsewhere and where. Or do you just want to calculate the vertex color every time?

Edit: Re-read the question a couple of times. Doesn't make much sense to me as the both paragraphs essentially mean the same thing.

avatar image Matthew Scott · Mar 14, 2014 at 01:17 AM 0
Share

Essentially, I'm trying to calculate a vertex colour for each vertex by sampling the world pos of each vert and doing a dot product calculation against a predefined VectorPos which is sent to the shader via scripting. I will be mulitplying a colour (also sent to the shader via scripting with the result of this dot product. I then want to apply the resulting colour to that vertex.

The resulting colour will most likely need a dot clamped at 0-1 at least. I intend to then blend the pixels using the vert colours. I could also use Saturate() here ins$$anonymous$$d of a clamped dot product if I'm not mistaken...

avatar image Matthew Scott · Mar 14, 2014 at 01:33 AM 0
Share

It's simple really....(in pseudo)

Ins$$anonymous$$d of...(which there are examples of everywhere)

 col = vertex color;
 pixel = diffuse * col;

I'm wondering whether you can do this...

 _SomeColour (1, 1, 1, 1);
 newCol = some fancy math * _SomeColour;
 vertex color = newCol;
 pixel = diffuse * newCol;

All I wanna know is whether I can do something like this...

v.color.rgb = newCol;

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Dep · Mar 14, 2014 at 03:11 AM

Now there is no reason to use surface shader to set vertex colors, but here's a surface + vertex shader that does the job.

 Shader "Custom/PositionShade" {
     Properties {
         _MainTex ("Base (RGB)", 2D) = "black" {}
     }
     SubShader {
         Tags { "RenderType"="Opaque" }
         LOD 200
         
         CGPROGRAM
         #pragma surface surf Lambert vertex:vert
 
         void vert (inout appdata_full v)
         {
             float3 worldPos = mul (_Object2World, v.vertex).xyz;
             v.color.r = worldPos.x;
             v.color.g = worldPos.y;
             v.color.b = worldPos.z;
         }
 
         sampler2D _MainTex;
 
         struct Input {
             float2 uv_MainTex;
             float4 color : COLOR;
         };
 
         void surf (Input IN, inout SurfaceOutput o) {
             half4 c = tex2D (_MainTex, IN.uv_MainTex);
             o.Albedo = c.rgb;
             o.Albedo.r += IN.color.r;
             o.Albedo.g += IN.color.g;
             o.Albedo.b += IN.color.b;
             o.Alpha = c.a;
         }
         ENDCG
     } 
 }


The vertex color is modified in the vertex shader "vert" based on it's world position. Then later in surface shader "surf" I add the vertex color to the pixel color. Hope this get's you started.

Comment
Add comment · Show 1 · 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 Matthew Scott · Mar 15, 2014 at 06:56 PM 0
Share

I'm not having a problem writing the surface shader with vert colours, it doesn't matter, I'm using another tactic to achieve what I need. Thanks anyway...

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

21 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

Related Questions

Modifying Wireframe Vertex Shader to Include Lighting & Shadows 1 Answer

Vertex program max instructions limit 2 Answers

Shader add vertex displacement 0 Answers

How to change editor highlighting behavior? 0 Answers

Custom shader converting to urp need help 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