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 rustofelees · Dec 04, 2014 at 11:24 PM · shadermeshrenderer

Rendering hard edges via vertex shader

Is there any way to use a vertex shader to draw all edges as hard even though they have been imported from Maya as soft? I'd like to have a hard edge look to my geometry but I would prefer that the vertices not be split into multiple vertices. Seems as though a vertex shader could work but I'm completely new to writing them. I've read some of the docs on this but would appreciate guidance in how something like this might be achieved.

http://docs.unity3d.com/Manual/SL-VertexProgramInputs.html

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

2 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by Bunny83 · Dec 08, 2014 at 01:10 PM

As @tanoshimi said a vertex shader just processes each vertex once. So each vertex will have exactly one normal vector. If that vertex is shared by multiple faces / triangles they will be smooth at this point. A vertex shader can't change this.

However there are ways to get the actual face normal by using the partial derivative functions ddx and ddy in the fragment shader. With those you are able to get two tangent vectors at the current screenspace point which describe your surface plane. Take the crossproduct and normalize it and you have the normal vector of the flat surface. However the derivative functions are quite demanding. You literally are calculating the normal for each fragment.

Something like that:

 // in the cg fragment shader
 float3 x = ddx(IN.pos);  // IN.pos is the screen space vertex position
 float3 y = ddy(IN.pos);
 float3 normal = normalize(cross(x, y));

It all comes down to if your target platform supports the derivative functions and how they perform on that platform. In most cases it's more performant to split the vertices.

Comment
Add comment · Show 7 · 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 CHPedersen · Dec 08, 2014 at 01:41 PM 0
Share

Good answer. :)

avatar image rustofelees · Dec 08, 2014 at 05:38 PM 0
Share

Sounds like I'll need to split the vertices anyway. Very insightful Bunny83 :) Thank you.

avatar image tanoshimi · Dec 08, 2014 at 06:07 PM 0
Share

Alternatively, you could calculate the face normals for each triangle in the mesh.triangles array and assign this to each vertex using an otherwise unused vertex attribute (tangent, say). Then create a shader with a custom lighting model that uses this "face normal" value in place of the vertex normal.

avatar image rustofelees · Dec 08, 2014 at 08:37 PM 0
Share

Any gut thoughts on how expensive this might be?

avatar image tanoshimi · Dec 08, 2014 at 08:40 PM 0
Share

It depends how often you'd need to recalculate the normals. Is your mesh dynamic?

Show more comments
avatar image
1

Answer by tanoshimi · Dec 05, 2014 at 01:05 PM

I don't think you want a vertex shader - simply try on the "model" tab of the import settings to set "Normals" to "Calculate", and "Smoothing Angle" to 0"

Comment
Add comment · Show 3 · 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 rustofelees · Dec 05, 2014 at 06:28 PM 0
Share

Thank you for your response, but it's not quite what I'm looking for. I'm trying to prevent unity from splitting vertices when displaying hard edges. Whether I make edges hard in $$anonymous$$aya, or use the approach that you recommended, Unity continues to split the vertices, as it needs 3 normals to correctly display the hard edge.

However, if Unity can take a smoothed mesh from $$anonymous$$aya and and render it as hard edged when imported, I would assume there is a solution that will do this at runtime - hence the mention of vertex shader. Becuase I'm doing vertex manipulation at runtime, I would prefer to only modify a single vertex ins$$anonymous$$d of having to keep track of 3 at time.

avatar image rustofelees · Dec 08, 2014 at 06:16 AM 0
Share

I'm still hoping someone has some advice on how to make this happen. Does anyone have thoughts on an approach to try?

avatar image tanoshimi · Dec 08, 2014 at 06:57 AM 0
Share

I'm pretty sure flat shading requires the duplication of vertices to make them all share the same normal so that interpolation remains constant across a face. Vertex shaders cannot create new primitives - they can only modify existing vertices.

If you really wanted to do this at runtime, you could probably do so with a geometry shader, but that would limit you to DX11 platforms, and the shader would be doing more work and still have to render the same duplicated vertices at the end of the process.

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

26 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

Related Questions

Mesh rendered wierdley without depth buffer 1 Answer

Why is only the last Material in the Materials array on a Mesh Renderer used? 2 Answers

What are normals? 1 Answer

How to realize Unity Editor Scene Filter View Effect in my game. 0 Answers

GPU performance with tris and verts 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