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 allan36j · Jul 15, 2016 at 08:00 PM · shadermeshrenderersorting layersrenderqueueztest

Render mesh in front of another mesh but not in general

Hey there, I was wondering if it is possible to let a mesh appear to be in front of another mesh even though it is behind it world space. I need it for an optical illusion.
I figured there is a Option in ShaderLab called ZTest Always.
In this example I've seperated the mesh into two pieces. The one behind the other has a shader with ZTest Always. Thats what it looks like: illusion It kind of does what I want it to do but there is a problem with this method. If there is anything else, like a cube, on top of the mesh it appears to be behind the mesh again but it shouldn't. That is the problem. illusion with cube So I was wondering if there is a way to render the mesh only in front of a specific mesh but not all meshes.

I also stumbled across the option of sorting layers like it's used by the SpriteRenderer only for the MeshRenderer (here) but it didn't work for me.

I've also tried to change the "Custom Render Queue" of the Material but it didn't work too.
I also couldn't succeed with the Queue in the shader itself. I've tried something like this:

 Shader "Queue"
 {
 
     SubShader
     {
         Tags {  "Queue"="Geometry+1" }
         
         Pass
         {
 ....

What did I do wrong?
Thank you for reading.

optical-illusion-working.png (24.0 kB)
optical-illusion-cube.png (24.5 kB)
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 Glurth · Jul 16, 2016 at 02:34 PM 0
Share

This optical illusion is based upon PART of mesh 2 appearing behind mesh 1 and PART of mesh 2 appearing in front of mesh 1. Even if you make mesh 2 into two different parts, the issue remains, which of those goes in front of the other? As a human, we can deduce that if we separate mesh 2 into the CUBE (lets call it mesh 3) and the stretched-cube (lets keep calling it mesh 2), the cube is to be drawn on top of mesh 1 AND mesh 2. But, since this is an illusion, I do NOT see how to write an algorithm to figure that out dynamically.

Just to throw it out there, another way to do this would be to manipulate the mesh verticies (towards/away from the camera), forcing the z-ordering (per vertex, rather than per mesh). If you leave the mesh normals alone, this should result in the same face shading as before vertex manipulation. Though the mesh shapes wont ACTUALLY be the cubes and stretched-cubes anymore, it would still "look" that way to the camera.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by tanoshimi · Jul 15, 2016 at 08:52 PM

Generally, shaders on 3d meshes write to (and cull based on) the depth buffer. That's what the ZTest bit is for. So objects are drawn in front of others based on their distance to the camera. In contrast, the default shaders used to draw 2d sprites do not write to the depth buffer - the order in which they are drawn is instead based on the sorting layer.

But there's no reason why you can't apply the logic used in the Sprites-Default shader, say, onto a 3d mesh - disable depth testing and use sorting layers instead (you might find installing the "Sorting Layer Exposed" component" from here helpful to assign layers to meshrenderers in the inspector: www.github.com/nickgravelyn/UnityToolbag)

Comment
Add comment · Show 3 · 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 allan36j · Jul 15, 2016 at 10:32 PM 0
Share

Thank you! I already had the sorting layer exposer but it seems like I did use it in the wrong way.
Thanks to your hint I just added ZWrite Off to the code and the sorting layer worked.
Thanks again. it works great now

Edit: Exept for one little thing. Somehow the shading is broken. The illusion is working but the shading order is not right. $$anonymous$$aybe ZWrite Off was the wrong conclusion afterall or did I do something wrong? Could you please help me once more @tanoshimi? shading error

optical-illusion-working-shaded.png (18.5 kB)
avatar image tanoshimi allan36j · Jul 16, 2016 at 11:04 AM 0
Share

One of the problems with designing scenes around optical illusions is that it's kind of hard to figure out what the true shape of your meshes is :) Can you pull them apart a little bit to make it clear? If you're using sorting layers then the whole of one shape will be rendered on top of (or below) another - I thought that's what you wanted? - you can't have part on top and part below.

avatar image allan36j tanoshimi · Jul 16, 2016 at 11:14 AM 0
Share

@tanoshimi sorry for the imprecise explanation of my scene. perspective illusion
the part rendered on top of the other mesh is "$$anonymous$$esh 2". But my problem is that "$$anonymous$$esh 1" is somehow rendered on top of itself even though it's just one mesh I hope you can see what the problem is :)

optical-illusion-perspective.png (17.4 kB)
avatar image
0

Answer by Jacky2611 · Jul 16, 2016 at 06:19 PM

I would watch this video about the making of monumentum valley. I believe they also talk a bit about pathfinding in combination with such illusions.

https://www.youtube.com/watch?v=mCCC9hQm6MM

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Render sprite only on top of a specific sorting layer 1 Answer

Rendering a 3d mesh over Unity UI,Rendering over Unitys UI 3 Answers

Rendering order approach for a 2.5D game. 2 Answers

2D Sprite Always Visible - Shader / Material not working (ZTest, ZWrite, Culling) 1 Answer

Need help rendering beer glass in unity... 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