《How to bake LightMap》

 

light map的烘焙原理的核心算法是在VS里面利用贴图坐标变换到[-1,1]作为输出的顶点坐标。

 

实时渲染的顶点变换中, pos * WVP之后, 顶点坐标就变换到屏幕空间了[-1, 1](实际上还需要透视除法);

如果VertexShader里直接把纹理坐标做为变换结果输出(注意从[0,1]变换到[-1,1]), 那么相当于直接变换到了纹理坐标系, 这时在PixelShader里还是像原来那样计算光照, 输出的结果就可以拿来做lightmap了。

 

一个典型的Phong光照模型下的球渲染图:

其VS为:

VS_OUTPUT vs_main( VS_INPUT Input )  
{  
   VS_OUTPUT Output        = (VS_OUTPUT)0;  
  
   Output.Position         = mul( Input.Position, matViewProjection );  
     
   Output.Texcoord         = Input.Texcoord;  
     
   float3 fvObjectPosition = mul( Input.Position, matView );  
     
   Output.ViewDirection    = fvEyePosition - fvObjectPosition;  
   Output.LightDirection   = fvLightPosition - fvObjectPosition;  
   Output.Normal           = mul( Input.Normal, matView );  
        
   return( Output );  
} 

为了烘焙light map,可以变换UV坐标至[-1,1]作为position的x与y坐标,注意还要将w赋值为1,避免driver的透视除法使得x与y的值发生变化:

VS_OUTPUT vs_main( VS_INPUT Input )  
{  
   VS_OUTPUT Output        = (VS_OUTPUT)0;  
  
   Output.Position.xy      = Input.Texcoord * float2(2, -2) + float2(-1, 1);//[0,1]->[-1,1]  here seem is incorrect !
   Output.Position.w       = 1;//avoid perspective drive change x,y's value    
 
   Output.Texcoord         = Input.Texcoord;  
     
   float3 fvObjectPosition = mul( Input.Position, matView );  
     
   Output.ViewDirection    = fvEyePosition - fvObjectPosition;  
   Output.LightDirection   = fvLightPosition - fvObjectPosition;  
   Output.Normal           = mul( Input.Normal, matView );  
        
   return( Output );  
} 

渲染得到结果:

只保存明暗light信息:

 

实际上,上文中是只有一盏灯光的结果,如果有多盏灯光,那么就应该针对每个灯光进行烘焙,最粗糙的做法是得到多张light map,进行blend合并出最终结果。

 

posted on 2017-07-11 01:11 DeanWang 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/DeanWang/p/7148543.html

Unity light probes are an important part of every scene. It provide the transmission of direct, indirect and reflected light from light sources in mixed and baked modes to dynamic objects. The accuracy of lighting dynamic objects on the scene largely depends on how correctly the light probes are placed. Magic Light Probes is an editor extension that is designed to help you arrange your light probes in the automatic mode as quickly and correctly as possible. The main goal of the tool is to automatically install the probes exactly where they are needed – shadows borders, sharp changes in the color of textures, transitions from well-lit areas to dimly lit, corners and intersections of geometry, edges of geometry, ledges, breaks, sharp descents, two-sided geometry (to prevent leaks through walls, floors and ceilings). In addition, the modes of uniform filling of the entire volume and vertical duplication for particularly difficult cases are supported. This tool can guarantee that: – No probes will be located inside the geometry, no matter how complex it is, convex or concave. This is very important, because even in the simplest case of manually arranging probes by simple duplication, you will inevitably encounter probes within the geometry. – With high accuracy, light probes will be installed at corners and intersections of the geometry. In corners, as a rule, the light intensity is lower, therefore, in order to properly illuminate a dynamic object when approaching such places, it is necessary to install probes there. – Depending on the settings, the system will try to place the probes in the most contrasting places (errors are not excluded) It is guaranteed to be pretty fast in most cases.In addition to email and the forum, the Discord channel is available to you, where you can receive answers to any questions in real time. Features: – Automatically check for updates – Workflow selection Simple (quick and easy); Advanced (slower and more accurate) – Quick editing o
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值