For some reason (mostly lazy), I don’t want to make the normal map on the editor. so that, I made it on runtime
That is based on the Sobel operator
sobel operation…
float3 height2normal_sobel(float3x3 c)
{
float3x3 x = float3x3( 1.0, 0.0, -1.0,
2.0, 0.0, -2.0,
1.0, 0.0, -1.0 );
float3x3 y = float3x3( 1.0, 2.0, 1.0,
0.0, 0.0, 0.0,
-1.0, -2.0, -1.0 );
x = x * c;
y = y * c;
float cx = x[0][0] +x[0][2]
+x[1][0] +x[1][2]
+x[2][0] +x[2][2];
float cy = y[0][0] +y[0][1] +y[0][2]
+y[2][0] +y[2][1] +y[2][2];
float cz = sqrt(1-(cx*cx+cy*cy));
return float3(cx, cy, cz);
}
( Edit: almost forget the float3x3 could do multiply directly :p)
texture read in…
float3x3 img3x3(sampler2D color_map, float2 tc, float ts, int ch)
{
float d = 1.0/ts; // ts, texture sampling size
float3x3 c;
c[0][0] = tex2D(color_map,tc + float2(-d,-d))[ch];
c[0][1] = tex2D(color_map,tc + float2( 0,-d))[ch];
c[0][2] = tex2D(color_map,tc + float2( d,-d))[ch];
c[1][0] = tex2D(color_map,tc + float2(-d, 0))[ch];
c[1][1] = tex2D(color_map,tc )[ch];
c[1][2] = tex2D(color_map,tc + float2( d, 0))[ch];
c[2][0] = tex2D(color_map,tc + float2(-d, d))[ch];
c[2][1] = tex2D(color_map,tc + float2( 0, d))[ch];
c[2][2] = tex2D(color_map,tc + float2( d, d))[ch];
return c;
}
how to use …
float3x3 c = img3x3(color_map, IN.texcoord, texture_size, 0 );// red only
float3 normal = height2normal_sobel(c);
normal = normalize(float3(normal.xy, normal.z *bump_level));
Hope that is useful for someone like me (lazy :p)


