![be1a583b5bf73315a5fdd57544ade769.png](https://i-blog.csdnimg.cn/blog_migrate/7ee0371c9f68c50ed96a79efddaaaad3.jpeg)
本示例说明如何创建和显示复杂的三维对象并控制其外观。
获取对象的几何
本示例使用一个称为Newell茶壶的图形对象。通过teapotData
函数,可以计算茶壶的顶点,面和颜色索引数据。由于茶壶是复杂的几何形状,因此该函数返回了大量的顶点(4608)和面(3872)。
[verts, faces, cindex] = teapotGeometry;
创建茶壶贴片对象
使用几何数据,使用patch
命令绘制茶壶。该patch
命令创建一个贴片对象。
figure
p = patch('Faces',faces,'Vertices',verts,'FaceVertexCData',cindex,'FaceColor','interp')
![6ce69da0749caee4208c7094555afa19.png](https://i-blog.csdnimg.cn/blog_migrate/fcc5c7608b0ca4679e2289597351a93d.jpeg)
p =
Patch with properties:
FaceColor: 'interp'
FaceAlpha: 1
EdgeColor: [0 0 0]
LineStyle: '-'
Faces: [3872x4 double]
Vertices: [4608x3 double]
Show all properties
使用view
命令更改对象的方向。
view(-151,30) % change the orientation
axis equal off % make the axes equal and invisible
![4af118d5f893e0ed94f9ee964abc37a5.png](https://i-blog.csdnimg.cn/blog_migrate/5891650c656aaaf92d2cd9a3f2325564.png)
调整透明度
调整FaceAlpha
补丁对象的属性,改变对象透明度。
p.FaceAlpha = 0.3; % make the object semi-transparent
![935104af5f506c90cfc01709418909bf.png](https://i-blog.csdnimg.cn/blog_migrate/ae958707370a179292a356e7c61fb35f.png)
如果该FaceColor
属性设置为“ none”,则该对象显示为线框形状。
p.FaceColor = 'none'; % turn off the colors
![2d463b12814e3769e220a25e8265b5a3.png](https://i-blog.csdnimg.cn/blog_migrate/60b485b019fad624192f3de00dea9d30.png)
更改颜色模板
使用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
![77520af85de1312175eef60ca684f703.png](https://i-blog.csdnimg.cn/blog_migrate/fc4602cfd892bc929f26ed8fc983d2e5.png)
为物体设置光线
添加灯光以使对象看起来更逼真。
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
Show all properties
lighting gouraud
![1978f01f19d07201d6d06061b911d1a1.png](https://i-blog.csdnimg.cn/blog_migrate/ed47aa916e8797b602b7772ade07f77d.png)
贴片对象的这些属性影响光的强度和对象的反射属性:
AmbientStrength
-控制环境光的强度DiffuseStrength
-控制散射光的强度SpecularStrength
-控制反射光的强度SpecularExponent
-控制反射光的粗糙度SpecularColorReflectance
-控制如何计算反射色。
我们可以单独设置这些属性。要将这些属性设置为一组预定值,这些值近似于金属,光泽或暗淡材料的外观,请使用material
命令。
material shiny
![391c9ec749c3407936f9b9c46725cd37.png](https://i-blog.csdnimg.cn/blog_migrate/454e6869ba27ecec68ed12bc78818d3b.png)
使用Position
属性调整光源的位置。该位置在x,y和z坐标中。
l.Position = [-0.1 0.6 0.8]
![bdb1f5dd3426b83f06de2c1735092ab6.png](https://i-blog.csdnimg.cn/blog_migrate/7050c737cb24b5cd87841551d11ecd64.png)
l =
Light with properties:
Color: [1 1 1]
Style: 'infinite'
Position: [-0.1000 0.6000 0.8000]
Visible: on
Show all properties
注:本文根据MATLAB官网内容修改而成。
我推荐给你以下的视频教程,特点是没有PPT,不掺水,直接编程环境下的实操课程: 用360分钟了解MATLAB编程《MATLAB编程360》视频课程:
MATLAB编程360 - 网易云课堂study.163.comstudy.163.com