gpt4 book ai didi

matlab - 光照与 Matlab 中的动画表面保持一致

转载 作者:太空宇宙 更新时间:2023-11-03 19:36:56 25 4
gpt4 key购买 nike

我试图在 Matlab 中为一个旋转的球体制作动画,但是球体上的光照会随之旋转。相反,我希望球体旋转,而照明在坐标系中保持固定。这是我的代码当前生成的 gif:Animation .这是我的代码:

% 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

最佳答案

正如评论中已经提到的:

Seems like it might be an issue with the surface VertexNormals not updating.

解决方案是下载 rotate.m function fixed File Exchange提交。

描述:

Bug evidence:

[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 looks like the light has moved. This is due to a bug in rotate.m function. The "VertexNormals" property of the surf object is not updated as "xdata", "ydata" and "zdata" properties are.

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

关于matlab - 光照与 Matlab 中的动画表面保持一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24689540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com