Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 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
1
Question by imapler · Apr 20, 2012 at 08:50 AM · shaderdistanceparticle systemworldspace

Distance shader returns strage distance when not at 0,0,0

i have this shader that is suposed to make objects transparent if they are to close to the camera and it works perfectly when the object is at 0,0,0 but for example when the object is at 0,0,40 i get full fade at around 0,0,20 (for this example the variables FadeStartDistance = 0 and FadeDistance = 10)

i also have a problem that the range of the fade is half, ea if i have FadeDistance = 10 then the actual lenght of the fade is 5 meters. i currectly get around this problem by multiplaying the distance by 0.5 but that doesnt seem like the right way and im guessing that the problems are related

EDIT

changed to using the correct matrix to get the worldspace and now it works for normal mashes but the problem now is that when i use it for a particle shader (im using the new 3.5 version) it again only works when its at 0,0,0 if i move it to 0,0,20 it doesnt work at all and if its at 0,0,10 i can see some particles no matter where the camera is

EDIT2

i have preformed some more testing and it seems like the Object2World matrix always is the identity matrix for particle system is there some other matrix i can/should use instead?

Shader "Custom/DistanceFade" { Properties { _MainTex ("Main Tex", 2D) = "white" {} _FadeStartDistance ("Fade Start Distance", Float) = 0 _FadeDistance ("Fade Distance", Float) = 0 _MinFade ("Minimum Fade", Float) = 0 } SubShader { Pass { //Blend SrcAlpha One //Cull Off Lighting Off ZWrite Off Fog { Color (0,0,0,0) } CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc"

     sampler2D _MainTex;
     float4 _MainTex_ST;

     float _FadeStartDistance;
     float _FadeDistance;
     float _MinFade;
     
     struct v2f 
     {
         float4  pos : SV_POSITION;
         float2  uv : TEXCOORD0;
         float    newAlpha : TEXCOORD1;
     };
     
     v2f vert (appdata_full v)
     {
         v2f o;
         o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
         o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
 
         float3 worldSpaceObjectPos = mul(v.vertex, _Object2World).xyz;
         float dist = distance(worldSpaceObjectPos.xyz, _WorldSpaceCameraPos.xyz);
         float fade = (dist - _FadeStartDistance) / _FadeDistance;
         fade *= fade;
         o.newAlpha = clamp(fade, _MinFade, 1.0f);
         
         return o;
     }
     
     half4 frag (v2f i) : COLOR
     {
         fixed4 c = tex2D(_MainTex, i.uv);
         
         //c.w = i.newAlpha;
         if(i.newAlpha <= _MinFade)
             c = fixed4(1,0,0,1);
         else if(i.newAlpha >= 0.99)
             c = fixed4(0,1,0,1);
         else
             c = fixed4(i.newAlpha, i.newAlpha, i.newAlpha, 1.0f);
         
         return c;
     }
     ENDCG
     
     }
 }

}

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
1

Answer by Kryptos · Apr 20, 2012 at 10:01 AM

I'm not an expert but are you certain to use the right matrix conversion?

If I'm not mistaking, UNITY_MATRIX_MVP transform the coordinates into screen position, whereas you are checking against world camera position. Can't this work better with UNITY_MATRIX_MV?

Of course at the end, you will need to transform it again to screen position, with:

 o.pos = mul(UNITY_MATRIX_P, o.pos);

See here for a definition of the different matrices.

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 imapler · Apr 23, 2012 at 04:14 PM 0
Share

updated the question

avatar image
0

Answer by mrlinds · May 14, 2014 at 07:01 PM

length(mul (UNITY_MATRIX_MV,v.vertex)) is distance of vertex from camera.

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 Elecman · May 17, 2015 at 10:27 PM 0
Share

To calculate the distance of a vertex to the camera you can use this as well:

length(ObjSpaceViewDir(v.vertex))

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Shader distance between WorldSpaceCameraPos and Vertex 1 Answer

shader distance returns larger value further from 0,0,0 0 Answers

Particle system fine in Editor but Wrong in android build 0 Answers

Classic Game Distance masking darkness/fog help 1 Answer

How do I set up custom shader options without killing batching? 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