• 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 MaxHeyderArt-GoldenSkull · Jun 18, 2020 at 01:10 PM · shadertilemapshader programmingz axisdepth buffer

How to access Tilemap Tile's x-y-z positions from shader, to mask or move tiles

Hey there, I am trying to come up with ways to make a shader that masks out sprites that are overlapping the player when using 2D Tilemaps.

I found that when working with tilemaps, they are rendered flat. However, when rendering, the tilemaps do seem to have a z-value, because you can add new sprites in 3d space and move them around and they will sort correctly based on their y and z values as can be seen in t$$anonymous$$s image: alt text

Unity does seem to utilize a tile's x-y-z position from the grid, but rendering the tilemaps flat for performance reasons, w$$anonymous$$le still allowing independent sprites and other tilemaps to sort properly in relation to the tilemap's depth.

Now the big question is: in isometric games, objects often obscure the player character and what is being done in many games is that the obejcts/tiles that are obscuring the player are then masked out in an area around the player. W$$anonymous$$le it is possible to add custom shaders to the tiles, I have not figured out how to access a Tile's depth value that is being utilized by the Tilemap Renderer, in order to only mask out the ones that are in front and not also the ones that are in the back, as seen here: alt text

My code for the shader that masks out the tilemap is currently t$$anonymous$$s: (Please Keep in mind that I am an artist and not a professional coder and just try to make t$$anonymous$$ngs work)

 // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
 
 // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
 
 Shader "Sprites/Custom/SpriteMask1"
 {
     Properties
     {
         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "w$$anonymous$$te" {}
         _Color ("Tint", Color) = (1,1,1,1)
         [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
         [HideInInspector] _RendererColor ("RendererColor", Color) = (1,1,1,1)
         [HideInInspector] _Flip ("Flip", Vector) = (1,1,1,1)
         [PerRendererData] _AlphaTex ("External Alpha", 2D) = "w$$anonymous$$te" {}
         [PerRendererData] _EnableExternalAlpha ("Enable External Alpha", Float) = 0
         _Mask ("Alpha Mask", 2D) = "w$$anonymous$$te" {}
         _DepthCull ("DepthCull", Float) = 0
     }
 
     SubShader
     {
         Tags
         {
             "Queue"="Transparent"
             "IgnoreProjector"="True"
             "RenderType"="Transparent"
             "PreviewType"="Plane"
             "CanUseSpriteAtlas"="True"
         }
 
         Cull Off
         Lighting Off
         ZWrite Off
     Blend SrcAlpha OneMinusSrcAlpha
 
         Pass
         {
         CGPROGRAM
             #pragma vertex my_SpriteVert
             #pragma fragment my_SpriteFrag
             #pragma target 2.0
             #pragma multi_compile_instancing
             #pragma multi_compile_local _ PIXELSNAP_ON
             #pragma multi_compile _ ETC1_EXTERNAL_ALPHA
             #include "UnitySprites.cginc"
 
             sampler2D _Mask;
             fixed _DepthCull;
 
             struct my_appdata_t
             {
                 float4 vertex   : POSITION;
                 float4 color    : COLOR;
                 float2 texcoord : TEXCOORD0;
                 UNITY_VERTEX_INPUT_INSTANCE_ID
                 float2 depth : TEXCOORD2;
             };
 
             struct my_v2f
             {
                 float4 vertex   : SV_POSITION;
                 fixed4 color    : COLOR;
                 float2 texcoord : TEXCOORD0;
                 float2 worldCoord : TEXCOORD1;
                 float2 depth : TEXCOORD2;
                 UNITY_VERTEX_OUTPUT_STEREO
             };
 
             my_v2f my_SpriteVert(my_appdata_t IN)
             {
             my_v2f OUT;
 
             UNITY_SETUP_INSTANCE_ID (IN);
             UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
 
             OUT.vertex = UnityFlipSprite(IN.vertex, _Flip);
             OUT.vertex = UnityObjectToClipPos(OUT.vertex);
             OUT.texcoord = IN.texcoord;
             OUT.worldCoord = UnityObjectToViewPos (IN.vertex);
 
             UNITY_TRANSFER_DEPTH(OUT.depth);
 
             float4 scaledVertex = float4(OUT.vertex.xy,0,0);
             OUT.worldCoord =  ComputeScreenPos (OUT.vertex);
 
             OUT.color = IN.color * _Color * _RendererColor;
 
             #ifdef PIXELSNAP_ON
             OUT.vertex = UnityPixelSnap (OUT.vertex);
             #endif
 
             return OUT;
         }
 
         fixed4 my_SpriteFrag(my_v2f IN) : SV_Target
         {
             fixed4 c = SampleSpriteTexture (IN.texcoord);
             fixed4 alpha = tex2D (_Mask, IN.worldCoord);
             fixed depth = IN.depth;
             c.a *= 1- alpha;
             return c;
         }
         ENDCG
         }
     }
 }

If anyone could help me out with t$$anonymous$$s, it would be much appreciated and I would love to share the functional solution for free with other members of the community.

Wis$$anonymous$$ng a great day to everyone reading <3

2.jpg (222.0 kB)
3.jpg (182.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

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

202 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

Related Questions

Very bad float precision 0 Answers

How to add metallic and smoothness to a frag shader? 1 Answer

Why is my UV'd texture displaying wrapped with the wrong scale? 0 Answers

Help about adding transparency to Roystan's toon shader 0 Answers

Shader error in 'Deferred': undeclared identifier 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