• 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 Halleester · Nov 08, 2017 at 05:10 PM · shaderspritez-depth

Making a shader that respects Z Depth and Sprite Order?

So I've been messing around with sprites in a 3D environment, and one of the bigger problems I've been running into is making sprites function properly with semi-transparent materials using a modified sprite shader. With Z Write on, the character sprites shows properly when in front or behind a semi-transparent object, but I have multiple overlapping sprites that are ordered by their "Order in Layer" property which gets completely ignored when I turn on Z Write, meaning I get a bunch of sprites fighting over the same location. Without the z-depth, the sprites show correctly but the semi-transparent materials only appear either in front or behind the character and doesn't change based on the characters z position. It seems like there is a way to both use z position and sprite order since "Z Test Always" renders the sprites in the correct order (but shows the sprites in front of the meshes then), or at least possible using z offset in the shader. I would prefer to avoid changing the z position of every part of the character since I want to be able to rotate it 360 without seeing those position differences, so if anyone knows how to do this inside a shader, it would be extremely helpful. Here's the shader I currently use (using Z Write):

 Shader "Legacy Shaders/Transparent/Cutout/Custom Bumped Diffuse" {
 Properties {
     _Color ("Main Color", Color) = (1,1,1,0)
     _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
     _BumpMap ("Normalmap", 2D) = "bump" {}
     _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
 }
 
 SubShader {
     Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="TransparentCutout" "PreviewType" = "Plane" "CanUseSpriteAtlas" = "True"}
     LOD 300
     ZWrite On
     Cull Off
     Lighting On
 
 CGPROGRAM
 #pragma surface surf Lambert addshadow alphatest:_Cutoff
 sampler2D _MainTex;
 sampler2D _BumpMap;
 fixed4 _Color;
 
 struct Input
 {
     float2 uv_MainTex;
     float2 uv_BumpMap;
     float3 viewDir;
 };
 
 void surf (Input IN, inout SurfaceOutput o)
 {
     fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
     o.Albedo = c.rgb;
     o.Alpha = c.a;
     o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
     float3 n = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
      o.Normal = dot(IN.viewDir, float3(0, 0, 1)) > 0 ? n : -n;
 }
 ENDCG
 }
 
 FallBack "Legacy Shaders/Transparent/Cutout/Diffuse"
 }
Comment
Add comment · Show 1
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 nu51313932 · Aug 30, 2019 at 02:12 PM 0
Share

This is what I needdddddddd! Thank you so much.

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

141 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

Related Questions

Sprite not visible from behind 0 Answers

Sprite with MaterialPropertyBlock not updating until Animated 1 Answer

(Shader)How to find the cordinate of a given color inside a ramp texture? 0 Answers

Make Sprite-Diffuse shader work on mesh renderer? 0 Answers

Create a shader using mask for sprite 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