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 mouseyboy · Jan 06, 2016 at 11:30 AM · shaderrenderingshaders

Custom shaders cause objects to render behind other objects from certain angles

I've been trying to learn how to write shaders, and for the most part it has been going well, as I have achieved my desired effects. However for some reason, every custom shader I have written will randomly render behind other objects when viewed at certain angles.

The shader I made is just a simple texture distortion and scrolling effect to make a simple fog effect. In these images the red fog is using the shader, at this angle it renders in front as it should: Correct Render However, when viewed slightly to the right, it dissapears behind the grass object: Incorrect Render

I have tried many solutions from what seemed like similar problems on this site and unity forums, I've read up in the documentation about shaderlab and shaders. I have also tried changing the render queue, the ZTest, culling, and ZWrite.

Here's the shader:

 Shader "Custom/DistortBytexture" {
     Properties {
         _Color ("Color", Color) = (1,1,1,1)
         _MainTex ("Albedo (RGB)", 2D) = "white" {}
         _DistortTex ("Distort Texture (RGB)", 2D) = "white" {}
         _XScale ("XScale", Range(-1.0,1.0)) = 0.5
         _YScale ("YScale", Range(-1.0,1.0)) = 0.5
         _XSpeed ("X Scroll Speed", Range(-1.0,1.0)) = 0.5
         _YSpeed ("Y Scroll Speed", Range(-1.0,1.0)) = 0.5
         _XSpeedD ("Distort X Scroll Speed", Range(-0.1,0.1)) = 0.05
         _YSpeedD ("Distort Y Scroll Speed", Range(-0.1,0.1)) = 0.05
         _Alpha ("Alpha", Range(0,10.0)) = 0.5
     }
     SubShader {
         Tags { "RenderType"="Opaque" }
         LOD 200
         
         Lighting Off
         Cull Off
         Zwrite Off
         Ztest LEqual
         
         CGPROGRAM
         #pragma surface surf Lambert alpha:fade noambient noshadow nodynlightmap
         
         fixed4 _Color;
         half _XScale;
         half _XSpeed;
         half _XSpeedD;
         half _YScale;
         half _YSpeed;
         half _YSpeedD;
         half _Alpha;
         sampler2D _MainTex;
         sampler2D _DistortTex;
         
         struct Input {
             float2 uv_MainTex;
             float2 uv_DistortTex;
         };
 
         void surf (Input IN, inout SurfaceOutput o) {
             IN.uv_DistortTex.x += _Time[1] * _XSpeedD;
             IN.uv_DistortTex.y += _Time[1] * _YSpeedD;
             fixed4 d = tex2D (_DistortTex, IN.uv_DistortTex);
             IN.uv_MainTex.x += d.r*_XScale + _Time[1] * _XSpeed;
             IN.uv_MainTex.y += d.r*_YScale + _Time[1] * _YSpeed;
             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
             o.Albedo = c.rgb;
             o.Alpha = c.a * c.a * c.a * c.a * _Alpha;
         }
         ENDCG
     } 
     FallBack "Diffuse"
 }
 

Any help or advice would be greatly appreciated! For all I know, I've made a stupid mistake and missed out a crucial line, however nothing I tried worked so I caved in and am asking here. Thanks in advance! :)

renderinfront.png (497.5 kB)
renderbehind.png (460.0 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 Shadoninja · Aug 26, 2019 at 03:36 AM 0
Share

I am facing this exact same issue.... 3 years later

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by JonnyHilly · Aug 26, 2019 at 04:03 AM

It helps to know some general rendering principals when figuring out how to fix these things. You may know these already, but sometimes helps to go over them. first off solid (opaque) geometry is rendered in front to back order. (closest to camera first) far object won't render if Z-clipped by the earlier drawn close objects. Next transparent objects are drawn... they are drawn in reverse order... back to front (furthest first) they won't draw over solid geometry if the Z distance is further. But they will draw over other transparent objects. Now the main problem here is... how do you define closer or further.... when on a pixel by pixel basis... its easy... but when going through one object at a time.. you have to generalize and usually pick a point on the object... unity probably uses the center of the object. IMPORTANT :- it doesn't sort each polygon's z range it sorts by object Z range ...
Now consider this in reference to your project.. probably you have an object where depending on your view angle... its center causes it to draw before or after the other object.... Z-buffer pixel by pixel distance check is still done... BUT the order the objects are drawn can change... which can affect the display, especially with semi transparent objects.. With that in mind... you may have several ways to fix things..... 1) put you 2 objects on different layers... and have different cameras draw each layer... and use camera render order to force the draw order. 2) if one is solid and the other transparent... the transparent one will always render after. 3) if both are transparent, you could combine the layers into a single shader, with pass1, and pass2 rendering the layers. (or possibly in 1 pass but with consistent color combining) 4) check the size / shape of your objects and the center point... to see if that is causing your issue... you 'might' be able to alter the shape of the geometry so one center is always consistently closer to the camera.

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

Blender's Set Solid in Unity 1 Answer

How to write an additive HDRP shader without glow 0 Answers

U5 shader coming out solid black on newer iOS devices (iPad 2 Air / iPhone 6) 1 Answer

Universal Render is stuck applying Cyan 1 Answer

Scene Color Node in Shader Graph not working with Unity's 2D Renderer and URP 5 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