Matlab Animated Surface Lighting

I am trying to animate a rotating sphere in Matlab, however the lighting on the sphere rotates with it. Instead, I want the ball to rotate and the lighting to remain fixed using the coordinate system. Here's a gif of what my code is currently producing: Animation . And here is my code:

% 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 
+6
source share
1 answer

As mentioned in the comments:

Looks like this could be a surface VertexNormals not update.

The solution was to load the rotate.m fixed File Exchange function .

Description:

Error Details:

 [x,y,z] = sphere(20); hs=surf(x,y,z,'facecolor','y'); view(2) axis equal hl=light; lightangle(hl,0,0) % light is on -Y axis, thus at the % bottom rotate(hs,[0 0 1],30) % rotate sphere to the right from 30° 

It seems that the light has moved. This is due to an error in the rotate.m function. The VertexNormals property of the surf object is not updated as the "xdata", "ydata", and "zdata" properties.

This is fixed in the presented version of rotate.m.

+1
source

Source: https://habr.com/ru/post/972063/


All Articles