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
3
Question by daterre · Nov 07, 2012 at 08:26 PM · meshshaderspolygons

Hiding triangles on a mesh (via shader? script?)

I'm creating a set of polygon-based effects (scripts) that can be applied to objects with mesh-related components. One thing I would like to do is hide/show certain polygons (triangles), to create effects such as random flickering of polygons and ability to hide polys by clicking on them.

A simple method that can work is to simply reconstruct the Mesh.triangles array without the vertices of the triangle to be hidden. I'd have to rebuild the Mesh.normals array as well in that case. The problem is however that calling this repeatedly would require constantly changing and recalculating the mesh, which hardly seems to be suitable for real-time effects such as the aforementioned random flickering, and would also screw up MeshCollider and InteractiveCloth behavior.

I have very little (if any) knowledge of shaders, but I'm guessing they are probably a better approach than scripting. What kind of shader would I use, and what would I supply it? For random on/off I'm assuming I can determine this in the shader, but for hiding specific triangles, is it possible to pass into the shader an array of triangle indices (as a 2D texture maybe)?

Thanks for any help.

Comment
Add comment · Show 10
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 whydoidoit · Nov 07, 2012 at 09:50 PM 0
Share

Are you using triangle fans or strips? In other words are the vertices shared by multiple triangles?

From what I know the problem is that in the vertex shader you just have one vertex - if that vertex was legitimately in one visible triangle and one hidden triangle then I can't think how you would do it. If the vertices are unique then it would be possible.

avatar image whydoidoit · Nov 07, 2012 at 09:53 PM 1
Share

If I were you I would be setting the uv2 of the vertex to be a vertex index in the shader and use a 2D texture to control whether the vertex is visible or not - if the vertex is to be invisible then I would return a normal for that vertex that was the same as the current view direction (basically pointing it away from the camera and hence culled if back face culling is turned on).

But you have to do it at the vertex level (change all three vertices for a triangle at once) not at the triangle level. $$anonymous$$y Noobs Guide To Shaders has stuff one writing shaders if you are new to it.

avatar image whydoidoit · Nov 07, 2012 at 09:55 PM 0
Share

Actually it might be too late to turn the normal on consideration. You would probably be best off indicating that it should be a transparent colour if the normal dot the view direction is beyond visible or discard the fragment in that case (though that can be expensive).

avatar image daterre · Nov 07, 2012 at 09:55 PM 0
Share

I'm not necessarily in control of that since I'd like to apply it to any given mesh. But since this is ultimately for a specific project I could have the artists do that. I could also in theory dynamically rebuild the mesh so that each vertex is used by only one triangle.

avatar image whydoidoit · Nov 08, 2012 at 09:26 AM 1
Share

If all vertices are unique then the interpolation between the vertices of the triangle will yield a constant u (in uv2) - you can use that to lookup into a texture which now contains an rgba value to multiply your colour by. So you could make triangles fade out and fade in using that method (or change colour tint).

Show more comments

4 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by aldonaletto · Nov 07, 2012 at 10:46 PM

I would simply reverse the winding order of the desired triangles: since most shaders use Cull Back, triangles facing the opposite side (with reversed winding order) are invisible:

 function RevTriangle(numTriangle: int){
   var mesh : Mesh = GetComponent(MeshFilter).mesh;
   var triIndex = numTriangle * 3;
   // swap two vertex indexes in the tri:
   var tmp: int = mesh.triangles[triIndex];
   mesh.triangles[triIndex] = mesh.triangles[triIndex+1];
   mesh.triangles[triIndex+1] = tmp;
 }

This method is simple, but may produce weird results when the camera moves in such a direction that makes the "invisible" triangles face it. Another possible solution (not tested!) could be to null the triangle: set all 3 triangle vertices to the same vertex index, so that the resulting triangle have zero area:

 function EmptyTriangle(numTriangle: int){
   var mesh : Mesh = GetComponent(MeshFilter).mesh;
   var triIndex = numTriangle * 3;
   // make the three vertex equal to the first one:
   mesh.triangles[triIndex+1] = mesh.triangles[triIndex];
   mesh.triangles[triIndex+2] = mesh.triangles[triIndex];
 }

I've not tested this alternative, but based solely in logic arguments it should work perfectly: all GPUs should handle correctly zero area triangles, since small far triangles eventually fall in this case.
Notice that none of the above alternatives change the vertices, uvs, normals etc. - only the desired triangles are affected, and even the triangle count remains the same, thus both are very fast.

Comment
Add comment · Show 2 · 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 daterre · Nov 08, 2012 at 09:19 AM 0
Share

I had thought of the reversal method but like you say it won't work with anything but a static camera and mesh.

The empty method is a great idea. I implemented it and it works. 2 things though: 1. Won't this still mess with $$anonymous$$eshCollider and InteractiveCloth components that use the geometry for computing physics, animation etc.? 2. Weird behavior in the editor - apparently after modifying the shared$$anonymous$$esh of a Skinned$$anonymous$$eshRenderer in runtime, the editor doesn't reload the original mesh from the assets library (I'm using beta 4). I have to restart the editor to get the intact model back.

avatar image aldonaletto · Nov 08, 2012 at 01:09 PM 0
Share

AFAI$$anonymous$$, the $$anonymous$$eshCollider isn't updated automatically (we have to assign the modified mesh to collider.shared$$anonymous$$esh in order to update it) thus the hole in the mesh should not affect collisions. But things seem different with the InteractiveCloth: apparently, any change in the mesh may affect the behaviour of neighbor triangles. If this is true, only UV manipulation and transparent shaders could produce transparency without affecting other triangles, but this would require non-shared vertices.

avatar image
1

Answer by mitaywalle · May 22, 2018 at 12:11 PM

Simplies way is to mark vertex indexes, that you want to hide, make their vertex Color.a = 0, then use Particle Standard Surface shader, in cutout mode, or make any cutout shader use vertex Color alpha for hidealt text


image-2018-05-22-00-11-392222.png (173.5 kB)
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
avatar image
0

Answer by DaveA · Nov 07, 2012 at 10:09 PM

Depending on how many triangles you have, you may want to construct submeshes, one per triangle (or group of them) you would be hiding, and then adjust the alpha (say) of the material corresponding to that submesh.

Comment
Add comment · Show 2 · 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 daterre · Nov 08, 2012 at 09:05 AM 0
Share

I did that originally but it's really only for very few triangles, not for animated skinned meshes.

avatar image DaveA · Nov 08, 2012 at 11:19 PM 0
Share

Doesn't have anything to do with animation or skinned meshes. Any mesh can have submeshes, thus, multiple materials.

avatar image
0

Answer by StephenWebb · Sep 27, 2019 at 11:46 AM

Use a mask with a cutout or transparent shader. I was going to write an envelopment system - using the normals of the "bottom most" mesh to cast a ray into the scene and if it came into contact with another face on a mesh within a certain distance (like a piece of clothing), it would hide the verts from which the ray was cast...but it then occurred to me that if the UVs are set up properly on your mesh, you should be able to provide a simple black/white mask in the opacity channel to cover areas of the mesh you want to hide...

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

15 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

Related Questions

How do I make a horizontal gradient shader for a 2D mesh? 1 Answer

Outline flat mesh 1 Answer

Get mesh size in frag/vert shader 1 Answer

Hiding parts of mesh but only on defined objects 0 Answers

How do I debug the following error DirectX 11 shader input signature is null ? 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