显示复杂三维对象
此示例说明如何创建并显示复杂三维对象以及控制其外观。
获取对象的几何图
此示例使用一个称为 Newell 茶壶的图形对象。茶壶的顶点、面和颜色索引数据由 teapotData
函数计算得出。由于茶壶是一个复杂的几何形状,函数因而返回大量的顶点(4608 个)和面(3872 个)。
[verts, faces, cindex] = teapotGeometry;
创建茶壶补片对象
使用几何数据,用 patch
命令绘制茶壶。patch
命令创建补片对象。
figure p = patch('Faces',faces,'Vertices',verts,'FaceVertexCData',cindex,'FaceColor','interp')
p = Patch with properties: FaceColor: 'interp' FaceAlpha: 1 EdgeColor: [0 0 0] LineStyle: '-' Faces: [3872x4 double] Vertices: [4608x3 double] Use GET to show all properties
使用 view
命令更改对象的方向。
view(-151,30) % change the orientation axis equal off % make the axes equal and invisible
调整透明度
使用补片对象的 FaceAlpha
属性使对象变得透明。
p.FaceAlpha = 0.3; % make the object semi-transparent
如果 FaceColor
属性设置为“none”,则该对象会作为线框图显示。
p.FaceColor = 'none'; % turn off the colors
更改颜色图
使用 colormap
函数更改对象的颜色。
p.FaceAlpha = 1; % remove the transparency p.FaceColor = 'interp'; % set the face colors to be interpolated p.LineStyle = 'none'; % remove the lines colormap(copper) % change the colormap
用光源照射对象
添加一个光源,使对象看起来更加逼真。
l = light('Position',[-0.4 0.2 0.9],'Style','infinite')
l = Light with properties: Color: [1 1 1] Style: 'infinite' Position: [-0.4000 0.2000 0.9000] Visible: on Use GET to show all properties
lighting gouraud
补片对象的以下属性会影响光照强度和对象的反光属性。
AmbientStrength
- 控制环境光的强度DiffuseStrength
- 控制散射光的强度SpecularStrength
- 控制反射光的强度SpecularExponent
- 控制反射光的粗糙度SpecularColorReflectance
- 控制反射颜色的计算方式。
您可以分别设置这些属性。若要将这些属性设置为一组预先确定的值来获得近似金属材料、闪光材料或哑光材料的外观,请使用 material
命令。
material shiny
使用光源的 Position
属性调整其位置。位置以 x、y、z 坐标表示。
l.Position = [-0.1 0.6 0.8]
l = Light with properties: Color: [1 1 1] Style: 'infinite' Position: [-0.1000 0.6000 0.8000] Visible: on Use GET to show all properties