- Home /
Problem reducing reflections in water shader on PC
Hi,
I've been looking around to find out how to reduce the reflections in Pro FX Water Shader (Unity 3), I found this very useful answer:
I've updated the shader with the amendments listed in the answer and it works perfectly on my mac but default to the fallback texture on PC's. I've tested on several different PC's with different graphics cards and it doesn't work on any of them. The main PC i'm testing on has exactly the same graphics card as my mac, so I know it's not that.
Has anyone experienced this, or knows why this might be?
The code for my shader now looks like this:
// Upgrade NOTE: replaced 'PositionFog()' with multiply of UNITY_MATRIX_MVP by position // Upgrade NOTE: replaced 'V2F_POS_FOG' with 'float4 pos : SV_POSITION'
Shader "FX/Water" { Properties { _WaveScale ("Wave scale", Range (0.02,0.15)) = 0.063 _ReflDistort ("Reflection distort", Range (0,1.5)) = 0.44 _RefrDistort ("Refraction distort", Range (0,1.5)) = 0.40 _RefrColor ("Refraction color", COLOR) = ( .34, .85, .92, 1) _Fresnel ("Fresnel (A) ", 2D) = "gray" {} _BumpMap ("Normalmap ", 2D) = "bump" {} WaveSpeed ("Wave speed (map1 x,y; map2 x,y)", Vector) = (19,9,-16,-7) _ReflectiveColor ("Reflective color (RGB) fresnel (A) ", 2D) = "" {} _ReflectionStrength ("Reflection strength", Range(0.0,1.0)) = 0.5 _ReflectiveColorCube ("Reflective color cube (RGB) fresnel (A)", Cube) = "" { TexGen CubeReflect } _HorizonColor ("Simple water horizon color", COLOR) = ( .172, .463, .435, 1) _MainTex ("Fallback texture", 2D) = "" {} _ReflectionTex ("Internal Reflection", 2D) = "" {} _RefractionTex ("Internal Refraction", 2D) = "" {} }
// ----------------------------------------------------------- // Fragment program cards
Subshader { Tags { "WaterMode"="Refractive" "RenderType"="Opaque" } Pass { CGPROGRAM // Upgrade NOTE: excluded shader from OpenGL ES 2.0 because it uses non-square matrices #pragma exclude_renderers gles #pragma vertex vert #pragma fragment frag #pragma fragmentoption ARB_precision_hint_fastest #pragma fragmentoption ARB_fog_exp2 #pragma multi_compile WATER_REFRACTIVE WATER_REFLECTIVE WATER_SIMPLE
if defined (WATER_REFLECTIVE) || defined (WATER_REFRACTIVE)
define HAS_REFLECTION 1 #endif #if defined (WATER_REFRACTIVE) #define HAS_REFRACTION 1 #endif
include "UnityCG.cginc"
uniform float4 _WaveScale4; uniform float4 _WaveOffset;
if HAS_REFLECTION
niform float _ReflDistort; #endif #if HAS_REFRACTION uniform float _RefrDistort; #endif
struct appdata { float4 vertex : POSITION; float3 normal : NORMAL; };
struct v2f { float4 pos : SV_POSITION; #if HAS_REFLECTION || HAS_REFRACTION float3 ref : TEXCOORD0; float2 bumpuv0 : TEXCOORD1; float2 bumpuv1 : TEXCOORD2; float3 viewDir : TEXCOORD3; #else float2 bumpuv0 : TEXCOORD0; float2 bumpuv1 : TEXCOORD1; float3 viewDir : TEXCOORD2; #endif
};
v2f vert(appdata v) { v2f o; o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
// scroll bump waves
float4 temp;
temp.xyzw = v.vertex.xzxz * _WaveScale4 + _WaveOffset;
o.bumpuv0 = temp.xy;
o.bumpuv1 = temp.wz;
// object space view direction (will normalize per pixel)
o.viewDir.xzy = ObjSpaceViewDir(v.vertex);
#if HAS_REFLECTION || HAS_REFRACTION
// calculate the reflection vector
float3x4 mat = float3x4 (
0.5, 0, 0, 0.5,
0, 0.5 * _ProjectionParams.x, 0, 0.5,
0, 0, 0, 1
);
o.ref = mul (mat, o.pos);
#endif
return o;
}
if defined (WATER_REFLECTIVE) || defined (WATER_REFRACTIVE)
ampler2D _ReflectionTex; uniform float _ReflectionStrength; #endif sampler2D _ReflectiveColor; #if defined (WATER_REFRACTIVE) sampler2D _Fresnel; sampler2D _RefractionTex; uniform float4 _RefrColor; #endif #if defined (WATER_SIMPLE) uniform float4 _HorizonColor; #endif sampler2D _BumpMap;
half4 frag( v2f i ) : COLOR { i.viewDir = normalize(i.viewDir);
// combine two scrolling bumpmaps into one
half3 bump1 = UnpackNormal(tex2D( _BumpMap, i.bumpuv0 )).rgb;
half3 bump2 = UnpackNormal(tex2D( _BumpMap, i.bumpuv1 )).rgb;
half3 bump = (bump1 + bump2) * 0.5;
// fresnel factor
half fresnelFac = dot( i.viewDir, bump );
// perturb reflection/refraction UVs by bumpmap, and lookup colors
#if HAS_REFLECTION
float3 uv1 = i.ref; uv1.xy += bump * _ReflDistort;
half4 refl = tex2Dproj( _ReflectionTex, uv1 );
#endif
#if HAS_REFRACTION
float3 uv2 = i.ref; uv2.xy -= bump * _RefrDistort;
half4 refr = tex2Dproj( _RefractionTex, uv2 ) * _RefrColor;
#endif
// final color is between refracted and reflected based on fresnel
half4 color;
#if defined(WATER_REFRACTIVE)
half fresnel = tex2D( _Fresnel, float2(fresnelFac,fresnelFac) ).a;
half4 water = tex2D( _ReflectiveColor, float2(fresnelFac,fresnelFac) );
color.rgb = lerp( water.rgb, refl.rgb, _ReflectionStrength );
color = lerp( refr, color, fresnel );
#endif
#if defined(WATER_REFLECTIVE)
half4 water = tex2D( _ReflectiveColor, float2(fresnelFac,fresnelFac) );
color.rgb = lerp( water.rgb, refl.rgb, _ReflectionStrength );
color.a = refl.a * water.a;
#endif
#if defined(WATER_SIMPLE)
half4 water = tex2D( _ReflectiveColor, float2(fresnelFac,fresnelFac) );
color.rgb = lerp( water.rgb, _HorizonColor.rgb, water.a );
color.a = _HorizonColor.a;
#endif
return color;
} ENDCG
}
}
// ----------------------------------------------------------- // Old cards
// three texture, cubemaps Subshader { Tags { "WaterMode"="Simple" "RenderType"="Opaque" } Pass { Color (0.5,0.5,0.5,0.5) SetTexture [_MainTex] { Matrix [_WaveMatrix] combine texture primary } SetTexture [_MainTex] { Matrix [_WaveMatrix2] combine texture primary + previous } SetTexture [_ReflectiveColorCube] { combine texture +- previous, primary Matrix [_Reflection] } } }
// dual texture, cubemaps Subshader { Tags { "WaterMode"="Simple" "RenderType"="Opaque" } Pass { Color (0.5,0.5,0.5,0.5) SetTexture [_MainTex] { Matrix [_WaveMatrix] combine texture } SetTexture [_ReflectiveColorCube] { combine texture +- previous, primary Matrix [_Reflection] } } }
// single texture Subshader { Tags { "WaterMode"="Simple" "RenderType"="Opaque" } Pass { Color (0.5,0.5,0.5,0) SetTexture [_MainTex] { Matrix [_WaveMatrix] combine texture, primary } } }
}
I'm having the same problem, if anyone has an answer please share!
Answer by RMsoftware1 · Jan 25, 2012 at 02:10 AM
I believe this is caused by an error in the code...
Shader error in 'FX/Water': D3D shader assembly failed with: (3): error X5204: Read of uninitialized components(*) in r0: *r/x/0 *g/y/1 *b/z/2 *a/w/3
Your answer
Follow this Question
Related Questions
Reflection/Refraction water 1 Answer
Puddle reflection effect 2 Answers
Unity Water Shadows? 0 Answers
Whether enabling fog conflict with water reflection? 1 Answer
Unity water change color? (with picture) 0 Answers