在Matlab中使用动画表面进行照明

jos*_*hab 6 matlab animation lighting surface rotation

我试图在Matlab中为旋转球体设置动画,但是球体上的光照随之旋转.我反而希望球体旋转,同时照明保持与坐标系固定.这是我的代码当前产生的gif:动画.这是我的代码:

% Simulation Time
dt = 0.05;
time = 0:dt:5;

% Prep Figure
figure('Color',[1 1 1],'Renderer','zbuffer','ColorMap', [1,0,0; 0,0,1])

% Generate Sphere
[X,Y,Z] = sphere(20);
r = 0.75*25.4;
h = surf(r*X,r*Y,r*Z,Z,'FaceColor','interp');
hold on

% Adjust Axes, Lighting, and Shading
axis equal
view([40 25]);
light('Position',[1 1 1])
set(findobj(gca,'type','surface'),...
            'FaceLighting','phong',...
            'AmbientStrength',.3,'DiffuseStrength',.8,...
            'SpecularStrength',.9,'SpecularExponent',25,...
            'BackFaceLighting','unlit','EdgeColor','k')

filename = 'Rotation.gif';
for n = 1:36

      rotate(h,[0 0 1],10,[0 0 0])
      im = frame2im(getframe(1));
      [imind,cm] = rgb2ind(im,256);

      if n == 1;
          imwrite(imind,cm,filename,'gif', 'Loopcount',inf,'DelayTime',dt);
      else
          imwrite(imind,cm,filename,'gif','WriteMode','append','DelayTime',dt);
      end

end
Run Code Online (Sandbox Code Playgroud)

Den*_*din 1

正如评论中已经提到的:

\n\n
\n

VertexNormals看起来这可能是表面未更新的问题。

\n
\n\n

解决方案是下载rotate.m函数修复文件交换提交。

\n\n

说明:

\n\n
\n

错误证据:

\n\n
[x,y,z] = sphere(20); \nhs=surf(x,y,z,\'facecolor\',\'y\'); \nview(2) \naxis equal \nhl=light; \nlightangle(hl,0,0) \n% light is on -Y axis, thus at the \n% bottom \nrotate(hs,[0 0 1],30) \n% rotate sphere to the right from 30\xc2\xb0\n
Run Code Online (Sandbox Code Playgroud)\n\n

看起来光线已经移动了。这是由于rotate.m\n 函数中的错误造成的。surf 对象的“VertexNormals”属性不会像“xdata”、“ydata”和“zdata”属性那样更新。

\n\n

这已在提交的rotate.m 版本中修复。

\n
\n