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 Balphagore · Mar 26, 2020 at 09:36 AM · shaders3d models

How to draw a line on a mesh edge using a shader

I tried a lot of different options, like a outline, a wireframe shader, but they all do not give the desired effect.

 Shader "Custom/WireframeShader"
 {
     Properties
     {
         _WireColor("Edges Color", Color) = (0,0,0,1)
         _WireThickness("Edge Thickness", RANGE(0, 800)) = 100
         _WireSmoothness("Edge Smoothness", RANGE(0, 20)) = 3
         _AlbedoColor("Albedo Color", Color) = (1,1,1,1)
         _MainTex("Albedo (RGB)", 2D) = "white" {}
     }
 
         SubShader
     {
         Tags{ "RenderType" = "Opaque" }
         LOD 200
 
         Pass
     {
         CGPROGRAM
         // Use shader model 3.0 target, to get nicer looking lighting
 #pragma target 3.0
 
 #include "UnityCG.cginc"
 
 #pragma vertex vert
 #pragma fragment frag
 #pragma geometry geom
 
         uniform sampler2D _MainTex;
     uniform float4 _MainTex_ST;
 
     struct Input
     {
         float2 uv_MainTex;
     };
 
     fixed4 _WireColor;
     fixed4 _AlbedoColor;
 
     uniform float _WireThickness;
     uniform float _WireSmoothness;
 
     struct appdata
     {
         float4 vertex : POSITION;
         float2 texcoord0 : TEXCOORD0;
         UNITY_VERTEX_INPUT_INSTANCE_ID
     };
 
     struct v2g
     {
         float4 projSpaceVertex : SV_POSITION;
         float2 uv0 : TEXCOORD0;
         float4 worldSpacePosition : TEXCOORD1;
         UNITY_VERTEX_OUTPUT_STEREO
     };
 
     struct g2f
     {
         float4 projSpaceVertex : SV_POSITION;
         float2 uv0 : TEXCOORD0;
         float4 worldSpacePosition : TEXCOORD1;
         float4 dist : TEXCOORD2;
         UNITY_VERTEX_OUTPUT_STEREO
     };
 
     v2g vert(appdata v)
     {
         v2g o;
         UNITY_SETUP_INSTANCE_ID(v);
         UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
         o.projSpaceVertex = UnityObjectToClipPos(v.vertex);
         o.worldSpacePosition = mul(unity_ObjectToWorld, v.vertex);
         o.uv0 = TRANSFORM_TEX(v.texcoord0, _MainTex);
         return o;
     }
 
 
     [maxvertexcount(3)]
     void geom(triangle v2g i[3], inout TriangleStream<g2f> triangleStream)
     {
         float2 p0 = i[0].projSpaceVertex.xy / i[0].projSpaceVertex.w;
         float2 p1 = i[1].projSpaceVertex.xy / i[1].projSpaceVertex.w;
         float2 p2 = i[2].projSpaceVertex.xy / i[2].projSpaceVertex.w;
 
         float2 edge0 = p2 - p1;
         float2 edge1 = p2 - p0;
         float2 edge2 = p1 - p0;
 
         float area = abs(edge1.x * edge2.y - edge1.y * edge2.x);
         float wireThickness = 800 - _WireThickness;
 
         g2f o;
 
         o.uv0 = i[0].uv0;
         o.worldSpacePosition = i[0].worldSpacePosition;
         o.projSpaceVertex = i[0].projSpaceVertex;
         o.dist.xyz = float3((area / length(edge0)), 0.0, 0.0) * o.projSpaceVertex.w * wireThickness;
         o.dist.w = 1.0 / o.projSpaceVertex.w;
         UNITY_TRANSFER_VERTEX_OUTPUT_STEREO(i[0], o);
         triangleStream.Append(o);
 
         o.uv0 = i[1].uv0;
         o.worldSpacePosition = i[1].worldSpacePosition;
         o.projSpaceVertex = i[1].projSpaceVertex;
         o.dist.xyz = float3(0.0, (area / length(edge1)), 0.0) * o.projSpaceVertex.w * wireThickness;
         o.dist.w = 1.0 / o.projSpaceVertex.w;
         UNITY_TRANSFER_VERTEX_OUTPUT_STEREO(i[1], o);
         triangleStream.Append(o);
 
         o.uv0 = i[2].uv0;
         o.worldSpacePosition = i[2].worldSpacePosition;
         o.projSpaceVertex = i[2].projSpaceVertex;
         o.dist.xyz = float3(0.0, 0.0, (area / length(edge2))) * o.projSpaceVertex.w * wireThickness;
         o.dist.w = 1.0 / o.projSpaceVertex.w;
         UNITY_TRANSFER_VERTEX_OUTPUT_STEREO(i[2], o);
         triangleStream.Append(o);
     }
 
     fixed4 frag(g2f i) : SV_Target
     {
         float minDistanceToEdge = min(i.dist[0], min(i.dist[1], i.dist[2])) * i.dist[3];
 
     float4 baseColor = _AlbedoColor * tex2D(_MainTex, i.uv0);
 
     // Early out if we know we are not on a line segment.
     if (minDistanceToEdge > 0.9)
     {
         return fixed4(baseColor.rgb,0);
     }
 
     // Smooth our line out
     float t = exp2(_WireSmoothness * -1.0 * minDistanceToEdge * minDistanceToEdge);
     fixed4 finalColor = lerp(baseColor, _WireColor, t);
     finalColor.a = t;
 
     return finalColor;
     }
         ENDCG
     }
     }
         FallBack "Diffuse"
 }

For example, such a shader gives the following result: alt text

How can I leave only the outer edges of the mesh?

безымянныи.png (136.6 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 Milun · May 19, 2020 at 12:14 PM

Hi; I don't know much about shaders, I was just looking around for a similar shader to the one you're requesting. However, perhaps this tutorial could be of use to you?

https://roystan.net/articles/outline-shader.html

Or this one:

https://forum.unity.com/threads/image-effect-edge-detect-normals-colours-rel.310280/

Furthermore, while I couldn't tell you how to script it, I know if you can somehow determine, for each edge, if the culling on its two faces doesn't match (as in, if one face is culled, the other isn't), then it's an outer edge.

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

138 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

Related Questions

texture looks pix-elated where-ever there is shininess and lighting 1 Answer

How Do I Achieve an Additively Blended Look with a 3D Object? 1 Answer

Help Turning Sprites into 3D Objects and Casting Shadows 1 Answer

Correct Workflow (QC fbx to Unity) 0 Answers

When I Import models from blender they have a yellow-brown tint 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