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 Pranaryx · Nov 03, 2016 at 01:02 PM · shaderspritecgdepth-buffer

Getting the correct object level depth in shader

Hey people. So I am trying to write a shader that will render a tilted, 45°, billboard sprite at what would be (0,y,0,) of the model transform.

The reason for doing this is that we are using 2D sprites in a 3D enviorment angled to look isometric. Using 2D sprites requires angeling them at 45° to look correct. This results in either normal sized colliders and clipping or stupidly large colliders.

From my limited knowledge of shaders and scouring forums I have figured out how to write to the depth shader but, as you can see in the image, the sprite renders correctly when in front of a 3D object, but incorrectly when behind a 3D object.

Behind 3D object but rendering on top. In between objects

In front of 3D and clipping but rendering correctly. alt text

So, how do I get the correct depth value to render this sprite as if it were not tilted 45°?

     SubShader 
     {
         Tags 
         {     
             "Queue"="AlphaTest" 
             "IgnoreProjector"="True" 
             "RenderType"="TransparentCutout" 
             "PreviewType"="Plane"
             "CanUseSpriteAtlas"="True"
 
         }
             Lighting Off
             ZWrite On
             Cull Off
             Blend One OneMinusSrcAlpha
         
         Pass 
         {
 
         CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #pragma multi_compile _ PIXELSNAP_ON
             #pragma shader_feature ETC1_EXTERNAL_ALPHA
             #include "UnityCG.cginc"
 
             struct appdata
             {
               float4 vertex    : POSITION;  
               float3 normal    : NORMAL;   
               float4 texcoord  : TEXCOORD0; 
               float4 texcoord1 : TEXCOORD1; .
               float4 tangent   : TANGENT;   
               float4 color     : COLOR;  
             };
 
                struct v2f 
                {
                 float4 pos : POSITION0;
                    float4 objpos : POSITION1;
                 fixed4 color    : COLOR;
                 float2 texcoord  : TEXCOORD0;
             };
 
             //added
             fixed4 _Color;
 
             v2f vert (appdata v) 
             {
                v2f o;
 
                float4 c = mul(_Object2World, float4(0.0, 0.0, 0.0, 1.0));
                float4 ws = mul(_Object2World, v.vertex);
 
                ws.x = c.x + length(float2(ws.x - c.x, ws.z - c.z));
                   ws.z = c.z;
      
                o.objpos = mul(UNITY_MATRIX_VP, ws);
 
                o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
 
                o.texcoord = v.texcoord;
                o.color = v.color * _Color;
 
                return o;
              }
 
             sampler2D _MainTex;
             sampler2D _AlphaTex;
 
             fixed4 SampleSpriteTexture (float2 uv)
             {
                 fixed4 color = tex2D (_MainTex, uv);
 
                 #if ETC1_EXTERNAL_ALPHA
                 // get the color from an external texture (usecase: Alpha support for ETC1 on android)
                 color.a = tex2D (_AlphaTex, uv).r;
                 #endif //ETC1_EXTERNAL_ALPHA
                 return color;
             }
             
             struct Output 
             {
                 float4 col:COLOR;
                 float dep:DEPTH;
             };   
         
             Output frag( v2f i ) 
             {
                 Output o;
                 o.dep = i.objpos.z / i.objpos.w;
                 o.col = SampleSpriteTexture (i.texcoord) * i.color;
                 o.col.rgb *= o.col.a;
                 return o;
             }
           
         ENDCG
         }
     }


screen-shot-2016-11-02-at-15315-pm.jpg (134.6 kB)
screen-shot-2016-11-02-at-123109-pm.jpg (114.4 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 Rockythechao · Jun 18, 2017 at 05:22 PM

I'm actually working on this exact issue myself right now! Were you ever able to fix it, @Pranaryx ?

I don't know much about shaders myself so I can't tell what's going on in yours, but going by the pictures and your description of the situation it seems like you may be rendering each pixel of the sprite at the same depth value, possibly even the top? If I understand a friend's advice correctly it needs to calculate the coordinates of the quad's verts as if the sprite was standing upright, perpendicular to the floor, then calculate the depth relative to the camera angle accordingly... though since we're billboarding we probably only need to worry about the central vertical line.

Some quick research brought me to rendering the camera's depth texture as a possible means of troubleshooting this stuff. Can't try it myself right now, but I imagine this should help show how the depth of the sprite gradates relative to the walls and floor. https://docs.unity3d.com/Manual/SL-CameraDepthTexture.html https://docs.unity3d.com/Manual/SL-DepthTextures.html

EDIT: Yeah, something like this. https://www.youtube.com/watch?v=ISySavaHmHM

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I write a cutout sprite shader to depth depthtexture ? 0 Answers

How to force the compilation of a shader in Unity? 5 Answers

UnityPixelSnap in custom vertex shader behaves differently to implementation in default sprite shader. 0 Answers

Find out if an object is in front of a target in fragment shader? 0 Answers

How to overlay textures without border 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