Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

NEED HELP! depth texture intersection highlight shader on mobile android

Discussion in 'Shaders' started by glowe, Feb 23, 2016.

  1. glowe

    glowe

    Joined:
    Jun 10, 2014
    Posts:
    68
    So, I took this shader and added it to a game I'm making.
    https://chrismflynn.wordpress.com/2012/09/06/fun-with-shaders-and-the-depth-buffer/

    I love it and it works really great in the editor. But, I want to use this effect in a mobile game. On my android devices it looks all wrong. :''(
    editor: this is correct - the intersecting outline follows the terrain and objects it intersects with
    good-shader.jpg
    android: it appears as weird diagonal lines along the plane the material is used on (tested with note 2 and galaxy s6 - same results)
    bad-shader.jpg

    I'm new to shaders and I've been learning them so I can debug this issues. But, I just can't figure out why it works this way on android.
     
  2. glowe

    glowe

    Joined:
    Jun 10, 2014
    Posts:
    68
    Problem solved!!

    After a week of racking my brain I found and bought this asset.
    https://www.assetstore.unity3d.com/en/#!/content/47893

    it saved me a ton of time and headache! I had to mess around with it a bit, but I finally got it to work the way I needed on mobile. (Actually, it works out of the box, but you have to mess with camera clip planes a bit to make them look right on mobile.)

    great-shader.jpg

    Here's a rundown.

    First off, make sure you have a script on your camera that turns on the depth texture:

    Code (CSharp):
    1.     [ExecuteInEditMode]
    2.     public class TurnOnDepthBuffer : MonoBehaviour
    3.     {
    4.         // Use this for initialization
    5.         void Start()
    6.         {
    7.             var camera = GetComponent<Camera>();
    8.             camera.depthTextureMode = DepthTextureMode.Depth;
    9.         }
    10.     }
    For mobile, the camera that renders objects with this shader needs a relatively high near clipping plane. The image above uses Near = 10 to Far = 1500. (Since it's a pretty high near value I added a second camera to display Near = 1 to Far = 10. It's kind of hackish, but it makes the shader look pro on mobile.) Some more interesting facts: this will work fine with gles 2.0. I ran it on a Note 2 with the correct camera configuration and it works really well. Just make sure your mobile device has the hardware requirements for Depth Textures.
    http://docs.unity3d.com/Manual/SL-DepthTextures.html

    And, for kicks, here's what mobile looks like when the near clipping plane is a low value, ex: Near = 1. Here the camera is suspended in the air looking at objects at different distances. Objects that are near appear fine. But the ground (which is far away and below) is intersected with a very large cube. Being far away from the camera, it has weird display artifacts. I'm pretty sure this is because of how mobile devices capture their depth texture and the logarithmic nature of their precision.

    test.jpg
     
    Last edited: Feb 25, 2016
  3. Dannark

    Dannark

    Joined:
    Jul 24, 2016
    Posts:
    14
    I'm having the same problem but in my case it is Ocen-Sphere that should intersect with a procedural planet, the Near = 10 did fix the problem indeed!! thxxxx for sharing this info.