creating normals from alpha/heightmap inside a shader
http://www.polycount.com/forum/showthread.php?t=117185
I am making some custom terrain shaders with strumpy's editor and I want to be able to create normals based on my blend mask. Does anyone know how I can turn grayscale data into basic normal mapping info? I have seen someone do this in unreal but I can't remember where.
You probably want to get the difference of the values pixel-by-pixel using ddx, I think.
Off the top of my head... for CG/HLSL;
float heightmap = your height map value;
float3 normal;
normal.x = ddx(heightmap);
normal.y = ddy(heightmap);
normal.z = sqrt(1 - normal.x*normal.x - normal.y * normal.y); // Reconstruct z component to get a unit normal.
For OpenGL, I think the functions are dFdx and dFdy.
DirectX11 has ddx_fine and ddy_fine which I think give more accurate results.
That'll give you the normal in, ehr... screen space I think?
Otherwise you can sample the heightmap multiple times, offsetting it by a pixel and working out the difference from those values. That should get you it in whatever space the base normal is in.
Nicked frm here; http://www.gamedev.net/topic/594781-...mal-algorithm/
float me = tex2D(heightMapSampler,IN.tex).x;
float n = tex2D(heightMapSampler,float2(IN.tex.x,IN.tex.y+1.0/heightMapSizeY)).x;
float s = tex2D(heightMapSampler,float2(IN.tex.x,IN.tex.y-1.0/heightMapSizeY)).x;
float e = tex2D(heightMapSampler,float2(IN.tex.x+1.0/heightMapSizeX,IN.tex.y)).x;
float w = tex2D(heightMapSampler,float2(IN.tex.x-1.0/heightMapSizeX,IN.tex.y)).x; //find perpendicular vector to norm:
float3 temp = norm; //a temporary vector that is not parallel to norm
if(norm.x==)
temp.y+=0.5;
else
temp.x+=0.5; //form a basis with norm being one of the axes:
float3 perp1 = normalize(cross(norm,temp));
float3 perp2 = normalize(cross(norm,perp1)); //use the basis to move the normal in its own space by the offset
float3 normalOffset = -bumpHeightScale*(((n-me)-(s-me))*perp1 + ((e-me)-(w-me))*perp2);
norm += normalOffset;
norm = normalize(norm);
Gave it a try out of curiosity.
Here's the multi-sample method in a stripped-down surface shader.
I had to change the handedness from the code above to match Unity (changing the sign of the operation when sampling the e and w values from + to - and vice versa).
Shader "Debug/Normal Map From Height" {
Properties {
_Color ("Main Color", Color) = (,,,)
_MainTex ("Diffuse (RGB) Alpha (A)", 2D) = "white" {}
_BumpMap ("Normal (Normal)", 2D) = "bump" {}
_HeightMap ("Heightmap (R)", 2D) = "grey" {}
_HeightmapStrength ("Heightmap Strength", Float) = 1.0
_HeightmapDimX ("Heightmap Width", Float) =
_HeightmapDimY ("Heightmap Height", Float) =
}
SubShader{
Tags { "RenderType" = "Opaque" }
CGPROGRAM
#pragma surface surf NormalsHeight
#pragma target 3.0
struct Input
{
float2 uv_MainTex;
};
sampler2D _MainTex, _BumpMap, _HeightMap;
float _HeightmapStrength, _HeightmapDimX, _HeightmapDimY;
void surf (Input IN, inout SurfaceOutput o)
{
o.Albedo = fixed3(0.5);
float3 normal = UnpackNormal(tex2D(_BumpMap, IN.uv_MainTex));
float me = tex2D(_HeightMap,IN.uv_MainTex).x;
float n = tex2D(_HeightMap,float2(IN.uv_MainTex.x,IN.uv_MainTex.y+1.0/_HeightmapDimY)).x;
float s = tex2D(_HeightMap,float2(IN.uv_MainTex.x,IN.uv_MainTex.y-1.0/_HeightmapDimY)).x;
float e = tex2D(_HeightMap,float2(IN.uv_MainTex.x-1.0/_HeightmapDimX,IN.uv_MainTex.y)).x;
float w = tex2D(_HeightMap,float2(IN.uv_MainTex.x+1.0/_HeightmapDimX,IN.uv_MainTex.y)).x;
float3 norm = normal;
float3 temp = norm; //a temporary vector that is not parallel to norm
if(norm.x==)
temp.y+=0.5;
else
temp.x+=0.5;
//form a basis with norm being one of the axes:
float3 perp1 = normalize(cross(norm,temp));
float3 perp2 = normalize(cross(norm,perp1));
//use the basis to move the normal in its own space by the offset
float3 normalOffset = -_HeightmapStrength * ( ( (n-me) - (s-me) ) * perp1 + ( ( e - me ) - ( w - me ) ) * perp2 );
norm += normalOffset;
norm = normalize(norm);
o.Normal = norm;
}
inline fixed4 LightingNormalsHeight (SurfaceOutput s, fixed3 lightDir, fixed3 viewDir, fixed atten)
{
viewDir = normalize(viewDir);
lightDir = normalize(lightDir);
s.Normal = normalize(s.Normal);
float NdotL = dot(s.Normal, lightDir);
_LightColor0.rgb = _LightColor0.rgb;
fixed4 c;
c.rgb = float3(0.5) * saturate ( NdotL ) * _LightColor0.rgb * atten;
c.a = 1.0;
return c;
}
ENDCG
}
FallBack "VertexLit"
}
Derivative method works, but it's fucking ugly 'cause it's in screen space - really noisy.
ddx_fine might give better results in DX11, but it looks like crap in DX9.
creating normals from alpha/heightmap inside a shader的更多相关文章
- Creating Materials at runtime And Issue of Shader.Find()
Creating Materials at runtimehttp://forum.unity3d.com/threads/create-materials-at-runtime.72952/ //通 ...
- surface shader相关参数,命令
https://docs.unity3d.com/Manual/SL-SurfaceShaders.html 说明: 注意下surfaceshader相关开关选项,input结构体全部可用参数 goo ...
- OpenGL shader 中关于顶点坐标值的思考
今天工作中需要做一个事情: 在shader内部做一些空间距离上的计算,而且需要对所有的点进行计算,符合条件的显示,不符合条件的点不显示. 思路很简单,在vertex shader内知道顶点坐标,进行计 ...
- 【Unity Shaders】Alpha Test和Alpha Blending
写在前面 关于alpha的问题一直是个比较容易摸不清头脑的事情,尤其是涉及到半透明问题的时候,总是不知道为什么A就遮挡了B,而B明明在A前面.这篇文章就总结一下我现在的认识~ Alpha Test和A ...
- Unity Shader 基础(3) 获取深度纹理
Unity提供了很多Image Effect效果,包含Global Fog.DOF.Boom.Blur.Edge Detection等等,这些效果里面都会使用到摄像机深度或者根据深度还原世界坐标实现各 ...
- (转)【Unity Shaders】Alpha Test和Alpha Blending
转自:http://blog.csdn.net/candycat1992/article/details/41599167 写在前面 关于alpha的问题一直是个比较容易摸不清头脑的事情,尤其是涉及到 ...
- 雷达波Shader
OSG版本: vert #version varying out vec3 v; void main() { gl_FrontColor = gl_Color; gl_Position = ftran ...
- Unity关闭shader中的光照模型以及如何自定义光照模型
// Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject' // Upgrade NOTE: replaced '_Wor ...
- unity, unlit surface shader (texColor only surface shader)
要实现双面透明无光照只有纹理色的surface shader. 错误的写法:(导致带有曝光) Shader "Custom/doubleFaceTranspTexColor" { ...
随机推荐
- OpenStack项目列表
这个也是必须要熟悉的哟. ~~~~~~~~~~ OpenStack是一个美国国家航空航天局和Rackspace合作研发的,以Apache许可证授权,并且是一个自由软件和开放源代码项目.OpenStac ...
- thinkphp 模板替换
具体详见tp手册. 如果需要修改模板替换映射路径. 则需: 'TMPL_PARSE_STRING'=>array( '__PUBLIC__'=>__ROOT__.'/'.APP_NAME. ...
- SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-007-表单验证@Valid、Error
一. Starting with Spring 3.0, Spring supports the Java Validation API in Spring MVC . No extra config ...
- 11个强大的Visual Studio调试小技巧(转)
简介 调试是软件开发周期中很重要的一部分.它具有挑战性,同时也很让人疑惑和烦恼.总的来说,对于稍大一点的程序,调试是不可避免的.最近几年,调试工具的发展让很多调试任务变的越来越简单和省时. 这篇文章总 ...
- Android 内核初识(6)SystemServer进程
简介 SystemServer的进程名实际上叫做“system_server”,通常简称为SS. 系统中的服务驻留在其中,常见的比如WindowManagerServer(Wms).ActivityM ...
- STL总结之bitset
STL的bitset是一个对位进行存储和操作的容器,可以轻松对bit位进行访问. bitset的模板声明如下: template<size_t _Bits> class bitset; ...
- app图片规格
我们定义的app图片规格 app图标需要分iphone和android两套 iphone: 名称 Iphone4 Iphone5 手机尺寸 960*640(高*宽) 1136*640 (高*宽) 电池 ...
- CodeForces - 445A - DZY Loves Chessboard解题报告
对于这题本人刚开始的时候觉得应该用DFS来解决实现这个问题,但由于本人对于DFS并不是太熟,所以就放弃了这个想法: 但又想了想要按照这个要求实现问题则必须是黑白相间,然后把是字符是'B'或'W'改为' ...
- Web---JSP注册技术的的演绎(3代)-JSP/EJB/Servlet/POJO/JavaBean
我们可以这么理解JSP注册技术的发展过程: 第一代JSP技术:纯JSP开发. 第二代JSP技术:JSP+EJB开发. (EJB简单来说就是把已经编写好的程序(即:类)打包放在服务器上执行.) 第三代J ...
- Oracle的dmp文件的导入
项目开始拿到了dmp文件,数据库用的是10g的,但是尽然没导成功,后来想可能导出的时候用11导出的,决定试一下. 正好自己的机器是11的客户端,结果不识别imp命令,到安装目录下的bin文件夹下看尽然 ...