Shader breaks after reopening Unity for first time

First of all, the effect I am trying to get is a simple ripple effect on a textured plane. Ideally the ripple should distort the texture instead of just overlaying a ripple texture on top of my texture.

The good news, I found pretty much exactly what I wanted in this thread, http://forum.unity3d.com/threads/182296-GPU-Water-ripple-effect-Desktop-and-iOS-source-code-available. The bad news, I’ve been having a lot of strange bugs with this package that I will describe in detail below.

Here is a link to an imgur album with pics of the problems

Import Package Into Empty Project

At this point the plane with the WaterMobile.shader applied to it now is only displayed behind other objects that have materials applied to them (Import Package 5 and 6 in imgur album.) The tags in WaterMobile.shader are “IgnoreProjector”=“True”, “Queue”=“Transparent”, “RenderType”=“Transparent.” I’ve tried changing them to “Queue”=“Overlay” and/or “RenderType”=“Opaque” to force the object to be displayed on top, that didn’t work. I tried manually changing the material.renderQueue of the material that uses the WaterMobile.shader to int.maxValue to force the object to be displayed on top, didn’t work. Nothing I have found seemed to affect the rendering of the plane. I did isolate the problem to only occurring after I close unity and reopen it for the first time after the package has been imported, regardless of what I do between the time of importing and the time of closing and reopening unity.

I figured maybe there was something in the package that was screwing with my code so I tried this again but instead of importing the entire package, I just imported the few assets from the package that I actually needed.

Import Assets Into Empty Project

Again, I come across the problem of the water plane being displayed behind all the objects. The gif (Import Assets 7 in imgur album) shows that the box is displayed on top but is really below.

I am at a loss as to why this is happening. The only thing that is changing is the fact that I close and then open unity and it breaks. I am assuming that it is something related to the shader but I could be wrong. I am currently using Unity 4.2.2f1 Pro, I’m not sure what version the package was created for. If anyone has ANY ideas on what could be causing this or could give me some advice in how I might debug this I would be terribly grateful. I have spent all week trying to isolate this bug and fix it or find an alternative but so far this is the only effect I’ve found that handles the ripple the way I wanted.

Thanks

Update: I have been able to reproduce this on two other machines, one running 4.1.0 Pro and one running 4.3.0 Pro trial. I have not been able to access a Pro or Pro trial version of 3.x to test on.

Update: I’ve been able to further pinpoint the source of the problem. The problem is tied to the first time the project is closed/reopened after importing the WaterMobile.shader file. Currently, I can recreate the issue by following steps below.

  • Create empty project
  • Import only WaterMobile.shader and an arbitrary texture for testing
  • Create a plane and a cube
  • Place plane in front of camera, cube behind plane
  • Set material on plane to use WaterMobile.shader
  • Add test texture to shader
  • Save scene
  • Close Unity
  • Open Unity

The plane displays properly until Unity is closed and reopened. No other files are added or created.

I have also noticed something interesting. If I do the following steps,

  • Create empty project
  • Import only WaterMobile.shader and an arbitrary texture for testing
  • Create a plane and a cube
  • Place plane in front of camera, cube behind plane
  • Set material on plane to use WaterMobile.shader
  • Add test texture to shader
  • Save scene
  • Close Unity
  • Open Unity
  • Edit WaterMobile.shader in Monodevelop
  • Change first line from Shader “Custom/Water Ripple/Mobile” to Shader “Custom/Water Ripple/MobileTest”
  • Save as new file
  • Change material on plane to use the shader MobileTest instead of Mobile
  • Save scene
  • Close Unity
  • Open Unity

After I reopen Unity the first time, the plane no longer displays properly. However, after I essentially duplicate the shader and assign this new, yet functionally identical, shader to the plane it displays properly. I can switch between the Mobile and MobileTest shaders and see the plane alternate between its incorrect and correct displays respectively. After I reopen Unity the second time, this new WaterMobileTest.shader now behaves the same as WaterMobile.shader, incorrectly. I can keep doing this, create new and identical shaders that work until the first time I reopen Unity after they’ve been made.

Any ideas on why reopening Unity causes changes in the behavior of these shaders?

Edit: Also keep in mind that this rendering behind everything else issue only appears in the editor, game and scene views. If the project is built after the project has been closed and reopened, the executable will display the plane correctly.

Update: I seem to have found a suitable workaround for now. The Subshader section of WaterMobile.shader looks as follows

Subshader 
{
	Pass 
	{
 		Tags { "IgnoreProjector"="True" "Queue"="Transparent" "RenderType"="Transparent"}
 		ZWrite Off
 		Blend One One
		CGPROGRAM
		#pragma vertex vert 
		#pragma fragment frag 
		ENDCG
	}
}

If I delete line 6. “ZWrite Off,” the plane is no longer being rendered behind all other objects. However, this does break the transparency of the shader which luckily I do not need. The next line, “Blend One One,” still affects the coloring of the object which I don’t want either so I change this to “Blend Off.” This results in a completely non-transparent texture, which is what I want, but more importantly it results in a shader that seems to be consistent through multiple saves and projects opens and closes.

I would still like to know why changing the ZWrite fixed this and why having it turned off, which is supposed to be turned off for semi-transparent shaders, caused this odd issue.

Any guesses?