- Home /
Adding object to scene causes problem with other objects material/shader
Hello!
I'm working on a simple shader to make wavy water and have run into a strange issue. The only thing the shader does is offset the y-position of each vertex with the value from a sine wave (code below). I set up a 3x3 grid of planes using the shader and everything works fine on its own.
However, when I add any other model into the scene, the closest tile to the camera becomes disconnected from the others. The wave still moves across the mesh, but it's as if the time value that is used to calculate the y-position is different from the other tiles.
Disabling the added object "fixes" the issue (disabling the mesh renderer specifically also works). The problem also only occurs at certain camera angles and distances. To top it off, changing the shader of the object to the water shader makes it so the tile furthest away has the problem, rather than the closest.
Here is an image of the scene with and without a barrel (The material on the barrel uses only the Standard shader with a texture):
There are no scripts attached to the barrel, it is simply the model dropped into the scene.
This issue also occurs in the editor view (The camera is zoomed out a bit to "fix" the tile in the second image):
Here is the water shader:
Shader "Custom/Simple Water"
{
Properties
{
_MainTex( "Texture", 2D ) = "white" {}
_Speed( "Wave Speed", Float ) = 1.0
_Amplitude( "Wave Amplitude", Float ) = 0.1
_Period( "Wave Period", Float ) = 1.0
}
SubShader
{
Tags { "RenderType" = "Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS( 1 )
float4 vertex : POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float _Speed;
float _Amplitude;
float _Period;
v2f vert( appdata v )
{
v2f o;
float t = _Time * _Speed;
v.vertex.y = sin( ( v.vertex.x + t ) * _Period ) * _Amplitude;
o.vertex = mul( UNITY_MATRIX_MVP, v.vertex );
o.uv = TRANSFORM_TEX( v.uv, _MainTex );
UNITY_TRANSFER_FOG( o, o.vertex );
return o;
}
fixed4 frag( v2f i ) : SV_Target
{
// sample the texture
fixed4 col = tex2D( _MainTex, i.uv );
// apply fog
UNITY_APPLY_FOG( i.fogCoord, col );
return col;
}
ENDCG
}
}
}
I would love some hints on what might be wrong here, because I'm completely stumped.
In case it matters, the version of Unity I'm using is 5.3.2f1, Personal Edition. Let me know if any additional details are needed.