• [翻译]XNA 3.0 Game Programming Recipes之forty


    PS:自己翻译的,转载请著明出处格
                                                            6-4 添加镜子的高亮光到反射表面
    问题
                             即使对per-pixel阴影的启用,你绘制的一些金属或者有光泽的物体仍然看上去有点阴暗。在真实生活中,当观察一个反射表面如一个金属,玻璃,或者一些塑料,你会看到有些地方光源反射非常的亮。这样一个地方正如图6-6中的圆圈所表示的。这些亮点被称为specular highlights。
    解决方案
                             正如一个per-pixel光照,你可以简单的启用镜面高光通过告诉BasicEffect去创建它们。
    注意:镜面高光应该只添加到反射材质上。不要添加它们到柔软材质如衣服或者一个草的材质,或至少使他们的影响非常微弱。
    它是如何工作的
                             你可以启用镜面高光突出极端易于使用的BasicEffect.每一个光,你可以指定镜子的颜色。接下来,你可以设置镜子的强度在BasicEffect中。这个强度允许你去指定你的高光的宽度。强度越强,你的镜面高光就会越窄。参看6-8节得到更多细节。
    1 basicEffect.LightingEnable=true;
    2 basicEffect.DirectionalLight0.Direction=lightDirection;
    3 basicEffect.DirectionalLight0.DiffuseColor=Color.White.ToVector3();
    4 basicEffect.DirectionalLight0.Enabled=true;
    5 basicEffect.PreferPerPixelLighting=true;
    6 basicEffect.DirectionalLight0.SpecularColor=Color.White.ToVector3();
    7 basicEffect.SpecularPower=32;
    注意:当使用镜面高光,你总是会想使用per-pixel光照,因为镜子高光类型很少,非线性的点。因此,它们应该不能以内插值替换,而是为每个象素分别的计算。
    代码
                             这个代码定义顶点去绘制一个单独的四方格。它应该被调用在程序的开始时,直到它使用相关的设备,它应该被调用在LoadContent方法结束的时候。
    1 private void InitVertices()
    2 {
    3       vertices=new VertexPositionNormalTexture[4];
    4       vertices[0]=new VertexPositionNormalTexture(new Vector3(0,0,0),new Vector3(0,1,0),new Vector2(0,1));
    5       vertices[1]=new VertexPositionNormalTexture(new Vector3(0,0,-10),new Vector3(0,1,0),new Vector2(0,0));
    6       vertices[2]=new VertexPositionNormalTexture(new Vector3(10,0,0),new Vector3(0,1,0),new Vector2(1,1));
    7       vertices[3]=new VertexPositionNormalTexture(new Vector3(10,0,-10),new Vector3(0,1,0),new Vector2(1,0));
    8       myVertexDeclaration=new VertexDeclaration(device,VertexPositionNormalTexture.VertexElement);
    9 }
                             在你的Draw方法,添加这代码去设置你的BasicEffect到添加镜面高光到你的四方格:
     1 basicEffect.World=Matrix.Identity;
     2 basicEffect.View=fpsCam.ViewMatrix;
     3 basicEffect.Projection=fpsCam.ProjectionMatrix;
     4 basicEffect.Texture=blueTexture;
     5 basicEffect.TextureEnabled=true;
     6 Vector3 lightDirection=new Vector3(0,-3,-10);
     7 lightDirection.Normalize();
     8 basicEffect.LightingEnable=true;
     9 lingtDirection.Normalize();
    10 basicEffect.DirectionalLight0.Direction=lightDirection;
    11 basicEffect.DirectionalLight0.DiffuseColor=Color.White.ToVector3();
    12 basicEffect.DirectionalLight0.Enable=true;
    13 basicEffect.PreferPerPixelLighting=true;
    14 basicEffect.DirectionalLight0.SpecularColor=Color.White.ToVector3();
    15 basicEffect.SpecularPower=32;
    16 basicEffect.Begin();
    17 foreach(EffectPass pass in basicEffect.CurrentTechnique.Passes)
    18 {
    19      pass.Begin();
    20      device.VertexDeclaration=myVertexDeclaration;
    21      device.DrawUserPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleStrip,vertices,0,2);
    22      pass.End();   
    23 }
    24 basicEffect.End();
  • 相关阅读:
    app卡顿问题检测--KMCGeigerCounter
    报错---[UIApplication _runWithMainScene:transitionContext:completion:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3505.16/UIApplication.m:3294**
    键盘工具栏的快速集成--IQKeyboardManager
    iOS 对网络视频采集视频截图
    iOS-label出现未知边框线的bug
    iOS开发中图片方向的获取与更改
    通过代码设置button中文字的对齐方式
    util.date
    统计字符串每个字母的个数
    异常处理之多重catch
  • 原文地址:https://www.cnblogs.com/315358525/p/1543729.html
Copyright © 2020-2023  润新知