Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 andyMill · Dec 02, 2017 at 12:44 PM · shader programmingcoordinatestrianglesfragmentgeometry shader

Getting triangle center in fragment shader

Hello,

I am creating a custom shader to display a point cloud. The shader creates a triangle for each vertex centered on the vertex itself.

I would like to color the triangle as a circle. More in detail, I am trying to set the alpha of each pixel decreasing as it gets further away from the center of the triangle.

However my code right now always displays alpha = 1

How might I solve this problem?

 Shader "Custom/SpherePoint" {
 Properties {
     //_Color ("Color", Color) = (1,1,1,1)
     _Radius ("Sphere Radius", float) = 0.01
 }
 SubShader {
     Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
     ZWrite Off
     Blend SrcAlpha OneMinusSrcAlpha
     LOD 200
     Pass {
         CGPROGRAM
         #pragma vertex vert
         #pragma fragment frag
         #pragma geometry geom

         #pragma target 4.0                    // Use shader model 3.0 target, to get nicer looking lighting
         #include "UnityCG.cginc"

         struct vertexIn {
             float4 pos : POSITION;
             float4 color : COLOR;
         };

         struct vertexOut {
             float4 pos : SV_POSITION;
             float4 color : COLOR;
             float3 normal : NORMAL;
         };

         struct geomOut {
             float4 pos : SV_POSITION;
             float4 color : COLOR;
             float3 normal : NORMAL;
             float3 triangleCenter : TANGENT;
             float norm : PSIZE;
         };

         //Vertex shader: computes normal wrt camera
         vertexOut vert (vertexIn i) {
             vertexOut o;
             //o.pos = i.pos;
             //o.pos = UnityObjectToClipPos(i.pos);
             o.pos = mul(unity_ObjectToWorld, i.pos);
             o.color = i.color;
             //o.normal = normalize(_WorldSpaceCameraPos - mul(unity_ObjectToWorld, o.pos).xyz);    //normal is towards the camera
             o.normal = ObjSpaceViewDir(o.pos);
             //o.normal = i.normal;
             return o;
         }

         float _Radius;    

         //Geometry shaders: Creates an equilateral triangle with the original vertex in the orthocenter
         [maxvertexcount(3)]
         void geom(point vertexOut i[1], inout TriangleStream<geomOut> OutputStream)
         {
             float3 perpendicular = normalize(float3(i[0].normal.y - i[0].normal.x, i[0].normal.x, i[0].normal.x)) * _Radius;
             geomOut o, p;

             o.color = i[0].color;
             //o.normal = i[0].normal;
             //o.pos = i[0].pos;
             //o.pos = float4(i[0].pos.x * _Radius, i[0].pos.yz, 1);
             o.pos = float4(perpendicular + i[0].pos.xyz, 1);
             //o.pos = float4(normalize(perpendicular + i[0].pos).xyz * _Radius , 1);    //Generic perpendicular to the normal
             o.pos = UnityObjectToClipPos(o.pos);
             o.normal = ObjSpaceViewDir(o.pos);
             //o.triangleCenter = UnityObjectToClipPos(i[0].pos).xyz;
             o.triangleCenter = o.pos;
             o.norm = distance(o.pos, o.triangleCenter);
             OutputStream.Append(o);

             p.color = i[0].color;
             //p.normal = i[0].normal;
             //p.pos = float4(perpendicular.xyz * _Radius, 1);
             //p.pos = float4(i[0].pos.x, i[0].pos.y * _Radius, i[0].pos.z, 1);
             //p.pos = float4(    (normalize(cross(i[0].pos.xyz, perp.xyz)) - perp.xyz / 2 + i[0].pos) * _Radius,1);
             p.pos = float4((-(normalize(cross(i[0].pos.xyz, perpendicular)) * _Radius) + perpendicular / 2) + i[0].pos.xyz, 1);
             p.pos = UnityObjectToClipPos(p.pos);
             p.normal = ObjSpaceViewDir(p.pos);
             p.triangleCenter = o.triangleCenter;
             p.norm = o.norm;
             OutputStream.Append(p);

             p.color = i[0].color;
             //p.pos = float4(perpendicular.xyz * _Radius * -1, 1);
             //o.pos = float4(i[0].pos.x * _Radius, i[0].pos.yz, 1);
             //p.pos = float4(-1 * (normalize(cross(i[0].pos.xyz, perp.xyz)) * _Radius) /* - perp.xyz / 2 + i[0].pos*/, 1);
             p.pos = float4((-(normalize(cross(i[0].pos.xyz, perpendicular)) * _Radius) - perpendicular / 2) + i[0].pos.xyz, 1);
             p.pos = UnityObjectToClipPos(p.pos);
             ObjSpaceViewDir(o.pos);
             p.normal = ObjSpaceViewDir(p.pos);
             p.triangleCenter = o.triangleCenter;
             p.norm = o.norm;
             OutputStream.Append(p);

             OutputStream.RestartStrip();
         }

         float4 frag(geomOut i) : COLOR
         {
             float4 col = i.color;
             col.a = distance(i.pos.xyz, i.triangleCenter.xyz)/ i.norm;
             return col;
         }
         ENDCG
     }
 }
 FallBack "Diffuse"
 }

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 squidgemelent · Dec 02, 2017 at 01:20 PM

Is there a reason you are using one triangle and not two triangles resulting in a square to draw a circle? While it may seem more efficient to draw just one triangle instead of two, you need to consider that you may be over-complicating. Firstly, the GPU is insanely fast and depending on your task is likely not an impactful optimisation. Secondly, you're performing a distance operation on every single fragment anyway. Thirdly, drawing two triangles instead of one is not necessarily taking double the time depending on how you're rendering the result on screen. And lastly, a triangle could potentially result in more overdraw depending on how you're doing it.

If you drew a square instead, it would be very easy to assign a UV coordinate to each geometry vertex. The GPU will calculate the UV coordinate of each individual fragment for you, and you can choose the alpha simply based off a much simpler calculation than distance(x, y)

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

73 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

Related Questions

How to set an index buffer primitive restart value. 0 Answers

vertex and geometry shader change with distance 0 Answers

How to convert surface shader to Vertex/Fragment code? 0 Answers

How to add normal map to "color" shader? 0 Answers

What is more efficent? Multiple passes or a single pass with iterative loop? 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