在场景中添加光线——在反光表面添加镜面高光
2011-02-14 14:36
239 查看
问题 就算开启了逐像素明暗,有些金属或闪光表面仍显得有点暗淡。在现实生活中,当观察诸如金属、玻璃或一些塑料时,你会发现某些区域的反光非常强烈。这样的区域如图6-6的虚线圆圈所示。这些高亮的区域叫做镜面高光(specular highlights)。
图6-6 镜面高光
解决方案 和逐像素光照一样,你只需简单地告知BasicEffect创建镜面高光就可以开启它。
注意:镜面高光只应添加到发光材质上。不要将它们添加到诸如衣服或草地的柔软表面,或至少让反光效果非常微弱。
工作原理 使用BasicEffect开启镜面高光非常简单。对每个光源,你指定高光颜色。然后,设置高光强度。这个强度可以指定高光的宽度。强度越大,镜面高光越窄。可见教程6-8学习相关细节。
注意:当使用镜面高光时,总要使用逐像素光照,这是因为镜面高光通常是一个小的,非线性的点,所以它们不应该被插值,而是应该逐像素地计算。
代码
下面的代码定义顶点并绘制一个矩形:
在Draw方法中,添加如下代码创建BasicEffect,在矩形上添加镜面高光:
图6-6 镜面高光
解决方案 和逐像素光照一样,你只需简单地告知BasicEffect创建镜面高光就可以开启它。
注意:镜面高光只应添加到发光材质上。不要将它们添加到诸如衣服或草地的柔软表面,或至少让反光效果非常微弱。
工作原理 使用BasicEffect开启镜面高光非常简单。对每个光源,你指定高光颜色。然后,设置高光强度。这个强度可以指定高光的宽度。强度越大,镜面高光越窄。可见教程6-8学习相关细节。
basicEffect.LightingEnabled = true; basicEffect.DirectionalLight0.Direction = lightDirection; basicEffect.DirectionalLight0.DiffuseColor = Color.White.ToVector3(); basicEffect.DirectionalLight0.Enabled = true; basicEffect.PreferPerPixelLighting = true; basicEffect.DirectionalLight0.SpecularColor = Color.White.ToVector3(); basicEffect.SpecularPower = 32;
注意:当使用镜面高光时,总要使用逐像素光照,这是因为镜面高光通常是一个小的,非线性的点,所以它们不应该被插值,而是应该逐像素地计算。
代码
下面的代码定义顶点并绘制一个矩形:
private void InitVertices() { vertices = new VertexPositionNormalTexture[4]; vertices[0] = new VertexPositionNormalTexture(new Vector3(0, 0, 0), new Vector3(0, 1, 0), new Vector2(0, 1)); vertices[1] = new VertexPositionNormalTexture(new Vector3(0, 0, - 10), new Vector3(0, 1, 0), new Vector2(0, 0)); vertices[2] = new VertexPositionNormalTexture(new Vector3(10, 0, 0), new Vector3(0, 1, 0), new Vector2(1, 1)); vertices[3] = new VertexPositionNormalTexture(new Vector3(10, 0,- 10), new Vector3(0, 1, 0), new Vector2(1, 0)); myVertexDeclaration = new VertexDeclaration(device, VertexPositionNormalTexture.VertexElements); }
在Draw方法中,添加如下代码创建BasicEffect,在矩形上添加镜面高光:
basicEffect.World = Matrix.Identity; basicEffect.View = fpsCam.ViewMatrix; basicEffect.Projection = fpsCam.ProjectionMatrix; basicEffect.Texture = blueTexture; basicEffect.TextureEnabled = true; Vector3 lightDirection = new Vector3(0, -3, -10); lightDirection.Normalize(); basicEffect.LightingEnabled = true; basicEffect.DirectionalLight0.Direction = lightDirection; basicEffect.DirectionalLight0.DiffuseColor = Color.White.ToVector3(); basicEffect.DirectionalLight0.Enabled = true; basicEffect.PreferPerPixelLighting = true; basicEffect.DirectionalLight0.SpecularColor = Color.White.ToVector3(); basicEffect.SpecularPower = 32; basicEffect.Begin(); foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes) { pass.Begin(); device.VertexDeclaration = myVertexDeclaration; device.DrawUserPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleStrip, vertices, 0, 2); pass.End(); } basicEffect.End();
相关文章推荐
- 在场景中添加光线——添加HLSL镜面高光
- 在场景中添加光线——概述
- 在场景中添加光线——定义法线和使用BasicEffect
- 在场景中添加光线——在顶点间共享法线
- 在场景中添加光线——给光线添加更高的细节:逐像素光照
- 在场景中添加光线——添加HLSL Vertex Shading
- 在场景中添加光线——使用HLSL定义点光源
- 在场景中添加光线——添加HLSL逐像素光照
- 在场景中添加光线——使用HLSL定义聚光灯
- 在场景中添加光线——使用Deferred Shading在场景中添加多光源
- 在场景中添加光线——在Deferred Shading引擎中添加阴影能力
- 【转】android Camera 中添加一种场景模式
- Unity NGUI 2D场景添加按钮
- OGRE 向场景中添加船模型
- Unity中在场景中添加水资源效果的方法
- 【译】光线跟踪:理论与实现(二)Phong模型,镜面反射及阴影
- iOS Sprite Kit教程之真机测试以及场景的添加与展示
- Cocos2d-x如何添加新场景及切换新场景(包括场景特效)
- Q102:光线追踪场景(5)——驭龙台
- 利用镜面反射让游戏闪耀起来 - 使用贴图对模型的高光进行遮罩