Well I don’t know if anyone tried this or not, but what I am trying is to add bumpmapping to the reflective surface and to the reflected image.
My first question is, can the main texture bumpmapping be added to the same pass where the reflection is calculated or it must be in a separate pass?
EDIT: I’ve just realized that I lost the shadows. Is it possible to get them back on the reflective surface?
At the moment I can perturb the reflection image with the bumpmap (here the tiling of the bumpmap should be the same as the tiling of the texture) but I cannot apply the bumpmap on the base texture.
here is the shader code:
Shader "FX/SurfaceReflection"
{
Properties
{
_Color ("Reflection strength", color) = (1,1,1,1)
_MainTex ("Main texture", 2D) = "" {}
_BumpMap ("Bumpmap (RGB) ", 2D) = "bump" {}
_Fresnel ("Fresnel (A) ", 2D) = "gray" {}
_ReflectionTex ("Internal Reflection", 2D) = "" {}
}
// -----------------------------------------------------------
// Fragment program cards
Subshader
{
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_builtin
#pragma fragmentoption ARB_precision_hint_fastest
#pragma fragmentoption ARB_fog_exp2
#include "UnityCG.cginc"
#include "AutoLight.cginc"
float4 _MainTex_ST, _BumpMap_ST;
struct v2f {
V2F_POS_FOG;
LIGHTING_COORDS
float3 ref;
float3 viewDir;
float3 lightDirT;
float2 uv;
float2 uv2;
float4 color: COLOR;
};
v2f vert(appdata_tan v)
{
v2f o;
PositionFog( v.vertex, o.pos, o.fog );
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
o.uv2 = TRANSFORM_TEX(v.texcoord, _BumpMap);
// object space view direction (will normalize per pixel)
o.viewDir.xzy = ObjSpaceViewDir(v.vertex);
TANGENT_SPACE_ROTATION;
o.lightDirT = mul( rotation, ObjSpaceLightDir( v.vertex ) );
// 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);
TRANSFER_VERTEX_TO_FRAGMENT(o);
return o;
}
sampler2D _MainTex;
sampler2D _BumpMap;
sampler2D _Fresnel;
sampler2D _ReflectionTex;
float4 _Color;
half4 frag( v2f i ) : COLOR
{
i.viewDir = normalize(i.viewDir);
float3 normal = tex2D(_BumpMap, i.uv2).xyz * 2 - 1;
// fresnel factor
half fresnelFac = dot( i.viewDir, normal );
// perturb reflection/refraction UVs by bumpmap, and lookup colors
float3 uv1 = i.ref; uv1.xy += normal;
half4 refl = tex2Dproj( _ReflectionTex, uv1 );
refl *= _Color;
// final color is between refracted and reflected based on fresnel
half4 color;
float4 base = tex2D(_MainTex, i.uv);
half4 surface = tex2D( _Fresnel, float2(fresnelFac,fresnelFac) );
color.rgb = lerp( surface.rgb, refl.rgb, surface.a );
color.a = refl.a * surface.a;
DiffuseLight(i.lightDirT, normal, base, LIGHT_ATTENUATION(i) );
return color + base;
}
ENDCG
}
}
would be nice if someone could point out where to add the main texture bumpmapping
Thanks
Greg
