Z Offset shader?

0
LyingDwarf posted this 08 October 2017

Is it possible to apply a Z buffer shader offset to the existing PicaVoxel shaders? I'm having Z flicker fighting with some overlapping meshes, and I know there are other possible solutions, but a Z buffer offset seems to be the most appropriate solution for my circumstances, if it can be done without losing the vertex coloring of your custom shaders.

Last edited 08 October 2017

4 Comments
Order By: Standard | Newest | Votes
0
GarethIW posted this 08 October 2017

Hmmm, I’m not familiar with the technique. If you can find me an example of a Unity shader that does what you need then I’ll be happy to try and combine it with PicaVoxel/vertex colouring shaders.

0
LyingDwarf posted this 08 October 2017

Thank you for such a quick response.

Here's a short video that introduced me to the technique; https://www.youtube.com/watch?v=MlLvQxZv3h4.

And here's my tiny test shader using it;

Shader "Custom/ZOffsetVariable" {
Properties {
    _Color ("Main Color", Color) = (1,1,1,0)
    _MainTex ("Base (RGB)", 2D) = "white" {}
    _Offset ("Offset", Float) = 0
}
SubShader {
    Pass {
        Offset [_Offset], [_Offset]

        Material {
            Diffuse [_Color]
            Ambient [_Color]
        }

        Lighting On

        SetTexture [_MainTex] {
            Combine texture * primary DOUBLE, texture * primary
        }
    }
}
}

Normally people use a single Z offset applied to the entire mesh as shown above, but I'd actually like to make the offsets be pulled from 1 of the UV texture arrays. I am however a complete novice when it comes to shaders and still working out how to do that.

0
GarethIW posted this 08 October 2017

Well, here's a PBR and a standard diffuse PicaVoxel shader with the Offset implemented as per your example. I tested it and it seems to do the job as long as the two volumes have different materials with different Offset values (kinda obvious):

https://gist.github.com/GarethIW/e474194df7e7883c95ca884b7b0db607

I don't know how you'd go about getting the offset values from uv data, as the offset seems to be applied outside of vertex or fragment functions, I suppose at the "meta" level. But I'm certainly no shader guru either.

0
LyingDwarf posted this 09 October 2017

That solves my problem, thank you very much.

By the way, I found while testing that the transparency of PicaVoxel Diffuse Transparent.shader doesn't work. Line 14 needs to be changed to; #pragma surface surf Lambert alpha


Our 103 members have posted 431 times in 104 discussions