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 Tiras69 · Nov 16, 2016 at 09:34 PM · c#shaderalphasmoothcg

Getting the world position of the pixel in the fragment shader.

Hello guys !
I'm struggling with a custom shader :

 Shader "Custom/HaloEffect" {
     Properties{
         _Position("Position", Vector) = (.0, .0, .0)
         _HaloColor("Halo Color", Color) = (1.0, 1.0, 1.0, 1.0)
     }
 
     SubShader {
         Tags { "Queue"="Transparent" "Render"="Transparent" "IgnoreProjector"="True"}
         LOD 200
         
         ZWrite Off
         Blend SrcAlpha OneMinusSrcAlpha
 
         Pass{
             CGPROGRAM
 
             #pragma target 3.0
             #pragma vertex vert
             #pragma fragment frag
 
             #include "UnityCG.cginc"
 
             struct appdata {
                 float4 vertex : POSITION;
                 
             };
 
             struct v2f {
                 float4 vertex : SV_POSITION;
                 float4 color : COLOR0;
             };
 
             uniform float3 _Position;
             uniform float4 _HaloColor;
             
 
             v2f vert(appdata v) {
                 v2f o;
 
                 o.color = _HaloColor;
                 o.color.a = 1 - distance(mul(unity_ObjectToWorld, v.vertex), _Position) ;
                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
 
                 return o;
             }
 
             fixed4 frag(v2f i) : SV_Target {
                 return i.color;
             }
 
             ENDCG
         }
 
     
     }
     FallBack "Diffuse"
 }
 

The desired behaviour would have been to smootly set the alpha around the world coordinates named "_Position" on the mesh that would execute this shader. In this version the results are pretty near of what I expected. But the distances calculus are operated only on the vertices and the final interpolation is weird.

As you can see here the _Position is between 4 vertices of the plane:
alt text

And here is on one vertex: alt text

I think It does the right work regarding what I wrote. But, what I wished was a kind of a smooth circle.
I think I can managed to obtain this by doing the distance calculus in the Fragment Shader.
But I dont know how to get the world coordinates of the current pixel.
I try to compute an inverse of the MVP Matrix applied to the SV_POSITION, but it turns really bad.
Is the SV_POSITION is the same that gl_FragCoord ?
If I'am totally on the wrong way I wish you guys would tell me.
Please help me find a solution.

plane-between-4-vertices.png (341.5 kB)
plane-on-vertice.png (342.3 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
6
Best Answer

Answer by Namey5 · Nov 16, 2016 at 10:56 PM

  Shader "Custom/HaloEffect" {
      Properties{
          _Position("Position", Vector) = (.0, .0, .0)
          _HaloColor("Halo Color", Color) = (1.0, 1.0, 1.0, 1.0)
      }
  
      SubShader {
          Tags { "Queue"="Transparent" "Render"="Transparent" "IgnoreProjector"="True"}
          LOD 200
          
          ZWrite Off
          Blend SrcAlpha OneMinusSrcAlpha
  
          Pass{
              CGPROGRAM
  
              #pragma target 3.0
              #pragma vertex vert
              #pragma fragment frag
  
              #include "UnityCG.cginc"
  
              struct appdata {
                  float4 vertex : POSITION;
                  
              };
  
              struct v2f {
                  float4 vertex : SV_POSITION;
                  float3 worldPos : TEXCOORD0;
              };
  
              uniform float3 _Position;
              uniform float4 _HaloColor;
              
  
              v2f vert(appdata v) {
                  v2f o;
  
                  o.worldPos = mul (unity_ObjectToWorld, v.vertex);
                  o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
  
                  return o;
              }
  
              fixed4 frag(v2f i) : SV_Target {
                  fixed4 col = _HaloColor;
                  col.a = 1 - distance(i.worldPos), _Position) ;
                  return col;
              }
  
              ENDCG
          }
  
      
      }
      FallBack "Diffuse"
  }

Does it not work to just move the calculations into the fragment shader?

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 Tiras69 · Nov 17, 2016 at 06:32 AM 0
Share

Thanks !
It works Like a charm In my attemps to pass a position from the vertex shader to the fragment shader I used
the semantics POSITION, unfortunately that wasn't the right way to do it.
Thanks to you know I have a better understanding of shaders, I know what TEXCOORD0 is.
And it make perfecty sense.

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

247 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 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

Problem with alpha coloured shader 1 Answer

custom Standard Shader alpha issues. 1 Answer

How do you make an X-ray vision following the mouse? 1 Answer

Is there a way to set an alpha color? 2 Answers

Multiple Cars not working 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