Help with transparency and z buffer... I think.... (completely clueless)

OK, first off: I am a complete shader noob. I am not looking to learn shading, but was trying to hack some stuff editing shaders I found in the forum and wikis. My goal was to get two shaders with a rim highlight, one of them with no transparency. I sort of got what I want, but they dont seem to play well together.

Here is an example of what I expected (imagine the rocky features and eyes to have a white rim)

1064305--39576--$rimtest1.png

As you can see, part of the rock and eyes are inside the green ooze, and part is outside.

Now, this is what I’m actually getting:

1064305--39575--$rimtest2.png

Here everything seems to show as if it was either inside or behind the ooze.

Here is the code for the transparent shader:

Shader "Rim/Light Specular With Alpha" {

	Properties {
		_MainTex ("Texture", 2D) = "white" {}
		_Cube ("Cubemap", CUBE) = "" {}
		_ReflColor ("Relection Color", Color) = (0.26,0.19,0.16,0.0)
		_RimColor ("Rim Color", Color) = (0.26,0.19,0.16,0.0)
		_RimPower ("Rim Power", Range(0.5,8.0)) = 3.0
		_AlphPower ("Alpha Rim Power", Range(0.0,8.0)) = 3.0
		_AlphaMin ("Alpha Minimum", Range(0.0,1.0)) = 0.5
	
	}

	SubShader {
		Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
		
		ZWrite On
		Alphatest Greater 0
		Blend SrcAlpha OneMinusSrcAlpha 
		
		Fog{}

		CGPROGRAM
		#pragma surface surf Lambert alpha
		struct Input {
			float2 uv_MainTex;
			float3 worldRefl;
			float3 viewDir;
			INTERNAL_DATA
		};
		
		sampler2D _MainTex;
		samplerCUBE _Cube;
		float4 _RimColor;
		float4 _ReflColor;
		float _RimPower;
		float _AlphPower;
		float _AlphaMin;
		void surf (Input IN, inout SurfaceOutput o) {
			o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
			half rim = 1.0 - saturate(dot (normalize(IN.viewDir), o.Normal));
			o.Emission = _RimColor.rgb * pow (rim, _RimPower) + texCUBE(_Cube, WorldReflectionVector (IN, o.Normal)).rgb*_ReflColor*2 ;
			o.Alpha = (pow (rim, _AlphPower)*(1-_AlphaMin))+_AlphaMin ;
		}
		ENDCG
	}
	Fallback "VertexLit"
}

And here is the code for the opaque shader:

Shader "Rim/Light Specular" {
 
	Properties {
		_MainTex ("Texture", 2D) = "white" {}
		_Cube ("Cubemap", CUBE) = "" {}
		_ReflColor ("Relection Color", Color) = (0.26,0.19,0.16,0.0)
		_RimColor ("Rim Color", Color) = (0.26,0.19,0.16,0.0)
		_RimPower ("Rim Power", Range(0.5,8.0)) = 3.0
	
	}

	SubShader {
		Tags {"Queue"="Geometry" "IgnoreProjector"="True" "RenderType"="Opaque"}
		
		ZWrite On
		Alphatest Greater 0
		Blend SrcAlpha OneMinusSrcAlpha 
		
		Fog{}

		CGPROGRAM
		#pragma surface surf Lambert alpha
		struct Input {
			float2 uv_MainTex;
			float3 worldRefl;
			float3 viewDir;
			INTERNAL_DATA
		};
		
		sampler2D _MainTex;
		samplerCUBE _Cube;
		float4 _RimColor;
		float4 _ReflColor;
		float _RimPower;
		void surf (Input IN, inout SurfaceOutput o) {
			o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
			half rim = 1.0 - saturate(dot (normalize(IN.viewDir), o.Normal));
			o.Emission = _RimColor.rgb * pow (rim, _RimPower) + texCUBE(_Cube, WorldReflectionVector (IN, o.Normal)).rgb*_ReflColor*2 ;
			o.Alpha = 1.0;
		}
		ENDCG
	}
	Fallback "VertexLit"
}

I have no clue if this is an easy fix or something that would involve lots of code, as I said: I am an entire noob as far as shaders go and was hoping I was able to just hack some stuff together (or at least find a direction to where I can find something like what I want.)

Thanks in advance!

Currently both shaders aren’t writing to the ZBuffer, so it won’t be able to tell what’s behind what.

In the opaue shader, if you change ZWrite Off to ZWrite On (or remove the ZWrite Off line, because ZWrite On is the default setting anyway), you should start to see the results you’re after.

Setting the “Queue” = “Geometry” in the opaque shader’s tags might be worthwhile, too. As that’ll ensure it draws before the transparent shader.

OK I tried that, changed ZWrite to On on both versions and added “Queue” = “Geometry” to the opaque shader. Still getting the same result.

The opaque shader was not rendered correctly. Removing the parameter ‘alpha’ from the #pragma surface directive seems to solve the problem.

Since this shader is not meant to be transparent, there are a few more things you might want to remove.

ZWrite On
This is On by default. Although I don’t think it hurts to leave this in

Alphatest Greater 0
Since you set the alpha to 1.0 in the surface shader, it will always pass this test. Not doing this test at all, should be faster.

Blend SrcAlpha OneMinusSrcAlpha
Similar to the one above. The alpha of this surface is always 1.0, so it will always be fully blended in.

Here’s the modified Rim/Light Specular shader:

Shader "Rim/Light Specular" { 
    Properties {
        _MainTex ("Texture", 2D) = "white" {}
        _Cube ("Cubemap", CUBE) = "" {}
        _ReflColor ("Relection Color", Color) = (0.26,0.19,0.16,0.0)
        _RimColor ("Rim Color", Color) = (0.26,0.19,0.16,0.0)
        _RimPower ("Rim Power", Range(0.5,8.0)) = 3.0
   
    }
 
    SubShader {
        Tags {"Queue"="Geometry" "IgnoreProjector"="True" "RenderType"="Opaque"}
       
        Fog{}
 
        CGPROGRAM
        #pragma surface surf Lambert
        struct Input {
            float2 uv_MainTex;
            float3 worldRefl;
            float3 viewDir;
            INTERNAL_DATA
        };
       
        sampler2D _MainTex;
        samplerCUBE _Cube;
        float4 _RimColor;
        float4 _ReflColor;
        float _RimPower;
        void surf (Input IN, inout SurfaceOutput o) {
            o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
            half rim = 1.0 - saturate(dot (normalize(IN.viewDir), o.Normal));
            o.Emission = _RimColor.rgb * pow (rim, _RimPower) + texCUBE(_Cube, WorldReflectionVector (IN, o.Normal)).rgb*_ReflColor*2 ;
            o.Alpha = 1.0;
        }
        ENDCG
    }
    Fallback "VertexLit"
}

I don’t think the fog block will do much without parameters, but I’m not sure, so I left it in, in case it had some special purpose.

[Edit] One thing I forgot to mention: Awesome! A jelly creature :smile:

Thank you!!! For all the tips too! This did the trick indeed! I really am all clueless about shaders, this is my first try at it and was mostly copy pasting different shaders to see what would work. The fog thing was, I think, taken from somewhere in one of my fix attempts so I removed it from both shaders.

Thanks, it’s a prototype idea for my old Plat Norris project. Sort of abandoned it but decided to pick it back up when I realized I may be able to do some cool jelly monsters with rocky pieces popping over their bodies, and maybe also blinking eyes. I may be able to come up with a lot of cool monster designs for the game that way, just got to see if I have the time now!

Here is a new screen capture of the shaders at work:

1065714--39637--$rimtest3.png

Again, thanks for the help!