MATLAB: How to make camera light follow 3D Rotation

maz*_*maz 4 3d matlab user-interface lighting rotation

I have come into a problem recently when trying to rotate 3D objects. I am building a GUI and I have a separate figure where an object is plotted. In the figure, I allow the user to use MATLAB's built-in rotate button to move the object around. However, I am unable to have the light also follow the rotation as it seems to be fixed on one part of the object. To create the light, I am using

c=camlight('right');
set(c,'style','infinite');
Run Code Online (Sandbox Code Playgroud)

One solution I have thought of is to add light whenever the user releases the rotate button, but this is not very nice. I also do not know how to use the rotate callbacks when the button is in a separate figure.

Does anyone know how to make the light "track" the 3D rotation, so that the current view is illuminated?

Thanks!

Mat*_*att 5

实现自己的旋转功能

您可以实现自己的功能来同时调整轴和灯光。通过这种方式,在旋转轴的同时不断调整光线。

function follow_me_1
    figure
    axes('buttondownfcn', @buttondownfcn);  % assign callback
    set(gca,'NextPlot','add');              % add next plot to current axis
    surf(peaks,'hittest','off');            % hittest -> off is important
    view(3);                                % view to start from
    c = camlight('headlight');              % add light
    set(c,'style','infinite');              % set style of light

    function buttondownfcn(ax,~)
        fig = ancestor(ax,'figure');        % get figure handle
        [oaz, oel] = view(ax);              % get current azimuth and elevation
        oloc = get(0,'PointerLocation');    % get starting point
        set(fig,'windowbuttonmotionfcn',{@rotationcallback,ax,oloc,oaz,oel});
        set(fig,'windowbuttonupfcn',{@donecallback});
    end

    function rotationcallback(~,~,ax,oloc,oaz,oel)
        locend = get(0, 'PointerLocation'); % get mouse location
        dx = locend(1) - oloc(1);           % calculate difference x
        dy = locend(2) - oloc(2);           % calculate difference y
        factor = 2;                         % correction mouse -> rotation
        newaz = oaz-dx/factor;              % calculate new azimuth
        newel = oel-dy/factor;              % calculate new elevation
        view(ax,newaz,newel);               % adjust view
        c = camlight(c,'headlight');        % adjust light
    end

    function donecallback(src,~)
        fig = ancestor(src,'figure');           % get figure handle
        set(fig,'windowbuttonmotionfcn',[]);    % unassign windowbuttonmotionfcn
        set(fig,'windowbuttonupfcn',[]);        % unassign windowbuttonupfcn
    end

end
Run Code Online (Sandbox Code Playgroud)

使用 rotate3d

此示例使用内置rotate3d和分配的回调函数。这是“不是很好”的解决方案,但只需要几行代码。

function follow_me_2
    surf(peaks);                  % Load demo data
    c = camlight('headlight');    % Create light
    set(c,'style','infinite');    % Set style
    h = rotate3d;                 % Create rotate3d-handle
    h.ActionPostCallback = @RotationCallback; % assign callback-function
    h.Enable = 'on';              % no need to click the UI-button

    % Sub function for callback
    function RotationCallback(~,~)
        c = camlight(c,'headlight');
    end
end
Run Code Online (Sandbox Code Playgroud)