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
1
Question by Santolin · Apr 03, 2014 at 10:58 PM · shadersurfacegradientworld space

World/screen space gradient overlay in surface shader

I'm trying to make a surface shader that lays a gradient blending two colors over the mesh. The gradient would always work in the vertical direction, from the cottom vertex to the top either in world or screen space, maintaining it's direction regardless of the of the transformations done to the mesh.

This mockup shows more or less what I mean: alt text

I've tried with a vertex shader:

 struct v2f {
     float4 pos : SV_POSITION;
     fixed4 color : COLOR;
 }
 
 v2f vert (appdata_full v) {
     v2f o;
     o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
     o.col = lerp(_Color1,_Color2, v.vertex.y);
     return o;
 }
 
 float4 frag (v2f i) : COLOR {
     float4 c = i.color;
     return c;
 }

but the vertex colors remain painted the same color regardless of how the mesh is transformed. So if the cube in the picture above was turned upside down, the gold part would face bottom, which is not what I'm looking for. Furthermore I was unable to pass the functions to a surface shader, and I would like to combine the snippet with other stuff.

I'd be grateful for any directions on how to achieve this.

gradient2.png (484.5 kB)
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

Answer by gnp89 · May 27, 2014 at 08:44 PM

You are using de vertex.y in model space, thats why if you make some transformations the color is always the same. For the lerp, you should use the vertex.y but in world space. So try

 o.col = lerp(_Color1,_Color2, o.pos.y);


Note: the third parameter in lerp must should be between 0 and 1. If its not, it will probably clamp it and the color won't be the one you want. To achieve this you should calculate the lerp weight as follows:

 lerpWeight = (o.pos.y - lowestVertex.y) / (highestVertex.y - lowestVertex.y)

and then

 o.col = lerp(_Color1,_Color2, lerpWeight);

The challenge here is to calculate the lowest and highest (using their 'y' value) vertices for that model in world space, for every frame. I'm not sure if you can do that.

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 gnp89 · May 27, 2014 at 08:52 PM 0
Share

$$anonymous$$aybe you can calc the highest and lowest in a first pass and in a second pass set the color. Anyone knows if this can be done?

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

Error in shadow received by vertex displacement shader 1 Answer

Why the shader responds differently to Tri-color ambient light vs. Skybox lighting? 0 Answers

Vertex program max instructions limit 2 Answers

Gradient over multiple tiled objects 1 Answer

Additional blending when using properties rather than hard-coded values for colors 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