I've found a solution to this problem thanks to the folks over at stackoverflow. The issue is caused by the surface VertexNormals not updating properly in the rotation function. This updated rotation function solves the issue:
Lighting Stays with Surface in Animation
3 次查看(过去 30 天)
显示 更早的评论
I'm attempting to animate a rotating sphere, however the lighting on the sphere rotates with it in certain situations. I instead want the the lighting to remain fixed with the coordinate system while the sphere rotates. Here's a gif of what my code is currently producing: Animation . Below is the code I used to produce the animation. The weird thing is this only happens with rotations about the z-axis. As can be seen in the animation, the lighting works fine for x-axis rotation.
% 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 off
axis equal
view([40 25]);
lgt = 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
for n = 1:36
rotate(h,[1 0 0],10,[0 0 0])
im = frame2im(getframe(1));
[imind,cm] = rgb2ind(im,256);
imwrite(imind,cm,filename,'gif','WriteMode','append','DelayTime',dt);
end
0 个评论
采纳的回答
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Coordinate Systems 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!